From 69458ad8521a32d86e01b66b2631f3c869c11b6f Mon Sep 17 00:00:00 2001 From: marko-bekhta Date: Mon, 18 Sep 2023 19:23:01 +0200 Subject: [PATCH] HSEARCH-4947 Make sure that nested jars can be read on Windows --- .../search/util/common/jar/impl/CodeSource.java | 2 +- .../search/util/common/jar/impl/CodeSourceTest.java | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/util/common/src/main/java/org/hibernate/search/util/common/jar/impl/CodeSource.java b/util/common/src/main/java/org/hibernate/search/util/common/jar/impl/CodeSource.java index 83cd3bf43f2..7248804d362 100644 --- a/util/common/src/main/java/org/hibernate/search/util/common/jar/impl/CodeSource.java +++ b/util/common/src/main/java/org/hibernate/search/util/common/jar/impl/CodeSource.java @@ -150,7 +150,7 @@ else if ( "file".equals( codeSourceLocation.getProtocol() ) ) { else { // The URI points to a regular file, so hopefully an actual JAR file. // We'll try to open a ZIP filesystem to work on the contents of the JAR file. - URI jarUri = new URI( "jar:file", null, path.toString(), null ); + URI jarUri = new URI( "jar:file", null, path.toUri().getPath(), null ); tryInitJarFileSystem( jarUri ); } } diff --git a/util/common/src/test/java/org/hibernate/search/util/common/jar/impl/CodeSourceTest.java b/util/common/src/test/java/org/hibernate/search/util/common/jar/impl/CodeSourceTest.java index 5b8b6714f98..e4fcc766601 100644 --- a/util/common/src/test/java/org/hibernate/search/util/common/jar/impl/CodeSourceTest.java +++ b/util/common/src/test/java/org/hibernate/search/util/common/jar/impl/CodeSourceTest.java @@ -51,13 +51,14 @@ public void directory() throws Exception { addSimpleClass( root ); } ); - try ( URLClassLoader isolatedClassLoader = createIsolatedClassLoader( dirPath.toUri().toURL() ) ) { + URL dirPathUrl = dirPath.toUri().toURL(); + try ( URLClassLoader isolatedClassLoader = createIsolatedClassLoader( dirPathUrl ) ) { Class classInIsolatedClassLoader = isolatedClassLoader.loadClass( SimpleClass.class.getName() ); // Check preconditions: this is the situation that we want to test. URL location = classInIsolatedClassLoader.getProtectionDomain().getCodeSource().getLocation(); assertThat( location.getProtocol() ).isEqualTo( "file" ); - assertThat( location.toExternalForm() ).contains( dirPath.toString() ); + assertThat( location.toExternalForm() ).contains( dirPathUrl.toString() ); // Check that the JAR can be opened and that we can access other files within it try ( CodeSource codeSource = new CodeSource( location ) ) { @@ -86,13 +87,14 @@ public void jar_fileScheme() throws Exception { addSimpleClass( root ); } ); - try ( URLClassLoader isolatedClassLoader = createIsolatedClassLoader( jarPath.toUri().toURL() ) ) { + URL jarPathUrl = jarPath.toUri().toURL(); + try ( URLClassLoader isolatedClassLoader = createIsolatedClassLoader( jarPathUrl ) ) { Class classInIsolatedClassLoader = isolatedClassLoader.loadClass( SimpleClass.class.getName() ); // Check preconditions: this is the situation that we want to test. URL location = classInIsolatedClassLoader.getProtectionDomain().getCodeSource().getLocation(); assertThat( location.getProtocol() ).isEqualTo( "file" ); - assertThat( location.toExternalForm() ).contains( jarPath.toString() ); + assertThat( location.toExternalForm() ).contains( jarPathUrl.toString() ); // Check that the JAR can be opened and that we can access other files within it try ( CodeSource codeSource = new CodeSource( location ) ) { @@ -130,7 +132,7 @@ public void jar_jarScheme_classesInRoot() throws Exception { URL location = classInIsolatedClassLoader.getProtectionDomain().getCodeSource().getLocation(); // For some reason the "jar" scheme gets replaced with "file" assertThat( location.getProtocol() ).isEqualTo( "file" ); - assertThat( location.toExternalForm() ).contains( jarPath.toString() ); + assertThat( location.toExternalForm() ).contains( jarPath.toUri().toURL().toString() ); // Check that the JAR can be opened and that we can access other files within it try ( CodeSource codeSource = new CodeSource( location ) ) {