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 java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.Collections;
25  import org.apache.maven.plugin.surefire.report.TestSetStats;
26  import org.apache.maven.surefire.suite.RunResult;
27  
28  /**
29   * @author Kristian Rosenvold
30   */
31  public class RunStatistics
32  {
33      /**
34       * Holds the source(s) that causes the error(s).
35       */
36      private final Sources errorSources = new Sources();
37  
38      /**
39       * Holds the source(s) that causes the failure(s).
40       */
41      private final Sources failureSources = new Sources();
42  
43      private int completedCount;
44  
45      private int errors;
46  
47      private int failures;
48  
49      private int skipped;
50  
51  
52      public void addErrorSource( StackTraceWriter stackTraceWriter )
53      {
54          if ( stackTraceWriter == null )
55          {
56              throw new IllegalArgumentException( "Cant be null" );
57          }
58          errorSources.addSource( stackTraceWriter );
59      }
60  
61      public void addFailureSource( StackTraceWriter stackTraceWriter )
62      {
63          if ( stackTraceWriter == null )
64          {
65              throw new IllegalArgumentException( "Cant be null" );
66          }
67          failureSources.addSource( stackTraceWriter );
68      }
69  
70      public Collection<String> getErrorSources()
71      {
72          return errorSources.getListOfSources();
73      }
74  
75      public Collection<String> getFailureSources()
76      {
77          return failureSources.getListOfSources();
78      }
79  
80      public synchronized boolean hadFailures()
81      {
82          return failures > 0;
83      }
84  
85      public synchronized boolean hadErrors()
86      {
87          return errors > 0;
88      }
89  
90      public synchronized int getCompletedCount()
91      {
92          return completedCount;
93      }
94  
95      public synchronized int getSkipped()
96      {
97          return skipped;
98      }
99  
100     public synchronized void add( TestSetStats testSetStats )
101     {
102         this.completedCount += testSetStats.getCompletedCount();
103         this.errors += testSetStats.getErrors();
104         this.failures += testSetStats.getFailures();
105         this.skipped += testSetStats.getSkipped();
106     }
107 
108     public synchronized RunResult getRunResult()
109     {
110         return new RunResult( completedCount, errors, failures, skipped );
111     }
112 
113     public synchronized String getSummary()
114     {
115         return "Tests run: " + completedCount + ", Failures: " + failures + ", Errors: " + errors + ", Skipped: "
116             + skipped;
117     }
118 
119 
120     private static class Sources
121     {
122         private final Collection<String> listOfSources = new ArrayList<String>();
123 
124         void addSource( String source )
125         {
126             synchronized ( listOfSources )
127             {
128                 listOfSources.add( source );
129             }
130         }
131 
132         void addSource( StackTraceWriter stackTraceWriter )
133         {
134             addSource( stackTraceWriter.smartTrimmedStackTrace() );
135         }
136 
137         Collection<String> getListOfSources()
138         {
139             synchronized ( listOfSources )
140             {
141                 return Collections.unmodifiableCollection( listOfSources );
142             }
143         }
144     }
145 }