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 org.apache.maven.surefire.util.internal.StringUtils;
23  
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.Collections;
27  
28  /**
29   * @author Kristian Rosenvold
30   */
31  public class RunStatistics
32      extends TestSetStatistics
33  {
34      /**
35       * Holds the source(s) that causes the error(s).
36       */
37      private final Sources errorSources = new Sources();
38  
39      /**
40       * Holds the source(s) that causes the failure(s).
41       */
42      private final Sources failureSources = new Sources();
43  
44  
45      // Todo remove when building with 2.7.2
46      public void addErrorSource( String errorSource )
47      {
48          errorSources.addSource( errorSource );
49      }
50  
51      public void addErrorSource( String errorSource, StackTraceWriter stackTraceWriter )
52      {
53          errorSources.addSource( errorSource, stackTraceWriter );
54      }
55  
56      // Todo remove when building with 2.7.2
57      public void addFailureSource( String failureSource )
58      {
59      	failureSources.addSource( failureSource );
60      }
61  
62      public void addFailureSource( String failureSource, StackTraceWriter stackTraceWriter  )
63      {
64      	failureSources.addSource( failureSource, stackTraceWriter );
65      }
66  
67      public Collection getErrorSources()
68      {
69          return errorSources.getListOfSources();
70      }
71  
72      public Collection getFailureSources()
73      {
74          return failureSources.getListOfSources();
75      }
76  
77      private static class Sources
78      {
79          private final Collection listOfSources = new ArrayList();
80  
81          void addSource( String source )
82          {
83              synchronized ( listOfSources )
84              {
85                  listOfSources.add( source );  		
86              }
87          }
88  
89          void addSource( String source, StackTraceWriter stackTraceWriter )
90          {
91              String message = getMessageOfThrowable( stackTraceWriter );
92              String extendedSource = StringUtils.isBlank( message ) ? source : source + ": " + trimToSingleLine(message);
93              addSource( extendedSource );
94          }
95  
96          private String trimToSingleLine(String str){
97              int i = str.indexOf( "\n" );
98              return i >= 0 ? str.substring( 0, i ) : str;
99          }
100 
101         Collection getListOfSources()
102         {
103             synchronized ( listOfSources )
104             {
105                 return Collections.unmodifiableCollection( listOfSources );
106             }
107         }
108 
109         private String getMessageOfThrowable( StackTraceWriter stackTraceWriter )
110         {
111             //noinspection ThrowableResultOfMethodCallIgnored
112             return stackTraceWriter != null ? getMessageOfThrowable( stackTraceWriter.getThrowable() ) : "";
113         }
114 
115         private String getMessageOfThrowable( Throwable throwable )
116         {
117             return throwable != null ? throwable.getLocalizedMessage() : "";
118         }
119     }
120 }