smTest: better naming
[idea/community.git] / platform / smRunner / src / com / intellij / execution / testframework / sm / runner / events / TestFailedEvent.java
1 /*
2  * Copyright 2000-2015 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.intellij.execution.testframework.sm.runner.events;
17
18 import com.intellij.openapi.util.text.StringUtil;
19 import jetbrains.buildServer.messages.serviceMessages.TestFailed;
20 import org.jetbrains.annotations.NotNull;
21 import org.jetbrains.annotations.Nullable;
22
23 public class TestFailedEvent extends TreeNodeEvent {
24
25   private final String myLocalizedFailureMessage;
26   private final String myStacktrace;
27   private final boolean myTestError;
28   private final String myComparisonFailureActualText;
29   private final String myComparisonFailureExpectedText;
30   private final String myExpectedFilePath;
31   private final String myActualFilePath;
32   private final long myDurationMillis;
33
34   public TestFailedEvent(@NotNull TestFailed testFailed, boolean testError) {
35     this(testFailed, testError, null);
36   }
37   public TestFailedEvent(@NotNull TestFailed testFailed, boolean testError, @Nullable String expectedFilePath) {
38     this(testFailed, testError, expectedFilePath, null);
39   }  
40   public TestFailedEvent(@NotNull TestFailed testFailed,
41                          boolean testError,
42                          @Nullable String expectedFilePath,
43                          @Nullable String actualFilePath) {
44     super(testFailed.getTestName(), TreeNodeEvent.getNodeId(testFailed));
45     if (testFailed.getFailureMessage() == null) throw new NullPointerException();
46     myLocalizedFailureMessage = testFailed.getFailureMessage();
47     myStacktrace = testFailed.getStacktrace();
48     myTestError = testError;
49     myComparisonFailureActualText = testFailed.getActual();
50     myComparisonFailureExpectedText = testFailed.getExpected();
51     myExpectedFilePath = expectedFilePath;
52     myActualFilePath = actualFilePath;
53     myDurationMillis = parseDuration(testFailed.getAttributes().get("duration"));
54   }
55
56   private static long parseDuration(@Nullable String durationStr) {
57     if (!StringUtil.isEmpty(durationStr)) {
58       try {
59         return Long.parseLong(durationStr);
60       }
61       catch (NumberFormatException ignored) {
62       }
63     }
64     return -1;
65   }
66
67   public TestFailedEvent(@NotNull String testName,
68                          @NotNull String localizedFailureMessage,
69                          @Nullable String stackTrace,
70                          boolean testError,
71                          @Nullable String comparisonFailureActualText,
72                          @Nullable String comparisonFailureExpectedText) {
73     this(testName,
74          null,
75          localizedFailureMessage,
76          stackTrace,
77          testError,
78          comparisonFailureActualText,
79          comparisonFailureExpectedText,
80          null,
81          -1);
82   }
83
84   public TestFailedEvent(@Nullable String testName,
85                          @Nullable String id,
86                          @NotNull String localizedFailureMessage,
87                          @Nullable String stackTrace,
88                          boolean testError,
89                          @Nullable String comparisonFailureActualText,
90                          @Nullable String comparisonFailureExpectedText,
91                          @Nullable String expectedFilePath,
92                          long durationMillis) {
93     super(testName, id);
94     myLocalizedFailureMessage = localizedFailureMessage;
95     myStacktrace = stackTrace;
96     myTestError = testError;
97     myComparisonFailureActualText = comparisonFailureActualText;
98     myComparisonFailureExpectedText = comparisonFailureExpectedText;
99     myExpectedFilePath = expectedFilePath;
100     myActualFilePath = null;
101     myDurationMillis = durationMillis;
102   }
103
104   @NotNull
105   public String getLocalizedFailureMessage() {
106     return myLocalizedFailureMessage;
107   }
108
109   @Nullable
110   public String getStacktrace() {
111     return myStacktrace;
112   }
113
114   public boolean isTestError() {
115     return myTestError;
116   }
117
118   @Nullable
119   public String getComparisonFailureActualText() {
120     return myComparisonFailureActualText;
121   }
122
123   @Nullable
124   public String getComparisonFailureExpectedText() {
125     return myComparisonFailureExpectedText;
126   }
127
128   @Override
129   protected void appendToStringInfo(@NotNull StringBuilder buf) {
130     append(buf, "localizedFailureMessage", myLocalizedFailureMessage);
131     append(buf, "stacktrace", myStacktrace);
132     append(buf, "isTestError", myTestError);
133     append(buf, "comparisonFailureActualText", myComparisonFailureActualText);
134     append(buf, "comparisonFailureExpectedText", myComparisonFailureExpectedText);
135   }
136
137   /**
138    * @deprecated use {@link #getExpectedFilePath()} instead
139    */
140   public String getFilePath() {
141     return myExpectedFilePath;
142   }
143
144   @Nullable
145   public String getExpectedFilePath() {
146     return myExpectedFilePath;
147   }
148
149   @Nullable
150   public String getActualFilePath() {
151     return myActualFilePath;
152   }
153   
154   /**
155    * @return the test duration in milliseconds, or -1 if undefined
156    */
157   public long getDurationMillis() {
158     return myDurationMillis;
159   }
160 }