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
This commit is contained in:
Andy Wilkinson 2018-02-01 13:07:50 +00:00
parent cafe6dbbb1
commit 1d2b85b2b8
1 changed files with 10 additions and 1 deletions

View File

@ -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<ZipEntrySource> 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;
}