View Javadoc

1   package org.apache.maven.plugin.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.Collections;
24  import java.util.List;
25  import org.apache.maven.plugin.surefire.StartupReportConfiguration;
26  import org.apache.maven.plugin.surefire.runorder.StatisticsReporter;
27  import org.apache.maven.surefire.report.DefaultDirectConsoleReporter;
28  import org.apache.maven.surefire.report.ReporterFactory;
29  import org.apache.maven.surefire.report.RunListener;
30  import org.apache.maven.surefire.report.RunStatistics;
31  import org.apache.maven.surefire.suite.RunResult;
32  
33  /**
34   * Provides reporting modules on the plugin side.
35   * <p/>
36   * Keeps a centralized count of test run results.
37   *
38   * @author Kristian Rosenvold
39   */
40  public class DefaultReporterFactory
41      implements ReporterFactory
42  {
43  
44      private final RunStatistics globalStats = new RunStatistics();
45  
46      private final StartupReportConfiguration reportConfiguration;
47  
48      private final StatisticsReporter statisticsReporter;
49  
50      private final List<TestSetRunListener> listeners =
51          Collections.synchronizedList( new ArrayList<TestSetRunListener>() );
52  
53      public DefaultReporterFactory( StartupReportConfiguration reportConfiguration )
54      {
55          this.reportConfiguration = reportConfiguration;
56          this.statisticsReporter = reportConfiguration.instantiateStatisticsReporter();
57          runStarting();
58      }
59  
60      public RunListener createReporter()
61      {
62          return createTestSetRunListener();
63      }
64  
65      public RunListener createTestSetRunListener()
66      {
67          TestSetRunListener testSetRunListener =
68              new TestSetRunListener( reportConfiguration.instantiateConsoleReporter(),
69                                      reportConfiguration.instantiateFileReporter(),
70                                      reportConfiguration.instantiateStatelessXmlReporter(),
71                                      reportConfiguration.instantiateConsoleOutputFileReporter(), statisticsReporter,
72                                      globalStats, reportConfiguration.isTrimStackTrace(),
73                                      ConsoleReporter.PLAIN.equals( reportConfiguration.getReportFormat() ),
74                                      reportConfiguration.isBriefOrPlainFormat() );
75          listeners.add( testSetRunListener );
76          return testSetRunListener;
77      }
78  
79      public RunResult close()
80      {
81          runCompleted();
82          for ( TestSetRunListener listener : listeners )
83          {
84              listener.close();
85          }
86          return globalStats.getRunResult();
87      }
88  
89      private DefaultDirectConsoleReporter createConsoleLogger()
90      {
91          return new DefaultDirectConsoleReporter( reportConfiguration.getOriginalSystemOut() );
92      }
93  
94      public void runStarting()
95      {
96          final DefaultDirectConsoleReporter consoleReporter = createConsoleLogger();
97          consoleReporter.info( "" );
98          consoleReporter.info( "-------------------------------------------------------" );
99          consoleReporter.info( " T E S T S" );
100         consoleReporter.info( "-------------------------------------------------------" );
101     }
102 
103     private void runCompleted()
104     {
105         final DefaultDirectConsoleReporter logger = createConsoleLogger();
106         if ( reportConfiguration.isPrintSummary() )
107         {
108             logger.info( "" );
109             logger.info( "Results :" );
110             logger.info( "" );
111         }
112         if ( globalStats.hadFailures() )
113         {
114             logger.info( "Failed tests: " );
115             for ( Object o : this.globalStats.getFailureSources() )
116             {
117                 logger.info( "  " + o );
118             }
119             logger.info( "" );
120         }
121         if ( globalStats.hadErrors() )
122         {
123             logger.info( "Tests in error: " );
124             for ( Object o : this.globalStats.getErrorSources() )
125             {
126                 logger.info( "  " + o );
127             }
128             logger.info( "" );
129         }
130         logger.info( globalStats.getSummary() );
131         logger.info( "" );
132     }
133 
134     public RunStatistics getGlobalRunStatistics()
135     {
136         return globalStats;
137     }
138 
139     public static DefaultReporterFactory defaultNoXml()
140     {
141         return new DefaultReporterFactory( StartupReportConfiguration.defaultNoXml() );
142     }
143 }