Coverage Report - org.apache.maven.surefire.report.ReporterManagerFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
ReporterManagerFactory
0 %
0/50
0 %
0/14
2,333
 
 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.suite.RunResult;
 23  
 import org.apache.maven.surefire.util.ReflectionUtils;
 24  
 import org.apache.maven.surefire.util.SurefireReflectionException;
 25  
 
 26  
 import java.lang.reflect.Constructor;
 27  
 import java.util.ArrayList;
 28  
 import java.util.Iterator;
 29  
 import java.util.List;
 30  
 
 31  
 /**
 32  
  * Creates ReporterManager instances for the providers.
 33  
  * <p/>
 34  
  * A ReporterManager and the underlying reporters are stateful objects. For safe concurrent usage
 35  
  * of the reporting infrastructure, each thread needs its own instance.
 36  
  * <p/>
 37  
  * This factory also ensures that runStarting/runCompleted is called on the FIRST reporterManger that
 38  
  * is allocated, but none of the subsequent managers.
 39  
  *
 40  
  * @author Jason van Zyl
 41  
  * @author Kristian Rosenvold
 42  
  */
 43  
 
 44  
 /**
 45  
  * Note; this class contains "old" and "new" style instantiation, "old" style can be removed once we build 2.7.X + with 2.7
 46  
  */
 47  
 public class ReporterManagerFactory
 48  
     implements ReporterFactory
 49  
 {
 50  
     private final List reportDefinitions;
 51  
 
 52  
     private final ClassLoader surefireClassLoader;
 53  
 
 54  0
     private final RunStatistics globalRunStatistics = new RunStatistics();
 55  
 
 56  
     private final ReporterConfiguration reporterConfiguration;
 57  
 
 58  
     private RunReporter first;
 59  
 
 60  0
     private final Object lock = new Object();
 61  
 
 62  
     private final ConsoleOutputCapture consoleOutputCapture;
 63  
 
 64  
     public ReporterManagerFactory( ClassLoader surefireClassLoader, ReporterConfiguration reporterConfiguration )
 65  0
     {
 66  0
         this.reportDefinitions = reporterConfiguration.getReports();
 67  0
         this.surefireClassLoader = surefireClassLoader;
 68  0
         this.reporterConfiguration = reporterConfiguration;
 69  0
         SystemConsoleOutputReceiver systemConsoleOutputReceiver =
 70  
             new SystemConsoleOutputReceiver( reporterConfiguration.getOriginalSystemOut(),
 71  
                                              reporterConfiguration.getOriginalSystemErr() );
 72  
         // todo: Fix parallel (must keep quiet on default stream?)
 73  0
         final DefaultConsoleOutputReceiver target =
 74  
             new DefaultConsoleOutputReceiver( systemConsoleOutputReceiver, true );
 75  0
         this.consoleOutputCapture = new ConsoleOutputCapture( target );
 76  0
     }
 77  
 
 78  
     public RunStatistics getGlobalRunStatistics()
 79  
     {
 80  0
         return globalRunStatistics;
 81  
     }
 82  
 
 83  
     public RunListener createReporter()
 84  
     {
 85  0
         return setupReporter(
 86  
             instantiateReportsNewStyle( reportDefinitions, reporterConfiguration, surefireClassLoader ) );
 87  
     }
 88  
 
 89  
 
 90  
     private RunListener setupReporter( List reports )
 91  
     {
 92  
         // Note, if we ever start making >1 reporter Managers, we have to aggregate run statistics
 93  
         // i.e. we cannot use a single "globalRunStatistics"
 94  0
         final TestSetRunListener reporterManager = new TestSetRunListener( reports, globalRunStatistics );
 95  0
         if ( first == null )
 96  
         {
 97  0
             synchronized ( lock )
 98  
             {
 99  0
                 if ( first == null )
 100  
                 {
 101  0
                     first = reporterManager;
 102  0
                     reporterManager.runStarting();
 103  
                 }
 104  0
             }
 105  
         }
 106  0
         return reporterManager;
 107  
     }
 108  
 
 109  
 
 110  
     public RunResult close()
 111  
     {
 112  0
         consoleOutputCapture.restoreStreams();
 113  0
         warnIfNoTests();
 114  0
         synchronized ( lock )
 115  
         {
 116  0
             if ( first != null )
 117  
             {
 118  0
                 first.runCompleted();
 119  
             }
 120  0
             return globalRunStatistics.getRunResult();
 121  0
         }
 122  
     }
 123  
 
 124  
     protected List instantiateReportsNewStyle( List reportDefinitions, ReporterConfiguration reporterConfiguration,
 125  
                                                ClassLoader classLoader )
 126  
     {
 127  0
         List reports = new ArrayList();
 128  
 
 129  0
         for ( Iterator i = reportDefinitions.iterator(); i.hasNext(); )
 130  
         {
 131  
 
 132  0
             String className = (String) i.next();
 133  
 
 134  0
             Reporter report = instantiateReportNewStyle( className, reporterConfiguration, classLoader );
 135  
 
 136  0
             reports.add( report );
 137  0
         }
 138  
 
 139  0
         return reports;
 140  
     }
 141  
 
 142  
     private static Reporter instantiateReportNewStyle( String className, ReporterConfiguration params,
 143  
                                                        ClassLoader classLoader )
 144  
     {
 145  0
         Class clazz = ReflectionUtils.loadClass( classLoader, className );
 146  
 
 147  0
         if ( params != null )
 148  
         {
 149  0
             Class[] paramTypes = new Class[1];
 150  0
             paramTypes[0] = ReflectionUtils.loadClass( classLoader, ReporterConfiguration.class.getName() );
 151  0
             Constructor constructor = ReflectionUtils.getConstructor( clazz, paramTypes );
 152  0
             return (Reporter) ReflectionUtils.newInstance( constructor, new Object[]{ params } );
 153  
         }
 154  
         else
 155  
         {
 156  
             try
 157  
             {
 158  0
                 return (Reporter) clazz.newInstance();
 159  
             }
 160  0
             catch ( IllegalAccessException e )
 161  
             {
 162  0
                 throw new SurefireReflectionException( e );
 163  
             }
 164  0
             catch ( InstantiationException e )
 165  
             {
 166  0
                 throw new SurefireReflectionException( e );
 167  
             }
 168  
         }
 169  
 
 170  
     }
 171  
 
 172  
     public DirectConsoleReporter createConsoleReporter()
 173  
     {
 174  0
         return new DefaultDirectConsoleReporter( reporterConfiguration.getOriginalSystemOut() );
 175  
     }
 176  
 
 177  
     private void warnIfNoTests()
 178  
     {
 179  0
         if ( getGlobalRunStatistics().getRunResult().getCompletedCount() == 0 )
 180  
         {
 181  0
             final List target =
 182  
                 instantiateReportsNewStyle( reportDefinitions, reporterConfiguration, surefireClassLoader );
 183  0
             new MulticastingReporter( target ).writeMessage( "There are no tests to run." );
 184  
         }
 185  0
     }
 186  
 }