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.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<String, String> layers = new LinkedMultiValueMap<>();
private final Map<String, List<String>> 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<String> 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");

@ -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<String> iterator() {
return Arrays.asList("a", "b", "c").iterator();
return Arrays.asList("a", "b", "c", "d").iterator();
}
@Override

@ -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

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

Loading…
Cancel
Save