View Javadoc

1   package org.apache.maven.surefire.booter;
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.io.File;
23  import java.io.FileInputStream;
24  import java.io.InputStream;
25  import java.io.PrintStream;
26  import org.apache.maven.surefire.suite.RunResult;
27  
28  /**
29   * The part of the booter that is unique to a forked vm.
30   * <p/>
31   * Deals with deserialization of the booter wire-level protocol
32   * <p/>
33   *
34   * @author Jason van Zyl
35   * @author Emmanuel Venisse
36   * @author Kristian Rosenvold
37   * @version $Id$
38   */
39  public class ForkedBooter
40  {
41  
42      /**
43       * This method is invoked when Surefire is forked - this method parses and organizes the arguments passed to it and
44       * then calls the Surefire class' run method. <p/> The system exit code will be 1 if an exception is thrown.
45       *
46       * @param args Commandline arguments
47       * @throws Throwable Upon throwables
48       */
49      public static void main( String[] args )
50          throws Throwable
51      {
52          try
53          {
54              if ( args.length > 1 )
55              {
56                  SystemPropertyManager.setSystemProperties( new File( args[1] ) );
57              }
58  
59              File surefirePropertiesFile = new File( args[0] );
60              InputStream stream = surefirePropertiesFile.exists() ? new FileInputStream( surefirePropertiesFile ) : null;
61              BooterDeserializer booterDeserializer = new BooterDeserializer( stream );
62              ProviderConfiguration providerConfiguration = booterDeserializer.deserialize();
63              final StartupConfiguration startupConfiguration = booterDeserializer.getProviderConfiguration();
64  
65              TypeEncodedValue forkedTestSet = providerConfiguration.getTestForFork();
66  
67              final ClasspathConfiguration classpathConfiguration = startupConfiguration.getClasspathConfiguration();
68              final ClassLoader testClassLoader = classpathConfiguration.createForkingTestClassLoader(
69                  startupConfiguration.isManifestOnlyJarRequestedAndUsable() );
70  
71              startupConfiguration.writeSurefireTestClasspathProperty();
72  
73              Object testSet = forkedTestSet != null ? forkedTestSet.getDecodedValue() : null;
74              runSuitesInProcess( testSet, testClassLoader, startupConfiguration, providerConfiguration );
75  
76              // noinspection CallToSystemExit
77              System.exit( 0 );
78          }
79          catch ( Throwable t )
80          {
81              // Just throwing does getMessage() and a local trace - we want to call printStackTrace for a full trace
82              // noinspection UseOfSystemOutOrSystemErr
83              t.printStackTrace( System.err );
84              // noinspection ProhibitedExceptionThrown,CallToSystemExit
85              System.exit( 1 );
86          }
87      }
88  
89      public static RunResult runSuitesInProcess( Object testSet, ClassLoader testsClassLoader,
90                                                  StartupConfiguration startupConfiguration,
91                                                  ProviderConfiguration providerConfiguration )
92          throws SurefireExecutionException
93      {
94          final ClasspathConfiguration classpathConfiguration = startupConfiguration.getClasspathConfiguration();
95          ClassLoader surefireClassLoader = classpathConfiguration.createSurefireClassLoader( testsClassLoader );
96  
97          SurefireReflector surefireReflector = new SurefireReflector( surefireClassLoader );
98  
99          final Object factory = createForkingReporterFactory( surefireReflector, providerConfiguration );
100 
101         return ProviderFactory.invokeProvider( testSet, testsClassLoader, surefireClassLoader, factory,
102                                                providerConfiguration, true, startupConfiguration );
103     }
104 
105     private static Object createForkingReporterFactory( SurefireReflector surefireReflector,
106                                                         ProviderConfiguration providerConfiguration )
107     {
108         final Boolean trimStackTrace = providerConfiguration.getReporterConfiguration().isTrimStackTrace();
109         final PrintStream originalSystemOut = providerConfiguration.getReporterConfiguration().getOriginalSystemOut();
110         return surefireReflector.createForkingReporterFactory( trimStackTrace, originalSystemOut );
111     }
112 }