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.commons.io.FileUtils;
28  import org.apache.commons.lang3.RandomStringUtils;
29  import org.apache.commons.lang3.SystemUtils;
30  import org.apache.maven.shared.utils.StringUtils;
31  import org.apache.maven.shared.utils.cli.Commandline;
32  import org.apache.maven.surefire.booter.Classpath;
33  import org.apache.maven.surefire.booter.SurefireBooterForkException;
34  
35  import junit.framework.TestCase;
36  
37  public class ForkConfigurationTest
38      extends TestCase
39  {
40  
41      public void testCreateCommandLine_UseSystemClassLoaderForkOnce_ShouldConstructManifestOnlyJar()
42          throws IOException, SurefireBooterForkException
43      {
44          ForkConfiguration config = getForkConfiguration( null, "java" );
45          File cpElement = getTempClasspathFile();
46  
47          Commandline cli =
48              config.createCommandLine( Collections.singletonList( cpElement.getAbsolutePath() ), true, false, null, 1 );
49  
50          String line = StringUtils.join( cli.getCommandline(), " " );
51          assertTrue( line.contains( "-jar" ) );
52      }
53  
54      public void testArglineWithNewline()
55          throws IOException, SurefireBooterForkException
56      {
57          // SUREFIRE-657
58          File cpElement = getTempClasspathFile();
59          ForkConfiguration forkConfiguration = getForkConfiguration( "abc\ndef", null );
60  
61          final Commandline commandLine =
62              forkConfiguration.createCommandLine( Collections.singletonList( cpElement.getAbsolutePath() ), false, false,
63                                                   null, 1 );
64          assertTrue( commandLine.toString().contains( "abc def" ) );
65      }
66  
67      public void testCurrentWorkingDirectoryPropagationIncludingForkNumberExpansion()
68          throws IOException, SurefireBooterForkException
69      {
70          // SUREFIRE-1136
71          File baseDir =
72              new File( FileUtils.getTempDirectory(), "SUREFIRE-1136-" + RandomStringUtils.randomAlphabetic( 3 ) );
73          baseDir.mkdirs();
74          baseDir.deleteOnExit();
75  
76          File cwd = new File( baseDir, "fork_${surefire.forkNumber}" );
77  
78          ForkConfiguration config = getForkConfiguration( null, "java", cwd.getCanonicalFile() );
79          Commandline commandLine = config.createCommandLine( Collections.<String>emptyList(), true, false, null, 1 );
80  
81          File forkDirectory = new File( baseDir, "fork_1" );
82          forkDirectory.deleteOnExit();
83          assertTrue( forkDirectory.getCanonicalPath().equals(
84              commandLine.getShell().getWorkingDirectory().getCanonicalPath() ) );
85      }
86  
87      public void testExceptionWhenCurrentDirectoryIsNotRealDirectory()
88          throws IOException, SurefireBooterForkException
89      {
90          // SUREFIRE-1136
91          File baseDir =
92              new File( FileUtils.getTempDirectory(), "SUREFIRE-1136-" + RandomStringUtils.randomAlphabetic( 3 ) );
93          baseDir.mkdirs();
94          baseDir.deleteOnExit();
95  
96          File cwd = new File( baseDir, "cwd.txt" );
97          FileUtils.touch( cwd );
98          cwd.deleteOnExit();
99  
100         ForkConfiguration config = getForkConfiguration( null, "java", cwd.getCanonicalFile() );
101 
102         try
103         {
104             config.createCommandLine( Collections.<String>emptyList(), true, false, null, 1 );
105         }
106         catch ( SurefireBooterForkException sbfe )
107         {
108             // To handle issue with ~ expansion on Windows
109             String absolutePath = cwd.getCanonicalPath();
110             assertEquals( "WorkingDirectory " + absolutePath + " exists and is not a directory", sbfe.getMessage() );
111             return;
112         }
113 
114         fail();
115     }
116 
117     public void testExceptionWhenCurrentDirectoryCannotBeCreated()
118         throws IOException, SurefireBooterForkException
119     {
120         // SUREFIRE-1136
121         File baseDir =
122             new File( FileUtils.getTempDirectory(), "SUREFIRE-1136-" + RandomStringUtils.randomAlphabetic( 3 ) );
123         baseDir.mkdirs();
124         baseDir.deleteOnExit();
125 
126         // NULL is invalid for JDK starting from 1.7.60 - https://github.com/openjdk-mirror/jdk/commit/e5389115f3634d25d101e2dcc71f120d4fd9f72f
127         // ? character is invalid on Windows, seems to be imposable to create invalid directory using Java on Linux
128         File cwd = new File( baseDir, "?\u0000InvalidDirectoryName" );
129         ForkConfiguration config = getForkConfiguration( null, "java", cwd.getAbsoluteFile() );
130 
131         try
132         {
133             config.createCommandLine( Collections.<String>emptyList(), true, false, null, 1 );
134         }
135         catch ( SurefireBooterForkException sbfe )
136         {
137             assertEquals( "Cannot create workingDirectory " + cwd.getAbsolutePath(), sbfe.getMessage() );
138             return;
139         }
140 
141         if ( SystemUtils.IS_OS_WINDOWS || isJavaVersionAtLeast( 7, 60 ) )
142         {
143             fail();
144         }
145     }
146 
147     private File getTempClasspathFile()
148         throws IOException
149     {
150         File cpElement = File.createTempFile( "ForkConfigurationTest.", ".file" );
151         cpElement.deleteOnExit();
152         return cpElement;
153     }
154 
155     public static ForkConfiguration getForkConfiguration( String argLine, String jvm )
156         throws IOException
157     {
158         return getForkConfiguration( argLine, jvm, new File( "." ).getCanonicalFile() );
159     }
160 
161     public static ForkConfiguration getForkConfiguration( String argLine, String jvm, File cwd )
162         throws IOException
163     {
164         return new ForkConfiguration( Classpath.emptyClasspath(), null, null, jvm, cwd, new Properties(), argLine, null,
165                                       false, 1, false );
166     }
167 
168     // based on http://stackoverflow.com/questions/2591083/getting-version-of-java-in-runtime
169     private boolean isJavaVersionAtLeast( int major, int update )
170     {
171         String[] javaVersionElements = System.getProperty( "java.runtime.version" ).split( "\\.|_|-b" );
172         return Integer.valueOf( javaVersionElements[1] ) >= major
173             && Integer.valueOf( javaVersionElements[4] ) >= update;
174     }
175 }