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 sourceQualifiedName;
46  
47      private final String name;
48  
49      private final String nameText;
50  
51      private final StackTraceWriter stackTraceWriter;
52  
53      private final Integer elapsed;
54  
55      private final String message;
56  
57      public SimpleReportEntry(
58              @Nonnull RunMode runMode, Long testRunId, String source, String sourceText, String name, String nameText) {
59          this(runMode, testRunId, source, sourceText, name, nameText, null, null);
60      }
61  
62      public SimpleReportEntry(
63              @Nonnull RunMode runMode,
64              Long testRunId,
65              String source,
66              String sourceText,
67              String name,
68              String nameText,
69              Map<String, String> systemProperties) {
70          this(runMode, testRunId, source, sourceText, name, nameText, null, null, systemProperties);
71      }
72  
73      private SimpleReportEntry(
74              @Nonnull RunMode runMode,
75              Long testRunId,
76              String source,
77              String sourceText,
78              String name,
79              String nameText,
80              StackTraceWriter stackTraceWriter) {
81          this(runMode, testRunId, source, sourceText, name, nameText, stackTraceWriter, null);
82      }
83  
84      public SimpleReportEntry(
85              @Nonnull RunMode runMode,
86              Long testRunId,
87              String source,
88              String sourceText,
89              String name,
90              String nameText,
91              Integer elapsed) {
92          this(runMode, testRunId, source, sourceText, name, nameText, null, elapsed);
93      }
94  
95      public SimpleReportEntry(
96              @Nonnull RunMode runMode,
97              Long testRunId,
98              String source,
99              String sourceText,
100             String name,
101             String nameText,
102             String message) {
103         this(
104                 runMode,
105                 testRunId,
106                 source,
107                 sourceText,
108                 name,
109                 nameText,
110                 null,
111                 null,
112                 message,
113                 Collections.<String, String>emptyMap());
114     }
115 
116     public SimpleReportEntry(
117             @Nonnull RunMode runMode,
118             Long testRunId,
119             String source,
120             String sourceText,
121             String name,
122             String nameText,
123             StackTraceWriter stackTraceWriter,
124             Integer elapsed,
125             String message,
126             Map<String, String> systemProperties) {
127         this(
128                 runMode,
129                 testRunId,
130                 source,
131                 sourceText,
132                 null,
133                 name,
134                 nameText,
135                 stackTraceWriter,
136                 elapsed,
137                 message,
138                 systemProperties);
139     }
140 
141     public SimpleReportEntry(
142             @Nonnull RunMode runMode,
143             Long testRunId,
144             String source,
145             String sourceText,
146             String sourceQualifiedName,
147             String name,
148             String nameText,
149             StackTraceWriter stackTraceWriter,
150             Integer elapsed,
151             String message,
152             Map<String, String> systemProperties) {
153         this.runMode = runMode;
154         this.testRunId = testRunId;
155         this.source = source;
156         this.sourceText = sourceText;
157         this.sourceQualifiedName = sourceQualifiedName;
158         this.name = name;
159         this.nameText = nameText;
160         this.stackTraceWriter = stackTraceWriter;
161         this.message = message;
162         this.elapsed = elapsed;
163         this.systemProperties = new ImmutableMap<>(systemProperties);
164     }
165 
166     public SimpleReportEntry(
167             @Nonnull RunMode runMode,
168             Long testRunId,
169             String source,
170             String sourceText,
171             String name,
172             String nameText,
173             StackTraceWriter stackTraceWriter,
174             Integer elapsed) {
175         this(
176                 runMode,
177                 testRunId,
178                 source,
179                 sourceText,
180                 name,
181                 nameText,
182                 stackTraceWriter,
183                 elapsed,
184                 Collections.<String, String>emptyMap());
185     }
186 
187     public SimpleReportEntry(
188             @Nonnull RunMode runMode,
189             Long testRunId,
190             String source,
191             String sourceText,
192             String name,
193             String nameText,
194             StackTraceWriter stackTraceWriter,
195             Integer elapsed,
196             Map<String, String> systemProperties) {
197         this(
198                 runMode,
199                 testRunId,
200                 source,
201                 sourceText,
202                 name,
203                 nameText,
204                 stackTraceWriter,
205                 elapsed,
206                 safeGetMessage(stackTraceWriter),
207                 systemProperties);
208     }
209 
210     public static SimpleReportEntry assumption(
211             RunMode runMode,
212             Long testRunId,
213             String source,
214             String sourceText,
215             String name,
216             String nameText,
217             String message) {
218         return new SimpleReportEntry(runMode, testRunId, source, sourceText, name, nameText, message);
219     }
220 
221     public static SimpleReportEntry ignored(
222             RunMode runMode,
223             Long testRunId,
224             String source,
225             String sourceText,
226             String name,
227             String nameText,
228             String message) {
229         return new SimpleReportEntry(runMode, testRunId, source, sourceText, name, nameText, message);
230     }
231 
232     public static SimpleReportEntry withException(
233             RunMode runMode,
234             Long testRunId,
235             String source,
236             String sourceText,
237             String name,
238             String nameText,
239             StackTraceWriter stackTraceWriter) {
240         return new SimpleReportEntry(runMode, testRunId, source, sourceText, name, nameText, stackTraceWriter);
241     }
242 
243     private static String safeGetMessage(StackTraceWriter stackTraceWriter) {
244         try {
245             SafeThrowable t = stackTraceWriter == null ? null : stackTraceWriter.getThrowable();
246             return t == null ? null : t.getMessage();
247         } catch (Throwable t) {
248             return t.getMessage();
249         }
250     }
251 
252     @Override
253     public String getSourceName() {
254         return source;
255     }
256 
257     @Override
258     public String getSourceText() {
259         return sourceText;
260     }
261 
262     @Override
263     public String getSourceQualifiedName() {
264         return sourceQualifiedName;
265     }
266 
267     @Override
268     public String getName() {
269         return name;
270     }
271 
272     @Override
273     public String getNameText() {
274         return nameText;
275     }
276 
277     @Override
278     public String getGroup() {
279         return null;
280     }
281 
282     @Override
283     public StackTraceWriter getStackTraceWriter() {
284         return stackTraceWriter;
285     }
286 
287     @Override
288     public Integer getElapsed() {
289         return elapsed;
290     }
291 
292     @Override
293     public int getElapsed(int fallback) {
294         return elapsed == null ? fallback : elapsed;
295     }
296 
297     @Override
298     public String toString() {
299         return "ReportEntry{" + "runMode='" + runMode + "', testRunId='" + testRunId
300                 + "', source='" + source + "', sourceText='" + sourceText
301                 + "', name='" + name + "', nameText='" + nameText + "', stackTraceWriter='" + stackTraceWriter
302                 + "', elapsed='" + elapsed + "', message='" + message + "'}";
303     }
304 
305     @Override
306     public String getMessage() {
307         return message;
308     }
309 
310     @Override
311     public boolean equals(Object o) {
312         if (this == o) {
313             return true;
314         }
315         if (o == null || getClass() != o.getClass()) {
316             return false;
317         }
318 
319         SimpleReportEntry that = (SimpleReportEntry) o;
320         return isRunModeEqual(that)
321                 && isTestRunIdEqual(that)
322                 && isSourceEqual(that)
323                 && isSourceTextEqual(that)
324                 && isNameEqual(that)
325                 && isNameTextEqual(that)
326                 && isStackEqual(that)
327                 && isElapsedTimeEqual(that)
328                 && isSystemPropertiesEqual(that)
329                 && isMessageEqual(that);
330     }
331 
332     @Override
333     public int hashCode() {
334         int result = Objects.hashCode(getSourceName());
335         result = 31 * result + Objects.hashCode(getRunMode());
336         result = 31 * result + Objects.hashCode(getTestRunId());
337         result = 31 * result + Objects.hashCode(getSourceText());
338         result = 31 * result + Objects.hashCode(getName());
339         result = 31 * result + Objects.hashCode(getNameText());
340         result = 31 * result + Objects.hashCode(getStackTraceWriter());
341         result = 31 * result + Objects.hashCode(getElapsed());
342         result = 31 * result + Objects.hashCode(getSystemProperties());
343         result = 31 * result + Objects.hashCode(getMessage());
344         return result;
345     }
346 
347     @Override
348     public String getNameWithGroup() {
349         return getSourceName();
350     }
351 
352     @Override
353     public String getReportNameWithGroup() {
354         return getSourceText();
355     }
356 
357     @Override
358     @Nonnull
359     public final RunMode getRunMode() {
360         return runMode;
361     }
362 
363     @Override
364     public final Long getTestRunId() {
365         return testRunId;
366     }
367 
368     @Override
369     public Map<String, String> getSystemProperties() {
370         return systemProperties;
371     }
372 
373     private boolean isRunModeEqual(SimpleReportEntry en) {
374         return Objects.equals(getRunMode(), en.getRunMode());
375     }
376 
377     private boolean isTestRunIdEqual(SimpleReportEntry en) {
378         return Objects.equals(getTestRunId(), en.getTestRunId());
379     }
380 
381     private boolean isElapsedTimeEqual(SimpleReportEntry en) {
382         return Objects.equals(getElapsed(), en.getElapsed());
383     }
384 
385     private boolean isNameTextEqual(SimpleReportEntry en) {
386         return Objects.equals(getNameText(), en.getNameText());
387     }
388 
389     private boolean isNameEqual(SimpleReportEntry en) {
390         return Objects.equals(getName(), en.getName());
391     }
392 
393     private boolean isSourceEqual(SimpleReportEntry en) {
394         return Objects.equals(getSourceName(), en.getSourceName());
395     }
396 
397     private boolean isSourceTextEqual(SimpleReportEntry en) {
398         return Objects.equals(getSourceText(), en.getSourceText());
399     }
400 
401     private boolean isStackEqual(SimpleReportEntry en) {
402         return Objects.equals(getStackTraceWriter(), en.getStackTraceWriter());
403     }
404 
405     private boolean isSystemPropertiesEqual(SimpleReportEntry en) {
406         return Objects.equals(getSystemProperties(), en.getSystemProperties());
407     }
408 
409     private boolean isMessageEqual(SimpleReportEntry en) {
410         return Objects.equals(getMessage(), en.getMessage());
411     }
412 }