From d6a9760510f3dce6b91e2868f5e1462ecc3c81e3 Mon Sep 17 00:00:00 2001 From: Jesse Glick Date: Wed, 7 Nov 2018 14:48:30 -0500 Subject: [PATCH 1/5] [SUREFIRE-1593] Correcting relativization logic to produce valid URIs on Windows. --- .../JarManifestForkConfiguration.java | 34 +++-- .../JarManifestForkConfigurationTest.java | 119 ++++++++++++++++++ .../maven/surefire/JUnit4SuiteTest.java | 2 + 3 files changed, 143 insertions(+), 12 deletions(-) create mode 100644 maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfigurationTest.java diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfiguration.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfiguration.java index 70f95d9532..25f55066e4 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfiguration.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfiguration.java @@ -33,7 +33,7 @@ import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; -import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -101,7 +101,7 @@ private File createJar( @Nonnull List classPath, @Nonnull String startCl { file.deleteOnExit(); } - Path parent = file.getParentFile().toPath(); + String parent = file.getParent(); FileOutputStream fos = new FileOutputStream( file ); try ( JarOutputStream jos = new JarOutputStream( fos ) ) { @@ -116,10 +116,10 @@ private File createJar( @Nonnull List classPath, @Nonnull String startCl StringBuilder cp = new StringBuilder(); for ( Iterator it = classPath.iterator(); it.hasNext(); ) { - File classPathElement = new File( it.next() ); + String classPathElement = it.next(); String uri = toClasspathElementUri( parent, classPathElement, dumpLogDirectory ); cp.append( uri ); - if ( classPathElement.isDirectory() && !uri.endsWith( "/" ) ) + if ( new File( classPathElement ).isDirectory() && !uri.endsWith( "/" ) ) { cp.append( '/' ); } @@ -143,24 +143,34 @@ private File createJar( @Nonnull List classPath, @Nonnull String startCl } } - private static String toClasspathElementUri( @Nonnull Path parent, - @Nonnull File classPathElement, - @Nonnull File dumpLogDirectory ) + static String relativize( @Nonnull String parent, @Nonnull String child ) + throws IllegalArgumentException + { + return Paths.get( parent ).relativize( Paths.get( child ) ).toString(); + } + + static String absoluteUri( @Nonnull String file ) + { + return Paths.get( file ).toUri().toASCIIString(); + } + + static String toClasspathElementUri( @Nonnull String parent, + @Nonnull String classPathElement, + @Nonnull File dumpLogDirectory ) throws IOException { try { - return new URI( null, parent.relativize( classPathElement.toPath() ).toString(), null ) - .toASCIIString(); + String uriPath = relativize( parent, classPathElement ).replace( '\\', '/' ); + return new URI( null, uriPath, null ).toASCIIString(); } catch ( IllegalArgumentException e ) { - String error = "Boot Manifest-JAR contains absolute paths in classpath " + classPathElement.getPath(); + String error = "Boot Manifest-JAR contains absolute paths in classpath " + classPathElement; InPluginProcessDumpSingleton.getSingleton() .dumpException( e, error, dumpLogDirectory ); - return classPathElement.toURI() - .toASCIIString(); + return absoluteUri( classPathElement ); } catch ( URISyntaxException e ) { diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfigurationTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfigurationTest.java new file mode 100644 index 0000000000..7e25e7fee3 --- /dev/null +++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfigurationTest.java @@ -0,0 +1,119 @@ +package org.apache.maven.plugin.surefire.booterclient; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.File; +import org.apache.maven.plugin.surefire.booterclient.output.InPluginProcessDumpSingleton; +import static org.fest.assertions.Assertions.assertThat; +import org.junit.Test; +import org.junit.runner.RunWith; +import static org.mockito.ArgumentMatchers.*; +import static org.powermock.api.mockito.PowerMockito.*; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +/** + * Unit tests for {@link JarManifestForkConfiguration}. + */ +@RunWith( PowerMockRunner.class ) +@PrepareForTest( { JarManifestForkConfiguration.class, InPluginProcessDumpSingleton.class } ) +public class JarManifestForkConfigurationTest +{ + + @Test + public void relativeClasspathUnixSimple() + throws Exception + { + mockStatic( JarManifestForkConfiguration.class ); + String parent = "/home/me/prj/target/surefire"; + String classPathElement = "/home/me/.m2/repository/grp/art/1.0/art-1.0.jar"; + when( JarManifestForkConfiguration.relativize( parent, classPathElement ) ). + thenReturn( "../../../.m2/repository/grp/art/1.0/art-1.0.jar" ); + when( JarManifestForkConfiguration.toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ). + thenCallRealMethod(); + assertThat( JarManifestForkConfiguration.toClasspathElementUri( parent, classPathElement, new File(".") ) ). + isEqualTo( "../../../.m2/repository/grp/art/1.0/art-1.0.jar" ); + } + + @Test + public void relativeClasspathUnixTricky() + throws Exception + { + mockStatic( JarManifestForkConfiguration.class ); + String parent = "/home/me/prj/target/surefire"; + String classPathElement = "/the Maven repo/grp/art/1.0/art-1.0.jar"; + when( JarManifestForkConfiguration.relativize( parent, classPathElement ) ). + thenReturn( "../../../../../the Maven repo/grp/art/1.0/art-1.0.jar" ); + when( JarManifestForkConfiguration.toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ). + thenCallRealMethod(); + assertThat( JarManifestForkConfiguration.toClasspathElementUri( parent, classPathElement, new File(".") ) ). + isEqualTo( "../../../../../the%20Maven%20repo/grp/art/1.0/art-1.0.jar" ); + } + + @Test + public void relativeClasspathWindowsSimple() + throws Exception + { + mockStatic( JarManifestForkConfiguration.class ); + String parent = "C:\\Windows\\Temp\\surefire"; + String classPathElement = "C:\\Users\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar"; + when( JarManifestForkConfiguration.relativize( parent, classPathElement ) ). + thenReturn( "..\\..\\..\\Users\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar" ); + when( JarManifestForkConfiguration.toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ). + thenCallRealMethod(); + assertThat( JarManifestForkConfiguration.toClasspathElementUri( parent, classPathElement, new File(".") ) ). + isEqualTo( "../../../Users/me/.m2/repository/grp/art/1.0/art-1.0.jar" ); + } + + @Test + public void relativeClasspathWindowsTricky() + throws Exception + { + mockStatic( JarManifestForkConfiguration.class ); + String parent = "C:\\Windows\\Temp\\surefire"; + String classPathElement = "C:\\Test User\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar"; + when( JarManifestForkConfiguration.relativize( parent, classPathElement ) ). + thenReturn( "..\\..\\..\\Test User\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar" ); + when( JarManifestForkConfiguration.toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ). + thenCallRealMethod(); + assertThat( JarManifestForkConfiguration.toClasspathElementUri( parent, classPathElement, new File(".") ) ). + isEqualTo( "../../../Test%20User/me/.m2/repository/grp/art/1.0/art-1.0.jar" ); + } + + @Test + public void crossDriveWindows() + throws Exception + { + mockStatic( JarManifestForkConfiguration.class ); + mockStatic( InPluginProcessDumpSingleton.class ); + when( InPluginProcessDumpSingleton.getSingleton() ).thenReturn(mock( InPluginProcessDumpSingleton.class )); + String parent = "C:\\Windows\\Temp\\surefire"; + String classPathElement = "X:\\Users\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar"; + when( JarManifestForkConfiguration.relativize( parent, classPathElement ) ). + thenThrow( new IllegalArgumentException() ); + when( JarManifestForkConfiguration.absoluteUri( classPathElement ) ). + thenReturn( "file:///X:/Users/me/.m2/repository/grp/art/1.0/art-1.0.jar" ); + when( JarManifestForkConfiguration.toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ). + thenCallRealMethod(); + assertThat( JarManifestForkConfiguration.toClasspathElementUri( parent, classPathElement, new File(".") ) ). + isEqualTo( "file:///X:/Users/me/.m2/repository/grp/art/1.0/art-1.0.jar" ); + } + +} diff --git a/maven-surefire-common/src/test/java/org/apache/maven/surefire/JUnit4SuiteTest.java b/maven-surefire-common/src/test/java/org/apache/maven/surefire/JUnit4SuiteTest.java index 728cef3a38..c248de8669 100644 --- a/maven-surefire-common/src/test/java/org/apache/maven/surefire/JUnit4SuiteTest.java +++ b/maven-surefire-common/src/test/java/org/apache/maven/surefire/JUnit4SuiteTest.java @@ -34,6 +34,7 @@ import org.apache.maven.plugin.surefire.booterclient.DefaultForkConfigurationTest; import org.apache.maven.plugin.surefire.booterclient.ForkConfigurationTest; import org.apache.maven.plugin.surefire.booterclient.ForkingRunListenerTest; +import org.apache.maven.plugin.surefire.booterclient.JarManifestForkConfigurationTest; import org.apache.maven.plugin.surefire.booterclient.ModularClasspathForkConfigurationTest; import org.apache.maven.plugin.surefire.booterclient.lazytestprovider.TestLessInputStreamBuilderTest; import org.apache.maven.plugin.surefire.booterclient.lazytestprovider.TestProvidingInputStreamTest; @@ -88,6 +89,7 @@ public static Test suite() suite.addTest( new JUnit4TestAdapter( SurefireHelperTest.class ) ); suite.addTest( new JUnit4TestAdapter( AbstractSurefireMojoTest.class ) ); suite.addTest( new JUnit4TestAdapter( DefaultForkConfigurationTest.class ) ); + suite.addTest( new JUnit4TestAdapter( JarManifestForkConfigurationTest.class ) ); suite.addTest( new JUnit4TestAdapter( ModularClasspathForkConfigurationTest.class ) ); if ( JAVA_RECENT.atLeast( JAVA_1_7 ) ) { From 7456273aed3b542f4d91e51d82b3e3afc1109aed Mon Sep 17 00:00:00 2001 From: Tibor17 Date: Sat, 10 Nov 2018 20:59:11 +0100 Subject: [PATCH 2/5] more unit tests and code style refactoring --- .../JarManifestForkConfiguration.java | 22 ++- .../JarManifestForkConfigurationTest.java | 139 +++++++++++++----- 2 files changed, 119 insertions(+), 42 deletions(-) diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfiguration.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfiguration.java index 25f55066e4..e805a33b85 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfiguration.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfiguration.java @@ -146,23 +146,29 @@ private File createJar( @Nonnull List classPath, @Nonnull String startCl static String relativize( @Nonnull String parent, @Nonnull String child ) throws IllegalArgumentException { - return Paths.get( parent ).relativize( Paths.get( child ) ).toString(); + return Paths.get( parent ) + .relativize( Paths.get( child ) ) + .toString(); } - static String absoluteUri( @Nonnull String file ) + static String toAbsoluteUri( @Nonnull String absolutePath ) { - return Paths.get( file ).toUri().toASCIIString(); + return Paths.get( absolutePath ) + .toUri() + .toASCIIString(); } static String toClasspathElementUri( @Nonnull String parent, - @Nonnull String classPathElement, - @Nonnull File dumpLogDirectory ) + @Nonnull String classPathElement, + @Nonnull File dumpLogDirectory ) throws IOException { try { - String uriPath = relativize( parent, classPathElement ).replace( '\\', '/' ); - return new URI( null, uriPath, null ).toASCIIString(); + String relativeUriPath = relativize( parent, classPathElement ) + .replace( '\\', '/' ); + + return new URI( null, relativeUriPath, null ).toASCIIString(); } catch ( IllegalArgumentException e ) { @@ -170,7 +176,7 @@ static String toClasspathElementUri( @Nonnull String parent, InPluginProcessDumpSingleton.getSingleton() .dumpException( e, error, dumpLogDirectory ); - return absoluteUri( classPathElement ); + return toAbsoluteUri( classPathElement ); } catch ( URISyntaxException e ) { diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfigurationTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfigurationTest.java index 7e25e7fee3..58ef76001b 100644 --- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfigurationTest.java +++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfigurationTest.java @@ -20,12 +20,17 @@ */ import java.io.File; +import java.net.URI; + import org.apache.maven.plugin.surefire.booterclient.output.InPluginProcessDumpSingleton; import static org.fest.assertions.Assertions.assertThat; import org.junit.Test; import org.junit.runner.RunWith; -import static org.mockito.ArgumentMatchers.*; -import static org.powermock.api.mockito.PowerMockito.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.powermock.api.mockito.PowerMockito.mock; +import static org.powermock.api.mockito.PowerMockito.mockStatic; +import static org.powermock.api.mockito.PowerMockito.when; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @@ -36,6 +41,7 @@ @PrepareForTest( { JarManifestForkConfiguration.class, InPluginProcessDumpSingleton.class } ) public class JarManifestForkConfigurationTest { + private static final String BASE_DIR = System.getProperty( "user.dir" ); @Test public void relativeClasspathUnixSimple() @@ -44,12 +50,15 @@ public void relativeClasspathUnixSimple() mockStatic( JarManifestForkConfiguration.class ); String parent = "/home/me/prj/target/surefire"; String classPathElement = "/home/me/.m2/repository/grp/art/1.0/art-1.0.jar"; - when( JarManifestForkConfiguration.relativize( parent, classPathElement ) ). - thenReturn( "../../../.m2/repository/grp/art/1.0/art-1.0.jar" ); - when( JarManifestForkConfiguration.toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ). - thenCallRealMethod(); - assertThat( JarManifestForkConfiguration.toClasspathElementUri( parent, classPathElement, new File(".") ) ). - isEqualTo( "../../../.m2/repository/grp/art/1.0/art-1.0.jar" ); + when( JarManifestForkConfiguration.relativize( parent, classPathElement ) ) + .thenReturn( "../../../.m2/repository/grp/art/1.0/art-1.0.jar" ); + when( JarManifestForkConfiguration.toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ) + .thenCallRealMethod(); + File dump = new File( BASE_DIR, "target/test-dump" ); + //noinspection ResultOfMethodCallIgnored + dump.mkdir(); + assertThat( JarManifestForkConfiguration.toClasspathElementUri( parent, classPathElement, dump ) ) + .isEqualTo( "../../../.m2/repository/grp/art/1.0/art-1.0.jar" ); } @Test @@ -59,12 +68,13 @@ public void relativeClasspathUnixTricky() mockStatic( JarManifestForkConfiguration.class ); String parent = "/home/me/prj/target/surefire"; String classPathElement = "/the Maven repo/grp/art/1.0/art-1.0.jar"; - when( JarManifestForkConfiguration.relativize( parent, classPathElement ) ). - thenReturn( "../../../../../the Maven repo/grp/art/1.0/art-1.0.jar" ); - when( JarManifestForkConfiguration.toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ). - thenCallRealMethod(); - assertThat( JarManifestForkConfiguration.toClasspathElementUri( parent, classPathElement, new File(".") ) ). - isEqualTo( "../../../../../the%20Maven%20repo/grp/art/1.0/art-1.0.jar" ); + when( JarManifestForkConfiguration.relativize( parent, classPathElement ) ) + .thenReturn( "../../../../../the Maven repo/grp/art/1.0/art-1.0.jar" ); + when( JarManifestForkConfiguration.toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ) + .thenCallRealMethod(); + File dump = new File( BASE_DIR, "target/test-dump" ); + assertThat( JarManifestForkConfiguration.toClasspathElementUri( parent, classPathElement, dump ) ) + .isEqualTo( "../../../../../the%20Maven%20repo/grp/art/1.0/art-1.0.jar" ); } @Test @@ -74,12 +84,13 @@ public void relativeClasspathWindowsSimple() mockStatic( JarManifestForkConfiguration.class ); String parent = "C:\\Windows\\Temp\\surefire"; String classPathElement = "C:\\Users\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar"; - when( JarManifestForkConfiguration.relativize( parent, classPathElement ) ). - thenReturn( "..\\..\\..\\Users\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar" ); - when( JarManifestForkConfiguration.toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ). - thenCallRealMethod(); - assertThat( JarManifestForkConfiguration.toClasspathElementUri( parent, classPathElement, new File(".") ) ). - isEqualTo( "../../../Users/me/.m2/repository/grp/art/1.0/art-1.0.jar" ); + when( JarManifestForkConfiguration.relativize( parent, classPathElement ) ) + .thenReturn( "..\\..\\..\\Users\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar" ); + when( JarManifestForkConfiguration.toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ) + .thenCallRealMethod(); + File dump = new File( BASE_DIR, "target/test-dump" ); + assertThat( JarManifestForkConfiguration.toClasspathElementUri( parent, classPathElement, dump ) ) + .isEqualTo( "../../../Users/me/.m2/repository/grp/art/1.0/art-1.0.jar" ); } @Test @@ -89,12 +100,13 @@ public void relativeClasspathWindowsTricky() mockStatic( JarManifestForkConfiguration.class ); String parent = "C:\\Windows\\Temp\\surefire"; String classPathElement = "C:\\Test User\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar"; - when( JarManifestForkConfiguration.relativize( parent, classPathElement ) ). - thenReturn( "..\\..\\..\\Test User\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar" ); - when( JarManifestForkConfiguration.toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ). - thenCallRealMethod(); - assertThat( JarManifestForkConfiguration.toClasspathElementUri( parent, classPathElement, new File(".") ) ). - isEqualTo( "../../../Test%20User/me/.m2/repository/grp/art/1.0/art-1.0.jar" ); + when( JarManifestForkConfiguration.relativize( parent, classPathElement ) ) + .thenReturn( "..\\..\\..\\Test User\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar" ); + when( JarManifestForkConfiguration.toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ) + .thenCallRealMethod(); + File dump = new File( BASE_DIR, "target/test-dump" ); + assertThat( JarManifestForkConfiguration.toClasspathElementUri( parent, classPathElement, dump ) ) + .isEqualTo( "../../../Test%20User/me/.m2/repository/grp/art/1.0/art-1.0.jar" ); } @Test @@ -106,14 +118,73 @@ public void crossDriveWindows() when( InPluginProcessDumpSingleton.getSingleton() ).thenReturn(mock( InPluginProcessDumpSingleton.class )); String parent = "C:\\Windows\\Temp\\surefire"; String classPathElement = "X:\\Users\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar"; - when( JarManifestForkConfiguration.relativize( parent, classPathElement ) ). - thenThrow( new IllegalArgumentException() ); - when( JarManifestForkConfiguration.absoluteUri( classPathElement ) ). - thenReturn( "file:///X:/Users/me/.m2/repository/grp/art/1.0/art-1.0.jar" ); - when( JarManifestForkConfiguration.toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ). - thenCallRealMethod(); - assertThat( JarManifestForkConfiguration.toClasspathElementUri( parent, classPathElement, new File(".") ) ). - isEqualTo( "file:///X:/Users/me/.m2/repository/grp/art/1.0/art-1.0.jar" ); + when( JarManifestForkConfiguration.relativize( parent, classPathElement ) ) + .thenThrow( new IllegalArgumentException() ); + when( JarManifestForkConfiguration.toAbsoluteUri( classPathElement ) ) + .thenReturn( "file:///X:/Users/me/.m2/repository/grp/art/1.0/art-1.0.jar" ); + when( JarManifestForkConfiguration.toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ) + .thenCallRealMethod(); + File dump = new File( BASE_DIR, "target/test-dump" ); + assertThat( JarManifestForkConfiguration.toClasspathElementUri( parent, classPathElement, dump ) ) + .isEqualTo( "file:///X:/Users/me/.m2/repository/grp/art/1.0/art-1.0.jar" ); } + @Test + public void shouldRelativizeOnRealPlatform() + throws Exception + { + File parentDir = new File( BASE_DIR, "target/test-parent" ); + //noinspection ResultOfMethodCallIgnored + parentDir.mkdir(); + File testDir = new File( BASE_DIR, "target/test with white spaces" ); + //noinspection ResultOfMethodCallIgnored + testDir.mkdir(); + + String relativeTestDir = + JarManifestForkConfiguration.relativize( parentDir.getCanonicalPath(), testDir.getCanonicalPath() ); + + assertThat( relativeTestDir ) + .isEqualTo( ".." + File.separator + "test with white spaces" ); + } + + @Test + public void shouldMakeAbsoluteUriOnRealPlatform() + throws Exception + { + File testDir = new File( BASE_DIR, "target/@2 test with white spaces" ); + //noinspection ResultOfMethodCallIgnored + testDir.mkdir(); + + URI testDirUri = new URI( JarManifestForkConfiguration.toAbsoluteUri( testDir.getCanonicalPath() ) ); + + assertThat( testDirUri.getScheme() ) + .isEqualTo( "file" ); + + assertThat( testDirUri.getRawPath() ) + .isEqualTo( testDir.toURI().getRawPath() ); + } + + @Test + public void shouldMakeRelativeUriOnRealPlatform() + throws Exception + { + File parentDir = new File( BASE_DIR, "target/test-parent" ); + //noinspection ResultOfMethodCallIgnored + parentDir.mkdir(); + File testDir = new File( BASE_DIR, "target/@3 test with white spaces" ); + //noinspection ResultOfMethodCallIgnored + testDir.mkdir(); + + File dump = new File( BASE_DIR, "target/test-dump" ); + //noinspection ResultOfMethodCallIgnored + dump.mkdir(); + + String parent = parentDir.getCanonicalPath(); + String cpElement = testDir.getCanonicalPath(); + + String testDirUriPath = JarManifestForkConfiguration.toClasspathElementUri( parent, cpElement, dump ); + + assertThat( testDirUriPath ) + .isEqualTo( "../@3%20test%20with%20white%20spaces" ); + } } From 22abd4cb008fefcfe4400fc80dcd92a234eeba2a Mon Sep 17 00:00:00 2001 From: Tibor17 Date: Tue, 13 Nov 2018 23:40:42 +0100 Subject: [PATCH 3/5] using system Temp in unit test --- .../JarManifestForkConfigurationTest.java | 102 +++++++++--------- 1 file changed, 52 insertions(+), 50 deletions(-) diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfigurationTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfigurationTest.java index 58ef76001b..f4d3367ab2 100644 --- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfigurationTest.java +++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfigurationTest.java @@ -20,11 +20,20 @@ */ import java.io.File; +import java.io.IOException; import java.net.URI; import org.apache.maven.plugin.surefire.booterclient.output.InPluginProcessDumpSingleton; + +import static org.apache.maven.plugin.surefire.booterclient.JarManifestForkConfiguration.relativize; +import static org.apache.maven.plugin.surefire.booterclient.JarManifestForkConfiguration.toAbsoluteUri; +import static org.apache.maven.plugin.surefire.booterclient.JarManifestForkConfiguration.toClasspathElementUri; import static org.fest.assertions.Assertions.assertThat; + +import org.junit.BeforeClass; +import org.junit.ClassRule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.junit.runner.RunWith; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; @@ -41,7 +50,18 @@ @PrepareForTest( { JarManifestForkConfiguration.class, InPluginProcessDumpSingleton.class } ) public class JarManifestForkConfigurationTest { - private static final String BASE_DIR = System.getProperty( "user.dir" ); + @ClassRule + public static final TemporaryFolder TMP = new TemporaryFolder(); + + private static File dumpDirectory; + + @BeforeClass + public static void createSystemTemporaryDir() + throws IOException + { + TMP.create(); + dumpDirectory = TMP.newFolder(); + } @Test public void relativeClasspathUnixSimple() @@ -50,14 +70,11 @@ public void relativeClasspathUnixSimple() mockStatic( JarManifestForkConfiguration.class ); String parent = "/home/me/prj/target/surefire"; String classPathElement = "/home/me/.m2/repository/grp/art/1.0/art-1.0.jar"; - when( JarManifestForkConfiguration.relativize( parent, classPathElement ) ) + when( relativize( parent, classPathElement ) ) .thenReturn( "../../../.m2/repository/grp/art/1.0/art-1.0.jar" ); - when( JarManifestForkConfiguration.toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ) + when( toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ) .thenCallRealMethod(); - File dump = new File( BASE_DIR, "target/test-dump" ); - //noinspection ResultOfMethodCallIgnored - dump.mkdir(); - assertThat( JarManifestForkConfiguration.toClasspathElementUri( parent, classPathElement, dump ) ) + assertThat( toClasspathElementUri( parent, classPathElement, dumpDirectory ) ) .isEqualTo( "../../../.m2/repository/grp/art/1.0/art-1.0.jar" ); } @@ -68,12 +85,11 @@ public void relativeClasspathUnixTricky() mockStatic( JarManifestForkConfiguration.class ); String parent = "/home/me/prj/target/surefire"; String classPathElement = "/the Maven repo/grp/art/1.0/art-1.0.jar"; - when( JarManifestForkConfiguration.relativize( parent, classPathElement ) ) + when( relativize( parent, classPathElement ) ) .thenReturn( "../../../../../the Maven repo/grp/art/1.0/art-1.0.jar" ); - when( JarManifestForkConfiguration.toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ) + when( toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ) .thenCallRealMethod(); - File dump = new File( BASE_DIR, "target/test-dump" ); - assertThat( JarManifestForkConfiguration.toClasspathElementUri( parent, classPathElement, dump ) ) + assertThat( toClasspathElementUri( parent, classPathElement, dumpDirectory ) ) .isEqualTo( "../../../../../the%20Maven%20repo/grp/art/1.0/art-1.0.jar" ); } @@ -84,12 +100,11 @@ public void relativeClasspathWindowsSimple() mockStatic( JarManifestForkConfiguration.class ); String parent = "C:\\Windows\\Temp\\surefire"; String classPathElement = "C:\\Users\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar"; - when( JarManifestForkConfiguration.relativize( parent, classPathElement ) ) + when( relativize( parent, classPathElement ) ) .thenReturn( "..\\..\\..\\Users\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar" ); - when( JarManifestForkConfiguration.toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ) + when( toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ) .thenCallRealMethod(); - File dump = new File( BASE_DIR, "target/test-dump" ); - assertThat( JarManifestForkConfiguration.toClasspathElementUri( parent, classPathElement, dump ) ) + assertThat( toClasspathElementUri( parent, classPathElement, dumpDirectory ) ) .isEqualTo( "../../../Users/me/.m2/repository/grp/art/1.0/art-1.0.jar" ); } @@ -100,12 +115,11 @@ public void relativeClasspathWindowsTricky() mockStatic( JarManifestForkConfiguration.class ); String parent = "C:\\Windows\\Temp\\surefire"; String classPathElement = "C:\\Test User\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar"; - when( JarManifestForkConfiguration.relativize( parent, classPathElement ) ) + when( relativize( parent, classPathElement ) ) .thenReturn( "..\\..\\..\\Test User\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar" ); - when( JarManifestForkConfiguration.toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ) + when( toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ) .thenCallRealMethod(); - File dump = new File( BASE_DIR, "target/test-dump" ); - assertThat( JarManifestForkConfiguration.toClasspathElementUri( parent, classPathElement, dump ) ) + assertThat( toClasspathElementUri( parent, classPathElement, dumpDirectory ) ) .isEqualTo( "../../../Test%20User/me/.m2/repository/grp/art/1.0/art-1.0.jar" ); } @@ -118,14 +132,13 @@ public void crossDriveWindows() when( InPluginProcessDumpSingleton.getSingleton() ).thenReturn(mock( InPluginProcessDumpSingleton.class )); String parent = "C:\\Windows\\Temp\\surefire"; String classPathElement = "X:\\Users\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar"; - when( JarManifestForkConfiguration.relativize( parent, classPathElement ) ) + when( relativize( parent, classPathElement ) ) .thenThrow( new IllegalArgumentException() ); - when( JarManifestForkConfiguration.toAbsoluteUri( classPathElement ) ) + when( toAbsoluteUri( classPathElement ) ) .thenReturn( "file:///X:/Users/me/.m2/repository/grp/art/1.0/art-1.0.jar" ); - when( JarManifestForkConfiguration.toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ) + when( toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ) .thenCallRealMethod(); - File dump = new File( BASE_DIR, "target/test-dump" ); - assertThat( JarManifestForkConfiguration.toClasspathElementUri( parent, classPathElement, dump ) ) + assertThat( toClasspathElementUri( parent, classPathElement, dumpDirectory ) ) .isEqualTo( "file:///X:/Users/me/.m2/repository/grp/art/1.0/art-1.0.jar" ); } @@ -133,29 +146,26 @@ public void crossDriveWindows() public void shouldRelativizeOnRealPlatform() throws Exception { - File parentDir = new File( BASE_DIR, "target/test-parent" ); - //noinspection ResultOfMethodCallIgnored - parentDir.mkdir(); - File testDir = new File( BASE_DIR, "target/test with white spaces" ); - //noinspection ResultOfMethodCallIgnored - testDir.mkdir(); + File parentDir = TMP.newFolder( "test-parent-1" ) + .getCanonicalFile(); - String relativeTestDir = - JarManifestForkConfiguration.relativize( parentDir.getCanonicalPath(), testDir.getCanonicalPath() ); + File testDir = TMP.newFolder( "@1 test with white spaces" ) + .getCanonicalFile(); + + String relativeTestDir = relativize( parentDir.getPath(), testDir.getPath() ); assertThat( relativeTestDir ) - .isEqualTo( ".." + File.separator + "test with white spaces" ); + .isEqualTo( ".." + File.separator + "@1 test with white spaces" ); } @Test public void shouldMakeAbsoluteUriOnRealPlatform() throws Exception { - File testDir = new File( BASE_DIR, "target/@2 test with white spaces" ); - //noinspection ResultOfMethodCallIgnored - testDir.mkdir(); + File testDir = TMP.newFolder( "@2 test with white spaces" ) + .getCanonicalFile(); - URI testDirUri = new URI( JarManifestForkConfiguration.toAbsoluteUri( testDir.getCanonicalPath() ) ); + URI testDirUri = new URI( toAbsoluteUri( testDir.getPath() ) ); assertThat( testDirUri.getScheme() ) .isEqualTo( "file" ); @@ -168,21 +178,13 @@ public void shouldMakeAbsoluteUriOnRealPlatform() public void shouldMakeRelativeUriOnRealPlatform() throws Exception { - File parentDir = new File( BASE_DIR, "target/test-parent" ); - //noinspection ResultOfMethodCallIgnored - parentDir.mkdir(); - File testDir = new File( BASE_DIR, "target/@3 test with white spaces" ); - //noinspection ResultOfMethodCallIgnored - testDir.mkdir(); - - File dump = new File( BASE_DIR, "target/test-dump" ); - //noinspection ResultOfMethodCallIgnored - dump.mkdir(); + File parentDir = TMP.newFolder( "test-parent-2" ) + .getCanonicalFile(); - String parent = parentDir.getCanonicalPath(); - String cpElement = testDir.getCanonicalPath(); + File testDir = TMP.newFolder( "@3 test with white spaces" ) + .getCanonicalFile(); - String testDirUriPath = JarManifestForkConfiguration.toClasspathElementUri( parent, cpElement, dump ); + String testDirUriPath = toClasspathElementUri( parentDir.getPath(), testDir.getPath(), dumpDirectory ); assertThat( testDirUriPath ) .isEqualTo( "../@3%20test%20with%20white%20spaces" ); From 5da8cbe760fa94c0891b7254945b82206e830234 Mon Sep 17 00:00:00 2001 From: Tibor17 Date: Wed, 14 Nov 2018 02:06:46 +0100 Subject: [PATCH 4/5] creating Path object once instead of multiple times from a String --- .../JarManifestForkConfiguration.java | 25 ++--- .../JarManifestForkConfigurationTest.java | 94 ++++++++++++------- 2 files changed, 72 insertions(+), 47 deletions(-) diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfiguration.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfiguration.java index e805a33b85..79db5c6f30 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfiguration.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfiguration.java @@ -33,6 +33,7 @@ import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.Iterator; import java.util.List; @@ -42,6 +43,7 @@ import java.util.jar.JarOutputStream; import java.util.jar.Manifest; +import static java.nio.file.Files.isDirectory; import static org.apache.maven.plugin.surefire.SurefireHelper.escapeToPlatformPath; /** @@ -101,7 +103,7 @@ private File createJar( @Nonnull List classPath, @Nonnull String startCl { file.deleteOnExit(); } - String parent = file.getParent(); + Path parent = file.getParentFile().toPath(); FileOutputStream fos = new FileOutputStream( file ); try ( JarOutputStream jos = new JarOutputStream( fos ) ) { @@ -116,10 +118,10 @@ private File createJar( @Nonnull List classPath, @Nonnull String startCl StringBuilder cp = new StringBuilder(); for ( Iterator it = classPath.iterator(); it.hasNext(); ) { - String classPathElement = it.next(); + Path classPathElement = Paths.get( it.next() ); String uri = toClasspathElementUri( parent, classPathElement, dumpLogDirectory ); cp.append( uri ); - if ( new File( classPathElement ).isDirectory() && !uri.endsWith( "/" ) ) + if ( isDirectory( classPathElement ) && !uri.endsWith( "/" ) ) { cp.append( '/' ); } @@ -143,23 +145,21 @@ private File createJar( @Nonnull List classPath, @Nonnull String startCl } } - static String relativize( @Nonnull String parent, @Nonnull String child ) + static String relativize( @Nonnull Path parent, @Nonnull Path child ) throws IllegalArgumentException { - return Paths.get( parent ) - .relativize( Paths.get( child ) ) + return parent.relativize( child ) .toString(); } - static String toAbsoluteUri( @Nonnull String absolutePath ) + static String toAbsoluteUri( @Nonnull Path absolutePath ) { - return Paths.get( absolutePath ) - .toUri() + return absolutePath.toUri() .toASCIIString(); } - static String toClasspathElementUri( @Nonnull String parent, - @Nonnull String classPathElement, + static String toClasspathElementUri( @Nonnull Path parent, + @Nonnull Path classPathElement, @Nonnull File dumpLogDirectory ) throws IOException { @@ -168,7 +168,8 @@ static String toClasspathElementUri( @Nonnull String parent, String relativeUriPath = relativize( parent, classPathElement ) .replace( '\\', '/' ); - return new URI( null, relativeUriPath, null ).toASCIIString(); + return new URI( null, relativeUriPath, null ) + .toASCIIString(); } catch ( IllegalArgumentException e ) { diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfigurationTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfigurationTest.java index f4d3367ab2..da8f031beb 100644 --- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfigurationTest.java +++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfigurationTest.java @@ -22,6 +22,8 @@ import java.io.File; import java.io.IOException; import java.net.URI; +import java.net.URISyntaxException; +import java.nio.file.Path; import org.apache.maven.plugin.surefire.booterclient.output.InPluginProcessDumpSingleton; @@ -35,11 +37,13 @@ import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.junit.runner.RunWith; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.same; import static org.powermock.api.mockito.PowerMockito.mock; import static org.powermock.api.mockito.PowerMockito.mockStatic; import static org.powermock.api.mockito.PowerMockito.when; + +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @@ -68,11 +72,13 @@ public void relativeClasspathUnixSimple() throws Exception { mockStatic( JarManifestForkConfiguration.class ); - String parent = "/home/me/prj/target/surefire"; - String classPathElement = "/home/me/.m2/repository/grp/art/1.0/art-1.0.jar"; + Path parent = mock( Path.class ); + when( parent.toString() ).thenReturn( "/home/me/prj/target/surefire" ); + Path classPathElement = mock( Path.class ); + when( classPathElement.toString() ).thenReturn( "/home/me/.m2/repository/grp/art/1.0/art-1.0.jar" ); when( relativize( parent, classPathElement ) ) .thenReturn( "../../../.m2/repository/grp/art/1.0/art-1.0.jar" ); - when( toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ) + when( toClasspathElementUri( same( parent ), same( classPathElement ), same( dumpDirectory ) ) ) .thenCallRealMethod(); assertThat( toClasspathElementUri( parent, classPathElement, dumpDirectory ) ) .isEqualTo( "../../../.m2/repository/grp/art/1.0/art-1.0.jar" ); @@ -83,11 +89,13 @@ public void relativeClasspathUnixTricky() throws Exception { mockStatic( JarManifestForkConfiguration.class ); - String parent = "/home/me/prj/target/surefire"; - String classPathElement = "/the Maven repo/grp/art/1.0/art-1.0.jar"; + Path parent = mock( Path.class ); + when( parent.toString() ).thenReturn( "/home/me/prj/target/surefire" ); + Path classPathElement = mock( Path.class ); + when( classPathElement.toString() ).thenReturn( "/the Maven repo/grp/art/1.0/art-1.0.jar" ); when( relativize( parent, classPathElement ) ) .thenReturn( "../../../../../the Maven repo/grp/art/1.0/art-1.0.jar" ); - when( toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ) + when( toClasspathElementUri( same( parent ), same( classPathElement ), same( dumpDirectory ) ) ) .thenCallRealMethod(); assertThat( toClasspathElementUri( parent, classPathElement, dumpDirectory ) ) .isEqualTo( "../../../../../the%20Maven%20repo/grp/art/1.0/art-1.0.jar" ); @@ -98,11 +106,13 @@ public void relativeClasspathWindowsSimple() throws Exception { mockStatic( JarManifestForkConfiguration.class ); - String parent = "C:\\Windows\\Temp\\surefire"; - String classPathElement = "C:\\Users\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar"; + Path parent = mock( Path.class ); + when( parent.toString() ).thenReturn( "C:\\Windows\\Temp\\surefire" ); + Path classPathElement = mock( Path.class ); + when( classPathElement.toString() ).thenReturn( "C:\\Users\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar" ); when( relativize( parent, classPathElement ) ) .thenReturn( "..\\..\\..\\Users\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar" ); - when( toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ) + when( toClasspathElementUri( same( parent ), same( classPathElement ), same( dumpDirectory ) ) ) .thenCallRealMethod(); assertThat( toClasspathElementUri( parent, classPathElement, dumpDirectory ) ) .isEqualTo( "../../../Users/me/.m2/repository/grp/art/1.0/art-1.0.jar" ); @@ -113,11 +123,13 @@ public void relativeClasspathWindowsTricky() throws Exception { mockStatic( JarManifestForkConfiguration.class ); - String parent = "C:\\Windows\\Temp\\surefire"; - String classPathElement = "C:\\Test User\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar"; + Path parent = mock( Path.class ); + when( parent.toString() ).thenReturn( "C:\\Windows\\Temp\\surefire" ); + Path classPathElement = mock( Path.class ); + when( classPathElement.toString() ).thenReturn( "C:\\Test User\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar" ); when( relativize( parent, classPathElement ) ) .thenReturn( "..\\..\\..\\Test User\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar" ); - when( toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ) + when( toClasspathElementUri( same( parent ), same( classPathElement ), same( dumpDirectory ) ) ) .thenCallRealMethod(); assertThat( toClasspathElementUri( parent, classPathElement, dumpDirectory ) ) .isEqualTo( "../../../Test%20User/me/.m2/repository/grp/art/1.0/art-1.0.jar" ); @@ -129,14 +141,26 @@ public void crossDriveWindows() { mockStatic( JarManifestForkConfiguration.class ); mockStatic( InPluginProcessDumpSingleton.class ); - when( InPluginProcessDumpSingleton.getSingleton() ).thenReturn(mock( InPluginProcessDumpSingleton.class )); - String parent = "C:\\Windows\\Temp\\surefire"; - String classPathElement = "X:\\Users\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar"; - when( relativize( parent, classPathElement ) ) + when( InPluginProcessDumpSingleton.getSingleton() ).thenReturn( mock( InPluginProcessDumpSingleton.class ) ); + Path parent = mock( Path.class ); + when( parent.toString() ).thenReturn( "C:\\Windows\\Temp\\surefire" ); + Path classPathElement = mock( Path.class ); + when( classPathElement.toString() ).thenReturn( "X:\\Users\\me\\.m2\\repository\\grp\\art\\1.0\\art-1.0.jar" ); + when( classPathElement.toUri() ) + .thenAnswer( new Answer() + { + @Override + public URI answer( InvocationOnMock invocation ) throws URISyntaxException + { + String path = invocation.getMock().toString(); + return new URI( "file", "", "/" + path.replace( '\\', '/' ), null ); + } + } ); + when( relativize( same( parent ), same( classPathElement ) ) ) .thenThrow( new IllegalArgumentException() ); - when( toAbsoluteUri( classPathElement ) ) - .thenReturn( "file:///X:/Users/me/.m2/repository/grp/art/1.0/art-1.0.jar" ); - when( toClasspathElementUri( anyString(), anyString(), any( File.class ) ) ) + when( toClasspathElementUri( same( parent ), same( classPathElement ), same( dumpDirectory ) ) ) + .thenCallRealMethod(); + when( toAbsoluteUri( same( classPathElement ) ) ) .thenCallRealMethod(); assertThat( toClasspathElementUri( parent, classPathElement, dumpDirectory ) ) .isEqualTo( "file:///X:/Users/me/.m2/repository/grp/art/1.0/art-1.0.jar" ); @@ -146,13 +170,13 @@ public void crossDriveWindows() public void shouldRelativizeOnRealPlatform() throws Exception { - File parentDir = TMP.newFolder( "test-parent-1" ) - .getCanonicalFile(); + Path parentDir = TMP.newFolder( "test-parent-1" ) + .toPath(); - File testDir = TMP.newFolder( "@1 test with white spaces" ) - .getCanonicalFile(); + Path testDir = TMP.newFolder( "@1 test with white spaces" ) + .toPath(); - String relativeTestDir = relativize( parentDir.getPath(), testDir.getPath() ); + String relativeTestDir = relativize( parentDir, testDir ); assertThat( relativeTestDir ) .isEqualTo( ".." + File.separator + "@1 test with white spaces" ); @@ -162,29 +186,29 @@ public void shouldRelativizeOnRealPlatform() public void shouldMakeAbsoluteUriOnRealPlatform() throws Exception { - File testDir = TMP.newFolder( "@2 test with white spaces" ) - .getCanonicalFile(); + Path testDir = TMP.newFolder( "@2 test with white spaces" ) + .toPath(); - URI testDirUri = new URI( toAbsoluteUri( testDir.getPath() ) ); + URI testDirUri = new URI( toAbsoluteUri( testDir ) ); assertThat( testDirUri.getScheme() ) .isEqualTo( "file" ); assertThat( testDirUri.getRawPath() ) - .isEqualTo( testDir.toURI().getRawPath() ); + .isEqualTo( testDir.toUri().getRawPath() ); } @Test public void shouldMakeRelativeUriOnRealPlatform() throws Exception { - File parentDir = TMP.newFolder( "test-parent-2" ) - .getCanonicalFile(); + Path parentDir = TMP.newFolder( "test-parent-2" ) + .toPath(); - File testDir = TMP.newFolder( "@3 test with white spaces" ) - .getCanonicalFile(); + Path testDir = TMP.newFolder( "@3 test with white spaces" ) + .toPath(); - String testDirUriPath = toClasspathElementUri( parentDir.getPath(), testDir.getPath(), dumpDirectory ); + String testDirUriPath = toClasspathElementUri( parentDir, testDir, dumpDirectory ); assertThat( testDirUriPath ) .isEqualTo( "../@3%20test%20with%20white%20spaces" ); From 4ec94cb833b72cd1c6eab0ee9db11e9c09aa9c8f Mon Sep 17 00:00:00 2001 From: Tibor17 Date: Thu, 15 Nov 2018 20:36:41 +0100 Subject: [PATCH 5/5] Avoided @Rule in PowerMock test. Used assertion f/w fest-assert (org.fest.util.Files). --- .../JarManifestForkConfigurationTest.java | 33 +++++++++++-------- pom.xml | 2 +- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfigurationTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfigurationTest.java index da8f031beb..18b205ab1b 100644 --- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfigurationTest.java +++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/JarManifestForkConfigurationTest.java @@ -20,7 +20,6 @@ */ import java.io.File; -import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.nio.file.Path; @@ -32,11 +31,13 @@ import static org.apache.maven.plugin.surefire.booterclient.JarManifestForkConfiguration.toClasspathElementUri; import static org.fest.assertions.Assertions.assertThat; +import org.junit.AfterClass; import org.junit.BeforeClass; -import org.junit.ClassRule; import org.junit.Test; -import org.junit.rules.TemporaryFolder; import org.junit.runner.RunWith; + +import static org.fest.util.Files.delete; +import static org.fest.util.Files.newTemporaryFolder; import static org.mockito.ArgumentMatchers.same; import static org.powermock.api.mockito.PowerMockito.mock; import static org.powermock.api.mockito.PowerMockito.mockStatic; @@ -54,17 +55,22 @@ @PrepareForTest( { JarManifestForkConfiguration.class, InPluginProcessDumpSingleton.class } ) public class JarManifestForkConfigurationTest { - @ClassRule - public static final TemporaryFolder TMP = new TemporaryFolder(); + private static final File TMP = newTemporaryFolder(); private static File dumpDirectory; @BeforeClass public static void createSystemTemporaryDir() - throws IOException { - TMP.create(); - dumpDirectory = TMP.newFolder(); + dumpDirectory = new File( TMP, "dump" ); + assertThat( dumpDirectory.mkdir() ) + .isTrue(); + } + + @AfterClass + public static void deleteSystemTemporaryDir() + { + delete( TMP ); } @Test @@ -168,12 +174,11 @@ public URI answer( InvocationOnMock invocation ) throws URISyntaxException @Test public void shouldRelativizeOnRealPlatform() - throws Exception { - Path parentDir = TMP.newFolder( "test-parent-1" ) + Path parentDir = new File( TMP, "test-parent-1" ) .toPath(); - Path testDir = TMP.newFolder( "@1 test with white spaces" ) + Path testDir = new File( TMP, "@1 test with white spaces" ) .toPath(); String relativeTestDir = relativize( parentDir, testDir ); @@ -186,7 +191,7 @@ public void shouldRelativizeOnRealPlatform() public void shouldMakeAbsoluteUriOnRealPlatform() throws Exception { - Path testDir = TMP.newFolder( "@2 test with white spaces" ) + Path testDir = new File( TMP, "@2 test with white spaces" ) .toPath(); URI testDirUri = new URI( toAbsoluteUri( testDir ) ); @@ -202,10 +207,10 @@ public void shouldMakeAbsoluteUriOnRealPlatform() public void shouldMakeRelativeUriOnRealPlatform() throws Exception { - Path parentDir = TMP.newFolder( "test-parent-2" ) + Path parentDir = new File( TMP, "test-parent-2" ) .toPath(); - Path testDir = TMP.newFolder( "@3 test with white spaces" ) + Path testDir = new File( TMP, "@3 test with white spaces" ) .toPath(); String testDirUriPath = toClasspathElementUri( parentDir, testDir, dumpDirectory ); diff --git a/pom.xml b/pom.xml index c3b6e5bd80..c61e5969ef 100644 --- a/pom.xml +++ b/pom.xml @@ -91,7 +91,7 @@ 2.6 3.1.0 - 2.0.0-RC.1 + 2.0.0-RC.3 scm:git:https://gitbox.apache.org/repos/asf/maven-surefire.git surefire-archives/surefire-LATEST