changeset 1668:d3648557391b

8010511: Tests are creating files in /tmp Reviewed-by: darcy
author jjg
date Thu, 28 Mar 2013 10:58:45 -0700
parents 991f11e13598
children 29c6984a1673
files test/tools/javac/T6558476.java test/tools/javac/T6900149.java test/tools/javac/diags/CheckExamples.java test/tools/javac/diags/RunExamples.java
diffstat 4 files changed, 82 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/test/tools/javac/T6558476.java	Thu Mar 28 10:49:39 2013 -0700
+++ b/test/tools/javac/T6558476.java	Thu Mar 28 10:58:45 2013 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -23,6 +23,8 @@
 
 /*
  * @test
+ * @bug 6558476
+ * @summary com/sun/tools/javac/Main.compile don't release file handles on return
  * @run main/othervm -Xmx512m -Xms512m  T6558476
  */
 
@@ -70,8 +72,7 @@
 
     public static void main(String[] args) throws IOException {
         File javaHomeDir = new File(System.getProperty("java.home"));
-        File tmpDir = new File(System.getProperty("java.io.tmpdir"));
-        File outputDir = new File(tmpDir, "outputDir" + new Random().nextInt(65536));
+        File outputDir = new File("outputDir" + new Random().nextInt(65536));
         outputDir.mkdir();
         outputDir.deleteOnExit();
 
--- a/test/tools/javac/T6900149.java	Thu Mar 28 10:49:39 2013 -0700
+++ b/test/tools/javac/T6900149.java	Thu Mar 28 10:58:45 2013 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -39,7 +39,7 @@
         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
         StandardJavaFileManager fm =
                 compiler.getStandardFileManager(null, null, null);
-        File emptyFile = File.createTempFile("Empty", ".java");
+        File emptyFile = createTempFile("Empty.java");
         File[] files = new File[] { emptyFile, emptyFile };
         CompilationTask task = compiler.getTask(null, fm, diag,
                 null, null, fm.getJavaFileObjects(files));
@@ -47,4 +47,10 @@
             throw new AssertionError("compilation failed");
         }
     }
+
+    private static File createTempFile(String path) throws IOException {
+        File f = new File(path);
+        try (FileWriter out = new FileWriter(f)) { }
+        return f;
+    }
 }
--- a/test/tools/javac/diags/CheckExamples.java	Thu Mar 28 10:49:39 2013 -0700
+++ b/test/tools/javac/diags/CheckExamples.java	Thu Mar 28 10:58:45 2013 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -34,6 +34,8 @@
  */
 
 import java.io.*;
+import java.nio.file.*;
+import java.nio.file.attribute.BasicFileAttributes;
 import java.util.*;
 
 /**
@@ -53,7 +55,27 @@
      * Standard entry point.
      */
     public static void main(String... args) throws Exception {
-        new CheckExamples().run();
+        boolean jtreg = (System.getProperty("test.src") != null);
+        Path tmpDir;
+        boolean deleteOnExit;
+        if (jtreg) {
+            // use standard jtreg scratch directory: the current directory
+            tmpDir = Paths.get(System.getProperty("user.dir"));
+            deleteOnExit = false;
+        } else {
+            tmpDir = Files.createTempDirectory(Paths.get(System.getProperty("java.io.tmpdir")),
+                    CheckExamples.class.getName());
+            deleteOnExit = true;
+        }
+        Example.setTempDir(tmpDir.toFile());
+
+        try {
+            new CheckExamples().run();
+        } finally {
+            if (deleteOnExit) {
+                clean(tmpDir);
+            }
+        }
     }
 
     /**
@@ -190,6 +212,25 @@
 
     int errors;
 
+    /**
+     * Clean the contents of a directory.
+     */
+    static void clean(Path dir) throws IOException {
+        Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
+            @Override
+            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
+                Files.delete(file);
+                return super.visitFile(file, attrs);
+            }
+
+            @Override
+            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
+                if (exc == null) Files.delete(dir);
+                return super.postVisitDirectory(dir, exc);
+            }
+        });
+    }
+
     static class Counts {
         static String[] prefixes = {
             "compiler.err.",
--- a/test/tools/javac/diags/RunExamples.java	Thu Mar 28 10:49:39 2013 -0700
+++ b/test/tools/javac/diags/RunExamples.java	Thu Mar 28 10:58:45 2013 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -33,7 +33,8 @@
  */
 
 import java.io.*;
-import java.text.SimpleDateFormat;
+import java.nio.file.*;
+import java.nio.file.attribute.BasicFileAttributes;
 import java.util.*;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -56,16 +57,18 @@
 public class RunExamples {
     public static void main(String... args) throws Exception {
         jtreg = (System.getProperty("test.src") != null);
-        File tmpDir;
+        Path tmpDir;
+        boolean deleteOnExit;
         if (jtreg) {
             // use standard jtreg scratch directory: the current directory
-            tmpDir = new File(System.getProperty("user.dir"));
+            tmpDir = Paths.get(System.getProperty("user.dir"));
+            deleteOnExit = false;
         } else {
-            tmpDir = new File(System.getProperty("java.io.tmpdir"),
-                    RunExamples.class.getName()
-                    + (new SimpleDateFormat("yyMMddHHmmss")).format(new Date()));
+            tmpDir = Files.createTempDirectory(Paths.get(System.getProperty("java.io.tmpdir")),
+                    RunExamples.class.getName());
+            deleteOnExit = true;
         }
-        Example.setTempDir(tmpDir);
+        Example.setTempDir(tmpDir.toFile());
 
         RunExamples r = new RunExamples();
 
@@ -73,15 +76,8 @@
             if (r.run(args))
                 return;
         } finally {
-            /* VERY IMPORTANT NOTE. In jtreg mode, tmpDir is set to the
-             * jtreg scratch directory, which is the current directory.
-             * In case someone is faking jtreg mode, make sure to only
-             * clean tmpDir when it is reasonable to do so.
-             */
-            if (tmpDir.isDirectory() &&
-                    tmpDir.getName().startsWith(RunExamples.class.getName())) {
-                if (clean(tmpDir))
-                    tmpDir.delete();
+            if (deleteOnExit) {
+                clean(tmpDir);
             }
         }
 
@@ -203,14 +199,20 @@
     /**
      * Clean the contents of a directory.
      */
-    static boolean clean(File dir) {
-        boolean ok = true;
-        for (File f: dir.listFiles()) {
-            if (f.isDirectory())
-                ok &= clean(f);
-            ok &= f.delete();
-        }
-        return ok;
+    static void clean(Path dir) throws IOException {
+        Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
+            @Override
+            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
+                Files.delete(file);
+                return super.visitFile(file, attrs);
+            }
+
+            @Override
+            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
+                if (exc == null) Files.delete(dir);
+                return super.postVisitDirectory(dir, exc);
+            }
+        });
     }
 
     static abstract class Runner {