From bd5c6304a2382d2804f1afe7dd1fbb5bbce2af83 Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Sat, 8 Jan 2022 16:36:23 +0100 Subject: [PATCH] [SUREFIRE-1869] Replace deprecated File.toURL() by File.toURI().toURL() File.toURL() - does not automatically escape illegal characters --- .../maven/surefire/booter/Classpath.java | 2 +- .../maven/surefire/booter/ClasspathTest.java | 23 +++++++++---------- .../booter/IsolatedClassLoaderTest.java | 2 +- .../surefire/booter/NewClassLoaderRunner.java | 11 +++++---- .../surefire-946-self-destruct-plugin/pom.xml | 2 +- .../test-helper-dump-pid-plugin/pom.xml | 2 +- 6 files changed, 22 insertions(+), 20 deletions(-) diff --git a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/Classpath.java b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/Classpath.java index b2ed4e6f02..ef5265c63e 100644 --- a/surefire-booter/src/main/java/org/apache/maven/surefire/booter/Classpath.java +++ b/surefire-booter/src/main/java/org/apache/maven/surefire/booter/Classpath.java @@ -146,7 +146,7 @@ public ClassLoader createClassLoader( boolean childDelegation, boolean enableAss IsolatedClassLoader classLoader = new IsolatedClassLoader( parent, childDelegation, roleName ); for ( String classPathElement : unmodifiableElements ) { - classLoader.addURL( new File( classPathElement ).toURL() ); + classLoader.addURL( new File( classPathElement ).toURI().toURL() ); } if ( parent != null ) { diff --git a/surefire-booter/src/test/java/org/apache/maven/surefire/booter/ClasspathTest.java b/surefire-booter/src/test/java/org/apache/maven/surefire/booter/ClasspathTest.java index 38387d593e..023e8360b9 100644 --- a/surefire-booter/src/test/java/org/apache/maven/surefire/booter/ClasspathTest.java +++ b/surefire-booter/src/test/java/org/apache/maven/surefire/booter/ClasspathTest.java @@ -20,6 +20,7 @@ */ import java.io.File; +import java.net.URI; import java.net.URL; import java.util.Iterator; import java.util.List; @@ -201,8 +202,8 @@ public void testLoadInNewClassLoader() throws Exception URL url = target.getResource( thisPath ); assertTrue( url.toString().endsWith( thisPath ) ); String s = url.toString().replace( thisPath, "" ).replace( "!", "" ).replace( "jar:file:", "file:" ); - String oneClasspath = new URL( s ).getFile(); - assertTrue( new File( oneClasspath ).exists() ); + URI oneClasspath = new URI( s ); + assertTrue( "File: '" + oneClasspath + "' should exist", new File( oneClasspath ).exists() ); Classpath classpath = Classpath.emptyClasspath(); ClassLoader classLoader = classpath.addClassPathElementUrl( new File( oneClasspath ).getCanonicalPath() ) .createClassLoader( false, true, "" ); @@ -212,22 +213,20 @@ public void testLoadInNewClassLoader() throws Exception assertNotSame( cls, target ); } - public void testDontLoadInNewClassLoader() + public void testDontLoadInNewClassLoader() throws SurefireExecutionException { Class target = ConsoleLogger.class; - String thisPath = "/" + target.getName().replace( '.', '/' ) + ".class"; - URL url = target.getResource( thisPath ); - assertTrue( url.toString().endsWith( thisPath ) ); + + ClassLoader classLoader = emptyClasspath().createClassLoader( false, true, "" ); + try { - Classpath.emptyClasspath() - .addClassPathElementUrl( "\u0000" ) - .createClassLoader( false, true, "" ); - fail(); + classLoader.loadClass( target.getName() ); + fail( "Class should not be loaded" ); } - catch ( SurefireExecutionException e ) + catch ( ClassNotFoundException e ) { - // expected + assertEquals( target.getName(), e.getMessage() ); } } } diff --git a/surefire-booter/src/test/java/org/apache/maven/surefire/booter/IsolatedClassLoaderTest.java b/surefire-booter/src/test/java/org/apache/maven/surefire/booter/IsolatedClassLoaderTest.java index 9b93e522a2..2e002909a8 100644 --- a/surefire-booter/src/test/java/org/apache/maven/surefire/booter/IsolatedClassLoaderTest.java +++ b/surefire-booter/src/test/java/org/apache/maven/surefire/booter/IsolatedClassLoaderTest.java @@ -50,7 +50,7 @@ public void prepareClassLoader() throws Exception for ( String file : files ) { - URL fileUrl = new File( file ).toURL(); + URL fileUrl = new File( file ).toURI().toURL(); classLoader.addURL( fileUrl ); } } diff --git a/surefire-booter/src/test/java/org/apache/maven/surefire/booter/NewClassLoaderRunner.java b/surefire-booter/src/test/java/org/apache/maven/surefire/booter/NewClassLoaderRunner.java index 8fd653b204..154a87ea88 100644 --- a/surefire-booter/src/test/java/org/apache/maven/surefire/booter/NewClassLoaderRunner.java +++ b/surefire-booter/src/test/java/org/apache/maven/surefire/booter/NewClassLoaderRunner.java @@ -216,6 +216,7 @@ private static URL[] toClassPath() } catch ( IOException e ) { + e.printStackTrace(); return new URL[0]; } } @@ -225,7 +226,7 @@ private static Collection toPathList( String path ) throws MalformedURLExce Collection classPath = new HashSet<>(); for ( String file : path.split( pathSeparator ) ) { - classPath.add( new File( file ).toURL() ); + classPath.add( new File( file ).toURI().toURL() ); } return classPath; } @@ -241,13 +242,15 @@ private static Collection toPathList() { File f = new File( file ); File dir = f.getParentFile(); - classPath.add( ( dir.getName().equals( "target" ) ? new File( dir, "classes" ) : f ).toURL() ); + classPath.add( + ( dir.getName().equals( "target" ) ? new File( dir, "classes" ) : f ).toURI().toURL() ); } - classPath.add( new File( "target/classes" ).toURL() ); - classPath.add( new File( "target/test-classes" ).toURL() ); + classPath.add( new File( "target/classes" ).toURI().toURL() ); + classPath.add( new File( "target/test-classes" ).toURI().toURL() ); } catch ( IOException e ) { + e.printStackTrace(); // turn to java.class.path classPath.clear(); } diff --git a/surefire-its/src/test/resources/surefire-946-self-destruct-plugin/pom.xml b/surefire-its/src/test/resources/surefire-946-self-destruct-plugin/pom.xml index 5abb0524bb..ddad27e4e6 100644 --- a/surefire-its/src/test/resources/surefire-946-self-destruct-plugin/pom.xml +++ b/surefire-its/src/test/resources/surefire-946-self-destruct-plugin/pom.xml @@ -29,7 +29,7 @@ org.apache.maven.plugins maven-plugin-plugin - 2.9 + 3.6.1 maven-selfdestruct-plugin diff --git a/surefire-its/src/test/resources/test-helper-dump-pid-plugin/pom.xml b/surefire-its/src/test/resources/test-helper-dump-pid-plugin/pom.xml index f6bfa7c1c0..4cc41d4909 100644 --- a/surefire-its/src/test/resources/test-helper-dump-pid-plugin/pom.xml +++ b/surefire-its/src/test/resources/test-helper-dump-pid-plugin/pom.xml @@ -40,7 +40,7 @@ org.apache.maven.plugins maven-plugin-plugin - 3.2 + 3.6.1 true