IDEA-142739 GeneralIdBasedToSMTRunnerEventsConvertor doesn't accept duration attribut...
[idea/community.git] / platform / smRunner / src / com / intellij / execution / testframework / sm / runner / events / TestFailedEvent.java
1 /*
2  * Copyright 2000-2012 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 myFilePath;
31   private final long myDurationMillis;
32
33   public TestFailedEvent(@NotNull TestFailed testFailed, boolean testError) {
34     this(testFailed, testError, null);
35   }
36   public TestFailedEvent(@NotNull TestFailed testFailed, boolean testError, String filePath) {
37     super(testFailed.getTestName(), TreeNodeEvent.getNodeId(testFailed));
38     if (testFailed.getFailureMessage() == null) throw new NullPointerException();
39     myLocalizedFailureMessage = testFailed.getFailureMessage();
40     myStacktrace = testFailed.getStacktrace();
41     myTestError = testError;
42     myComparisonFailureActualText = testFailed.getActual();
43     myComparisonFailureExpectedText = testFailed.getExpected();
44     myFilePath = filePath;
45     myDurationMillis = parseDuration(testFailed.getAttributes().get("duration"));
46   }
47
48   private static long parseDuration(@Nullable String durationStr) {
49     if (!StringUtil.isEmpty(durationStr)) {
50       try {
51         return Long.parseLong(durationStr);
52       }
53       catch (NumberFormatException ignored) {
54       }
55     }
56     return -1;
57   }
58
59   public TestFailedEvent(@NotNull String testName,
60                          @NotNull String localizedFailureMessage,
61                          @Nullable String stackTrace,
62                          boolean testError,
63                          @Nullable String comparisonFailureActualText,
64                          @Nullable String comparisonFailureExpectedText) {
65     this(testName,
66          -1,
67          localizedFailureMessage,
68          stackTrace,
69          testError,
70          comparisonFailureActualText,
71          comparisonFailureExpectedText,
72          null,
73          -1);
74   }
75
76   public TestFailedEvent(@Nullable String testName,
77                          int id,
78                          @NotNull String localizedFailureMessage,
79                          @Nullable String stackTrace,
80                          boolean testError,
81                          @Nullable String comparisonFailureActualText,
82                          @Nullable String comparisonFailureExpectedText,
83                          @Nullable String expectedTextFilePath,
84                          long durationMillis) {
85     super(testName, id);
86     myLocalizedFailureMessage = localizedFailureMessage;
87     myStacktrace = stackTrace;
88     myTestError = testError;
89     myComparisonFailureActualText = comparisonFailureActualText;
90     myComparisonFailureExpectedText = comparisonFailureExpectedText;
91     myFilePath = expectedTextFilePath;
92     myDurationMillis = durationMillis;
93   }
94
95   @NotNull
96   public String getLocalizedFailureMessage() {
97     return myLocalizedFailureMessage;
98   }
99
100   @Nullable
101   public String getStacktrace() {
102     return myStacktrace;
103   }
104
105   public boolean isTestError() {
106     return myTestError;
107   }
108
109   @Nullable
110   public String getComparisonFailureActualText() {
111     return myComparisonFailureActualText;
112   }
113
114   @Nullable
115   public String getComparisonFailureExpectedText() {
116     return myComparisonFailureExpectedText;
117   }
118
119   @Override
120   protected void appendToStringInfo(@NotNull StringBuilder buf) {
121     append(buf, "localizedFailureMessage", myLocalizedFailureMessage);
122     append(buf, "stacktrace", myStacktrace);
123     append(buf, "isTestError", myTestError);
124     append(buf, "comparisonFailureActualText", myComparisonFailureActualText);
125     append(buf, "comparisonFailureExpectedText", myComparisonFailureExpectedText);
126   }
127
128   public String getFilePath() {
129     return myFilePath;
130   }
131
132   /**
133    * @return the test duration in milliseconds, or -1 if undefined
134    */
135   public long getDurationMillis() {
136     return myDurationMillis;
137   }
138 }