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 507dfec1 Use try-with-resources
507dfec1 is described below
commit 507dfec1c5ad01a883316d9e375081b417dd3475
Author: Gary Gregory <[email protected]>
AuthorDate: Sat Nov 4 08:20:23 2023 -0400
Use try-with-resources
---
.../harmony/unpack200/tests/SegmentTest.java | 49 ++++++----------------
1 file changed, 12 insertions(+), 37 deletions(-)
diff --git
a/src/test/java/org/apache/commons/compress/harmony/unpack200/tests/SegmentTest.java
b/src/test/java/org/apache/commons/compress/harmony/unpack200/tests/SegmentTest.java
index 3cb4c85e..191407fb 100644
---
a/src/test/java/org/apache/commons/compress/harmony/unpack200/tests/SegmentTest.java
+++
b/src/test/java/org/apache/commons/compress/harmony/unpack200/tests/SegmentTest.java
@@ -22,7 +22,6 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
-import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.jar.JarEntry;
@@ -31,7 +30,6 @@ import java.util.jar.JarOutputStream;
import org.apache.commons.compress.AbstractTempDirTest;
import org.apache.commons.compress.harmony.unpack200.Segment;
-import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
/**
@@ -39,36 +37,13 @@ import org.junit.jupiter.api.Test;
*/
public class SegmentTest extends AbstractTempDirTest {
- InputStream in;
- JarOutputStream out;
-
- @AfterEach
- public void tearDown() throws Exception {
- if (in != null) {
- try {
- in.close();
- } catch (final IOException e) {
- e.printStackTrace();
- }
- }
- try {
- if (out != null) {
- out.close();
- }
- } catch (final IOException e) {
- e.printStackTrace();
- }
- }
-
@Test
public void testHelloWorld() throws Exception {
- in = Segment.class.getResourceAsStream("/pack200/HelloWorld.pack");
final File file = createTempFile("hello", "world.jar");
- out = new JarOutputStream(new FileOutputStream(file));
- final Segment segment = new Segment();
- segment.unpack(in, out);
- out.close();
- out = null;
+ try (InputStream in =
Segment.class.getResourceAsStream("/pack200/HelloWorld.pack");
+ JarOutputStream out = new JarOutputStream(new
FileOutputStream(file))) {
+ new Segment().unpack(in, out);
+ }
try (JarFile jarFile = new JarFile(file)) {
final JarEntry entry =
jarFile.getJarEntry("org/apache/harmony/archive/tests/internal/pack200/HelloWorld.class");
assertNotNull(entry);
@@ -97,20 +72,20 @@ public class SegmentTest extends AbstractTempDirTest {
@Test
public void testInterfaceOnly() throws Exception {
- in = Segment.class.getResourceAsStream("/pack200/InterfaceOnly.pack");
final File file = createTempFile("Interface", "Only.jar");
- out = new JarOutputStream(new FileOutputStream(file));
- final Segment segment = new Segment();
- segment.unpack(in, out);
+ try (InputStream in =
Segment.class.getResourceAsStream("/pack200/InterfaceOnly.pack");
+ JarOutputStream out = new JarOutputStream(new
FileOutputStream(file))) {
+ new Segment().unpack(in, out);
+ }
}
@Test
public void testJustResources() throws Exception {
- in = Segment.class.getResourceAsStream("/pack200/JustResources.pack");
final File file = createTempFile("just", "resources.jar");
- out = new JarOutputStream(new FileOutputStream(file));
- final Segment segment = new Segment();
- segment.unpack(in, out);
+ try (InputStream in =
Segment.class.getResourceAsStream("/pack200/JustResources.pack");
+ JarOutputStream out = new JarOutputStream(new
FileOutputStream(file))) {
+ new Segment().unpack(in, out);
+ }
}
}