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.surefire.util.internal.StringUtils;
26  
27  /**
28   * @author Kristian Rosenvold
29   */
30  public class RunStatistics
31      extends TestSetStatistics
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  
44      public void addErrorSource( String errorSource, StackTraceWriter stackTraceWriter )
45      {
46          errorSources.addSource( errorSource, stackTraceWriter );
47      }
48  
49      public void addFailureSource( String failureSource, StackTraceWriter stackTraceWriter )
50      {
51          failureSources.addSource( failureSource, stackTraceWriter );
52      }
53  
54      public Collection getErrorSources()
55      {
56          return errorSources.getListOfSources();
57      }
58  
59      public Collection getFailureSources()
60      {
61          return failureSources.getListOfSources();
62      }
63  
64  
65      private static class Sources
66      {
67          private final Collection listOfSources = new ArrayList();
68  
69          void addSource( String source )
70          {
71              synchronized ( listOfSources )
72              {
73                  listOfSources.add( source );
74              }
75          }
76  
77          void addSource( String source, StackTraceWriter stackTraceWriter )
78          {
79              String message = getMessageOfThrowable( stackTraceWriter );
80              String extendedSource =
81                  StringUtils.isBlank( message ) ? source : source + ": " + trimToSingleLine( message );
82              addSource( extendedSource );
83          }
84  
85          private String trimToSingleLine( String str )
86          {
87              int i = str.indexOf( "\n" );
88              return i >= 0 ? str.substring( 0, i ) : str;
89          }
90  
91          Collection getListOfSources()
92          {
93              synchronized ( listOfSources )
94              {
95                  return Collections.unmodifiableCollection( listOfSources );
96              }
97          }
98  
99          private String getMessageOfThrowable( StackTraceWriter stackTraceWriter )
100         {
101             //noinspection ThrowableResultOfMethodCallIgnored
102             return stackTraceWriter != null ? getMessageOfThrowable( stackTraceWriter.getThrowable() ) : "";
103         }
104 
105         private String getMessageOfThrowable( Throwable throwable )
106         {
107             return throwable != null ? throwable.getLocalizedMessage() : "";
108         }
109     }
110 }