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 org.apache.commons.io.FileUtils;
23  import org.apache.commons.lang3.RandomStringUtils;
24  import org.apache.commons.lang3.SystemUtils;
25  import org.apache.maven.plugin.surefire.JdkAttributes;
26  import org.apache.maven.plugin.surefire.log.api.NullConsoleLogger;
27  import org.apache.maven.shared.utils.StringUtils;
28  import org.apache.maven.shared.utils.cli.Commandline;
29  import org.apache.maven.surefire.booter.ClassLoaderConfiguration;
30  import org.apache.maven.surefire.booter.Classpath;
31  import org.apache.maven.surefire.booter.ClasspathConfiguration;
32  import org.apache.maven.surefire.booter.StartupConfiguration;
33  import org.apache.maven.surefire.booter.SurefireBooterForkException;
34  import org.junit.Test;
35  
36  import java.io.File;
37  import java.io.IOException;
38  import java.util.Collections;
39  import java.util.List;
40  import java.util.Properties;
41  
42  import static java.util.Collections.singletonList;
43  import static org.apache.maven.surefire.booter.Classpath.emptyClasspath;
44  import static org.junit.Assert.assertEquals;
45  import static org.junit.Assert.assertTrue;
46  import static org.junit.Assert.fail;
47  
48  public class ForkConfigurationTest
49  {
50      private static final StartupConfiguration STARTUP_CONFIG = new StartupConfiguration( "",
51              new ClasspathConfiguration( true, true ),
52              new ClassLoaderConfiguration( true, true ),
53              false,
54              false );
55  
56      @Test
57      public void testCreateCommandLine_UseSystemClassLoaderForkOnce_ShouldConstructManifestOnlyJar()
58          throws IOException, SurefireBooterForkException
59      {
60          ForkConfiguration config = getForkConfiguration( (String) null );
61          File cpElement = getTempClasspathFile();
62  
63          List<String> cp = singletonList( cpElement.getAbsolutePath() );
64          ClasspathConfiguration cpConfig = new ClasspathConfiguration( new Classpath( cp ), emptyClasspath(),
65                  emptyClasspath(), true, true );
66          ClassLoaderConfiguration clc = new ClassLoaderConfiguration( true, true );
67          StartupConfiguration startup = new StartupConfiguration( "", cpConfig, clc, false, false );
68  
69          Commandline cli = config.createCommandLine( startup, 1 );
70  
71          String line = StringUtils.join( cli.getCommandline(), " " );
72          assertTrue( line.contains( "-jar" ) );
73      }
74  
75      @Test
76      public void testArglineWithNewline()
77          throws IOException, SurefireBooterForkException
78      {
79          // SUREFIRE-657
80          ForkConfiguration config = getForkConfiguration( "abc\ndef" );
81          File cpElement = getTempClasspathFile();
82  
83          List<String> cp = singletonList( cpElement.getAbsolutePath() );
84          ClasspathConfiguration cpConfig = new ClasspathConfiguration( new Classpath( cp ), emptyClasspath(),
85                  emptyClasspath(), true, true );
86          ClassLoaderConfiguration clc = new ClassLoaderConfiguration( true, true );
87          StartupConfiguration startup = new StartupConfiguration( "", cpConfig, clc, false, false );
88  
89          Commandline commandLine = config.createCommandLine( startup, 1 );
90          assertTrue( commandLine.toString().contains( "abc def" ) );
91      }
92  
93      @Test
94      public void testCurrentWorkingDirectoryPropagationIncludingForkNumberExpansion()
95          throws IOException, SurefireBooterForkException
96      {
97          // SUREFIRE-1136
98          File baseDir =
99              new File( FileUtils.getTempDirectory(), "SUREFIRE-1136-" + RandomStringUtils.randomAlphabetic( 3 ) );
100         assertTrue( baseDir.mkdirs() );
101         baseDir.deleteOnExit();
102 
103         File cwd = new File( baseDir, "fork_${surefire.forkNumber}" );
104 
105         ClasspathConfiguration cpConfig = new ClasspathConfiguration( emptyClasspath(), emptyClasspath(),
106                 emptyClasspath(), true, true );
107         ClassLoaderConfiguration clc = new ClassLoaderConfiguration( true, true );
108         StartupConfiguration startup = new StartupConfiguration( "", cpConfig, clc, false, false );
109         ForkConfiguration config = getForkConfiguration( cwd.getCanonicalFile() );
110         Commandline commandLine = config.createCommandLine( startup, 1 );
111 
112         File forkDirectory = new File( baseDir, "fork_1" );
113         forkDirectory.deleteOnExit();
114 
115         String shellWorkDir = commandLine.getShell().getWorkingDirectory().getCanonicalPath();
116         assertEquals( shellWorkDir,  forkDirectory.getCanonicalPath() );
117     }
118 
119     @Test
120     public void testExceptionWhenCurrentDirectoryIsNotRealDirectory()
121         throws IOException, SurefireBooterForkException
122     {
123         // SUREFIRE-1136
124         File baseDir =
125             new File( FileUtils.getTempDirectory(), "SUREFIRE-1136-" + RandomStringUtils.randomAlphabetic( 3 ) );
126         assertTrue( baseDir.mkdirs() );
127         baseDir.deleteOnExit();
128 
129         File cwd = new File( baseDir, "cwd.txt" );
130         FileUtils.touch( cwd );
131         cwd.deleteOnExit();
132 
133         ForkConfiguration config = getForkConfiguration( cwd.getCanonicalFile() );
134 
135         try
136         {
137             config.createCommandLine( STARTUP_CONFIG, 1 );
138         }
139         catch ( SurefireBooterForkException sbfe )
140         {
141             // To handle issue with ~ expansion on Windows
142             String absolutePath = cwd.getCanonicalPath();
143             assertEquals( "WorkingDirectory " + absolutePath + " exists and is not a directory", sbfe.getMessage() );
144             return;
145         }
146 
147         fail();
148     }
149 
150     @Test
151     public void testExceptionWhenCurrentDirectoryCannotBeCreated()
152         throws IOException, SurefireBooterForkException
153     {
154         // SUREFIRE-1136
155         File baseDir =
156             new File( FileUtils.getTempDirectory(), "SUREFIRE-1136-" + RandomStringUtils.randomAlphabetic( 3 ) );
157         assertTrue( baseDir.mkdirs() );
158         baseDir.deleteOnExit();
159 
160         // NULL is invalid for JDK starting from 1.7.60 - https://github.com/openjdk-mirror/jdk/commit/e5389115f3634d25d101e2dcc71f120d4fd9f72f
161         // ? character is invalid on Windows, seems to be imposable to create invalid directory using Java on Linux
162         File cwd = new File( baseDir, "?\u0000InvalidDirectoryName" );
163         ForkConfiguration config = getForkConfiguration( cwd.getAbsoluteFile() );
164 
165         try
166         {
167             config.createCommandLine( STARTUP_CONFIG, 1 );
168         }
169         catch ( SurefireBooterForkException sbfe )
170         {
171             assertEquals( "Cannot create workingDirectory " + cwd.getAbsolutePath(), sbfe.getMessage() );
172             return;
173         }
174 
175         if ( SystemUtils.IS_OS_WINDOWS || isJavaVersionAtLeast7u60() )
176         {
177             fail();
178         }
179     }
180 
181     private File getTempClasspathFile()
182         throws IOException
183     {
184         File cpElement = File.createTempFile( "ForkConfigurationTest.", ".file" );
185         cpElement.deleteOnExit();
186         return cpElement;
187     }
188 
189     static ForkConfiguration getForkConfiguration( String argLine )
190         throws IOException
191     {
192         File jvm = new File( new File( System.getProperty( "java.home" ), "bin" ), "java" );
193         return getForkConfiguration( argLine, jvm.getAbsolutePath(), new File( "." ).getCanonicalFile() );
194     }
195 
196     private static ForkConfiguration getForkConfiguration( File cwd )
197             throws IOException
198     {
199         File jvm = new File( new File( System.getProperty( "java.home" ), "bin" ), "java" );
200         return getForkConfiguration( null, jvm.getAbsolutePath(), cwd );
201     }
202 
203     private static ForkConfiguration getForkConfiguration( String argLine, String jvm, File cwd )
204         throws IOException
205     {
206         Platform platform = new Platform().withJdkExecAttributesForTests( new JdkAttributes( jvm, false ) );
207         File tmpDir = File.createTempFile( "target", "surefire" );
208         assertTrue( tmpDir.delete() );
209         assertTrue( tmpDir.mkdirs() );
210         return new JarManifestForkConfiguration( emptyClasspath(), tmpDir, null,
211                 cwd, new Properties(), argLine, Collections.<String, String>emptyMap(), false, 1, false,
212                 platform, new NullConsoleLogger() );
213     }
214 
215     // based on http://stackoverflow.com/questions/2591083/getting-version-of-java-in-runtime
216     private static boolean isJavaVersionAtLeast7u60()
217     {
218         String[] javaVersionElements = System.getProperty( "java.runtime.version" ).split( "\\.|_|-b" );
219         return Integer.valueOf( javaVersionElements[1] ) >= 7 && Integer.valueOf( javaVersionElements[3] ) >= 60;
220     }
221 }