]> source.dussan.org Git - aspectj.git/commitdiff
Improve 2 tests do delete temporary files
authorAlexander Kriegisch <Alexander@Kriegisch.name>
Fri, 9 Apr 2021 07:05:00 +0000 (14:05 +0700)
committerAlexander Kriegisch <Alexander@Kriegisch.name>
Fri, 9 Apr 2021 07:05:00 +0000 (14:05 +0700)
There were some problems in file handling: One file in was not deleted
in case an exception was thrown during the test. Another case was a
JarFile which was not closed before deletion, which might work on Linux,
but not on Windows where the open file is still locked after usage.

Signed-off-by: Alexander Kriegisch <Alexander@Kriegisch.name>
taskdefs/src/test/java/org/aspectj/tools/ant/taskdefs/AjcTaskTest.java
tests/bugs/serialVersionUID/Util.java

index 8e9008fba115ae1b96bfb7e926783a127f97e9e2..e6ae8876bd1fed7e23d6610bac74a4ece42fbc2f 100644 (file)
@@ -394,18 +394,19 @@ public class AjcTaskTest extends TestCase {
 
         checkRun(task, null);
 
-        JarFile jarFile = new JarFile(destJar);
-        String[] expected = {"copyMe.htm", "pack/includeme",
-                "pack/Pack.class", "Default.class"};
-        String[] unexpected = {"doNotCopy", "skipTxtFiles.txt", "pack/something.txt"};
-               for (String value : expected) {
-                       JarEntry entry = jarFile.getJarEntry(value);
-                       assertTrue(value + " not found", null != entry);
-               }
-               for (String s : unexpected) {
-                       JarEntry entry = jarFile.getJarEntry(s);
-                       assertTrue(s + " found", null == entry);
-               }
+        try (JarFile jarFile = new JarFile(destJar)) {
+            String[] expected = { "copyMe.htm", "pack/includeme",
+                    "pack/Pack.class", "Default.class" };
+            String[] unexpected = { "doNotCopy", "skipTxtFiles.txt", "pack/something.txt" };
+            for (String value : expected) {
+                    JarEntry entry = jarFile.getJarEntry(value);
+                    assertTrue(value + " not found", null != entry);
+            }
+            for (String s : unexpected) {
+                    JarEntry entry = jarFile.getJarEntry(s);
+                    assertTrue(s + " found", null == entry);
+            }
+        }
     }
 
     public void testInpathDirCopyFilterError() {
index 6d4dce2792c2b9e1ee5f2bb0ec3dc9708e0029db..d931ab13fa8dfa8c9ab4f1f3857bf4718c7799e4 100644 (file)
@@ -30,7 +30,7 @@ public class Util {
                Object obj;
                File file = new File(name);
                file.deleteOnExit();
-               ObjectInputStream in = null; 
+               ObjectInputStream in = null;
 
                try {
                        in = new ObjectInputStream(new FileInputStream(file));
@@ -38,14 +38,15 @@ public class Util {
                        System.out.println("? Util.read() obj=" + obj);
                }
                finally {
-                       in.close();
+                       if (in != null)
+                               in.close();
                }
-               
+
                return obj;
        }
 
        public static void write (String name, Object obj) throws IOException {
-               
+
                File file = new File(name);
 //             File file = File.createTempFile(name,null);
                ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
@@ -54,18 +55,22 @@ public class Util {
 
                System.out.println("? Util.write() suid=" + ObjectStreamClass.lookup(obj.getClass()));
        }
-       
+
        public static void main (String[] args) throws Exception {
                String command = (args.length > 0)? args[0] : DEFAULT_COMMAND;
                String name = (args.length > 1)? args[1] : DEFAULT_NAME;
 
                if (command.equals("-read")) {
-                       Object obj = read(name);
-                       new File(name).delete();
+                       try {
+                               read(name);
+                       }
+                       finally {
+                               new File(name).delete();
+                       }
                }
                else if (command.equals("-fail")) {
                        fail(name);
-               }                       
+               }
 //             if (args.length > 0) {
 //             }
 //             else {