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