Skip to content

Commit

Permalink
Merge pull request #805 from jwachter/fix/nested-with-space-and-hash-…
Browse files Browse the repository at this point in the history
…path

Handle paths with spaces and hashes especially with nested JARs
  • Loading branch information
lukehutch committed Nov 2, 2023
2 parents 8b51126 + 72c52de commit 07c2f49
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/io/github/classgraph/Scanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,9 @@ private static Object normalizeClasspathEntry(final Object classpathEntryObj) th
final boolean isURL = JarUtils.URL_SCHEME_PATTERN.matcher(classpathEntStr).matches();
final boolean isMultiSection = classpathEntStr.contains("!");
if (isURL || isMultiSection) {
// Encode spaces and hash symbols in classpath entry as they potentially can be invalid when
// converted to a URL/URI
classpathEntStr = classpathEntStr.replace(" ", "%20").replace("#", "%23");
// Convert back to URL (or URI) if this has a URL scheme or if this is a multi-section
// path (which needs the "jar:file:" scheme)
if (!isURL) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.github.classgraph.issues.issue804;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import io.github.classgraph.ClassGraph;
import io.github.classgraph.ScanResult;

/**
* Issue 804.
*/
public class Issue804Test {

private static final String NESTED_EXAMPLE_CLASS = "org.springframework.util.ResourceUtils";

@Test
void scanningNestedJarsInPathsContainingSpacesShouldNeverFail(@TempDir Path tempDir) throws IOException {
Path targetJar = createSpringBootJarInExampleDirectory(tempDir, "directory with spaces");

try (ScanResult scanResult = scanJar(targetJar)) {
assertThat(scanResult.getClassInfo(NESTED_EXAMPLE_CLASS)).isNotNull();
}
}

@Test
void scanningNestedJarsInPathsContainingHashesShouldNeverFail(@TempDir Path tempDir) throws IOException {
Path targetJar = createSpringBootJarInExampleDirectory(tempDir, "directory-without-spaces#123");

try (ScanResult scanResult = scanJar(targetJar)) {
assertThat(scanResult.getClassInfo(NESTED_EXAMPLE_CLASS)).isNotNull();
}
}

@Test
void scanningNestedJarsInPathsContainingSpacesAndHashesShouldNeverFail(@TempDir Path tempDir) throws IOException {
Path targetJar = createSpringBootJarInExampleDirectory(tempDir, "directory with spaces #123");

try (ScanResult scanResult = scanJar(targetJar)) {
assertThat(scanResult.getClassInfo(NESTED_EXAMPLE_CLASS)).isNotNull();
}
}

private Path createSpringBootJarInExampleDirectory(Path temporaryDirectory, String directoryName)
throws IOException {
Path directoryWithSpaces = temporaryDirectory.resolve(directoryName);
Files.createDirectories(directoryWithSpaces);
Path nestedJar = directoryWithSpaces.resolve("spring-boot-fully-executable-jar.jar");
try (InputStream nestedJarsExample = Issue804Test.class.getClassLoader()
.getResourceAsStream("spring-boot-fully-executable-jar.jar")) {
Files.copy(nestedJarsExample, nestedJar);
}
return nestedJar;
}

private ScanResult scanJar(Path targetJar) {
return new ClassGraph().enableClassInfo().overrideClasspath(targetJar.toUri()).scan();
}

}

0 comments on commit 07c2f49

Please sign in to comment.