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