View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.surefire;
20  
21  import javax.annotation.Nonnull;
22  
23  import java.io.File;
24  import java.lang.reflect.Constructor;
25  
26  import org.apache.maven.plugin.surefire.extensions.SurefireConsoleOutputReporter;
27  import org.apache.maven.plugin.surefire.extensions.SurefireStatelessReporter;
28  import org.apache.maven.plugin.surefire.extensions.SurefireStatelessTestsetInfoReporter;
29  import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
30  import org.apache.maven.plugin.surefire.log.api.ConsoleLoggerDecorator;
31  import org.apache.maven.plugin.surefire.report.DefaultReporterFactory;
32  import org.apache.maven.surefire.api.util.SurefireReflectionException;
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      private final Class<?> startupReportConfiguration;
43      private final Class<?> consoleLogger;
44      private final Class<?> statelessTestsetReporter;
45      private final Class<?> consoleOutputReporter;
46      private final Class<?> statelessTestsetInfoReporter;
47      private final ClassLoader surefireClassLoader;
48  
49      public CommonReflector(@Nonnull ClassLoader surefireClassLoader) {
50          this.surefireClassLoader = surefireClassLoader;
51  
52          try {
53              startupReportConfiguration = surefireClassLoader.loadClass(StartupReportConfiguration.class.getName());
54              consoleLogger = surefireClassLoader.loadClass(ConsoleLogger.class.getName());
55              statelessTestsetReporter = surefireClassLoader.loadClass(SurefireStatelessReporter.class.getName());
56              consoleOutputReporter = surefireClassLoader.loadClass(SurefireConsoleOutputReporter.class.getName());
57              statelessTestsetInfoReporter =
58                      surefireClassLoader.loadClass(SurefireStatelessTestsetInfoReporter.class.getName());
59          } catch (ClassNotFoundException e) {
60              throw new SurefireReflectionException(e);
61          }
62      }
63  
64      public Object createReportingReporterFactory(
65              @Nonnull StartupReportConfiguration startupReportConfiguration, @Nonnull ConsoleLogger consoleLogger) {
66          Class<?>[] args = {this.startupReportConfiguration, this.consoleLogger};
67          Object src = createStartupReportConfiguration(startupReportConfiguration);
68          Object logger = createConsoleLogger(consoleLogger, surefireClassLoader);
69          Object[] params = {src, logger};
70          return instantiateObject(DefaultReporterFactory.class.getName(), args, params, surefireClassLoader);
71      }
72  
73      private Object createStartupReportConfiguration(@Nonnull StartupReportConfiguration reporterConfiguration) {
74          Constructor<?> constructor = getConstructor(
75                  startupReportConfiguration,
76                  boolean.class,
77                  boolean.class,
78                  String.class,
79                  boolean.class,
80                  File.class,
81                  boolean.class,
82                  String.class,
83                  File.class,
84                  boolean.class,
85                  int.class,
86                  String.class,
87                  String.class,
88                  boolean.class,
89                  statelessTestsetReporter,
90                  consoleOutputReporter,
91                  statelessTestsetInfoReporter);
92          Object[] params = {
93              reporterConfiguration.isUseFile(),
94              reporterConfiguration.isPrintSummary(),
95              reporterConfiguration.getReportFormat(),
96              reporterConfiguration.isRedirectTestOutputToFile(),
97              reporterConfiguration.getReportsDirectory(),
98              reporterConfiguration.isTrimStackTrace(),
99              reporterConfiguration.getReportNameSuffix(),
100             reporterConfiguration.getStatisticsFile(),
101             reporterConfiguration.isRequiresRunHistory(),
102             reporterConfiguration.getRerunFailingTestsCount(),
103             reporterConfiguration.getXsdSchemaLocation(),
104             reporterConfiguration.getEncoding().name(),
105             reporterConfiguration.isForking(),
106             reporterConfiguration.getXmlReporter().clone(surefireClassLoader),
107             reporterConfiguration.getConsoleOutputReporter().clone(surefireClassLoader),
108             reporterConfiguration.getTestsetReporter().clone(surefireClassLoader)
109         };
110         return newInstance(constructor, params);
111     }
112 
113     static Object createConsoleLogger(ConsoleLogger consoleLogger, ClassLoader cl) {
114         try {
115             Class<?> decoratorClass = cl.loadClass(ConsoleLoggerDecorator.class.getName());
116             return getConstructor(decoratorClass, Object.class).newInstance(consoleLogger);
117         } catch (Exception e) {
118             throw new SurefireReflectionException(e);
119         }
120     }
121 }