From de1e3c606906d5d78b47e4f3e454fe9b6bc3d2ca Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 4 May 2020 15:15:14 +0100 Subject: [PATCH] Include empty layers when listing and extracting Fixes gh-21301 --- .../boot/jarmode/layertools/IndexedLayers.java | 13 +++++++------ .../jarmode/layertools/ExtractCommandTests.java | 5 +++-- .../boot/jarmode/layertools/IndexedLayersTests.java | 2 +- .../boot/jarmode/layertools/test-layers.idx | 1 + 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/main/java/org/springframework/boot/jarmode/layertools/IndexedLayers.java b/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/main/java/org/springframework/boot/jarmode/layertools/IndexedLayers.java index 14f66f3560a..1ae27236b46 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/main/java/org/springframework/boot/jarmode/layertools/IndexedLayers.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/main/java/org/springframework/boot/jarmode/layertools/IndexedLayers.java @@ -20,16 +20,16 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.NoSuchFileException; +import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.jar.JarFile; import java.util.zip.ZipEntry; import org.springframework.util.Assert; -import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; import org.springframework.util.StreamUtils; import org.springframework.util.StringUtils; @@ -41,18 +41,19 @@ import org.springframework.util.StringUtils; */ class IndexedLayers implements Layers { - private MultiValueMap layers = new LinkedMultiValueMap<>(); + private final Map> layers = new LinkedHashMap<>(); IndexedLayers(String indexFile) { String[] lines = Arrays.stream(indexFile.split("\n")).map((line) -> line.replace("\r", "")) .filter(StringUtils::hasText).toArray(String[]::new); - String layer = null; + List contents = null; for (String line : lines) { if (line.startsWith("- ")) { - layer = line.substring(3, line.length() - 2); + contents = new ArrayList<>(); + this.layers.put(line.substring(3, line.length() - 2), contents); } else if (line.startsWith(" - ")) { - this.layers.add(layer, line.substring(5, line.length() - 1)); + contents.add(line.substring(5, line.length() - 1)); } else { throw new IllegalStateException("Layer index file is malformed"); diff --git a/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/ExtractCommandTests.java b/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/ExtractCommandTests.java index cc0a9e23724..13646be6be9 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/ExtractCommandTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/ExtractCommandTests.java @@ -69,10 +69,11 @@ class ExtractCommandTests { @Test void runExtractsLayers() throws Exception { this.command.run(Collections.emptyMap(), Collections.emptyList()); - assertThat(this.extract.list()).containsOnly("a", "b", "c"); + assertThat(this.extract.list()).containsOnly("a", "b", "c", "d"); assertThat(new File(this.extract, "a/a/a.jar")).exists(); assertThat(new File(this.extract, "b/b/b.jar")).exists(); assertThat(new File(this.extract, "c/c/c.jar")).exists(); + assertThat(new File(this.extract, "d")).isDirectory(); } @Test @@ -119,7 +120,7 @@ class ExtractCommandTests { @Override public Iterator iterator() { - return Arrays.asList("a", "b", "c").iterator(); + return Arrays.asList("a", "b", "c", "d").iterator(); } @Override diff --git a/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/IndexedLayersTests.java b/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/IndexedLayersTests.java index db77bdb0f2c..aedbc284378 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/IndexedLayersTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/java/org/springframework/boot/jarmode/layertools/IndexedLayersTests.java @@ -52,7 +52,7 @@ class IndexedLayersTests { @Test void iteratorReturnsLayers() throws Exception { IndexedLayers layers = new IndexedLayers(getIndex()); - assertThat(layers).containsExactly("test", "application"); + assertThat(layers).containsExactly("test", "empty", "application"); } @Test diff --git a/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/resources/org/springframework/boot/jarmode/layertools/test-layers.idx b/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/resources/org/springframework/boot/jarmode/layertools/test-layers.idx index 0fcde4fdd1c..c1f53a927f3 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/resources/org/springframework/boot/jarmode/layertools/test-layers.idx +++ b/spring-boot-project/spring-boot-tools/spring-boot-jarmode-layertools/src/test/resources/org/springframework/boot/jarmode/layertools/test-layers.idx @@ -1,6 +1,7 @@ - "test": - "BOOT-INF/lib/a.jar" - "BOOT-INF/lib/b.jar" +- "empty": - "application": - "BOOT-INF/classes/Demo.class" - "META-INF/"