View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.surefire;
20  
21  import javax.annotation.Nonnull;
22  
23  import java.lang.reflect.InvocationTargetException;
24  import java.util.Map;
25  
26  import org.apache.maven.plugin.surefire.booterclient.Platform;
27  import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
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  import org.apache.maven.surefire.booter.ClasspathConfiguration;
32  import org.apache.maven.surefire.booter.ProviderConfiguration;
33  import org.apache.maven.surefire.booter.StartupConfiguration;
34  import org.apache.maven.surefire.booter.SurefireExecutionException;
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      private final StartupConfiguration startupConfig;
52      private final StartupReportConfiguration startupReportConfig;
53      private final ProviderConfiguration providerConfig;
54      private final ConsoleLogger consoleLogger;
55      private final Platform platform;
56  
57      public InPluginVMSurefireStarter(
58              @Nonnull StartupConfiguration startupConfig,
59              @Nonnull ProviderConfiguration providerConfig,
60              @Nonnull StartupReportConfiguration startupReportConfig,
61              @Nonnull ConsoleLogger consoleLogger,
62              @Nonnull Platform platform) {
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          // The test classloader must be constructed first to avoid issues with commons-logging until we properly
73          // separate the TestNG classloader
74  
75          Map<String, String> providerProperties = providerConfig.getProviderProperties();
76          scanResult.writeTo(providerProperties);
77  
78          startupConfig.writeSurefireTestClasspathProperty();
79          ClassLoader testClassLoader = startupConfig
80                  .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              return platform.isShutdown()
90                      ? new RunResult(0, 0, 0, 0)
91                      : invokeProvider(null, testClassLoader, factory, providerConfig, false, startupConfig, true);
92          } catch (InvocationTargetException e) {
93              throw new SurefireExecutionException("Exception in provider", e.getTargetException());
94          }
95      }
96  }