Coverage Report - org.apache.maven.surefire.booter.SurefireStarter
 
Classes in this File Line Coverage Branch Coverage Complexity
SurefireStarter
0%
0/48
0%
0/6
2.125
 
 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 org.apache.maven.surefire.providerapi.SurefireProvider;
 23  
 import org.apache.maven.surefire.report.ReporterConfiguration;
 24  
 import org.apache.maven.surefire.report.ReporterException;
 25  
 import org.apache.maven.surefire.suite.RunResult;
 26  
 import org.apache.maven.surefire.testset.TestSetFailedException;
 27  
 import org.apache.maven.surefire.util.NestedRuntimeException;
 28  
 
 29  
 import java.io.File;
 30  
 import java.io.IOException;
 31  
 import java.io.PrintStream;
 32  
 import java.util.Properties;
 33  
 
 34  
 /**
 35  
  * Invokes surefire with the correct classloader setup.
 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  
  * @version $Id$
 46  
  */
 47  
 public class SurefireStarter
 48  
 {
 49  
     private static final int NO_TESTS = 254;
 50  
 
 51  
     private final ProviderConfiguration providerConfiguration;
 52  
 
 53  
     private final StartupConfiguration startupConfiguration;
 54  
 
 55  0
     private final String SUREFIRE_TEST_CLASSPATH = "surefire.test.class.path";
 56  
 
 57  
     public SurefireStarter( StartupConfiguration startupConfiguration, ProviderConfiguration providerConfiguration )
 58  0
     {
 59  0
         this.providerConfiguration = providerConfiguration;
 60  0
         this.startupConfiguration = startupConfiguration;
 61  0
     }
 62  
 
 63  
     public int runSuitesInProcess( Object testSet, File surefirePropertiesFile, Properties p )
 64  
         throws SurefireExecutionException, IOException
 65  
     {
 66  0
         writeSurefireTestClasspathProperty();
 67  0
         final ClasspathConfiguration classpathConfiguration = startupConfiguration.getClasspathConfiguration();
 68  
 
 69  0
         ClassLoader testsClassLoader = classpathConfiguration.createTestClassLoaderConditionallySystem(
 70  
             startupConfiguration.useSystemClassLoader() );
 71  
 
 72  0
         ClassLoader surefireClassLoader = classpathConfiguration.createSurefireClassLoader( testsClassLoader );
 73  
 
 74  0
         final RunResult runResult = invokeProvider( testSet, testsClassLoader, surefireClassLoader );
 75  0
         updateResultsProperties( runResult, p );
 76  0
         SystemPropertyManager.writePropertiesFile( surefirePropertiesFile, "surefire", p );
 77  0
         return processRunCount( runResult );
 78  
     }
 79  
 
 80  
     public int runSuitesInProcess()
 81  
         throws SurefireExecutionException
 82  
     {
 83  
         // The test classloader must be constructed first to avoid issues with commons-logging until we properly
 84  
         // separate the TestNG classloader
 85  0
         ClassLoader testsClassLoader = createInProcessTestClassLoader();
 86  
 
 87  0
         final ClasspathConfiguration classpathConfiguration = startupConfiguration.getClasspathConfiguration();
 88  
 
 89  0
         ClassLoader surefireClassLoader = classpathConfiguration.createSurefireClassLoader( testsClassLoader );
 90  
 
 91  0
         final RunResult runResult = invokeProvider( null, testsClassLoader, surefireClassLoader );
 92  0
         return processRunCount( runResult);
 93  
     }
 94  
 
 95  
     private ClassLoader createInProcessTestClassLoader()
 96  
         throws SurefireExecutionException
 97  
     {
 98  0
         writeSurefireTestClasspathProperty();
 99  0
         ClasspathConfiguration classpathConfiguration = startupConfiguration.getClasspathConfiguration();
 100  0
         if ( startupConfiguration.isManifestOnlyJarRequestedAndUsable() )
 101  
         {
 102  0
             ClassLoader testsClassLoader = getClass().getClassLoader(); // ClassLoader.getSystemClassLoader()
 103  
             // SUREFIRE-459, trick the app under test into thinking its classpath was conventional
 104  
             // (instead of a single manifest-only jar)
 105  0
             System.setProperty( "surefire.real.class.path", System.getProperty( "java.class.path" ) );
 106  0
             classpathConfiguration.getTestClasspath().writeToSystemProperty( "java.class.path" );
 107  0
             return testsClassLoader;
 108  
         }
 109  
         else
 110  
         {
 111  0
             return classpathConfiguration.createTestClassLoader();
 112  
         }
 113  
     }
 114  
 
 115  
     private void writeSurefireTestClasspathProperty()
 116  
     {
 117  0
         ClasspathConfiguration classpathConfiguration = startupConfiguration.getClasspathConfiguration();
 118  0
         classpathConfiguration.getTestClasspath().writeToSystemProperty( SUREFIRE_TEST_CLASSPATH );
 119  0
     }
 120  
 
 121  
     private static final String RESULTS_ERRORS = "errors";
 122  
 
 123  
     private static final String RESULTS_COMPLETED_COUNT = "completedCount";
 124  
 
 125  
     private static final String RESULTS_FAILURES = "failures";
 126  
 
 127  
     private static final String RESULTS_SKIPPED = "skipped";
 128  
 
 129  
 
 130  
     private void updateResultsProperties( RunResult runResult, Properties results )
 131  
     {
 132  0
         results.setProperty( RESULTS_ERRORS, String.valueOf( runResult.getErrors() ) );
 133  0
         results.setProperty( RESULTS_COMPLETED_COUNT, String.valueOf( runResult.getCompletedCount() ) );
 134  0
         results.setProperty( RESULTS_FAILURES, String.valueOf( runResult.getFailures() ) );
 135  0
         results.setProperty( RESULTS_SKIPPED, String.valueOf( runResult.getSkipped() ) );
 136  0
     }
 137  
 
 138  
     private RunResult invokeProvider( Object testSet, ClassLoader testsClassLoader, ClassLoader surefireClassLoader )
 139  
     {
 140  0
         final PrintStream orgSystemOut = System.out;
 141  0
         final PrintStream orgSystemErr = System.err;
 142  
         // Note that System.out/System.err are also read in the "ReporterConfiguration" instatiation
 143  
         // in createProvider below. These are the same values as here.
 144  0
         ProviderFactory providerFactory =
 145  
             new ProviderFactory( startupConfiguration, providerConfiguration, surefireClassLoader, testsClassLoader );
 146  0
         final SurefireProvider provider = providerFactory.createProvider( );
 147  
 
 148  
         try
 149  
         {
 150  0
             return provider.invoke( testSet );
 151  
         }
 152  0
         catch ( TestSetFailedException e )
 153  
         {
 154  0
             throw new NestedRuntimeException( e );
 155  
         }
 156  0
         catch ( ReporterException e )
 157  
         {
 158  0
             throw new NestedRuntimeException( e );
 159  
         }
 160  
         finally
 161  
         {
 162  0
             System.setOut( orgSystemOut );
 163  0
             System.setErr( orgSystemErr );
 164  
         }
 165  
     }
 166  
 
 167  
     /**
 168  
      * Returns the process return code based on the RunResult
 169  
      *
 170  
      * @param runCount The run result
 171  
      * @return The process result code
 172  
      * @throws SurefireExecutionException When an exception is found
 173  
      */
 174  
     private int processRunCount( RunResult runCount )
 175  
         throws SurefireExecutionException
 176  
     {
 177  
 
 178  0
         if ( runCount.getCompletedCount() == 0 && providerConfiguration.isFailIfNoTests().booleanValue() )
 179  
         {
 180  0
             return NO_TESTS;
 181  
         }
 182  
 
 183  0
         return runCount.getBooterCode();
 184  
     }
 185  
 }