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