View Javadoc
1   package org.apache.maven.plugin.surefire;
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.plugin.surefire.extensions.SurefireConsoleOutputReporter;
23  import org.apache.maven.plugin.surefire.extensions.SurefireStatelessReporter;
24  import org.apache.maven.plugin.surefire.extensions.SurefireStatelessTestsetInfoReporter;
25  import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
26  import org.apache.maven.plugin.surefire.log.api.ConsoleLoggerDecorator;
27  import org.apache.maven.plugin.surefire.report.DefaultReporterFactory;
28  import org.apache.maven.surefire.api.util.SurefireReflectionException;
29  
30  import javax.annotation.Nonnull;
31  import java.io.File;
32  import java.lang.reflect.Constructor;
33  
34  import static org.apache.maven.surefire.api.util.ReflectionUtils.getConstructor;
35  import static org.apache.maven.surefire.api.util.ReflectionUtils.instantiateObject;
36  import static org.apache.maven.surefire.api.util.ReflectionUtils.newInstance;
37  
38  /**
39   * @author Kristian Rosenvold
40   */
41  public class CommonReflector
42  {
43      private final Class<?> startupReportConfiguration;
44      private final Class<?> consoleLogger;
45      private final Class<?> statelessTestsetReporter;
46      private final Class<?> consoleOutputReporter;
47      private final Class<?> statelessTestsetInfoReporter;
48      private final ClassLoader surefireClassLoader;
49  
50      public CommonReflector( @Nonnull ClassLoader surefireClassLoader )
51      {
52          this.surefireClassLoader = surefireClassLoader;
53  
54          try
55          {
56              startupReportConfiguration = surefireClassLoader.loadClass( StartupReportConfiguration.class.getName() );
57              consoleLogger = surefireClassLoader.loadClass( ConsoleLogger.class.getName() );
58              statelessTestsetReporter = surefireClassLoader.loadClass( SurefireStatelessReporter.class.getName() );
59              consoleOutputReporter = surefireClassLoader.loadClass( SurefireConsoleOutputReporter.class.getName() );
60              statelessTestsetInfoReporter =
61                      surefireClassLoader.loadClass( SurefireStatelessTestsetInfoReporter.class.getName() );
62          }
63          catch ( ClassNotFoundException e )
64          {
65              throw new SurefireReflectionException( e );
66          }
67      }
68  
69      public Object createReportingReporterFactory( @Nonnull StartupReportConfiguration startupReportConfiguration,
70                                                    @Nonnull ConsoleLogger consoleLogger )
71      {
72          Class<?>[] args = { this.startupReportConfiguration, this.consoleLogger };
73          Object src = createStartupReportConfiguration( startupReportConfiguration );
74          Object logger = createConsoleLogger( consoleLogger, surefireClassLoader );
75          Object[] params = { src, logger };
76          return instantiateObject( DefaultReporterFactory.class.getName(), args, params, surefireClassLoader );
77      }
78  
79      private Object createStartupReportConfiguration( @Nonnull StartupReportConfiguration reporterConfiguration )
80      {
81          Constructor<?> constructor = getConstructor( startupReportConfiguration, boolean.class, boolean.class,
82                                                       String.class, boolean.class, File.class,
83                                                       boolean.class, String.class, File.class, boolean.class,
84                                                       int.class, String.class, String.class, boolean.class,
85                                                       statelessTestsetReporter, consoleOutputReporter,
86                                                       statelessTestsetInfoReporter );
87          Object[] params = { reporterConfiguration.isUseFile(), reporterConfiguration.isPrintSummary(),
88              reporterConfiguration.getReportFormat(), reporterConfiguration.isRedirectTestOutputToFile(),
89              reporterConfiguration.getReportsDirectory(),
90              reporterConfiguration.isTrimStackTrace(), reporterConfiguration.getReportNameSuffix(),
91              reporterConfiguration.getStatisticsFile(), reporterConfiguration.isRequiresRunHistory(),
92              reporterConfiguration.getRerunFailingTestsCount(), reporterConfiguration.getXsdSchemaLocation(),
93              reporterConfiguration.getEncoding().name(), reporterConfiguration.isForkMode(),
94              reporterConfiguration.getXmlReporter().clone( surefireClassLoader ),
95              reporterConfiguration.getConsoleOutputReporter().clone( surefireClassLoader ),
96              reporterConfiguration.getTestsetReporter().clone( surefireClassLoader )
97          };
98          return newInstance( constructor, params );
99      }
100 
101     static Object createConsoleLogger( ConsoleLogger consoleLogger, ClassLoader cl )
102     {
103         try
104         {
105             Class<?> decoratorClass = cl.loadClass( ConsoleLoggerDecorator.class.getName() );
106             return getConstructor( decoratorClass, Object.class ).newInstance( consoleLogger );
107         }
108         catch ( Exception e )
109         {
110             throw new SurefireReflectionException( e );
111         }
112     }
113 }