From 1d2b85b2b81b120189deeb2e23acce17ab1d9d35 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 1 Feb 2018 13:07:50 +0000 Subject: [PATCH] Ensure that TestJarFile uses insertion order for jar's entries Previously, the order of the entries in a TestJarFile was determined by the underlying file system rather than by the order in which they were added. This could lead to unpredicatable ordering and failures in tests that verify archive entry ordering. This commit updates TestJarFile to add entries to the archive in insertion order. See gh-11695 See gh-11696 --- .../boot/loader/tools/TestJarFile.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/TestJarFile.java b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/TestJarFile.java index 0b4acbe9ab4..cca814dedb7 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/TestJarFile.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/TestJarFile.java @@ -22,10 +22,14 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; import java.util.jar.JarFile; import java.util.jar.Manifest; import org.junit.rules.TemporaryFolder; +import org.zeroturnaround.zip.FileSource; +import org.zeroturnaround.zip.ZipEntrySource; import org.zeroturnaround.zip.ZipUtil; /** @@ -40,6 +44,8 @@ public class TestJarFile { private final File jarSource; + private final List entries = new ArrayList<>(); + public TestJarFile(TemporaryFolder temporaryFolder) throws IOException { this.temporaryFolder = temporaryFolder; this.jarSource = temporaryFolder.newFolder(); @@ -59,6 +65,7 @@ public class TestJarFile { if (time != null) { file.setLastModified(time); } + this.entries.add(new FileSource(filename, file)); } public void addFile(String filename, File fileToCopy) throws IOException { @@ -67,6 +74,7 @@ public class TestJarFile { try (InputStream inputStream = new FileInputStream(fileToCopy)) { copyToFile(inputStream, file); } + this.entries.add(new FileSource(filename, file)); } public void addManifest(Manifest manifest) throws IOException { @@ -75,6 +83,7 @@ public class TestJarFile { try (OutputStream outputStream = new FileOutputStream(manifestFile)) { manifest.write(outputStream); } + this.entries.add(new FileSource("META-INF/MANIFEST.MF", manifestFile)); } private File getFilePath(String filename) { @@ -114,7 +123,7 @@ public class TestJarFile { public File getFile(String extension) throws IOException { File file = this.temporaryFolder.newFile(); file = new File(file.getParent(), file.getName() + "." + extension); - ZipUtil.pack(this.jarSource, file); + ZipUtil.pack(this.entries.toArray(new ZipEntrySource[this.entries.size()]), file); return file; }