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