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