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