View Javadoc

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 junit.framework.TestCase;
23  import org.apache.maven.surefire.booter.Classpath;
24  import org.apache.maven.surefire.booter.SurefireBooterForkException;
25  import org.codehaus.plexus.util.StringUtils;
26  import org.codehaus.plexus.util.cli.Commandline;
27  
28  import java.io.File;
29  import java.io.IOException;
30  import java.util.Collections;
31  
32  public class ForkConfigurationTest
33      extends TestCase
34  {
35  
36      public void testCreateCommandLine_UseSystemClassLoaderForkOnce_ShouldConstructManifestOnlyJar()
37          throws IOException, SurefireBooterForkException
38      {
39          ForkConfiguration config = getForkConfiguration();
40          File cpElement = getTempClasspathFile();
41          config.setJvmExecutable( "java" );
42  
43          Commandline cli = config.createCommandLine( Collections.singletonList( cpElement.getAbsolutePath() ), true,
44                                                      false );
45  
46          String line = StringUtils.join( cli.getCommandline(), " " );
47          assertTrue( line.indexOf( "-jar" ) > -1 );
48      }
49  
50      public void testArglineWithNewline()
51          throws IOException, SurefireBooterForkException
52      {
53          // SUREFIRE-657
54          File cpElement = getTempClasspathFile();
55          ForkConfiguration forkConfiguration = getForkConfiguration();
56  
57          forkConfiguration.setArgLine( "abc\ndef" );
58  
59          final Commandline commandLine =
60              forkConfiguration.createCommandLine( Collections.singletonList( cpElement.getAbsolutePath() ), false,
61                                                   false );
62          assertTrue( commandLine.toString().contains( "abc def" ) );
63      }
64  
65      private File getTempClasspathFile()
66          throws IOException
67      {
68          File cpElement = File.createTempFile( "ForkConfigurationTest.", ".file" );
69          cpElement.deleteOnExit();
70          return cpElement;
71      }
72  
73      public static ForkConfiguration getForkConfiguration()
74          throws IOException
75      {
76          ForkConfiguration forkConfiguration = new ForkConfiguration( new Classpath(), ForkConfiguration.FORK_ONCE, null );
77          forkConfiguration.setWorkingDirectory( new File( "." ).getCanonicalFile() );
78          return forkConfiguration;
79      }
80  
81  }