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 d1644262 Let JUnit manage temporary files
d1644262 is described below

commit d1644262b5389c498a69d649840a5bd9c7d8b235
Author: Gary Gregory <[email protected]>
AuthorDate: Sat Nov 4 08:09:04 2023 -0400

    Let JUnit manage temporary files
---
 .../commons/compress/AbstractTempDirTest.java      | 52 ++++++++++++++++++++++
 .../org/apache/commons/compress/AbstractTest.java  | 21 +--------
 .../harmony/pack200/tests/PackingOptionsTest.java  | 47 +++++++------------
 3 files changed, 69 insertions(+), 51 deletions(-)

diff --git a/src/test/java/org/apache/commons/compress/AbstractTempDirTest.java 
b/src/test/java/org/apache/commons/compress/AbstractTempDirTest.java
new file mode 100644
index 00000000..3f7e798a
--- /dev/null
+++ b/src/test/java/org/apache/commons/compress/AbstractTempDirTest.java
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.commons.compress;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Path;
+
+import org.junit.jupiter.api.io.TempDir;
+
+/**
+ * Abstracts the use of a JUnit {@link TempDir} for all subclasses.
+ */
+public class AbstractTempDirTest {
+
+    @TempDir
+    protected File tempDirFile;
+
+    protected File createTempFile() throws IOException {
+        return File.createTempFile("testfile", "", getTempDirFile());
+    }
+
+    protected File createTempFile(final String prefix, final String suffix) 
throws IOException {
+        return File.createTempFile(prefix, suffix, getTempDirFile());
+    }
+
+    protected File getTempDirFile() {
+        return tempDirFile;
+    }
+
+    protected Path getTempDirPath() {
+        return tempDirFile.toPath();
+    }
+
+}
diff --git a/src/test/java/org/apache/commons/compress/AbstractTest.java 
b/src/test/java/org/apache/commons/compress/AbstractTest.java
index ad0e4b22..8d1093d4 100644
--- a/src/test/java/org/apache/commons/compress/AbstractTest.java
+++ b/src/test/java/org/apache/commons/compress/AbstractTest.java
@@ -47,7 +47,7 @@ import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.io.TempDir;
 
