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 java.util.Collections;
25  import java.util.Map;
26  import java.util.Objects;
27  
28  /**
29   * Basic implementation of {@link TestSetReportEntry} (immutable and thread-safe object).
30   *
31   * @author Kristian Rosenvold
32   */
33  public class SimpleReportEntry
34      implements TestSetReportEntry
35  {
36      private final Map<String, String> systemProperties;
37  
38      private final String source;
39  
40      private final String sourceText;
41  
42      private final String name;
43  
44      private final String nameText;
45  
46      private final StackTraceWriter stackTraceWriter;
47  
48      private final Integer elapsed;
49  
50      private final String message;
51  
52      public SimpleReportEntry( String source, String sourceText, String name, String nameText )
53      {
54          this( source, sourceText, name, nameText, null, null );
55      }
56  
57      public SimpleReportEntry( String source, String sourceText, String name, String nameText,
58                                Map<String, String> systemProperties )
59      {
60          this( source, sourceText, name, nameText, null, null, systemProperties );
61      }
62  
63      private SimpleReportEntry( String source, String sourceText, String name, String nameText,
64                                 StackTraceWriter stackTraceWriter )
65      {
66          this( source, sourceText, name, nameText, stackTraceWriter, null );
67      }
68  
69      public SimpleReportEntry( String source, String sourceText, String name, String nameText, Integer elapsed )
70      {
71          this( source, sourceText, name, nameText, null, elapsed );
72      }
73  
74      public SimpleReportEntry( String source, String sourceText, String name, String nameText, String message )
75      {
76          this( source, sourceText, name, nameText, null, null, message, Collections.<String, String>emptyMap() );
77      }
78  
79      public SimpleReportEntry( String source, String sourceText, String name, String nameText,
80                                   StackTraceWriter stackTraceWriter, Integer elapsed, String message,
81                                   Map<String, String> systemProperties )
82      {
83          this.source = source;
84          this.sourceText = sourceText;
85          this.name = name;
86          this.nameText = nameText;
87          this.stackTraceWriter = stackTraceWriter;
88          this.message = message;
89          this.elapsed = elapsed;
90          this.systemProperties = new ImmutableMap<>( systemProperties );
91      }
92  
93      public SimpleReportEntry( String source, String sourceText, String name, String nameText,
94                                StackTraceWriter stackTraceWriter, Integer elapsed )
95      {
96          this( source, sourceText, name, nameText, stackTraceWriter, elapsed, Collections.<String, String>emptyMap() );
97      }
98  
99      public SimpleReportEntry( String source, String sourceText, String name, String nameText,
100                               StackTraceWriter stackTraceWriter, Integer elapsed, Map<String, String> systemProperties )
101     {
102         this( source, sourceText, name, nameText,
103                 stackTraceWriter, elapsed, safeGetMessage( stackTraceWriter ), systemProperties );
104     }
105 
106     public static SimpleReportEntry assumption( String source, String sourceText, String name, String nameText,
107                                                 String message )
108     {
109         return new SimpleReportEntry( source, sourceText, name, nameText, message );
110     }
111 
112     public static SimpleReportEntry ignored( String source, String sourceText, String name, String nameText,
113                                              String message )
114     {
115         return new SimpleReportEntry( source, sourceText, name, nameText, message );
116     }
117 
118     public static SimpleReportEntry withException( String source, String sourceText, String name, String nameText,
119                                                    StackTraceWriter stackTraceWriter )
120     {
121         return new SimpleReportEntry( source, sourceText, name, nameText, stackTraceWriter );
122     }
123 
124     private static String safeGetMessage( StackTraceWriter stackTraceWriter )
125     {
126         try
127         {
128             SafeThrowable t = stackTraceWriter == null ? null : stackTraceWriter.getThrowable();
129             return t == null ? null : t.getMessage();
130         }
131         catch ( Throwable t )
132         {
133             return t.getMessage();
134         }
135     }
136 
137     @Override
138     public String getSourceName()
139     {
140         return source;
141     }
142 
143     @Override
144     public String getSourceText()
145     {
146         return sourceText;
147     }
148 
149     @Override
150     public String getName()
151     {
152         return name;
153     }
154 
155     @Override
156     public String getNameText()
157     {
158         return nameText;
159     }
160 
161     @Override
162     public String getGroup()
163     {
164         return null;
165     }
166 
167     @Override
168     public StackTraceWriter getStackTraceWriter()
169     {
170         return stackTraceWriter;
171     }
172 
173     @Override
174     public Integer getElapsed()
175     {
176         return elapsed;
177     }
178 
179     @Override
180     public int getElapsed( int fallback )
181     {
182         return elapsed == null ? fallback : elapsed;
183     }
184 
185     @Override
186     public String toString()
187     {
188         return "ReportEntry{" + "source='" + source + "', sourceText='" + sourceText
189                 + "', name='" + name + "', nameText='" + nameText + "', stackTraceWriter='"
190                 + stackTraceWriter + "', elapsed='" + elapsed + "', message='" + message + "'}";
191     }
192 
193     @Override
194     public String getMessage()
195     {
196         return message;
197     }
198 
199     @Override
200     public boolean equals( Object o )
201     {
202         if ( this == o )
203         {
204             return true;
205         }
206         if ( o == null || getClass() != o.getClass() )
207         {
208             return false;
209         }
210 
211         SimpleReportEntry that = (SimpleReportEntry) o;
212         return isSourceEqual( that ) && isSourceTextEqual( that )
213                 && isNameEqual( that ) && isNameTextEqual( that )
214                 && isStackEqual( that )
215                 && isElapsedTimeEqual( that )
216                 && isSystemPropertiesEqual( that )
217                 && isMessageEqual( that );
218     }
219 
220     @Override
221     public int hashCode()
222     {
223         int result = Objects.hashCode( getSourceName() );
224         result = 31 * result + Objects.hashCode( getSourceText() );
225         result = 31 * result + Objects.hashCode( getName() );
226         result = 31 * result + Objects.hashCode( getNameText() );
227         result = 31 * result + Objects.hashCode( getStackTraceWriter() );
228         result = 31 * result + Objects.hashCode( getElapsed() );
229         result = 31 * result + Objects.hashCode( getSystemProperties() );
230         result = 31 * result + Objects.hashCode( getMessage() );
231         return result;
232     }
233 
234     @Override
235     public String getNameWithGroup()
236     {
237         return getSourceName();
238     }
239 
240     @Override
241     public String getReportNameWithGroup()
242     {
243         return getSourceText();
244     }
245 
246     @Override
247     public Map<String, String> getSystemProperties()
248     {
249         return systemProperties;
250     }
251 
252     private boolean isElapsedTimeEqual( SimpleReportEntry en )
253     {
254         return Objects.equals( getElapsed(), en.getElapsed() );
255     }
256 
257     private boolean isNameTextEqual( SimpleReportEntry en )
258     {
259         return Objects.equals( getNameText(), en.getNameText() );
260     }
261 
262     private boolean isNameEqual( SimpleReportEntry en )
263     {
264         return Objects.equals( getName(), en.getName() );
265     }
266 
267     private boolean isSourceEqual( SimpleReportEntry en )
268     {
269         return Objects.equals( getSourceName(), en.getSourceName() );
270     }
271 
272     private boolean isSourceTextEqual( SimpleReportEntry en )
273     {
274         return Objects.equals( getSourceText(), en.getSourceText() );
275     }
276 
277     private boolean isStackEqual( SimpleReportEntry en )
278     {
279         return Objects.equals( getStackTraceWriter(), en.getStackTraceWriter() );
280     }
281 
282     private boolean isSystemPropertiesEqual( SimpleReportEntry en )
283     {
284         return Objects.equals( getSystemProperties(), en.getSystemProperties() );
285     }
286 
287     private boolean isMessageEqual( SimpleReportEntry en )
288     {
289         return Objects.equals( getMessage(), en.getMessage() );
290     }
291 }