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.log.api.ConsoleLogger;
23  import org.apache.maven.surefire.booter.ClasspathConfiguration;
24  import org.apache.maven.surefire.booter.ProviderConfiguration;
25  import org.apache.maven.surefire.booter.StartupConfiguration;
26  import org.apache.maven.surefire.booter.SurefireExecutionException;
27  import org.apache.maven.surefire.suite.RunResult;
28  import org.apache.maven.surefire.testset.TestSetFailedException;
29  import org.apache.maven.surefire.util.DefaultScanResult;
30  
31  import javax.annotation.Nonnull;
32  import java.lang.reflect.InvocationTargetException;
33  import java.util.Map;
34  
35  import static org.apache.maven.surefire.booter.ProviderFactory.invokeProvider;
36  
37  /**
38   * Starts the provider in the same VM as the surefire plugin.
39   * <br>
40   * This part of the booter is always guaranteed to be in the
41   * same vm as the tests will be run in.
42   *
43   * @author Jason van Zyl
44   * @author Brett Porter
45   * @author Emmanuel Venisse
46   * @author Dan Fabulich
47   * @author Kristian Rosenvold
48   */
49  public class InPluginVMSurefireStarter
50  {
51      private final StartupConfiguration startupConfig;
52      private final StartupReportConfiguration startupReportConfig;
53      private final ProviderConfiguration providerConfig;
54      private final ConsoleLogger consoleLogger;
55  
56      public InPluginVMSurefireStarter( @Nonnull StartupConfiguration startupConfig,
57                                        @Nonnull ProviderConfiguration providerConfig,
58                                        @Nonnull StartupReportConfiguration startupReportConfig,
59                                        @Nonnull ConsoleLogger consoleLogger )
60      {
61          this.startupConfig = startupConfig;
62          this.startupReportConfig = startupReportConfig;
63          this.providerConfig = providerConfig;
64          this.consoleLogger = consoleLogger;
65      }
66  
67      public RunResult runSuitesInProcess( @Nonnull DefaultScanResult scanResult )
68          throws SurefireExecutionException, TestSetFailedException
69      {
70          // The test classloader must be constructed first to avoid issues with commons-logging until we properly
71          // separate the TestNG classloader
72  
73          Map<String, String> providerProperties = providerConfig.getProviderProperties();
74          scanResult.writeTo( providerProperties );
75  
76          startupConfig.writeSurefireTestClasspathProperty();
77          ClassLoader testClassLoader = startupConfig.getClasspathConfiguration()
78                  .toRealPath( ClasspathConfiguration.class )
79                  .createMergedClassLoader();
80  
81          CommonReflector surefireReflector = new CommonReflector( testClassLoader );
82  
83          Object factory = surefireReflector.createReportingReporterFactory( startupReportConfig, consoleLogger );
84  
85          try
86          {
87              return invokeProvider( null, testClassLoader, factory, providerConfig, false, startupConfig, true );
88          }
89          catch ( InvocationTargetException e )
90          {
91              throw new SurefireExecutionException( "Exception in provider", e.getTargetException() );
92          }
93      }
94  
95  }