-public abstract class AbstractTest {
+public abstract class AbstractTest extends AbstractTempDirTest {
 
     protected interface StreamWrapper<I extends InputStream> {
         I wrap(InputStream inputStream) throws Exception;
@@ -102,9 +102,6 @@ public abstract class AbstractTest {
         return Files.newInputStream(getPath(path));
     }
 
-    @TempDir
-    protected File tempDirFile;
-
     @TempDir
     protected File tempResultDir;
 
@@ -321,14 +318,6 @@ public abstract class AbstractTest {
         return Files.createTempDirectory(getTempDirPath(), prefix).toFile();
     }
 
-    protected File createTempFile() throws IOException {
-        return File.createTempFile("testfile", "", getTempDirFile());
-    }
-
-    protected File createTempFile(final String prefix, final String suffix) 
throws IOException {
-        return File.createTempFile(prefix, suffix, getTempDirFile());
-    }
-
     /**
      * Override this method to change what is to be compared in the List.
      * For example, size + name instead of just name.
@@ -340,14 +329,6 @@ public abstract class AbstractTest {
         return entry.getName();
     }
 
-    protected File getTempDirFile() {
-        return tempDirFile;
-    }
-
-    protected Path getTempDirPath() {
-        return tempDirFile.toPath();
-    }
-
     protected void setLongFileMode(final ArchiveOutputStream<?> outputStream) {
         if (outputStream instanceof ArArchiveOutputStream) {
             ((ArArchiveOutputStream) 
outputStream).setLongFileMode(ArArchiveOutputStream.LONGFILE_BSD);
diff --git 
a/src/test/java/org/apache/commons/compress/harmony/pack200/tests/PackingOptionsTest.java
 
b/src/test/java/org/apache/commons/compress/harmony/pack200/tests/PackingOptionsTest.java
index 608de3e5..1609cd8a 100644
--- 
a/src/test/java/org/apache/commons/compress/harmony/pack200/tests/PackingOptionsTest.java
+++ 
b/src/test/java/org/apache/commons/compress/harmony/pack200/tests/PackingOptionsTest.java
@@ -38,6 +38,7 @@ import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 import java.util.jar.JarOutputStream;
 
+import org.apache.commons.compress.AbstractTempDirTest;
 import org.apache.commons.compress.harmony.pack200.Archive;
 import org.apache.commons.compress.harmony.pack200.Pack200Exception;
 import org.apache.commons.compress.harmony.pack200.PackingOptions;
@@ -46,9 +47,7 @@ import org.junit.jupiter.api.Test;
 /**
  * Test different options for packing a Jar file
  */
-public class PackingOptionsTest {
-
-    private File file;
+public class PackingOptionsTest extends AbstractTempDirTest {
 
     private void compareFiles(final JarFile jarFile, final JarFile jarFile2) 
throws IOException {
         final Enumeration<JarEntry> entries = jarFile.entries();
@@ -110,8 +109,7 @@ public class PackingOptionsTest {
     @Test
     public void testPackEffort0() throws Pack200Exception, IOException, 
URISyntaxException {
         final File f1 = new 
File(Archive.class.getResource("/pack200/jndi.jar").toURI());
-        file = File.createTempFile("jndiE0", ".pack");
-        file.deleteOnExit();
+        File file = createTempFile("jndiE0", ".pack");
         try (JarFile in = new JarFile(f1);
                 FileOutputStream out = new FileOutputStream(file)) {
             final PackingOptions options = new PackingOptions();
@@ -127,8 +125,7 @@ public class PackingOptionsTest {
 
     @Test
     public void testErrorAttributes() throws Exception {
-        file = File.createTempFile("unknown", ".pack");
-        file.deleteOnExit();
+        File file = createTempFile("unknown", ".pack");
         try (JarFile in = new JarFile(new 
File(Archive.class.getResource("/pack200/jndiWithUnknownAttributes.jar").toURI()));
                 FileOutputStream out = new FileOutputStream(file)) {
             final PackingOptions options = new PackingOptions();
@@ -152,8 +149,7 @@ public class PackingOptionsTest {
         assertFalse(options.isKeepFileOrder());
 
         // Test option works correctly. Test 'True'.
-        file = File.createTempFile("sql", ".pack");
-        file.deleteOnExit();
+        File file = createTempFile("sql", ".pack");
         try (JarFile jarFile = new JarFile(new 
File(Archive.class.getResource("/pack200/sqlUnpacked.jar").toURI()));
                 FileOutputStream outputStream = new FileOutputStream(file)) {
             options = new PackingOptions();
@@ -236,8 +232,7 @@ public class PackingOptionsTest {
         options.setLogFile(logFile.getPath());
         assertEquals(logFile.getPath(), options.getLogFile());
 
-        file = File.createTempFile("helloworld", ".pack.gz");
-        file.deleteOnExit();
+        File file = createTempFile("helloworld", ".pack.gz");
         try (JarFile in = new JarFile(new 
File(Archive.class.getResource("/pack200/hw.jar").toURI()));
                 FileOutputStream out = new FileOutputStream(file)) {
             new Archive(in, out, options).pack();
@@ -275,8 +270,7 @@ public class PackingOptionsTest {
         }, "Should throw IllegalArgumentException for incorrect mod time");
 
         // Test option works correctly. Test 'keep'.
-        file = File.createTempFile("sql", ".pack");
-        file.deleteOnExit();
+        File file = createTempFile("sql", ".pack");
         try (JarFile in = new JarFile(new 
File(Archive.class.getResource("/pack200/sqlUnpacked.jar").toURI()));
                 FileOutputStream out = new FileOutputStream(file)) {
             options = new PackingOptions();
@@ -350,10 +344,9 @@ public class PackingOptionsTest {
 
     @Test
     public void testNewAttributes() throws Exception {
-        file = File.createTempFile("unknown", ".pack");
+        File file = createTempFile("unknown", ".pack");
         try (FileOutputStream out = new FileOutputStream(file);
                 JarFile in = new JarFile(new 
File(Archive.class.getResource("/pack200/jndiWithUnknownAttributes.jar").toURI())))
 {
-            file.deleteOnExit();
             final PackingOptions options = new PackingOptions();
             options.addClassAttributeAction("Pack200", "I");
             new Archive(in, out, options).pack();
@@ -382,8 +375,7 @@ public class PackingOptionsTest {
 
     @Test
     public void testNewAttributes2() throws Exception {
-        file = File.createTempFile("unknown", ".pack");
-        file.deleteOnExit();
+        File file = createTempFile("unknown", ".pack");
         try (FileOutputStream out = new FileOutputStream(file);
                 JarFile in = new JarFile(new 
File(Archive.class.getResource("/pack200/p200WithUnknownAttributes.jar").toURI())))
 {
             final PackingOptions options = new PackingOptions();
@@ -409,8 +401,7 @@ public class PackingOptionsTest {
 
     @Test
     public void testPassAttributes() throws Exception {
-        file = File.createTempFile("unknown", ".pack");
-        file.deleteOnExit();
+        File file = createTempFile("unknown", ".pack");
         try (FileOutputStream out = new FileOutputStream(file);
                 JarFile in = new JarFile(new 
File(Archive.class.getResource("/pack200/jndiWithUnknownAttributes.jar").toURI())))
 {
             final PackingOptions options = new PackingOptions();
@@ -446,8 +437,7 @@ public class PackingOptionsTest {
         }
 
         // Pass one file
-        file = File.createTempFile("sql", ".pack");
-        file.deleteOnExit();
+        File file = createTempFile("sql", ".pack");
         try (JarFile in = new JarFile(new 
File(Archive.class.getResource("/pack200/sqlUnpacked.jar").toURI()));
                 FileOutputStream out = new FileOutputStream(file)) {
             final PackingOptions options = new PackingOptions();
@@ -497,8 +487,7 @@ public class PackingOptionsTest {
 
     @Test
     public void testSegmentLimits() throws IOException, Pack200Exception, 
URISyntaxException {
-        file = File.createTempFile("helloworld", ".pack.gz");
-        file.deleteOnExit();
+        File file = createTempFile("helloworld", ".pack.gz");
         try (JarFile in = new JarFile(new 
File(Archive.class.getResource("/pack200/hw.jar").toURI()));
                 FileOutputStream out = new FileOutputStream(file)) {
             final PackingOptions options = new PackingOptions();
@@ -507,8 +496,7 @@ public class PackingOptionsTest {
             archive.pack();
         }
 
-        file = File.createTempFile("helloworld", ".pack.gz");
-        file.deleteOnExit();
+        file = createTempFile("helloworld", ".pack.gz");
         try (JarFile in = new JarFile(new 
File(Archive.class.getResource("/pack200/hw.jar").toURI()));
                 FileOutputStream out = new FileOutputStream(file)) {
             final PackingOptions options = new PackingOptions();
@@ -516,8 +504,7 @@ public class PackingOptionsTest {
             new Archive(in, out, options).pack();
         }
 
-        file = File.createTempFile("helloworld", ".pack.gz");
-        file.deleteOnExit();
+        file = createTempFile("helloworld", ".pack.gz");
         try (JarFile in = new JarFile(new 
File(Archive.class.getResource("/pack200/hw.jar").toURI()));
                 FileOutputStream out = new FileOutputStream(file)) {
             final PackingOptions options = new PackingOptions();
@@ -544,8 +531,7 @@ public class PackingOptionsTest {
 
     @Test
     public void testStripDebug() throws IOException, Pack200Exception, 
URISyntaxException {
-        file = File.createTempFile("sql", ".pack");
-        file.deleteOnExit();
+        File file = createTempFile("sql", ".pack");
         try (JarFile in = new JarFile(new 
File(Archive.class.getResource("/pack200/sqlUnpacked.jar").toURI()));
                 FileOutputStream out = new FileOutputStream(file)) {
             final PackingOptions options = new PackingOptions();
@@ -556,8 +542,7 @@ public class PackingOptionsTest {
         }
 
         // now unpack
-        final File file2 = File.createTempFile("sqloutNoDebug", ".jar");
-        file2.deleteOnExit();
+        final File file2 = createTempFile("sqloutNoDebug", ".jar");
         unpackJar(file, file2);
 
         final File compareFile = new 
File(Archive.class.getResource("/pack200/sqlUnpackedNoDebug.jar").toURI());

Reply via email to