Include empty layers when listing and extracting

Fixes gh-21301
pull/21310/head
Andy Wilkinson 5 years ago
parent 5d7df790f1
commit de1e3c6069

@ -20,16 +20,16 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.NoSuchFileException; import java.nio.file.NoSuchFileException;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StreamUtils; import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -41,18 +41,19 @@ import org.springframework.util.StringUtils;
*/ */
class IndexedLayers implements Layers { class IndexedLayers implements Layers {
private MultiValueMap<String, String> layers = new LinkedMultiValueMap<>(); private final Map<String, List<String>> layers = new LinkedHashMap<>();
IndexedLayers(String indexFile) { IndexedLayers(String indexFile) {
String[] lines = Arrays.stream(indexFile.split("\n")).map((line) -> line.replace("\r", "")) String[] lines = Arrays.stream(indexFile.split("\n")).map((line) -> line.replace("\r", ""))
.filter(StringUtils::hasText).toArray(String[]::new); .filter(StringUtils::hasText).toArray(String[]::new);
String layer = null; List<String> contents = null;
for (String line : lines) { for (String line : lines) {
if (line.startsWith("- ")) { 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(" - ")) { else if (line.startsWith(" - ")) {
this.layers.add(layer, line.substring(5, line.length() - 1)); contents.add(line.substring(5, line.length() - 1));
} }
else { else {
throw new IllegalStateException("Layer index file is malformed"); throw new IllegalStateException("Layer index file is malformed");

@ -69,10 +69,11 @@ class ExtractCommandTests {
@Test @Test
void runExtractsLayers() throws Exception { void runExtractsLayers() throws Exception {
this.command.run(Collections.emptyMap(), Collections.emptyList()); 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, "a/a/a.jar")).exists();
assertThat(new File(this.extract, "b/b/b.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, "c/c/c.jar")).exists();
assertThat(new File(this.extract, "d")).isDirectory();
} }
@Test @Test
@ -119,7 +120,7 @@ class ExtractCommandTests {
@Override @Override
public Iterator<String> iterator() { public Iterator<String> iterator() {
return Arrays.asList("a", "b", "c").iterator(); return Arrays.asList("a", "b", "c", "d").iterator();
} }
@Override @Override

@ -52,7 +52,7 @@ class IndexedLayersTests {
@Test @Test
void iteratorReturnsLayers() throws Exception { void iteratorReturnsLayers() throws Exception {
IndexedLayers layers = new IndexedLayers(getIndex()); IndexedLayers layers = new IndexedLayers(getIndex());
assertThat(layers).containsExactly("test", "application"); assertThat(layers).containsExactly("test", "empty", "application");
} }
@Test @Test

@ -1,6 +1,7 @@
- "test": - "test":
- "BOOT-INF/lib/a.jar" - "BOOT-INF/lib/a.jar"
- "BOOT-INF/lib/b.jar" - "BOOT-INF/lib/b.jar"
- "empty":
- "application": - "application":
- "BOOT-INF/classes/Demo.class" - "BOOT-INF/classes/Demo.class"
- "META-INF/" - "META-INF/"

Loading…
Cancel
Save