Skip to content

Commit

Permalink
Ensure that TestJarFile uses insertion order for jar's entries
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wilkinsona committed Feb 1, 2018
1 parent cafe6db commit 1d2b85b
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@
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;

/**
Expand All @@ -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();
Expand All @@ -59,6 +65,7 @@ public void addClass(String filename, Class<?> classToCopy, Long time)
if (time != null) {
file.setLastModified(time);
}
this.entries.add(new FileSource(filename, file));
}

public void addFile(String filename, File fileToCopy) throws IOException {
Expand All @@ -67,6 +74,7 @@ public void addFile(String filename, File fileToCopy) throws IOException {
try (InputStream inputStream = new FileInputStream(fileToCopy)) {
copyToFile(inputStream, file);
}
this.entries.add(new FileSource(filename, file));
}

public void addManifest(Manifest manifest) throws IOException {
Expand All @@ -75,6 +83,7 @@ public void addManifest(Manifest manifest) throws IOException {
try (OutputStream outputStream = new FileOutputStream(manifestFile)) {
manifest.write(outputStream);
}
this.entries.add(new FileSource("META-INF/MANIFEST.MF", manifestFile));
}

private File getFilePath(String filename) {
Expand Down Expand Up @@ -114,7 +123,7 @@ public File getFile() throws IOException {
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;
}

Expand Down

0 comments on commit 1d2b85b

Please sign in to comment.