View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.api.report;
20  
21  import javax.annotation.Nonnull;
22  
23  import java.util.Collections;
24  import java.util.Map;
25  import java.util.Objects;
26  
27  import org.apache.maven.surefire.api.util.internal.ImmutableMap;
28  
29  /**
30   * Basic implementation of {@link TestSetReportEntry} (immutable and thread-safe object).
31   *
32   * @author Kristian Rosenvold
33   */
34  public class SimpleReportEntry implements TestSetReportEntry {
35      private final RunMode runMode;
36  
37      private final Long testRunId;
38  
39      private final Map<String, String> systemProperties;
40  
41      private final String source;
42  
43      private final String sourceText;
44  
45      private final String name;
46  
47      private final String nameText;
48  
49      private final StackTraceWriter stackTraceWriter;
50  
51      private final Integer elapsed;
52  
53      private final String message;
54  
55      public SimpleReportEntry(
56              @Nonnull RunMode runMode, Long testRunId, String source, String sourceText, String name, String nameText) {
57          this(runMode, testRunId, source, sourceText, name, nameText, null, null);
58      }
59  
60      public SimpleReportEntry(
61              @Nonnull RunMode runMode,
62              Long testRunId,
63              String source,
64              String sourceText,
65              String name,
66              String nameText,
67              Map<String, String> systemProperties) {
68          this(runMode, testRunId, source, sourceText, name, nameText, null, null, systemProperties);
69      }
70  
71      private SimpleReportEntry(
72              @Nonnull RunMode runMode,
73              Long testRunId,
74              String source,
75              String sourceText,
76              String name,
77              String nameText,
78              StackTraceWriter stackTraceWriter) {
79          this(runMode, testRunId, source, sourceText, name, nameText, stackTraceWriter, null);
80      }
81  
82      public SimpleReportEntry(
83              @Nonnull RunMode runMode,
84              Long testRunId,
85              String source,
86              String sourceText,
87              String name,
88              String nameText,
89              Integer elapsed) {
90          this(runMode, testRunId, source, sourceText, name, nameText, null, elapsed);
91      }
92  
93      public SimpleReportEntry(
94              @Nonnull RunMode runMode,
95              Long testRunId,
96              String source,
97              String sourceText,
98              String name,
99              String nameText,
100             String message) {
101         this(
102                 runMode,
103                 testRunId,
104                 source,
105                 sourceText,
106                 name,
107                 nameText,
108                 null,
109                 null,
110                 message,
111                 Collections.<String, String>emptyMap());
112     }
113 
114     public SimpleReportEntry(
115             @Nonnull RunMode runMode,
116             Long testRunId,
117             String source,
118             String sourceText,
119             String name,
120             String nameText,
121             StackTraceWriter stackTraceWriter,
122             Integer elapsed,
123             String message,
124             Map<String, String> systemProperties) {
125         this.runMode = runMode;
126         this.testRunId = testRunId;
127         this.source = source;
128         this.sourceText = sourceText;
129         this.name = name;
130         this.nameText = nameText;
131         this.stackTraceWriter = stackTraceWriter;
132         this.message = message;
133         this.elapsed = elapsed;
134         this.systemProperties = new ImmutableMap<>(systemProperties);
135     }
136 
137     public SimpleReportEntry(
138             @Nonnull RunMode runMode,
139             Long testRunId,
140             String source,
141             String sourceText,
142             String name,
143             String nameText,
144             StackTraceWriter stackTraceWriter,
145             Integer elapsed) {
146         this(
147                 runMode,
148                 testRunId,
149                 source,
150                 sourceText,
151                 name,
152                 nameText,
153                 stackTraceWriter,
154                 elapsed,
155                 Collections.<String, String>emptyMap());
156     }
157 
158     public SimpleReportEntry(
159             @Nonnull RunMode runMode,
160             Long testRunId,
161             String source,
162             String sourceText,
163             String name,
164             String nameText,
165             StackTraceWriter stackTraceWriter,
166             Integer elapsed,
167             Map<String, String> systemProperties) {
168         this(
169                 runMode,
170                 testRunId,
171                 source,
172                 sourceText,
173                 name,
174                 nameText,
175                 stackTraceWriter,
176                 elapsed,
177                 safeGetMessage(stackTraceWriter),
178                 systemProperties);
179     }
180 
181     public static SimpleReportEntry assumption(
182             RunMode runMode,
183             Long testRunId,
184             String source,
185             String sourceText,
186             String name,
187             String nameText,
188             String message) {
189         return new SimpleReportEntry(runMode, testRunId, source, sourceText, name, nameText, message);
190     }
191 
192     public static SimpleReportEntry ignored(
193             RunMode runMode,
194             Long testRunId,
195             String source,
196             String sourceText,
197             String name,
198             String nameText,
199             String message) {
200         return new SimpleReportEntry(runMode, testRunId, source, sourceText, name, nameText, message);
201     }
202 
203     public static SimpleReportEntry withException(
204             RunMode runMode,
205             Long testRunId,
206             String source,
207             String sourceText,
208             String name,
209             String nameText,
210             StackTraceWriter stackTraceWriter) {
211         return new SimpleReportEntry(runMode, testRunId, source, sourceText, name, nameText, stackTraceWriter);
212     }
213 
214     private static String safeGetMessage(StackTraceWriter stackTraceWriter) {
215         try {
216             SafeThrowable t = stackTraceWriter == null ? null : stackTraceWriter.getThrowable();
217             return t == null ? null : t.getMessage();
218         } catch (Throwable t) {
219             return t.getMessage();
220         }
221     }
222 
223     @Override
224     public String getSourceName() {
225         return source;
226     }
227 
228     @Override
229     public String getSourceText() {
230         return sourceText;
231     }
232 
233     @Override
234     public String getName() {
235         return name;
236     }
237 
238     @Override
239     public String getNameText() {
240         return nameText;
241     }
242 
243     @Override
244     public String getGroup() {
245         return null;
246     }
247 
248     @Override
249     public StackTraceWriter getStackTraceWriter() {
250         return stackTraceWriter;
251     }
252 
253     @Override
254     public Integer getElapsed() {
255         return elapsed;
256     }
257 
258     @Override
259     public int getElapsed(int fallback) {
260         return elapsed == null ? fallback : elapsed;
261     }
262 
263     @Override
264     public String toString() {
265         return "ReportEntry{" + "runMode='" + runMode + "', testRunId='" + testRunId
266                 + "', source='" + source + "', sourceText='" + sourceText
267                 + "', name='" + name + "', nameText='" + nameText + "', stackTraceWriter='" + stackTraceWriter
268                 + "', elapsed='" + elapsed + "', message='" + message + "'}";
269     }
270 
271     @Override
272     public String getMessage() {
273         return message;
274     }
275 
276     @Override
277     public boolean equals(Object o) {
278         if (this == o) {
279             return true;
280         }
281         if (o == null || getClass() != o.getClass()) {
282             return false;
283         }
284 
285         SimpleReportEntry that = (SimpleReportEntry) o;
286         return isRunModeEqual(that)
287                 && isTestRunIdEqual(that)
288                 && isSourceEqual(that)
289                 && isSourceTextEqual(that)
290                 && isNameEqual(that)
291                 && isNameTextEqual(that)
292                 && isStackEqual(that)
293                 && isElapsedTimeEqual(that)
294                 && isSystemPropertiesEqual(that)
295                 && isMessageEqual(that);
296     }
297 
298     @Override
299     public int hashCode() {
300         int result = Objects.hashCode(getSourceName());
301         result = 31 * result + Objects.hashCode(getRunMode());
302         result = 31 * result + Objects.hashCode(getTestRunId());
303         result = 31 * result + Objects.hashCode(getSourceText());
304         result = 31 * result + Objects.hashCode(getName());
305         result = 31 * result + Objects.hashCode(getNameText());
306         result = 31 * result + Objects.hashCode(getStackTraceWriter());
307         result = 31 * result + Objects.hashCode(getElapsed());
308         result = 31 * result + Objects.hashCode(getSystemProperties());
309         result = 31 * result + Objects.hashCode(getMessage());
310         return result;
311     }
312 
313     @Override
314     public String getNameWithGroup() {
315         return getSourceName();
316     }
317 
318     @Override
319     public String getReportNameWithGroup() {
320         return getSourceText();
321     }
322 
323     @Override
324     @Nonnull
325     public final RunMode getRunMode() {
326         return runMode;
327     }
328 
329     @Override
330     public final Long getTestRunId() {
331         return testRunId;
332     }
333 
334     @Override
335     public Map<String, String> getSystemProperties() {
336         return systemProperties;
337     }
338 
339     private boolean isRunModeEqual(SimpleReportEntry en) {
340         return Objects.equals(getRunMode(), en.getRunMode());
341     }
342 
343     private boolean isTestRunIdEqual(SimpleReportEntry en) {
344         return Objects.equals(getTestRunId(), en.getTestRunId());
345     }
346 
347     private boolean isElapsedTimeEqual(SimpleReportEntry en) {
348         return Objects.equals(getElapsed(), en.getElapsed());
349     }
350 
351     private boolean isNameTextEqual(SimpleReportEntry en) {
352         return Objects.equals(getNameText(), en.getNameText());
353     }
354 
355     private boolean isNameEqual(SimpleReportEntry en) {
356         return Objects.equals(getName(), en.getName());
357     }
358 
359     private boolean isSourceEqual(SimpleReportEntry en) {
360         return Objects.equals(getSourceName(), en.getSourceName());
361     }
362 
363     private boolean isSourceTextEqual(SimpleReportEntry en) {
364         return Objects.equals(getSourceText(), en.getSourceText());
365     }
366 
367     private boolean isStackEqual(SimpleReportEntry en) {
368         return Objects.equals(getStackTraceWriter(), en.getStackTraceWriter());
369     }
370 
371     private boolean isSystemPropertiesEqual(SimpleReportEntry en) {
372         return Objects.equals(getSystemProperties(), en.getSystemProperties());
373     }
374 
375     private boolean isMessageEqual(SimpleReportEntry en) {
376         return Objects.equals(getMessage(), en.getMessage());
377     }
378 }