This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
The following commit(s) were added to refs/heads/master by this push:
new 8477d006 Use try-with-resources
8477d006 is described below
commit 8477d0069354c198a1fc9f4dd4645da6e827744b
Author: Gary Gregory <[email protected]>
AuthorDate: Sat Nov 4 07:24:59 2023 -0400
Use try-with-resources
---
.../harmony/unpack200/tests/ArchiveTest.java | 150 +++++++++------------
1 file changed, 61 insertions(+), 89 deletions(-)
diff --git
a/src/test/java/org/apache/commons/compress/harmony/unpack200/tests/ArchiveTest.java
b/src/test/java/org/apache/commons/compress/harmony/unpack200/tests/ArchiveTest.java
index 4808eb00..7089db71 100644
---
a/src/test/java/org/apache/commons/compress/harmony/unpack200/tests/ArchiveTest.java
+++
b/src/test/java/org/apache/commons/compress/harmony/unpack200/tests/ArchiveTest.java
@@ -42,8 +42,7 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
/**
- * Tests for org.apache.commons.compress.harmony.unpack200.Archive, which is
the main class for
- * unpack200.
+ * Tests for org.apache.commons.compress.harmony.unpack200.Archive, which is
the main class for unpack200.
*/
public class ArchiveTest {
@@ -72,8 +71,7 @@ public class ArchiveTest {
@Test
public void testAlternativeConstructor() throws Exception {
- final String inputFile = new File(Archive.class
- .getResource("/pack200/sql.pack.gz").toURI()).getPath();
+ final String inputFile = new
File(Archive.class.getResource("/pack200/sql.pack.gz").toURI()).getPath();
file = File.createTempFile("sql", ".jar");
file.deleteOnExit();
final String outputFile = file.getPath();
@@ -108,8 +106,7 @@ public class ArchiveTest {
@Test
public void testJustResourcesGZip() throws Exception {
- in = Archive.class
- .getResourceAsStream("/pack200/JustResources.pack.gz");
+ in =
Archive.class.getResourceAsStream("/pack200/JustResources.pack.gz");
file = File.createTempFile("Just", "ResourcesGz.jar");
out = new JarOutputStream(new FileOutputStream(file));
final Archive archive = new Archive(in, out);
@@ -120,8 +117,7 @@ public class ArchiveTest {
@Test
public void testLoggingOptions() throws Exception {
// test default option, which is quiet (no output at all)
- in = Archive.class
- .getResourceAsStream("/pack200/sql.pack.gz");
+ in = Archive.class.getResourceAsStream("/pack200/sql.pack.gz");
file = File.createTempFile("logtest", ".jar");
file.deleteOnExit();
out = new JarOutputStream(new FileOutputStream(file));
@@ -137,8 +133,7 @@ public class ArchiveTest {
}
// test verbose
- in = Archive.class
- .getResourceAsStream("/pack200/sql.pack.gz");
+ in = Archive.class.getResourceAsStream("/pack200/sql.pack.gz");
file = File.createTempFile("logtest", ".jar");
file.deleteOnExit();
out = new JarOutputStream(new FileOutputStream(file));
@@ -156,8 +151,7 @@ public class ArchiveTest {
// test append option
final long length = logFile.length();
- in = Archive.class
- .getResourceAsStream("/pack200/sql.pack.gz");
+ in = Archive.class.getResourceAsStream("/pack200/sql.pack.gz");
file = File.createTempFile("logtest", ".jar");
file.deleteOnExit();
out = new JarOutputStream(new FileOutputStream(file));
@@ -166,8 +160,7 @@ public class ArchiveTest {
archive.setVerbose(true);
archive.unpack();
assertTrue(logFile.length() > length);
- in = Archive.class
- .getResourceAsStream("/pack200/sql.pack.gz");
+ in = Archive.class.getResourceAsStream("/pack200/sql.pack.gz");
file = File.createTempFile("logtest", ".jar");
file.deleteOnExit();
out = new JarOutputStream(new FileOutputStream(file));
@@ -178,8 +171,7 @@ public class ArchiveTest {
assertEquals(logFile.length(), length);
// test setting quiet explicitly
- in = Archive.class
- .getResourceAsStream("/pack200/sql.pack.gz");
+ in = Archive.class.getResourceAsStream("/pack200/sql.pack.gz");
file = File.createTempFile("logtest", ".jar");
file.deleteOnExit();
out = new JarOutputStream(new FileOutputStream(file));
@@ -198,21 +190,17 @@ public class ArchiveTest {
@Test
public void testRemovePackFile() throws Exception {
- final File original = new File(Archive.class.getResource(
- "/pack200/sql.pack.gz").toURI());
+ final File original = new
File(Archive.class.getResource("/pack200/sql.pack.gz").toURI());
final File copy = File.createTempFile("sqlcopy", ".pack.gz");
- final BufferedInputStream inputStream = new BufferedInputStream(
- new FileInputStream(original));
- final BufferedOutputStream outputStream = new BufferedOutputStream(
- new FileOutputStream(copy));
- final byte[] bytes = new byte[256];
- int read = inputStream.read(bytes);
- while (read > 0) {
- outputStream.write(bytes, 0, read);
- read = inputStream.read(bytes);
+ try (BufferedInputStream inputStream = new BufferedInputStream(new
FileInputStream(original));
+ final BufferedOutputStream outputStream = new
BufferedOutputStream(new FileOutputStream(copy))) {
+ final byte[] bytes = new byte[256];
+ int read = inputStream.read(bytes);
+ while (read > 0) {
+ outputStream.write(bytes, 0, read);
+ read = inputStream.read(bytes);
+ }
}
- inputStream.close();
- outputStream.close();
final String inputFile = copy.getPath();
file = File.createTempFile("sqlout", ".jar");
file.deleteOnExit();
@@ -226,8 +214,7 @@ public class ArchiveTest {
// Test with an archive containing Annotations
@Test
public void testWithAnnotations() throws Exception {
- in = Archive.class
- .getResourceAsStream("/pack200/annotations.pack.gz");
+ in = Archive.class.getResourceAsStream("/pack200/annotations.pack.gz");
file = File.createTempFile("annotations", ".jar");
out = new JarOutputStream(new FileOutputStream(file));
final Archive archive = new Archive(in, out);
@@ -237,8 +224,7 @@ public class ArchiveTest {
// Test with an archive packed with the -E0 option
@Test
public void testWithE0() throws Exception {
- in = Archive.class
- .getResourceAsStream("/pack200/simple-E0.pack.gz");
+ in = Archive.class.getResourceAsStream("/pack200/simple-E0.pack.gz");
file = File.createTempFile("simple-e0", ".jar");
out = new JarOutputStream(new FileOutputStream(file));
final Archive archive = new Archive(in, out);
@@ -248,8 +234,7 @@ public class ArchiveTest {
// Test with an archive containing Harmony's JNDI module
@Test
public void testWithJNDIE1() throws Exception {
- in = Archive.class
- .getResourceAsStream("/pack200/jndi-e1.pack.gz");
+ in = Archive.class.getResourceAsStream("/pack200/jndi-e1.pack.gz");
file = File.createTempFile("jndi-e1", ".jar");
out = new JarOutputStream(new FileOutputStream(file));
final Archive archive = new Archive(in, out);
@@ -260,8 +245,7 @@ public class ArchiveTest {
// HARMONY-5470)
@Test
public void testWithLargeClass() throws Exception {
- in = Archive.class
- .getResourceAsStream("/pack200/LargeClass.pack.gz");
+ in = Archive.class.getResourceAsStream("/pack200/LargeClass.pack.gz");
file = File.createTempFile("largeClass", ".jar");
file.deleteOnExit();
out = new JarOutputStream(new FileOutputStream(file));
@@ -269,12 +253,10 @@ public class ArchiveTest {
archive.unpack();
}
-
// Test with an archive containing Harmony's Pack200 module
@Test
public void testWithPack200() throws Exception {
- in = Archive.class
- .getResourceAsStream("/pack200/pack200.pack.gz");
+ in = Archive.class.getResourceAsStream("/pack200/pack200.pack.gz");
file = File.createTempFile("p200", ".jar");
out = new JarOutputStream(new FileOutputStream(file));
final Archive archive = new Archive(in, out);
@@ -284,8 +266,7 @@ public class ArchiveTest {
// Test with an archive containing Harmony's Pack200 module, packed with
-E1
@Test
public void testWithPack200E1() throws Exception {
- in = Archive.class
- .getResourceAsStream("/pack200/pack200-e1.pack.gz");
+ in = Archive.class.getResourceAsStream("/pack200/pack200-e1.pack.gz");
file = File.createTempFile("p200-e1", ".jar");
out = new JarOutputStream(new FileOutputStream(file));
final Archive archive = new Archive(in, out);
@@ -295,65 +276,56 @@ public class ArchiveTest {
// Test with an archive containing Harmony's SQL module
@Test
public void testWithSql() throws Exception {
- in = Archive.class
- .getResourceAsStream("/pack200/sql.pack.gz");
+ in = Archive.class.getResourceAsStream("/pack200/sql.pack.gz");
file = File.createTempFile("sql", ".jar");
file.deleteOnExit();
out = new JarOutputStream(new FileOutputStream(file));
final Archive archive = new Archive(in, out);
archive.unpack();
- final JarFile jarFile = new JarFile(file);
-
- final File compareFile = new File(Archive.class.getResource(
- "/pack200/sqlUnpacked.jar").toURI());
-
- final JarFile jarFile2 = new JarFile(compareFile);
-
- final long differenceInJarSizes = Math.abs(compareFile.length()
- - file.length());
-
- assertTrue(differenceInJarSizes < 100, "Expected jar files to be a
similar size, difference was "
- + differenceInJarSizes + " bytes");
-
- final Enumeration<JarEntry> entries = jarFile.entries();
- final Enumeration<JarEntry> entries2 = jarFile2.entries();
- while(entries.hasMoreElements() && entries2.hasMoreElements()) {
-
- final JarEntry entry = entries.nextElement();
- assertNotNull(entry);
- final String name = entry.getName();
-
- final JarEntry entry2 = entries2.nextElement();
- assertNotNull(entry2);
- final String name2 = entry2.getName();
-
- assertEquals(name, name2);
-
- final InputStream ours = jarFile.getInputStream(entry);
- final InputStream expected = jarFile2.getInputStream(entry2);
-
- final BufferedReader reader1 = new BufferedReader(new
InputStreamReader(ours));
- final BufferedReader reader2 = new BufferedReader(new
InputStreamReader(
- expected));
- String line1 = reader1.readLine();
- String line2 = reader2.readLine();
- int i = 1;
- while (line1 != null || line2 != null) {
- assertEquals(line2, line1, "Unpacked class files differ for "
+ name);
- line1 = reader1.readLine();
- line2 = reader2.readLine();
- i++;
+ final File compareFile = new
File(Archive.class.getResource("/pack200/sqlUnpacked.jar").toURI());
+ try (JarFile jarFile = new JarFile(file);
+ JarFile jarFile2 = new JarFile(compareFile)) {
+
+ final long differenceInJarSizes = Math.abs(compareFile.length() -
file.length());
+ assertTrue(differenceInJarSizes < 100, "Expected jar files to be a
similar size, difference was " + differenceInJarSizes + " bytes");
+
+ final Enumeration<JarEntry> entries = jarFile.entries();
+ final Enumeration<JarEntry> entries2 = jarFile2.entries();
+ while (entries.hasMoreElements() && entries2.hasMoreElements()) {
+
+ final JarEntry entry = entries.nextElement();
+ assertNotNull(entry);
+ final String name = entry.getName();
+
+ final JarEntry entry2 = entries2.nextElement();
+ assertNotNull(entry2);
+ final String name2 = entry2.getName();
+
+ assertEquals(name, name2);
+
+ try (InputStream ours = jarFile.getInputStream(entry);
+ InputStream expected = jarFile2.getInputStream(entry2);
+ BufferedReader reader1 = new BufferedReader(new
InputStreamReader(ours));
+ BufferedReader reader2 = new BufferedReader(new
InputStreamReader(expected))) {
+ String line1 = reader1.readLine();
+ String line2 = reader2.readLine();
+ int i = 1;
+ while (line1 != null || line2 != null) {
+ assertEquals(line2, line1, "Unpacked class files
differ for " + name);
+ line1 = reader1.readLine();
+ line2 = reader2.readLine();
+ i++;
+ }
+ assertTrue(i > 0);
+ }
}
- reader1.close();
- reader2.close();
}
}
// Test with an archive containing Harmony's SQL module, packed with -E1
@Test
public void testWithSqlE1() throws Exception {
- in = Archive.class
- .getResourceAsStream("/pack200/sql-e1.pack.gz");
+ in = Archive.class.getResourceAsStream("/pack200/sql-e1.pack.gz");
file = File.createTempFile("sql-e1", ".jar");
out = new JarOutputStream(new FileOutputStream(file));
final Archive archive = new Archive(in, out);