View Javadoc
1   package org.apache.maven.surefire.suite;
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 java.io.ByteArrayOutputStream;
23  import java.io.PrintWriter;
24  
25  /**
26   * Represents a test-run-result; this may be from a single test run or an aggregated result.
27   * <br>
28   * In the case of timeout==true, the run-counts reflect the state of the test-run at the time
29   * of the timeout.
30   *
31   * @author Kristian Rosenvold
32   */
33  public class RunResult
34  {
35      private final int completedCount;
36  
37      private final int errors;
38  
39      private final int failures;
40  
41      private final int skipped;
42  
43      private final int flakes;
44  
45      private final String failure;
46  
47      private final boolean timeout;
48  
49      public static final int SUCCESS = 0;
50  
51      private static final int FAILURE = 255;
52  
53      private static final int NO_TESTS = 254;
54  
55      public static RunResult timeout( RunResult accumulatedAtTimeout )
56      {
57          return errorCode( accumulatedAtTimeout, accumulatedAtTimeout.getFailure(), true );
58      }
59  
60      public static RunResult failure( RunResult accumulatedAtTimeout, Exception cause )
61      {
62          return errorCode( accumulatedAtTimeout, getStackTrace( cause ), accumulatedAtTimeout.isTimeout() );
63      }
64  
65      private static RunResult errorCode( RunResult other, String failure, boolean timeout )
66      {
67          return new RunResult( other.getCompletedCount(), other.getErrors(), other.getFailures(), other.getSkipped(),
68                                      failure, timeout );
69      }
70  
71      public RunResult( int completedCount, int errors, int failures, int skipped )
72      {
73          this( completedCount, errors, failures, skipped, null, false );
74      }
75  
76      public RunResult( int completedCount, int errors, int failures, int skipped, int flakes )
77      {
78          this( completedCount, errors, failures, skipped, flakes, null, false );
79      }
80  
81      public RunResult( int completedCount, int errors, int failures, int skipped, String failure, boolean timeout )
82      {
83          this( completedCount, errors, failures, skipped, 0, failure, timeout );
84      }
85  
86      public RunResult( int completedCount, int errors, int failures, int skipped, int flakes, String failure,
87                        boolean timeout )
88      {
89          this.completedCount = completedCount;
90          this.errors = errors;
91          this.failures = failures;
92          this.skipped = skipped;
93          this.failure = failure;
94          this.timeout = timeout;
95          this.flakes = flakes;
96      }
97  
98      private static String getStackTrace( Exception e )
99      {
100         if ( e == null )
101         {
102             return null;
103         }
104         ByteArrayOutputStream out = new ByteArrayOutputStream();
105         PrintWriter pw = new PrintWriter( out );
106         try
107         {
108             e.printStackTrace( pw );
109             pw.flush();
110         }
111         finally
112         {
113             pw.close();
114         }
115         return new String( out.toByteArray() );
116     }
117 
118     public int getCompletedCount()
119     {
120         return completedCount;
121     }
122 
123     public int getErrors()
124     {
125         return errors;
126     }
127 
128     public int getFlakes()
129     {
130         return flakes;
131     }
132 
133     public int getFailures()
134     {
135         return failures;
136     }
137 
138     public int getSkipped()
139     {
140         return skipped;
141     }
142 
143     public Integer getFailsafeCode()  // Only used for compatibility reasons.
144     {
145         if ( completedCount == 0 )
146         {
147             return NO_TESTS;
148         }
149         if ( !isErrorFree() )
150         {
151             return FAILURE;
152         }
153         return null;
154     }
155 
156     /* Indicates if the tests are error free */
157     public boolean isErrorFree()
158     {
159         return getFailures() == 0 && getErrors() == 0 && !isFailure();
160     }
161 
162     public boolean isInternalError()
163     {
164         return getFailures() == 0 && getErrors() == 0 && isFailure();
165     }
166 
167     /* Indicates test timeout or technical failure */
168     public boolean isFailureOrTimeout()
169     {
170         return isTimeout() || isFailure();
171     }
172 
173     public boolean isFailure()
174     {
175         return failure != null;
176     }
177 
178     public String getFailure()
179     {
180         return failure;
181     }
182 
183     public boolean isTimeout()
184     {
185         return timeout;
186     }
187 
188     public RunResult aggregate( RunResult other )
189     {
190         String failureMessage = getFailure() != null ? getFailure() : other.getFailure();
191         boolean timeout = isTimeout() || other.isTimeout();
192         int completed = getCompletedCount() + other.getCompletedCount();
193         int fail = getFailures() + other.getFailures();
194         int ign = getSkipped() + other.getSkipped();
195         int err = getErrors() + other.getErrors();
196         int flakes = getFlakes() + other.getFlakes();
197         return new RunResult( completed, err, fail, ign, flakes, failureMessage, timeout );
198     }
199 
200     public static RunResult noTestsRun()
201     {
202         return new RunResult( 0, 0, 0, 0 );
203     }
204 
205     @Override
206     @SuppressWarnings( "RedundantIfStatement" )
207     public boolean equals( Object o )
208     {
209         if ( this == o )
210         {
211             return true;
212         }
213         if ( o == null || getClass() != o.getClass() )
214         {
215             return false;
216         }
217 
218         RunResult runResult = (RunResult) o;
219 
220         if ( completedCount != runResult.completedCount )
221         {
222             return false;
223         }
224         if ( errors != runResult.errors )
225         {
226             return false;
227         }
228         if ( failures != runResult.failures )
229         {
230             return false;
231         }
232         if ( skipped != runResult.skipped )
233         {
234             return false;
235         }
236         if ( timeout != runResult.timeout )
237         {
238             return false;
239         }
240         if ( failure != null ? !failure.equals( runResult.failure ) : runResult.failure != null )
241         {
242             return false;
243         }
244 
245         return true;
246     }
247 
248     @Override
249     public int hashCode()
250     {
251         int result = completedCount;
252         result = 31 * result + errors;
253         result = 31 * result + failures;
254         result = 31 * result + skipped;
255         result = 31 * result + ( failure != null ? failure.hashCode() : 0 );
256         result = 31 * result + ( timeout ? 1 : 0 );
257         return result;
258     }
259 }