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 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      private final RunStatistics globalRunStatistics = new RunStatistics();
55  
56      private final ReporterConfiguration reporterConfiguration;
57  
58      private RunReporter first;
59  
60      private final Object lock = new Object();
61  
62      private final ConsoleOutputCapture consoleOutputCapture;
63  
64      public ReporterManagerFactory( ClassLoader surefireClassLoader, ReporterConfiguration reporterConfiguration )
65      {
66          this.reportDefinitions = reporterConfiguration.getReports();
67          this.surefireClassLoader = surefireClassLoader;
68          this.reporterConfiguration = reporterConfiguration;
69          SystemConsoleOutputReceiver systemConsoleOutputReceiver =
70              new SystemConsoleOutputReceiver( reporterConfiguration.getOriginalSystemOut(),
71                                               reporterConfiguration.getOriginalSystemErr() );
72          // todo: Fix parallel (must keep quiet on default stream?)
73          final DefaultConsoleOutputReceiver target =
74              new DefaultConsoleOutputReceiver( systemConsoleOutputReceiver, true );
75          this.consoleOutputCapture = new ConsoleOutputCapture( target );
76      }
77  
78      public RunStatistics getGlobalRunStatistics()
79      {
80          return globalRunStatistics;
81      }
82  
83      public RunListener createReporter()
84      {
85          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          final TestSetRunListener reporterManager = new TestSetRunListener( reports, globalRunStatistics );
95          if ( first == null )
96          {
97              synchronized ( lock )
98              {
99                  if ( first == null )
100                 {
101                     first = reporterManager;
102                     reporterManager.runStarting();
103                 }
104             }
105         }
106         return reporterManager;
107     }
108 
109 
110     public RunResult close()
111     {
112         consoleOutputCapture.restoreStreams();
113         warnIfNoTests();
114         synchronized ( lock )
115         {
116             if ( first != null )
117             {
118                 first.runCompleted();
119             }
120             return globalRunStatistics.getRunResult();
121         }
122     }
123 
124     protected List instantiateReportsNewStyle( List reportDefinitions, ReporterConfiguration reporterConfiguration,
125                                                ClassLoader classLoader )
126     {
127         List reports = new ArrayList();
128 
129         for ( Iterator i = reportDefinitions.iterator(); i.hasNext(); )
130         {
131 
132             String className = (String) i.next();
133 
134             Reporter report = instantiateReportNewStyle( className, reporterConfiguration, classLoader );
135 
136             reports.add( report );
137         }
138 
139         return reports;
140     }
141 
142     private static Reporter instantiateReportNewStyle( String className, ReporterConfiguration params,
143                                                        ClassLoader classLoader )
144     {
145         Class clazz = ReflectionUtils.loadClass( classLoader, className );
146 
147         if ( params != null )
148         {
149             Class[] paramTypes = new Class[1];
150             paramTypes[0] = ReflectionUtils.loadClass( classLoader, ReporterConfiguration.class.getName() );
151             Constructor constructor = ReflectionUtils.getConstructor( clazz, paramTypes );
152             return (Reporter) ReflectionUtils.newInstance( constructor, new Object[]{ params } );
153         }
154         else
155         {
156             try
157             {
158                 return (Reporter) clazz.newInstance();
159             }
160             catch ( IllegalAccessException e )
161             {
162                 throw new SurefireReflectionException( e );
163             }
164             catch ( InstantiationException e )
165             {
166                 throw new SurefireReflectionException( e );
167             }
168         }
169 
170     }
171 
172     public DirectConsoleReporter createConsoleReporter()
173     {
174         return new DefaultDirectConsoleReporter( reporterConfiguration.getOriginalSystemOut() );
175     }
176 
177     private void warnIfNoTests()
178     {
179         if ( getGlobalRunStatistics().getRunResult().getCompletedCount() == 0 )
180         {
181             final List target =
182                 instantiateReportsNewStyle( reportDefinitions, reporterConfiguration, surefireClassLoader );
183             new MulticastingReporter( target ).writeMessage( "There are no tests to run." );
184         }
185     }
186 }