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.net.URI;
24  import java.net.URISyntaxException;
25  import java.nio.file.Path;
26  
27  import org.apache.maven.plugin.surefire.booterclient.output.InPluginProcessDumpSingleton;
28  
29  import static org.apache.maven.plugin.surefire.booterclient.JarManifestForkConfiguration.relativize;
30  import static org.apache.maven.plugin.surefire.booterclient.JarManifestForkConfiguration.toAbsoluteUri;
31  import static org.apache.maven.plugin.surefire.booterclient.JarManifestForkConfiguration.toClasspathElementUri;
32  import static org.fest.assertions.Assertions.assertThat;
33  
34  import org.junit.AfterClass;
35  import org.junit.BeforeClass;
36  import org.junit.Test;
37  import org.junit.runner.RunWith;
38  
39  import static org.fest.util.Files.delete;
40  import static org.fest.util.Files.newTemporaryFolder;
41  import static org.mockito.ArgumentMatchers.same;
42  import static org.powermock.api.mockito.PowerMockito.mock;
43  import static org.powermock.api.mockito.PowerMockito.mockStatic;
44  import static org.powermock.api.mockito.PowerMockito.when;
45  
46  import org.mockito.invocation.InvocationOnMock;
47  import org.mockito.stubbing.Answer;
48  import org.powermock.core.classloader.annotations.PrepareForTest;
49  import org.powermock.modules.junit4.PowerMockRunner;
50  
51  /**
52   * Unit tests for {@link JarManifestForkConfiguration}.
53   */
54  @RunWith( PowerMockRunner.class )
55  @PrepareForTest( { JarManifestForkConfiguration.class, InPluginProcessDumpSingleton.class } )
56  public class JarManifestForkConfigurationTest
57  {
58      private static final File TMP = newTemporaryFolder();
59  
60      private static File dumpDirectory;
61  
62      @BeforeClass
63      public static void createSystemTemporaryDir()
64      {
65          dumpDirectory = new File( TMP, "dump" );
66          assertThat( dumpDirectory.mkdir() )
67                  .isTrue();
68      }
69  
70      @AfterClass
71      public static void deleteSystemTemporaryDir()
72      {
73          delete( TMP );
74      }
75  
76      @Test
77      public void relativeClasspathUnixSimple()
78          throws Exception
79      {
80          mockStatic( JarManifestForkConfiguration.class );
81          Path parent = mock( Path.class );
82          when( parent.toString() ).thenReturn( "/home/me/prj/target/surefire" );
83          Path classPathElement = mock( Path.class );
84          when( classPathElement.toString() ).thenReturn( "/home/me/.m2/repository/grp/art/1.0/art-1.0.jar" );
85          when( relativize( parent, classPathElement ) )
86                  .thenReturn( "../../../.m2/repository/grp/art/1.0/art-1.0.jar" );
87          when( toClasspathElementUri( same( parent ), same( classPathElement ), same( dumpDirectory ) ) )
88                  .thenCallRealMethod();
89          assertThat( toClasspathElementUri( parent, classPathElement, dumpDirectory ) )
90                  .isEqualTo( "../../../.m2/repository/grp/art/1.0/art-1.0.jar" );
91      }
92  
93      @Test
94      public void relativeClasspathUnixTricky()
95          throws Exception
96      {
97          mockStatic( JarManifestForkConfiguration.class );
98          Path parent = mock( Path.class );
99          when( parent.toString() ).thenReturn( "/home/me/prj/target/surefire" );
100         Path classPathElement = mock( Path.class );
101         when( classPathElement.toString() ).thenReturn( "/the Maven repo/grp/art/1.0/art-1.0.jar" );
102         when( relativize( parent, classPathElement ) )
103                 .thenReturn( "../../../../../the Maven repo/grp/art/1.0/art-1.0.jar" );
104         when( toClasspathElementUri( same( parent ), same( classPathElement ), same( dumpDirectory ) ) )
105                 .thenCallRealMethod();
106         assertThat( toClasspathElementUri( parent, classPathElement, dumpDirectory ) )
107                 .isEqualTo( "../../../../../the%20Maven%20repo/grp/art/1.0/art-1.0.jar" );
108     }
109 
110     @Test
111     public void relativeClasspathWindowsSimple()
112         throws Exception
113     {
114         mockStatic( JarManifestForkConfiguration.class );
115         Path parent = mock( Path.class );
116         when( parent.toString() ).thenReturn( "C:\\Windows\\Temp\\surefire" );
117         Path classPathElement = mock( Path.class );
118         when( classPathElement.toString() ).thenReturn( "C:\\Users\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar" );
119         when( relativize( parent, classPathElement ) )
120                 .thenReturn( "..\\..\\..\\Users\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar" );
121         when( toClasspathElementUri( same( parent ), same( classPathElement ), same( dumpDirectory ) ) )
122                 .thenCallRealMethod();
123         assertThat( toClasspathElementUri( parent, classPathElement, dumpDirectory ) )
124                 .isEqualTo( "../../../Users/me/.m2/repository/grp/art/1.0/art-1.0.jar" );
125     }
126 
127     @Test
128     public void relativeClasspathWindowsTricky()
129         throws Exception
130     {
131         mockStatic( JarManifestForkConfiguration.class );
132         Path parent = mock( Path.class );
133         when( parent.toString() ).thenReturn( "C:\\Windows\\Temp\\surefire" );
134         Path classPathElement = mock( Path.class );
135         when( classPathElement.toString() ).thenReturn( "C:\\Test User\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar" );
136         when( relativize( parent, classPathElement ) )
137                 .thenReturn( "..\\..\\..\\Test User\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar" );
138         when( toClasspathElementUri( same( parent ), same( classPathElement ), same( dumpDirectory ) ) )
139                 .thenCallRealMethod();
140         assertThat( toClasspathElementUri( parent, classPathElement, dumpDirectory ) )
141                 .isEqualTo( "../../../Test%20User/me/.m2/repository/grp/art/1.0/art-1.0.jar" );
142     }
143 
144     @Test
145     public void crossDriveWindows()
146         throws Exception
147     {
148         mockStatic( JarManifestForkConfiguration.class );
149         mockStatic( InPluginProcessDumpSingleton.class );
150         when( InPluginProcessDumpSingleton.getSingleton() ).thenReturn( mock( InPluginProcessDumpSingleton.class ) );
151         Path parent = mock( Path.class );
152         when( parent.toString() ).thenReturn( "C:\\Windows\\Temp\\surefire" );
153         Path classPathElement = mock( Path.class );
154         when( classPathElement.toString() ).thenReturn( "X:\\Users\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar" );
155         when( classPathElement.toUri() )
156                 .thenAnswer( new Answer<URI>()
157                 {
158                     @Override
159                     public URI answer( InvocationOnMock invocation ) throws URISyntaxException
160                     {
161                         String path = invocation.getMock().toString();
162                         return new URI( "file", "", "/" + path.replace( '\\', '/' ), null );
163                     }
164                 } );
165         when( relativize( same( parent ), same( classPathElement ) ) )
166                 .thenThrow( new IllegalArgumentException() );
167         when( toClasspathElementUri( same( parent ), same( classPathElement ), same( dumpDirectory ) ) )
168                 .thenCallRealMethod();
169         when( toAbsoluteUri( same( classPathElement ) ) )
170                 .thenCallRealMethod();
171         assertThat( toClasspathElementUri( parent, classPathElement, dumpDirectory ) )
172                 .isEqualTo( "file:///X:/Users/me/.m2/repository/grp/art/1.0/art-1.0.jar" );
173     }
174 
175     @Test
176     public void shouldRelativizeOnRealPlatform()
177     {
178         Path parentDir = new File( TMP, "test-parent-1" )
179                 .toPath();
180 
181         Path testDir = new File( TMP, "@1 test with white spaces" )
182                 .toPath();
183 
184         String relativeTestDir = relativize( parentDir, testDir );
185 
186         assertThat( relativeTestDir )
187                 .isEqualTo( ".." + File.separator + "@1 test with white spaces" );
188     }
189 
190     @Test
191     public void shouldMakeAbsoluteUriOnRealPlatform()
192             throws Exception
193     {
194         Path testDir = new File( TMP, "@2 test with white spaces" )
195                 .toPath();
196 
197         URI testDirUri = new URI( toAbsoluteUri( testDir ) );
198 
199         assertThat( testDirUri.getScheme() )
200                 .isEqualTo( "file" );
201 
202         assertThat( testDirUri.getRawPath() )
203                 .isEqualTo( testDir.toUri().getRawPath() );
204     }
205 
206     @Test
207     public void shouldMakeRelativeUriOnRealPlatform()
208             throws Exception
209     {
210         Path parentDir = new File( TMP, "test-parent-2" )
211                 .toPath();
212 
213         Path testDir = new File( TMP, "@3 test with white spaces" )
214                 .toPath();
215 
216         String testDirUriPath = toClasspathElementUri( parentDir, testDir, dumpDirectory );
217 
218         assertThat( testDirUriPath )
219                 .isEqualTo( "../@3%20test%20with%20white%20spaces" );
220     }
221 }