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