Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public final class JarFileScanner implements ResourceFinder {
*/
public JarFileScanner(final InputStream inputStream, final String parent, final boolean recursive) throws IOException {
this.jarInputStream = new JarInputStream(inputStream);
this.parent = parent;
this.parent = (parent.isEmpty() || parent.endsWith(String.valueOf(JAR_FILE_SEPARATOR))) ? parent : parent + JAR_FILE_SEPARATOR;
this.recursive = recursive;
}

Expand All @@ -91,17 +91,8 @@ public boolean hasNext() {
break;
}
if (!next.isDirectory() && next.getName().startsWith(parent)) {
final String suffix = next.getName().substring(parent.length());
if (recursive) {
// accept any entries with the prefix
if (parent.isEmpty() || suffix.indexOf(JAR_FILE_SEPARATOR) == 0) {
break;
}
} else {
// accept only entries directly in the folder.
if (suffix.lastIndexOf(JAR_FILE_SEPARATOR) == (parent.isEmpty() ? -1 : 0)) {
break;
}
if (recursive || next.getName().substring(parent.length()).indexOf(JAR_FILE_SEPARATOR) == -1) {
break;
}
}
} while (true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ public void testNonRecursiveClassEnumerationWithExistantPackage() throws IOExcep
assertThat("Failed to enumerate package 'javax.ws.rs' of javax.ws.rs-api", scannedEntries, equalTo(actualEntries));
}

@Test
public void testRecursiveClassEnumerationWithOptionalTrailingSlash() throws IOException {
final int scannedEntriesWithoutSlash = countJarEntriesUsingScanner("javax/ws/rs", true);
final int scannedEntriesWithSlash = countJarEntriesUsingScanner("javax/ws/rs/", true);
assertThat("Adding a trailing slash incorrectly affects recursive scanning", scannedEntriesWithSlash, equalTo(scannedEntriesWithoutSlash));
}

@Test
public void testNonRecursiveClassEnumerationWithOptionalTrailingSlash() throws IOException {
final int scannedEntriesWithoutSlash = countJarEntriesUsingScanner("javax/ws/rs", false);
final int scannedEntriesWithSlash = countJarEntriesUsingScanner("javax/ws/rs/", false);
assertThat("Adding a trailing slash incorrectly affects recursive scanning", scannedEntriesWithSlash, equalTo(scannedEntriesWithoutSlash));
}

private int countJarEntriesByPattern(final Pattern pattern) throws IOException {
int matchingEntries = 0;

Expand Down