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  /**
23   * @author Kristian Rosenvold
24   */
25  public class SimpleReportEntry
26      implements ReportEntry
27  {
28      private final String source;
29  
30      private final String name;
31  
32      private final StackTraceWriter stackTraceWriter;
33  
34      private final Integer elapsed;
35  
36      private final String message;
37  
38      public SimpleReportEntry()
39      {
40          this( null, null );
41      }
42  
43      public SimpleReportEntry( String source, String name )
44      {
45          this( source, name, null, null );
46      }
47  
48      private SimpleReportEntry( String source, String name, StackTraceWriter stackTraceWriter )
49      {
50          this( source, name, stackTraceWriter, null );
51      }
52  
53      public SimpleReportEntry( String source, String name, Integer elapsed )
54      {
55          this( source, name, null, elapsed );
56      }
57  
58      protected SimpleReportEntry( String source, String name, StackTraceWriter stackTraceWriter, Integer elapsed,
59                                   String message )
60      {
61          if ( source == null )
62          {
63              source = "null";
64          }
65          if ( name == null )
66          {
67              name = "null";
68          }
69  
70          this.source = source;
71  
72          this.name = name;
73  
74          this.stackTraceWriter = stackTraceWriter;
75  
76          this.message = message;
77  
78          this.elapsed = elapsed;
79      }
80  
81  
82      public SimpleReportEntry( String source, String name, StackTraceWriter stackTraceWriter, Integer elapsed )
83      {
84          //noinspection ThrowableResultOfMethodCallIgnored
85          this( source, name, stackTraceWriter, elapsed, safeGetMessage( stackTraceWriter ) );
86      }
87  
88      public static SimpleReportEntry ignored( String source, String name, String message )
89      {
90          return new SimpleReportEntry( source, name, null, null, message );
91      }
92  
93      public static SimpleReportEntry withException( String source, String name, StackTraceWriter stackTraceWriter )
94      {
95          return new SimpleReportEntry( source, name, stackTraceWriter );
96      }
97  
98      private static String safeGetMessage( StackTraceWriter stackTraceWriter )
99      {
100         try
101         {
102             SafeThrowable t = stackTraceWriter == null ? null : stackTraceWriter.getThrowable();
103             return t == null ? null : t.getMessage();
104         }
105         catch ( Throwable t )
106         {
107             return t.getMessage();
108         }
109     }
110 
111     public String getSourceName()
112     {
113         return source;
114     }
115 
116     public String getName()
117     {
118         return name;
119     }
120 
121     public String getGroup()
122     {
123         return null;
124     }
125 
126     public StackTraceWriter getStackTraceWriter()
127     {
128         return stackTraceWriter;
129     }
130 
131     public Integer getElapsed()
132     {
133         return elapsed;
134     }
135 
136     public String toString()
137     {
138         return "ReportEntry{" + "source='" + source + '\'' + ", name='" + name + '\'' + ", stackTraceWriter="
139             + stackTraceWriter + ", elapsed=" + elapsed + ",message=" + message + '}';
140     }
141 
142     public String getMessage()
143     {
144         return message;
145     }
146 
147     /**
148      * @noinspection RedundantIfStatement
149      */
150     public boolean equals( Object o )
151     {
152         if ( this == o )
153         {
154             return true;
155         }
156         if ( o == null || getClass() != o.getClass() )
157         {
158             return false;
159         }
160 
161         SimpleReportEntry that = (SimpleReportEntry) o;
162 
163         if ( elapsed != null ? !elapsed.equals( that.elapsed ) : that.elapsed != null )
164         {
165             return false;
166         }
167         if ( name != null ? !name.equals( that.name ) : that.name != null )
168         {
169             return false;
170         }
171         if ( source != null ? !source.equals( that.source ) : that.source != null )
172         {
173             return false;
174         }
175         if ( stackTraceWriter != null
176             ? !stackTraceWriter.equals( that.stackTraceWriter )
177             : that.stackTraceWriter != null )
178         {
179             return false;
180         }
181 
182         return true;
183     }
184 
185     public int hashCode()
186     {
187         int result = source != null ? source.hashCode() : 0;
188         result = 31 * result + ( name != null ? name.hashCode() : 0 );
189         result = 31 * result + ( stackTraceWriter != null ? stackTraceWriter.hashCode() : 0 );
190         result = 31 * result + ( elapsed != null ? elapsed.hashCode() : 0 );
191         return result;
192     }
193 
194     public String getNameWithGroup()
195     {
196         return getName();
197     }
198 }