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