Update classpath index to use jar name instead of full path
See gh-20564
This commit is contained in:
@@ -29,8 +29,6 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -45,33 +43,15 @@ final class ClassPathIndexFile {
|
||||
|
||||
private final List<String> lines;
|
||||
|
||||
private final Set<String> folders;
|
||||
|
||||
private ClassPathIndexFile(File root, List<String> lines) {
|
||||
this.root = root;
|
||||
this.lines = lines;
|
||||
this.folders = this.lines.stream().map(this::getFolder).filter(Objects::nonNull).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
private String getFolder(String name) {
|
||||
int lastSlash = name.lastIndexOf('/');
|
||||
return (lastSlash != -1) ? name.substring(0, lastSlash) : null;
|
||||
}
|
||||
|
||||
int size() {
|
||||
return this.lines.size();
|
||||
}
|
||||
|
||||
boolean containsFolder(String name) {
|
||||
if (name == null || name.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
if (name.endsWith("/")) {
|
||||
return containsFolder(name.substring(0, name.length() - 1));
|
||||
}
|
||||
return this.folders.contains(name);
|
||||
}
|
||||
|
||||
boolean containsEntry(String name) {
|
||||
if (name == null || name.isEmpty()) {
|
||||
return false;
|
||||
|
||||
@@ -73,30 +73,16 @@ class ClassPathIndexFileTests {
|
||||
assertThat(indexFile.size()).isEqualTo(5);
|
||||
}
|
||||
|
||||
@Test
|
||||
void containsFolderWhenFolderIsPresentReturnsTrue() throws Exception {
|
||||
ClassPathIndexFile indexFile = copyAndLoadTestIndexFile();
|
||||
assertThat(indexFile.containsFolder("BOOT-INF/layers/one/lib")).isTrue();
|
||||
assertThat(indexFile.containsFolder("BOOT-INF/layers/one/lib/")).isTrue();
|
||||
assertThat(indexFile.containsFolder("BOOT-INF/layers/two/lib")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void containsFolderWhenFolderIsMissingReturnsFalse() throws Exception {
|
||||
ClassPathIndexFile indexFile = copyAndLoadTestIndexFile();
|
||||
assertThat(indexFile.containsFolder("BOOT-INF/layers/nope/lib/")).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getUrlsReturnsUrls() throws Exception {
|
||||
ClassPathIndexFile indexFile = copyAndLoadTestIndexFile();
|
||||
List<URL> urls = indexFile.getUrls();
|
||||
List<File> expected = new ArrayList<>();
|
||||
expected.add(new File(this.temp, "BOOT-INF/layers/one/lib/a.jar"));
|
||||
expected.add(new File(this.temp, "BOOT-INF/layers/one/lib/b.jar"));
|
||||
expected.add(new File(this.temp, "BOOT-INF/layers/one/lib/c.jar"));
|
||||
expected.add(new File(this.temp, "BOOT-INF/layers/two/lib/d.jar"));
|
||||
expected.add(new File(this.temp, "BOOT-INF/layers/two/lib/e.jar"));
|
||||
expected.add(new File(this.temp, "a.jar"));
|
||||
expected.add(new File(this.temp, "b.jar"));
|
||||
expected.add(new File(this.temp, "c.jar"));
|
||||
expected.add(new File(this.temp, "d.jar"));
|
||||
expected.add(new File(this.temp, "e.jar"));
|
||||
assertThat(urls).containsExactly(expected.stream().map(this::toUrl).toArray(URL[]::new));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
BOOT-INF/layers/one/lib/a.jar
|
||||
BOOT-INF/layers/one/lib/b.jar
|
||||
BOOT-INF/layers/one/lib/c.jar
|
||||
BOOT-INF/layers/two/lib/d.jar
|
||||
BOOT-INF/layers/two/lib/e.jar
|
||||
a.jar
|
||||
b.jar
|
||||
c.jar
|
||||
d.jar
|
||||
e.jar
|
||||
|
||||
Reference in New Issue
Block a user