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 java.io.File;
23  import java.lang.reflect.Constructor;
24  
25  import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
26  import org.apache.maven.plugin.surefire.report.DefaultReporterFactory;
27  import org.apache.maven.surefire.booter.SurefireReflector;
28  import org.apache.maven.surefire.util.SurefireReflectionException;
29  
30  import javax.annotation.Nonnull;
31  
32  import static org.apache.maven.surefire.util.ReflectionUtils.getConstructor;
33  import static org.apache.maven.surefire.util.ReflectionUtils.instantiateObject;
34  import static org.apache.maven.surefire.util.ReflectionUtils.newInstance;
35  
36  /**
37   * @author Kristian Rosenvold
38   */
39  public class CommonReflector
40  {
41      private final Class<?> startupReportConfiguration;
42      private final Class<?> consoleLogger;
43      private final ClassLoader surefireClassLoader;
44  
45      public CommonReflector( @Nonnull ClassLoader surefireClassLoader )
46      {
47          this.surefireClassLoader = surefireClassLoader;
48  
49          try
50          {
51              startupReportConfiguration = surefireClassLoader.loadClass( StartupReportConfiguration.class.getName() );
52              consoleLogger = surefireClassLoader.loadClass( ConsoleLogger.class.getName() );
53          }
54          catch ( ClassNotFoundException e )
55          {
56              throw new SurefireReflectionException( e );
57          }
58      }
59  
60      public Object createReportingReporterFactory( @Nonnull StartupReportConfiguration startupReportConfiguration,
61                                                    @Nonnull ConsoleLogger consoleLogger )
62      {
63          Class<?>[] args = { this.startupReportConfiguration, this.consoleLogger };
64          Object src = createStartupReportConfiguration( startupReportConfiguration );
65          Object logger = SurefireReflector.createConsoleLogger( consoleLogger, surefireClassLoader );
66          Object[] params = { src, logger };
67          return instantiateObject( DefaultReporterFactory.class.getName(), args, params, surefireClassLoader );
68      }
69  
70      private Object createStartupReportConfiguration( @Nonnull StartupReportConfiguration reporterConfiguration )
71      {
72          Constructor<?> constructor = getConstructor( startupReportConfiguration, boolean.class, boolean.class,
73                                                             String.class, boolean.class, boolean.class, File.class,
74                                                             boolean.class, String.class, File.class, boolean.class,
75                                                             int.class, String.class, String.class, boolean.class );
76          //noinspection BooleanConstructorCall
77          Object[] params = { reporterConfiguration.isUseFile(), reporterConfiguration.isPrintSummary(),
78              reporterConfiguration.getReportFormat(), reporterConfiguration.isRedirectTestOutputToFile(),
79              reporterConfiguration.isDisableXmlReport(), reporterConfiguration.getReportsDirectory(),
80              reporterConfiguration.isTrimStackTrace(), reporterConfiguration.getReportNameSuffix(),
81              reporterConfiguration.getStatisticsFile(), reporterConfiguration.isRequiresRunHistory(),
82              reporterConfiguration.getRerunFailingTestsCount(), reporterConfiguration.getXsdSchemaLocation(),
83              reporterConfiguration.getEncoding().name(), reporterConfiguration.isForkMode() };
84          return newInstance( constructor, params );
85      }
86  
87  }