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