Coverage Report - org.apache.maven.plugin.surefire.booterclient.ForkStarter
 
Classes in this File Line Coverage Branch Coverage Complexity
ForkStarter
0%
0/68
0%
0/18
4.8
 
 1  
 package org.apache.maven.plugin.surefire.booterclient;
 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.IOException;
 24  
 import java.util.Iterator;
 25  
 import java.util.Properties;
 26  
 import org.apache.maven.plugin.surefire.CommonReflector;
 27  
 import org.apache.maven.plugin.surefire.booterclient.output.ForkClient;
 28  
 import org.apache.maven.plugin.surefire.booterclient.output.ThreadedStreamConsumer;
 29  
 import org.apache.maven.plugin.surefire.report.FileReporterFactory;
 30  
 import org.apache.maven.surefire.booter.Classpath;
 31  
 import org.apache.maven.surefire.booter.ClasspathConfiguration;
 32  
 import org.apache.maven.surefire.booter.ProviderConfiguration;
 33  
 import org.apache.maven.surefire.booter.ProviderFactory;
 34  
 import org.apache.maven.surefire.booter.StartupConfiguration;
 35  
 import org.apache.maven.surefire.booter.StartupReportConfiguration;
 36  
 import org.apache.maven.surefire.booter.SurefireBooterForkException;
 37  
 import org.apache.maven.surefire.booter.SurefireExecutionException;
 38  
 import org.apache.maven.surefire.booter.SystemPropertyManager;
 39  
 import org.apache.maven.surefire.providerapi.SurefireProvider;
 40  
 import org.apache.maven.surefire.report.RunStatistics;
 41  
 import org.apache.maven.surefire.suite.RunResult;
 42  
 import org.codehaus.plexus.util.cli.CommandLineException;
 43  
 import org.codehaus.plexus.util.cli.CommandLineTimeOutException;
 44  
 import org.codehaus.plexus.util.cli.CommandLineUtils;
 45  
 import org.codehaus.plexus.util.cli.Commandline;
 46  
 
 47  
 
 48  
 /**
 49  
  * Starts the fork or runs in-process.
 50  
  * <p/>
 51  
  * Lives only on the plugin-side (not present in remote vms)
 52  
  * <p/>
 53  
  * Knows how to fork new vms and also how to delegate non-forking invocation to SurefireStarter directly
 54  
  *
 55  
  * @author Jason van Zyl
 56  
  * @author Emmanuel Venisse
 57  
  * @author Brett Porter
 58  
  * @author Dan Fabulich
 59  
  * @author Carlos Sanchez
 60  
  * @author Kristian Rosenvold
 61  
  * @version $Id: ForkStarter.java 1205565 2011-11-23 20:27:35Z krosenvold $
 62  
  */
 63  
 public class ForkStarter
 64  
 {
 65  
     private final int forkedProcessTimeoutInSeconds;
 66  
 
 67  
     private final ProviderConfiguration providerConfiguration;
 68  
 
 69  
     private final StartupConfiguration startupConfiguration;
 70  
 
 71  
     private final ForkConfiguration forkConfiguration;
 72  
 
 73  
     private final StartupReportConfiguration startupReportConfiguration;
 74  
 
 75  
     public ForkStarter( ProviderConfiguration providerConfiguration, StartupConfiguration startupConfiguration,
 76  
                         ForkConfiguration forkConfiguration, int forkedProcessTimeoutInSeconds,
 77  
                         StartupReportConfiguration startupReportConfiguration )
 78  0
     {
 79  0
         this.forkConfiguration = forkConfiguration;
 80  0
         this.providerConfiguration = providerConfiguration;
 81  0
         this.forkedProcessTimeoutInSeconds = forkedProcessTimeoutInSeconds;
 82  0
         this.startupConfiguration = startupConfiguration;
 83  0
         this.startupReportConfiguration = startupReportConfiguration;
 84  0
     }
 85  
 
 86  
     public RunResult run()
 87  
         throws SurefireBooterForkException, SurefireExecutionException
 88  
     {
 89  
         final RunResult result;
 90  
 
 91  0
         final String requestedForkMode = forkConfiguration.getForkMode();
 92  0
         final FileReporterFactory fileReporterFactory = new FileReporterFactory( startupReportConfiguration );
 93  
         try
 94  
         {
 95  0
             final ForkClient forkClient =
 96  
                 new ForkClient( fileReporterFactory, startupReportConfiguration.getTestVmSystemProperties() );
 97  0
             final RunStatistics globalRunStatistics = fileReporterFactory.getGlobalRunStatistics();
 98  0
             if ( ForkConfiguration.FORK_ONCE.equals( requestedForkMode ) )
 99  
             {
 100  0
                 result = fork( null, providerConfiguration.getProviderProperties(), forkClient, globalRunStatistics );
 101  
             }
 102  0
             else if ( ForkConfiguration.FORK_ALWAYS.equals( requestedForkMode ) )
 103  
             {
 104  0
                 result = runSuitesForkPerTestSet( providerConfiguration.getProviderProperties(), forkClient,
 105  
                                                   globalRunStatistics );
 106  
             }
 107  
             else
 108  
             {
 109  0
                 throw new SurefireExecutionException( "Unknown forkmode: " + requestedForkMode, null );
 110  
             }
 111  
         }
 112  
         finally
 113  
         {
 114  0
             fileReporterFactory.close();
 115  0
         }
 116  0
         return result;
 117  
     }
 118  
 
 119  
     private RunResult runSuitesForkPerTestSet( Properties properties, ForkClient forkClient,
 120  
                                                RunStatistics globalRunStatistics )
 121  
         throws SurefireBooterForkException
 122  
     {
 123  0
         RunResult globalResult = new RunResult( 0, 0, 0, 0 );
 124  
 
 125  0
         final Iterator suites = getSuitesIterator();
 126  
 
 127  0
         while ( suites.hasNext() )
 128  
         {
 129  0
             Object testSet = suites.next();
 130  0
             RunResult runResult = fork( testSet, properties, forkClient, globalRunStatistics );
 131  0
             globalResult = globalResult.aggregate( runResult );
 132  0
         }
 133  
 
 134  0
         return globalResult;
 135  
     }
 136  
 
 137  
     private RunResult fork( Object testSet, Properties properties, ForkClient forkClient,
 138  
                             RunStatistics globalRunStatistics )
 139  
         throws SurefireBooterForkException
 140  
     {
 141  
         File surefireProperties;
 142  0
         File systemProperties = null;
 143  
         try
 144  
         {
 145  0
             BooterSerializer booterSerializer = new BooterSerializer( forkConfiguration, properties );
 146  
 
 147  0
             surefireProperties = booterSerializer.serialize( providerConfiguration, startupConfiguration, testSet,
 148  
                                                              forkConfiguration.getForkMode() );
 149  
 
 150  0
             if ( forkConfiguration.getSystemProperties() != null )
 151  
             {
 152  0
                 systemProperties = SystemPropertyManager.writePropertiesFile( forkConfiguration.getSystemProperties(),
 153  
                                                                               forkConfiguration.getTempDirectory(),
 154  
                                                                               "surefire", forkConfiguration.isDebug() );
 155  
             }
 156  
         }
 157  0
         catch ( IOException e )
 158  
         {
 159  0
             throw new SurefireBooterForkException( "Error creating properties files for forking", e );
 160  0
         }
 161  
 
 162  0
         final Classpath bootClasspathConfiguration = forkConfiguration.getBootClasspath();
 163  
 
 164  0
         final Classpath additionlClassPathUrls = startupConfiguration.useSystemClassLoader()
 165  
             ? startupConfiguration.getClasspathConfiguration().getTestClasspath()
 166  
             : null;
 167  
 
 168  
         // Surefire-booter + all test classes if "useSystemClassloader"
 169  
         // Surefire-booter if !useSystemClassLoader
 170  0
         Classpath bootClasspath = Classpath.join( bootClasspathConfiguration, additionlClassPathUrls );
 171  
 
 172  0
         Commandline cli = forkConfiguration.createCommandLine( bootClasspath.getClassPath(),
 173  
                                                                startupConfiguration.getClassLoaderConfiguration(),
 174  
                                                                startupConfiguration.isShadefire() );
 175  
 
 176  0
         cli.createArg().setFile( surefireProperties );
 177  
 
 178  0
         if ( systemProperties != null )
 179  
         {
 180  0
             cli.createArg().setFile( systemProperties );
 181  
         }
 182  
 
 183  0
         ThreadedStreamConsumer threadedStreamConsumer = new ThreadedStreamConsumer( forkClient );
 184  
 
 185  0
         if ( forkConfiguration.isDebug() )
 186  
         {
 187  0
             System.out.println( "Forking command line: " + cli );
 188  
         }
 189  
 
 190  
         RunResult runResult;
 191  
 
 192  
         try
 193  
         {
 194  0
             final int timeout = forkedProcessTimeoutInSeconds > 0 ? forkedProcessTimeoutInSeconds : 0;
 195  0
             final int result =
 196  
                 CommandLineUtils.executeCommandLine( cli, threadedStreamConsumer, threadedStreamConsumer, timeout );
 197  
 
 198  0
             if ( result != RunResult.SUCCESS )
 199  
             {
 200  0
                 throw new SurefireBooterForkException( "Error occured in starting fork, check output in log" );
 201  
             }
 202  0
             threadedStreamConsumer.close();
 203  0
             forkClient.close();
 204  
 
 205  0
             runResult = globalRunStatistics.getRunResult();
 206  
         }
 207  0
         catch ( CommandLineTimeOutException e )
 208  
         {
 209  0
             runResult = RunResult.Timeout;
 210  
         }
 211  0
         catch ( CommandLineException e )
 212  
         {
 213  0
             throw new SurefireBooterForkException( "Error while executing forked tests.", e.getCause() );
 214  0
         }
 215  
 
 216  0
         return runResult;
 217  
     }
 218  
 
 219  
     private Iterator getSuitesIterator()
 220  
         throws SurefireBooterForkException
 221  
     {
 222  
         try
 223  
         {
 224  0
             final ClasspathConfiguration classpathConfiguration = startupConfiguration.getClasspathConfiguration();
 225  0
             ClassLoader testsClassLoader = classpathConfiguration.createTestClassLoader( false );
 226  0
             ClassLoader surefireClassLoader =
 227  
                 classpathConfiguration.createInprocSurefireClassLoader( testsClassLoader );
 228  
 
 229  0
             CommonReflector commonReflector = new CommonReflector( surefireClassLoader );
 230  0
             Object reporterFactory = commonReflector.createReportingReporterFactory( startupReportConfiguration );
 231  
 
 232  0
             final ProviderFactory providerFactory =
 233  
                 new ProviderFactory( startupConfiguration, providerConfiguration, surefireClassLoader, testsClassLoader,
 234  
                                      reporterFactory );
 235  0
             SurefireProvider surefireProvider = providerFactory.createProvider( false );
 236  0
             return surefireProvider.getSuites();
 237  
         }
 238  0
         catch ( SurefireExecutionException e )
 239  
         {
 240  0
             throw new SurefireBooterForkException( "Unable to create classloader to find test suites", e );
 241  
         }
 242  
     }
 243  
 
 244  
 
 245  
 }