Skip to content

Commit

Permalink
Include empty layers when listing and extracting
Browse files Browse the repository at this point in the history
Fixes gh-21301
  • Loading branch information
wilkinsona committed May 4, 2020
1 parent 5d7df79 commit de1e3c6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
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;

Expand All @@ -41,18 +41,19 @@
*/
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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ void setup() throws Exception {
@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
Expand Down Expand Up @@ -119,7 +120,7 @@ private static class TestLayers implements Layers {

@Override
public Iterator<String> iterator() {
return Arrays.asList("a", "b", "c").iterator();
return Arrays.asList("a", "b", "c", "d").iterator();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void createWhenIndexFileIsMalformedThrowsException() throws Exception {
@Test
void iteratorReturnsLayers() throws Exception {
IndexedLayers layers = new IndexedLayers(getIndex());
assertThat(layers).containsExactly("test", "application");
assertThat(layers).containsExactly("test", "empty", "application");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
- "test":
- "BOOT-INF/lib/a.jar"
- "BOOT-INF/lib/b.jar"
- "empty":
- "application":
- "BOOT-INF/classes/Demo.class"
- "META-INF/"
Expand Down

0 comments on commit de1e3c6

Please sign in to comment.