Skip to content

Commit

Permalink
Use try-with-resources
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Oct 29, 2023
1 parent 7564209 commit 2419480
Showing 1 changed file with 31 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,7 @@ public void testAlternativeConstructor() throws IOException, URISyntaxException,
@Test
public void testAnnotations() throws IOException, Pack200Exception,
URISyntaxException {
in = new JarFile(new File(Archive.class.getResource(
"/pack200/annotationsUnpacked.jar")
.toURI()));
in = new JarFile(new File(Archive.class.getResource("/pack200/annotationsUnpacked.jar").toURI()));
file = File.createTempFile("annotations", ".pack");
file.deleteOnExit();
out = new FileOutputStream(file);
Expand All @@ -147,14 +145,12 @@ public void testAnnotations() throws IOException, Pack200Exception,
final File file2 = File.createTempFile("annotationsout", ".jar");
file2.deleteOnExit();
final JarOutputStream out2 = new JarOutputStream(new FileOutputStream(file2));
final org.apache.commons.compress.harmony.unpack200.Archive archive = new org.apache.commons.compress.harmony.unpack200.Archive(
in2, out2);
final org.apache.commons.compress.harmony.unpack200.Archive archive = new org.apache.commons.compress.harmony.unpack200.Archive(in2, out2);
archive.unpack();
final JarFile jarFile = new JarFile(file2);
final JarFile jarFile2 = new JarFile(new File(Archive.class.getResource(
"/pack200/annotationsUnpacked.jar").toURI()));

compareFiles(jarFile, jarFile2);
try (JarFile jarFile = new JarFile(file2);
JarFile jarFile2 = new JarFile(new File(Archive.class.getResource("/pack200/annotationsUnpacked.jar").toURI()))) {
compareFiles(jarFile, jarFile2);
}
}

@Test
Expand Down Expand Up @@ -237,8 +233,7 @@ public void testHelloWorld() throws IOException, Pack200Exception, URISyntaxExce

@Test
public void testJNDI() throws IOException, Pack200Exception, URISyntaxException {
in = new JarFile(new File(Archive.class.getResource(
"/pack200/jndi.jar").toURI()));
in = new JarFile(new File(Archive.class.getResource("/pack200/jndi.jar").toURI()));
file = File.createTempFile("jndi", ".pack");
file.deleteOnExit();
out = new FileOutputStream(file);
Expand All @@ -255,19 +250,16 @@ public void testJNDI() throws IOException, Pack200Exception, URISyntaxException
final JarOutputStream out2 = new JarOutputStream(new FileOutputStream(file2));
final org.apache.commons.compress.harmony.unpack200.Archive archive = new org.apache.commons.compress.harmony.unpack200.Archive(in2, out2);
archive.unpack();
final JarFile jarFile = new JarFile(file2);
final JarFile jarFile2 = new JarFile(new File(Archive.class.getResource(
"/pack200/jndiUnpacked.jar").toURI()));

compareFiles(jarFile, jarFile2);
try (JarFile jarFile = new JarFile(file2);
JarFile jarFile2 = new JarFile(new File(Archive.class.getResource("/pack200/jndiUnpacked.jar").toURI()))) {
compareFiles(jarFile, jarFile2);
}
}

@Test
public void testLargeClass() throws IOException, Pack200Exception,
URISyntaxException {
in = new JarFile(new File(Archive.class.getResource(
"/pack200/largeClassUnpacked.jar")
.toURI()));
in = new JarFile(new File(Archive.class.getResource("/pack200/largeClassUnpacked.jar").toURI()));
file = File.createTempFile("largeClass", ".pack");
file.deleteOnExit();
out = new FileOutputStream(file);
Expand All @@ -284,15 +276,12 @@ public void testLargeClass() throws IOException, Pack200Exception,
final JarOutputStream out2 = new JarOutputStream(new FileOutputStream(file2));
final org.apache.commons.compress.harmony.unpack200.Archive archive = new org.apache.commons.compress.harmony.unpack200.Archive(in2, out2);
archive.unpack();
final JarFile jarFile = new JarFile(file2);

final File compareFile = new File(Archive.class.getResource(
"/pack200/largeClassUnpacked.jar").toURI());
final JarFile jarFile2 = new JarFile(compareFile);

assertEquals(jarFile2.size(), jarFile.size());

compareFiles(jarFile, jarFile2);
final File compareFile = new File(Archive.class.getResource("/pack200/largeClassUnpacked.jar").toURI());
try (JarFile jarFile = new JarFile(file2);
JarFile jarFile2 = new JarFile(compareFile)) {
assertEquals(jarFile2.size(), jarFile.size());
compareFiles(jarFile, jarFile2);
}
}

@ParameterizedTest
Expand All @@ -314,8 +303,7 @@ public void testMultipleJars(final Path path) throws IOException, Pack200Excepti

@Test
public void testSQL() throws IOException, Pack200Exception, URISyntaxException {
in = new JarFile(new File(Archive.class.getResource(
"/pack200/sqlUnpacked.jar").toURI()));
in = new JarFile(new File(Archive.class.getResource("/pack200/sqlUnpacked.jar").toURI()));
file = File.createTempFile("sql", ".pack");
file.deleteOnExit();
out = new FileOutputStream(file);
Expand All @@ -333,34 +321,27 @@ public void testSQL() throws IOException, Pack200Exception, URISyntaxException {
final JarOutputStream out2 = new JarOutputStream(new FileOutputStream(file2));
final org.apache.commons.compress.harmony.unpack200.Archive archive = new org.apache.commons.compress.harmony.unpack200.Archive(in2, out2);
archive.unpack();
final JarFile jarFile = new JarFile(file2);

final File compareFile = new File(Archive.class.getResource(
"/pack200/sqlUnpacked.jar").toURI());
final JarFile jarFile2 = new JarFile(compareFile);

assertEquals(jarFile2.size(), jarFile.size());

compareFiles(jarFile, jarFile2);
final File compareFile = new File(Archive.class.getResource("/pack200/sqlUnpacked.jar").toURI());
try (JarFile jarFile = new JarFile(file2);
JarFile jarFile2 = new JarFile(compareFile)) {
assertEquals(jarFile2.size(), jarFile.size());
compareFiles(jarFile, jarFile2);
}
}

// Test with an archive containing Annotations
@Test
public void testWithAnnotations2() throws Exception {
final InputStream i = Archive.class
.getResourceAsStream("/pack200/annotationsRI.pack.gz");
final InputStream i = Archive.class.getResourceAsStream("/pack200/annotationsRI.pack.gz");
file = File.createTempFile("annotations", ".jar");
file.deleteOnExit();
final JarOutputStream jout = new JarOutputStream(new FileOutputStream(file));
final org.apache.commons.compress.harmony.unpack200.Archive archive = new org.apache.commons.compress.harmony.unpack200.Archive(
i, jout);
final org.apache.commons.compress.harmony.unpack200.Archive archive = new org.apache.commons.compress.harmony.unpack200.Archive(i, jout);
archive.unpack();
final JarFile jarFile = new JarFile(file);
final JarFile jarFile2 = new JarFile(new File(Archive.class.getResource(
"/pack200/annotationsRI.jar")
.toURI()));

compareFiles(jarFile, jarFile2);
try (JarFile jarFile = new JarFile(file);
JarFile jarFile2 = new JarFile(new File(Archive.class.getResource("/pack200/annotationsRI.jar").toURI()))) {
compareFiles(jarFile, jarFile2);
}
}

}

0 comments on commit 2419480

Please sign in to comment.