You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2022/06/22 09:48:32 UTC

[groovy] branch GROOVY_4_0_X updated: GROOVY-10663: Fixed issue in PackageHelperImpl#getPackageNames

This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY_4_0_X by this push:
     new ee58e72cfc GROOVY-10663: Fixed issue in PackageHelperImpl#getPackageNames
ee58e72cfc is described below

commit ee58e72cfca039b43cc7a9c157bf610a8039a72e
Author: Justin Chu <ju...@gmail.com>
AuthorDate: Tue Jun 21 16:46:09 2022 -0400

    GROOVY-10663: Fixed issue in PackageHelperImpl#getPackageNames
    
    PackageHelperImpl#getPackageNames would not be able to find and return
    the package names if the package path or filename of the jar contains
    plus signs due to the behavior of URLDecoder#decode.
---
 .../groovy/groovysh/util/PackageHelperImpl.groovy  |  3 +-
 .../groovysh/util/PackageHelperImplTest.groovy     | 66 ++++++++++++++++++++++
 2 files changed, 68 insertions(+), 1 deletion(-)

diff --git a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/util/PackageHelperImpl.groovy b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/util/PackageHelperImpl.groovy
index 490f5c7a4b..6a98b8f518 100644
--- a/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/util/PackageHelperImpl.groovy
+++ b/subprojects/groovy-groovysh/src/main/groovy/org/apache/groovy/groovysh/util/PackageHelperImpl.groovy
@@ -25,6 +25,7 @@ import org.codehaus.groovy.tools.shell.util.Logger
 import org.codehaus.groovy.tools.shell.util.Preferences
 
 import java.nio.file.InvalidPathException
+import java.nio.file.Paths
 import java.util.jar.JarEntry
 import java.util.jar.JarFile
 import java.util.prefs.PreferenceChangeEvent
@@ -243,7 +244,7 @@ Files.walkFileTree(fs.getPath('modules'),
      * Returns all package names found at URL; accepts jar files and folders.
      */
     static Collection<String> getPackageNames(URL url) {
-        File urlFile = new File(URLDecoder.decode(url.file, 'UTF-8'))
+        File urlFile = Paths.get(url.toURI()).toFile()
 
         if (urlFile.isDirectory()) {
             return new HashSet<String>().tap {
diff --git a/subprojects/groovy-groovysh/src/test/groovy/org/apache/groovy/groovysh/util/PackageHelperImplTest.groovy b/subprojects/groovy-groovysh/src/test/groovy/org/apache/groovy/groovysh/util/PackageHelperImplTest.groovy
index 34d8f6e47b..db3e0760ff 100644
--- a/subprojects/groovy-groovysh/src/test/groovy/org/apache/groovy/groovysh/util/PackageHelperImplTest.groovy
+++ b/subprojects/groovy-groovysh/src/test/groovy/org/apache/groovy/groovysh/util/PackageHelperImplTest.groovy
@@ -20,6 +20,12 @@ package org.apache.groovy.groovysh.util
 
 import groovy.test.GroovyTestCase
 
+import java.nio.file.Files
+import java.nio.file.Path
+import java.util.jar.JarEntry
+import java.util.jar.JarOutputStream
+import java.util.jar.Manifest
+
 /**
  * Unit tests for the {@link PackageHelperImpl} class.
  */
@@ -58,4 +64,64 @@ class PackageHelperImplTest extends GroovyTestCase {
         PackageHelperImpl helper = new PackageHelperImpl(null)
         assert [] as Set<String> == helper.getContents('java.util.regex.tools')
     }
+
+    void testGetPackageNamesFromPathWithSign() {
+        Path tempTestDirPath = Files.createTempDirectory(this.getClass().getName())
+        Path folderWithSign = tempTestDirPath.resolve('dummy+folder++1%23%%')
+
+        Path dummyFilePath = folderWithSign.resolve('dummypackage1').resolve('Dummy.class')
+        Files.createDirectories(dummyFilePath.getParent())
+        File dummyFile = dummyFilePath.toFile()
+        try (FileOutputStream fos = new FileOutputStream(dummyFile)) {
+            fos.write(0)
+        }
+        assert dummyFile.exists()
+
+        Path jarWithSignPath = folderWithSign.resolve("dummy+lib++1%23%%.jar")
+        try (FileOutputStream fos = new FileOutputStream(jarWithSignPath.toFile())
+             JarOutputStream jos = new JarOutputStream(fos, new Manifest())) {
+            JarEntry jarEntry = new JarEntry("dummypackage2/Dummy.class")
+            jos.putNextEntry(jarEntry)
+            jos.write(0)
+        }
+        assert jarWithSignPath.toFile().exists()
+
+        PackageHelperImpl helper = new PackageHelperImpl(null)
+
+        Set<String> packageInFolder = helper.getPackageNames(folderWithSign.toUri().toURL())
+        assert packageInFolder.contains("dummypackage1")
+
+        Set<String> packageInJar = helper.getPackageNames(jarWithSignPath.toUri().toURL())
+        assert packageInJar.contains("dummypackage2")
+    }
+
+    void testGetPackageNamesFromPathWithSpace() {
+        Path tempTestDirPath = Files.createTempDirectory(this.getClass().getName())
+        Path folderWithSpacePath = Files.createTempDirectory(tempTestDirPath, 'dummy folder 1')
+
+        Path dummyFilePath = folderWithSpacePath.resolve('dummypackage1').resolve('Dummy.class')
+        Files.createDirectories(dummyFilePath.getParent())
+        File dummyFile = dummyFilePath.toFile()
+        try (FileOutputStream fos = new FileOutputStream(dummyFile)) {
+            fos.write(0)
+        }
+        assert dummyFile.exists()
+
+        Path jarWithSpacePath = folderWithSpacePath.resolve("dummy lib 2.jar")
+        try (FileOutputStream fos = new FileOutputStream(jarWithSpacePath.toFile())
+             JarOutputStream jos = new JarOutputStream(fos, new Manifest())) {
+            JarEntry jarEntry = new JarEntry("dummypackage2/Dummy.class")
+            jos.putNextEntry(jarEntry)
+            jos.write(0)
+        }
+        assert jarWithSpacePath.toFile().exists()
+
+        PackageHelperImpl helper = new PackageHelperImpl(null)
+
+        Set<String> packageInFolder = helper.getPackageNames(folderWithSpacePath.toUri().toURL())
+        assert packageInFolder.contains("dummypackage1")
+
+        Set<String> packageInJar = helper.getPackageNames(jarWithSpacePath.toUri().toURL())
+        assert packageInJar.contains("dummypackage2")
+    }
 }