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