Coverage Report - org.apache.maven.plugin.surefire.booterclient.ForkConfiguration
 
Classes in this File Line Coverage Branch Coverage Complexity
ForkConfiguration
0%
0/84
0%
0/34
1.762
 
 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 org.apache.maven.surefire.booter.ClassLoaderConfiguration;
 23  
 import org.apache.maven.surefire.booter.Classpath;
 24  
 import org.apache.maven.surefire.booter.ForkedBooter;
 25  
 import org.apache.maven.surefire.booter.SurefireBooterForkException;
 26  
 import org.apache.maven.surefire.util.Relocator;
 27  
 import org.apache.maven.surefire.util.UrlUtils;
 28  
 import org.codehaus.plexus.util.StringUtils;
 29  
 import org.codehaus.plexus.util.cli.Commandline;
 30  
 
 31  
 import java.io.File;
 32  
 import java.io.FileOutputStream;
 33  
 import java.io.IOException;
 34  
 import java.util.HashMap;
 35  
 import java.util.Iterator;
 36  
 import java.util.List;
 37  
 import java.util.Map;
 38  
 import java.util.Properties;
 39  
 import java.util.jar.JarEntry;
 40  
 import java.util.jar.JarOutputStream;
 41  
 import java.util.jar.Manifest;
 42  
 
 43  
 /**
 44  
  * Configuration for forking tests.
 45  
  *
 46  
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
 47  
  * @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
 48  
  * @author <a href="mailto:krosenvold@apache.org">Kristian Rosenvold</a>
 49  
  */
 50  
 public class ForkConfiguration
 51  
 {
 52  
     public static final String FORK_ONCE = "once";
 53  
 
 54  
     public static final String FORK_ALWAYS = "always";
 55  
 
 56  
     public static final String FORK_NEVER = "never";
 57  
 
 58  
     private final Classpath bootClasspathConfiguration;
 59  
 
 60  
     private final String forkMode;
 61  
 
 62  
     private Properties systemProperties;
 63  
 
 64  
     private String jvmExecutable;
 65  
 
 66  
     private String argLine;
 67  
 
 68  
     private Map environmentVariables;
 69  
 
 70  
     private File workingDirectory;
 71  
 
 72  
     private File tempDirectory;
 73  
 
 74  
     private boolean debug;
 75  
 
 76  
     private String debugLine;
 77  
 
 78  
     public ForkConfiguration( Classpath bootClasspathConfiguration, String forkMode, File tmpDir )
 79  0
     {
 80  0
         this.bootClasspathConfiguration = bootClasspathConfiguration;
 81  0
         this.forkMode = getForkMode( forkMode );
 82  0
         this.tempDirectory = tmpDir;
 83  0
     }
 84  
 
 85  
     public Classpath getBootClasspath()
 86  
     {
 87  0
         return bootClasspathConfiguration;
 88  
     }
 89  
 
 90  
     private static String getForkMode( String forkMode )
 91  
     {
 92  0
         if ( "pertest".equalsIgnoreCase( forkMode ) )
 93  
         {
 94  0
             return FORK_ALWAYS;
 95  
         }
 96  0
         else if ( "none".equalsIgnoreCase( forkMode ) )
 97  
         {
 98  0
             return FORK_NEVER;
 99  
         }
 100  0
         else if ( forkMode.equals( FORK_NEVER ) || forkMode.equals( FORK_ONCE ) || forkMode.equals( FORK_ALWAYS ) )
 101  
         {
 102  0
             return forkMode;
 103  
         }
 104  
         else
 105  
         {
 106  0
             throw new IllegalArgumentException( "Fork mode " + forkMode + " is not a legal value" );
 107  
         }
 108  
     }
 109  
 
 110  
     public boolean isForking()
 111  
     {
 112  0
         return !FORK_NEVER.equals( forkMode );
 113  
     }
 114  
 
 115  
     public void setSystemProperties( Properties systemProperties )
 116  
     {
 117  0
         this.systemProperties = (Properties) systemProperties.clone();
 118  0
     }
 119  
 
 120  
     public void setJvmExecutable( String jvmExecutable )
 121  
     {
 122  0
         this.jvmExecutable = jvmExecutable;
 123  0
     }
 124  
 
 125  
     public void setArgLine( String argLine )
 126  
     {
 127  0
         this.argLine = argLine;
 128  0
     }
 129  
 
 130  
     public void setDebugLine( String debugLine )
 131  
     {
 132  0
         this.debugLine = debugLine;
 133  0
     }
 134  
 
 135  
     public void setEnvironmentVariables( Map environmentVariables )
 136  
     {
 137  0
         this.environmentVariables = new HashMap( environmentVariables );
 138  0
     }
 139  
 
 140  
     public void setWorkingDirectory( File workingDirectory )
 141  
     {
 142  0
         this.workingDirectory = workingDirectory;
 143  0
     }
 144  
 
 145  
     public void setTempDirectory( File tempDirectory )
 146  
     {
 147  0
         this.tempDirectory = tempDirectory;
 148  0
     }
 149  
 
 150  
     public String getForkMode()
 151  
     {
 152  0
         return forkMode;
 153  
     }
 154  
 
 155  
     public Properties getSystemProperties()
 156  
     {
 157  0
         return systemProperties;
 158  
     }
 159  
 
 160  
     /**
 161  
      * @param classPath              cla the classpath arguments
 162  
      * @param classpathConfiguration the classpath configuration
 163  
      * @param shadefire true if running shadefire
 164  
      * @return A commandline
 165  
      * @throws org.apache.maven.surefire.booter.SurefireBooterForkException
 166  
      *          when unable to perform the fork
 167  
      */
 168  
     public Commandline createCommandLine( List classPath, ClassLoaderConfiguration classpathConfiguration,
 169  
                                           boolean shadefire )
 170  
         throws SurefireBooterForkException
 171  
     {
 172  0
         return createCommandLine( classPath, classpathConfiguration.isManifestOnlyJarRequestedAndUsable(), shadefire );
 173  
     }
 174  
 
 175  
     public Commandline createCommandLine( List classPath, boolean useJar, boolean shadefire )
 176  
         throws SurefireBooterForkException
 177  
     {
 178  0
         Commandline cli = new Commandline();
 179  
 
 180  0
         cli.setExecutable( jvmExecutable );
 181  
 
 182  0
         if ( argLine != null )
 183  
         {
 184  0
             cli.createArg().setLine( stripNewLines( argLine ) );
 185  
         }
 186  
 
 187  0
         if ( environmentVariables != null )
 188  
         {
 189  0
             Iterator iter = environmentVariables.keySet().iterator();
 190  
 
 191  0
             while ( iter.hasNext() )
 192  
             {
 193  0
                 String key = (String) iter.next();
 194  
 
 195  0
                 String value = (String) environmentVariables.get( key );
 196  
 
 197  0
                 cli.addEnvironment( key, value );
 198  0
             }
 199  
         }
 200  
 
 201  0
         if ( getDebugLine() != null && !"".equals( getDebugLine() ) )
 202  
         {
 203  0
             cli.createArg().setLine( getDebugLine() );
 204  
         }
 205  
 
 206  0
         if ( useJar )
 207  
         {
 208  
             File jarFile;
 209  
             try
 210  
             {
 211  0
                 jarFile = createJar( classPath );
 212  
             }
 213  0
             catch ( IOException e )
 214  
             {
 215  0
                 throw new SurefireBooterForkException( "Error creating archive file", e );
 216  0
             }
 217  
 
 218  0
             cli.createArg().setValue( "-jar" );
 219  
 
 220  0
             cli.createArg().setValue( jarFile.getAbsolutePath() );
 221  0
         }
 222  
         else
 223  
         {
 224  0
             cli.createArg().setValue( "-classpath" );
 225  
 
 226  0
             cli.createArg().setValue( StringUtils.join( classPath.iterator(), File.pathSeparator ) );
 227  
 
 228  0
             final String forkedBooter = ForkedBooter.class.getName();
 229  
 
 230  0
             cli.createArg().setValue( shadefire ? new Relocator( ).relocate( forkedBooter ) : forkedBooter);
 231  
         }
 232  
 
 233  0
         cli.setWorkingDirectory( workingDirectory.getAbsolutePath() );
 234  
 
 235  0
         return cli;
 236  
     }
 237  
 
 238  
     /**
 239  
      * Create a jar with just a manifest containing a Main-Class entry for BooterConfiguration and a Class-Path entry
 240  
      * for all classpath elements.
 241  
      *
 242  
      * @param classPath List&lt;String> of all classpath elements.
 243  
      * @return The file pointint to the jar
 244  
      * @throws java.io.IOException When a file operation fails.
 245  
      */
 246  
     public File createJar( List classPath )
 247  
         throws IOException
 248  
     {
 249  0
         File file = File.createTempFile( "surefirebooter", ".jar", tempDirectory );
 250  0
         if ( !debug )
 251  
         {
 252  0
             file.deleteOnExit();
 253  
         }
 254  0
         FileOutputStream fos = new FileOutputStream( file );
 255  0
         JarOutputStream jos = new JarOutputStream( fos );
 256  0
         jos.setLevel( JarOutputStream.STORED );
 257  0
         JarEntry je = new JarEntry( "META-INF/MANIFEST.MF" );
 258  0
         jos.putNextEntry( je );
 259  
 
 260  0
         Manifest man = new Manifest();
 261  
 
 262  
         // we can't use StringUtils.join here since we need to add a '/' to
 263  
         // the end of directory entries - otherwise the jvm will ignore them.
 264  0
         String cp = "";
 265  0
         for ( Iterator it = classPath.iterator(); it.hasNext(); )
 266  
         {
 267  0
             String el = (String) it.next();
 268  
             // NOTE: if File points to a directory, this entry MUST end in '/'.
 269  0
             cp += UrlUtils.getURL( new File( el ) ).toExternalForm() + " ";
 270  0
         }
 271  
 
 272  0
         man.getMainAttributes().putValue( "Manifest-Version", "1.0" );
 273  0
         man.getMainAttributes().putValue( "Class-Path", cp.trim() );
 274  0
         man.getMainAttributes().putValue( "Main-Class", ForkedBooter.class.getName() );
 275  
 
 276  0
         man.write( jos );
 277  0
         jos.close();
 278  
 
 279  0
         return file;
 280  
     }
 281  
 
 282  
     public void setDebug( boolean debug )
 283  
     {
 284  0
         this.debug = debug;
 285  0
     }
 286  
 
 287  
     public boolean isDebug()
 288  
     {
 289  0
         return debug;
 290  
     }
 291  
 
 292  
     public String stripNewLines( String argline )
 293  
     {
 294  0
         return argline.replace( "\n", " " ).replace( "\r", " " );
 295  
     }
 296  
 
 297  
     public String getDebugLine()
 298  
     {
 299  0
         return debugLine;
 300  
     }
 301  
 
 302  
 
 303  
     public File getTempDirectory()
 304  
     {
 305  0
         return tempDirectory;
 306  
     }
 307  
 }