Coverage Report - org.apache.maven.surefire.booter.ForkedBooter
 
Classes in this File Line Coverage Branch Coverage Complexity
ForkedBooter
0%
0/53
0%
0/8
2,333
ForkedBooter$1
0%
0/6
N/A
2,333
 
 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  
 import org.apache.maven.surefire.report.LegacyPojoStackTraceWriter;
 28  
 import org.apache.maven.surefire.report.StackTraceWriter;
 29  
 import org.apache.maven.surefire.suite.RunResult;
 30  
 import org.apache.maven.surefire.testset.TestSetFailedException;
 31  
 import org.apache.maven.surefire.util.LazyTestsToRun;
 32  
 
 33  
 /**
 34  
  * The part of the booter that is unique to a forked vm.
 35  
  * <p/>
 36  
  * Deals with deserialization of the booter wire-level protocol
 37  
  * <p/>
 38  
  *
 39  
  * @author Jason van Zyl
 40  
  * @author Emmanuel Venisse
 41  
  * @author Kristian Rosenvold
 42  
  */
 43  0
 public class ForkedBooter
 44  
 {
 45  
 
 46  
     /**
 47  
      * This method is invoked when Surefire is forked - this method parses and organizes the arguments passed to it and
 48  
      * then calls the Surefire class' run method. <p/> The system exit code will be 1 if an exception is thrown.
 49  
      *
 50  
      * @param args Commandline arguments
 51  
      * @throws Throwable Upon throwables
 52  
      */
 53  
     public static void main( String[] args )
 54  
         throws Throwable
 55  
     {
 56  0
         final PrintStream originalOut = System.out;
 57  
         try
 58  
         {
 59  0
             if ( args.length > 1 )
 60  
             {
 61  0
                 SystemPropertyManager.setSystemProperties( new File( args[1] ) );
 62  
             }
 63  
 
 64  0
             File surefirePropertiesFile = new File( args[0] );
 65  0
             InputStream stream = surefirePropertiesFile.exists() ? new FileInputStream( surefirePropertiesFile ) : null;
 66  0
             BooterDeserializer booterDeserializer = new BooterDeserializer( stream );
 67  0
             ProviderConfiguration providerConfiguration = booterDeserializer.deserialize();
 68  0
             final StartupConfiguration startupConfiguration = booterDeserializer.getProviderConfiguration();
 69  
 
 70  0
             TypeEncodedValue forkedTestSet = providerConfiguration.getTestForFork();
 71  0
             boolean readTestsFromInputStream = providerConfiguration.isReadTestsFromInStream();
 72  
 
 73  0
             final ClasspathConfiguration classpathConfiguration = startupConfiguration.getClasspathConfiguration();
 74  0
             final ClassLoader testClassLoader = classpathConfiguration.createForkingTestClassLoader(
 75  
                 startupConfiguration.isManifestOnlyJarRequestedAndUsable() );
 76  
 
 77  0
             startupConfiguration.writeSurefireTestClasspathProperty();
 78  
 
 79  
             Object testSet;
 80  0
             if ( forkedTestSet != null )
 81  
             {
 82  0
                 testSet = forkedTestSet.getDecodedValue( testClassLoader );
 83  
             }
 84  0
             else if ( readTestsFromInputStream )
 85  
             {
 86  0
                 testSet = new LazyTestsToRun( System.in, testClassLoader, originalOut );
 87  
             }
 88  
             else
 89  
             {
 90  0
                 testSet = null;
 91  
             }
 92  
 
 93  
             try
 94  
             {
 95  0
                 runSuitesInProcess( testSet, testClassLoader, startupConfiguration, providerConfiguration,
 96  
                                     originalOut );
 97  
             }
 98  0
             catch ( InvocationTargetException t )
 99  
             {
 100  
 
 101  0
                 LegacyPojoStackTraceWriter stackTraceWriter =
 102  
                     new LegacyPojoStackTraceWriter( "test subystem", "no method", t.getTargetException() );
 103  0
                 StringBuffer stringBuffer = new StringBuffer();
 104  0
                 ForkingRunListener.encode( stringBuffer, stackTraceWriter, false );
 105  0
                 originalOut.println( ( (char) ForkingRunListener.BOOTERCODE_ERROR ) + ",0," + stringBuffer.toString() );
 106  
             }
 107  0
             catch ( Throwable t )
 108  
             {
 109  0
                 StackTraceWriter stackTraceWriter = new LegacyPojoStackTraceWriter( "test subystem", "no method", t );
 110  0
                 StringBuffer stringBuffer = new StringBuffer();
 111  0
                 ForkingRunListener.encode( stringBuffer, stackTraceWriter, false );
 112  0
                 originalOut.println( ( (char) ForkingRunListener.BOOTERCODE_ERROR ) + ",0," + stringBuffer.toString() );
 113  0
             }
 114  
             // Say bye.
 115  0
             originalOut.println( ( (char) ForkingRunListener.BOOTERCODE_BYE ) + ",0,BYE!" );
 116  0
             originalOut.flush();
 117  
             // noinspection CallToSystemExit
 118  0
             exit( 0 );
 119  
         }
 120  0
         catch ( Throwable t )
 121  
         {
 122  
             // Just throwing does getMessage() and a local trace - we want to call printStackTrace for a full trace
 123  
             // noinspection UseOfSystemOutOrSystemErr
 124  0
             t.printStackTrace( System.err );
 125  
             // noinspection ProhibitedExceptionThrown,CallToSystemExit
 126  0
             exit( 1 );
 127  0
         }
 128  0
     }
 129  
 
 130  
     private final static long SYSTEM_EXIT_TIMEOUT = 30 * 1000;
 131  
 
 132  
     private static void exit( final int returnCode )
 133  
     {
 134  0
         launchLastDitchDaemonShutdownThread( returnCode );
 135  0
         System.exit( returnCode );
 136  0
     }
 137  
 
 138  
 
 139  
     private static RunResult runSuitesInProcess( Object testSet, ClassLoader testsClassLoader,
 140  
                                                  StartupConfiguration startupConfiguration,
 141  
                                                  ProviderConfiguration providerConfiguration,
 142  
                                                  PrintStream originalSystemOut )
 143  
         throws SurefireExecutionException, TestSetFailedException, InvocationTargetException
 144  
     {
 145  0
         final ClasspathConfiguration classpathConfiguration = startupConfiguration.getClasspathConfiguration();
 146  0
         ClassLoader surefireClassLoader = classpathConfiguration.createSurefireClassLoader( testsClassLoader );
 147  
 
 148  0
         SurefireReflector surefireReflector = new SurefireReflector( surefireClassLoader );
 149  
 
 150  0
         final Object factory =
 151  
             createForkingReporterFactory( surefireReflector, providerConfiguration, originalSystemOut );
 152  
 
 153  0
         return ProviderFactory.invokeProvider( testSet, testsClassLoader, surefireClassLoader, factory,
 154  
                                                providerConfiguration, true, startupConfiguration, false );
 155  
     }
 156  
 
 157  
     private static Object createForkingReporterFactory( SurefireReflector surefireReflector,
 158  
                                                         ProviderConfiguration providerConfiguration,
 159  
                                                         PrintStream originalSystemOut )
 160  
     {
 161  0
         final Boolean trimStackTrace = providerConfiguration.getReporterConfiguration().isTrimStackTrace();
 162  0
         return surefireReflector.createForkingReporterFactory( trimStackTrace, originalSystemOut );
 163  
     }
 164  
 
 165  
     private static void launchLastDitchDaemonShutdownThread( final int returnCode )
 166  
     {
 167  0
         Thread lastExit = new Thread( new Runnable()
 168  0
         {
 169  
             public void run()
 170  
             {
 171  
                 try
 172  
                 {
 173  0
                     Thread.sleep( SYSTEM_EXIT_TIMEOUT );
 174  0
                     Runtime.getRuntime().halt( returnCode );
 175  
                 }
 176  0
                 catch ( InterruptedException ignore )
 177  
                 {
 178  0
                 }
 179  0
             }
 180  
         } );
 181  0
         lastExit.setDaemon( true );
 182  0
         lastExit.start();
 183  0
     }
 184  
 
 185  
 }