Coverage Report - org.apache.maven.surefire.booter.ForkedBooter
 
Classes in this File Line Coverage Branch Coverage Complexity
ForkedBooter
0%
0/69
0%
0/14
2,5
ForkedBooter$1
0%
0/6
N/A
2,5
 
 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 java.lang.reflect.InvocationTargetException;
 27  
 
 28  
 import org.apache.maven.surefire.providerapi.ProviderParameters;
 29  
 import org.apache.maven.surefire.providerapi.SurefireProvider;
 30  
 import org.apache.maven.surefire.report.LegacyPojoStackTraceWriter;
 31  
 import org.apache.maven.surefire.report.ReporterFactory;
 32  
 import org.apache.maven.surefire.report.StackTraceWriter;
 33  
 import org.apache.maven.surefire.suite.RunResult;
 34  
 import org.apache.maven.surefire.testset.TestSetFailedException;
 35  
 import org.apache.maven.surefire.util.ReflectionUtils;
 36  
 
 37  
 /**
 38  
  * The part of the booter that is unique to a forked vm.
 39  
  * <p/>
 40  
  * Deals with deserialization of the booter wire-level protocol
 41  
  * <p/>
 42  
  *
 43  
  * @author Jason van Zyl
 44  
  * @author Emmanuel Venisse
 45  
  * @author Kristian Rosenvold
 46  
  */
 47  0
 public class ForkedBooter
 48  
 {
 49  
 
 50  
     /**
 51  
      * This method is invoked when Surefire is forked - this method parses and organizes the arguments passed to it and
 52  
      * then calls the Surefire class' run method. <p/> The system exit code will be 1 if an exception is thrown.
 53  
      *
 54  
      * @param args Commandline arguments
 55  
      * @throws Throwable Upon throwables
 56  
      */
 57  
     public static void main( String[] args )
 58  
         throws Throwable
 59  
     {
 60  0
         final PrintStream originalOut = System.out;
 61  
         try
 62  
         {
 63  0
             if ( args.length > 1 )
 64  
             {
 65  0
                 SystemPropertyManager.setSystemProperties( new File( args[1] ) );
 66  
             }
 67  
 
 68  0
             File surefirePropertiesFile = new File( args[0] );
 69  0
             InputStream stream = surefirePropertiesFile.exists() ? new FileInputStream( surefirePropertiesFile ) : null;
 70  0
             BooterDeserializer booterDeserializer = new BooterDeserializer( stream );
 71  0
             ProviderConfiguration providerConfiguration = booterDeserializer.deserialize();
 72  0
             final StartupConfiguration startupConfiguration = booterDeserializer.getProviderConfiguration();
 73  
 
 74  0
             TypeEncodedValue forkedTestSet = providerConfiguration.getTestForFork();
 75  0
             boolean readTestsFromInputStream = providerConfiguration.isReadTestsFromInStream();
 76  
 
 77  0
             final ClasspathConfiguration classpathConfiguration = startupConfiguration.getClasspathConfiguration();
 78  0
             if ( startupConfiguration.isManifestOnlyJarRequestedAndUsable() )
 79  
             {
 80  0
                 classpathConfiguration.trickClassPathWhenManifestOnlyClasspath();
 81  
             }
 82  
 
 83  0
             Thread.currentThread().getContextClassLoader().setDefaultAssertionStatus(
 84  
                 classpathConfiguration.isEnableAssertions() );
 85  0
             startupConfiguration.writeSurefireTestClasspathProperty();
 86  
 
 87  
             Object testSet;
 88  0
             if ( forkedTestSet != null )
 89  
             {
 90  0
                 testSet = forkedTestSet.getDecodedValue( Thread.currentThread().getContextClassLoader() );
 91  
             }
 92  0
             else if ( readTestsFromInputStream )
 93  
             {
 94  0
                 testSet = new LazyTestsToRun( System.in, originalOut );
 95  
             }
 96  
             else
 97  
             {
 98  0
                 testSet = null;
 99  
             }
 100  
 
 101  
             try
 102  
             {
 103  0
                 runSuitesInProcess( testSet, startupConfiguration, providerConfiguration, originalOut );
 104  
             }
 105  0
             catch ( InvocationTargetException t )
 106  
             {
 107  
 
 108  0
                 LegacyPojoStackTraceWriter stackTraceWriter =
 109  
                     new LegacyPojoStackTraceWriter( "test subystem", "no method", t.getTargetException() );
 110  0
                 StringBuilder stringBuilder = new StringBuilder();
 111  0
                 ForkingRunListener.encode( stringBuilder, stackTraceWriter, false );
 112  0
                 originalOut.println( ( (char) ForkingRunListener.BOOTERCODE_ERROR ) + ",0," + stringBuilder.toString() );
 113  
             }
 114  0
             catch ( Throwable t )
 115  
             {
 116  0
                 StackTraceWriter stackTraceWriter = new LegacyPojoStackTraceWriter( "test subystem", "no method", t );
 117  0
                 StringBuilder stringBuilder = new StringBuilder();
 118  0
                 ForkingRunListener.encode( stringBuilder, stackTraceWriter, false );
 119  0
                 originalOut.println( ( (char) ForkingRunListener.BOOTERCODE_ERROR ) + ",0," + stringBuilder.toString() );
 120  0
             }
 121  
             // Say bye.
 122  0
             originalOut.println( ( (char) ForkingRunListener.BOOTERCODE_BYE ) + ",0,BYE!" );
 123  0
             originalOut.flush();
 124  
             // noinspection CallToSystemExit
 125  0
             exit( 0 );
 126  
         }
 127  0
         catch ( Throwable t )
 128  
         {
 129  
             // Just throwing does getMessage() and a local trace - we want to call printStackTrace for a full trace
 130  
             // noinspection UseOfSystemOutOrSystemErr
 131  0
             t.printStackTrace( System.err );
 132  
             // noinspection ProhibitedExceptionThrown,CallToSystemExit
 133  0
             exit( 1 );
 134  0
         }
 135  0
     }
 136  
 
 137  
     private final static long SYSTEM_EXIT_TIMEOUT = 30 * 1000;
 138  
 
 139  
     private static void exit( final int returnCode )
 140  
     {
 141  0
         launchLastDitchDaemonShutdownThread( returnCode );
 142  0
         System.exit( returnCode );
 143  0
     }
 144  
 
 145  
 
 146  
     private static RunResult runSuitesInProcess( Object testSet, StartupConfiguration startupConfiguration,
 147  
                                                  ProviderConfiguration providerConfiguration,
 148  
                                                  PrintStream originalSystemOut )
 149  
         throws SurefireExecutionException, TestSetFailedException, InvocationTargetException
 150  
     {
 151  0
         final ReporterFactory factory = createForkingReporterFactory( providerConfiguration, originalSystemOut );
 152  
 
 153  0
         return invokeProviderInSameClassLoader( testSet, factory, providerConfiguration, true, startupConfiguration,
 154  
                                                 false );
 155  
     }
 156  
 
 157  
     private static ReporterFactory createForkingReporterFactory( ProviderConfiguration providerConfiguration,
 158  
                                                                  PrintStream originalSystemOut )
 159  
     {
 160  0
         final Boolean trimStackTrace = providerConfiguration.getReporterConfiguration().isTrimStackTrace();
 161  0
         return SurefireReflector.createForkingReporterFactoryInCurrentClassLoader( trimStackTrace, originalSystemOut );
 162  
     }
 163  
 
 164  
     private static void launchLastDitchDaemonShutdownThread( final int returnCode )
 165  
     {
 166  0
         Thread lastExit = new Thread( new Runnable()
 167  0
         {
 168  
             public void run()
 169  
             {
 170  
                 try
 171  
                 {
 172  0
                     Thread.sleep( SYSTEM_EXIT_TIMEOUT );
 173  0
                     Runtime.getRuntime().halt( returnCode );
 174  
                 }
 175  0
                 catch ( InterruptedException ignore )
 176  
                 {
 177  0
                 }
 178  0
             }
 179  
         } );
 180  0
         lastExit.setDaemon( true );
 181  0
         lastExit.start();
 182  0
     }
 183  
 
 184  
     public static RunResult invokeProviderInSameClassLoader( Object testSet, Object factory,
 185  
                                                              ProviderConfiguration providerConfiguration,
 186  
                                                              boolean insideFork,
 187  
                                                              StartupConfiguration startupConfiguration1,
 188  
                                                              boolean restoreStreams )
 189  
         throws TestSetFailedException, InvocationTargetException
 190  
     {
 191  0
         final PrintStream orgSystemOut = System.out;
 192  0
         final PrintStream orgSystemErr = System.err;
 193  
         // Note that System.out/System.err are also read in the "ReporterConfiguration" instatiation
 194  
         // in createProvider below. These are the same values as here.
 195  
 
 196  0
         final SurefireProvider provider =
 197  
             createProviderInCurrentClassloader( startupConfiguration1, insideFork, providerConfiguration, factory );
 198  
         try
 199  
         {
 200  0
             return provider.invoke( testSet );
 201  
         }
 202  
         finally
 203  
         {
 204  0
             if ( restoreStreams && System.getSecurityManager() == null )
 205  
             {
 206  0
                 System.setOut( orgSystemOut );
 207  0
                 System.setErr( orgSystemErr );
 208  
             }
 209  
         }
 210  
     }
 211  
 
 212  
     public static SurefireProvider createProviderInCurrentClassloader( StartupConfiguration startupConfiguration1,
 213  
                                                                        boolean isInsideFork,
 214  
                                                                        ProviderConfiguration providerConfiguration,
 215  
                                                                        Object reporterManagerFactory1 )
 216  
     {
 217  
 
 218  0
         BaseProviderFactory bpf = new BaseProviderFactory( (ReporterFactory) reporterManagerFactory1, isInsideFork );
 219  0
         bpf.setTestRequest( providerConfiguration.getTestSuiteDefinition() );
 220  0
         bpf.setReporterConfiguration( providerConfiguration.getReporterConfiguration() );
 221  0
         ClassLoader clasLoader = Thread.currentThread().getContextClassLoader();
 222  0
         bpf.setClassLoaders( clasLoader );
 223  0
         bpf.setTestArtifactInfo( providerConfiguration.getTestArtifact() );
 224  0
         bpf.setProviderProperties( providerConfiguration.getProviderProperties() );
 225  0
         bpf.setRunOrderParameters( providerConfiguration.getRunOrderParameters() );
 226  0
         bpf.setDirectoryScannerParameters( providerConfiguration.getDirScannerParams() );
 227  0
         return (SurefireProvider) ReflectionUtils.instantiateOneArg( clasLoader,
 228  
                                                                      startupConfiguration1.getActualClassName(),
 229  
                                                                      ProviderParameters.class, bpf );
 230  
     }
 231  
 }