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.Iterator;
24  import java.util.List;
25  import org.apache.maven.surefire.booter.StartupReportConfiguration;
26  import org.apache.maven.surefire.report.AbstractConsoleReporter;
27  import org.apache.maven.surefire.report.AbstractFileReporter;
28  import org.apache.maven.surefire.report.ConsoleLogger;
29  import org.apache.maven.surefire.report.DefaultDirectConsoleReporter;
30  import org.apache.maven.surefire.report.MulticastingReporter;
31  import org.apache.maven.surefire.report.Reporter;
32  import org.apache.maven.surefire.report.ReporterConfiguration;
33  import org.apache.maven.surefire.report.ReporterFactory;
34  import org.apache.maven.surefire.report.RunListener;
35  import org.apache.maven.surefire.report.RunStatistics;
36  import org.apache.maven.surefire.report.TestSetRunListener;
37  import org.apache.maven.surefire.report.XMLReporter;
38  import org.apache.maven.surefire.suite.RunResult;
39  
40  /**
41   * Provides RunListener implementations to the providers.
42   * <p/>
43   * Keeps a centralized count of test run results.
44   *
45   * @author Kristian Rosenvold
46   */
47  public class FileReporterFactory
48      implements ReporterFactory
49  {
50  
51      private final ReporterConfiguration reporterConfiguration;
52  
53      private final RunStatistics globalStats = new RunStatistics();
54  
55      private final MulticastingReporter multicastingReporter;
56  
57      private final StartupReportConfiguration reportConfiguration;
58  
59      public FileReporterFactory( StartupReportConfiguration reportConfiguration )
60      {
61          this.reportConfiguration = reportConfiguration;
62          this.reporterConfiguration = getReporterConfiguration();
63          multicastingReporter = new MulticastingReporter( instantiateReports() );
64          runStarting();
65      }
66  
67      private ReporterConfiguration getReporterConfiguration()
68      {
69          //noinspection BooleanConstructorCall
70          return new ReporterConfiguration( reportConfiguration.getReportsDirectory(),
71                                            new Boolean( reportConfiguration.isTrimStackTrace() ) );
72      }
73  
74      public RunListener createReporter()
75      {
76          return new TestSetRunListener( instantiateConsoleReporter(), instantiateFileReporter(),
77                                         instantiateXmlReporter(), instantiateConsoleOutputFileReporter(), globalStats );
78      }
79  
80      private AbstractConsoleReporter instantiateConsoleReporter()
81      {
82          return reportConfiguration.instantiateConsoleReporter();
83      }
84  
85      private AbstractFileReporter instantiateFileReporter()
86      {
87          return reportConfiguration.instantiateFileReporter();
88      }
89  
90      private XMLReporter instantiateXmlReporter()
91      {
92          return reportConfiguration.instantiateXmlReporter();
93      }
94  
95      private Reporter instantiateConsoleOutputFileReporter()
96      {
97          return reportConfiguration.instantiateConsoleOutputFileReporterName(
98              reporterConfiguration.getOriginalSystemOut() );
99      }
100 
101     private List instantiateReports()
102     {
103         List result = new ArrayList();
104         addIfNotNull( result, instantiateConsoleReporter() );
105         addIfNotNull( result, instantiateFileReporter() );
106         addIfNotNull( result, instantiateXmlReporter() );
107         addIfNotNull( result, instantiateConsoleOutputFileReporter() );
108         return result;
109     }
110 
111     private void addIfNotNull( List result, Reporter reporter )
112     {
113         if ( reporter != null )
114         {
115             result.add( reporter );
116         }
117     }
118 
119     public RunResult close()
120     {
121         runCompleted();
122         return globalStats.getRunResult();
123     }
124 
125     private ConsoleLogger createConsoleLogger()
126     {
127         return new DefaultDirectConsoleReporter( reporterConfiguration.getOriginalSystemOut() );
128     }
129 
130     public void runStarting()
131     {
132         final ConsoleLogger consoleReporter = createConsoleLogger();
133         consoleReporter.info( "" );
134         consoleReporter.info( "-------------------------------------------------------" );
135         consoleReporter.info( " T E S T S" );
136         consoleReporter.info( "-------------------------------------------------------" );
137     }
138 
139     private void runCompleted()
140     {
141         final ConsoleLogger logger = createConsoleLogger();
142         logger.info( "" );
143         logger.info( "Results :" );
144         logger.info( "" );
145         if ( globalStats.hadFailures() )
146         {
147             multicastingReporter.writeMessage( "Failed tests: " );
148             for ( Iterator iterator = this.globalStats.getFailureSources().iterator(); iterator.hasNext(); )
149             {
150                 logger.info( "  " + iterator.next() );
151             }
152             logger.info( "" );
153         }
154         if ( globalStats.hadErrors() )
155         {
156             logger.info( "Tests in error: " );
157             for ( Iterator iterator = this.globalStats.getErrorSources().iterator(); iterator.hasNext(); )
158             {
159                 logger.info( "  " + iterator.next() );
160             }
161             logger.info( "" );
162         }
163         logger.info( globalStats.getSummary() );
164         logger.info( "" );
165     }
166 
167     public RunStatistics getGlobalRunStatistics()
168     {
169         return globalStats;
170     }
171 }