langtools/test/com/sun/javadoc/lib/JavadocTester.java
changeset 24217 25b12d4d4192
parent 24072 e7549dcbc4af
child 24399 af1a0220d0fa
--- a/langtools/test/com/sun/javadoc/lib/JavadocTester.java	Wed Jul 05 19:38:35 2017 +0200
+++ b/langtools/test/com/sun/javadoc/lib/JavadocTester.java	Fri Apr 25 13:08:41 2014 -0700
@@ -114,6 +114,46 @@
     private File outputDir;
 
     /**
+     * Alternatives for checking the contents of a directory.
+     */
+    enum DirectoryCheck {
+        /**
+         * Check that the directory is empty.
+         */
+        EMPTY((file, name) -> true),
+        /**
+         * Check that the directory does not contain any HTML files,
+         * such as may have been generated by a prior run of javadoc
+         * using this directory.
+         * For now, the check is only performed on the top level directory.
+         */
+        NO_HTML_FILES((file, name) -> name.endsWith(".html")),
+        /**
+         * No check is performed on the directory contents.
+         */
+        NONE(null) { @Override void check(File dir) { } };
+
+        /** The filter used to detect that files should <i>not</i> be present. */
+        FilenameFilter filter;
+
+        DirectoryCheck(FilenameFilter f) {
+            filter = f;
+        }
+
+        void check(File dir) {
+            if (dir.isDirectory()) {
+                String[] contents = dir.list(filter);
+                if (contents == null)
+                    throw new Error("cannot list directory: " + dir);
+                if (contents.length > 0)
+                    throw new Error("directory has unexpected content: " + dir);
+            }
+        }
+    }
+
+    private DirectoryCheck outputDirectoryCheck = DirectoryCheck.EMPTY;
+
+    /**
      * The current subtest number.
      */
     private static int numTestsRun = 0;
@@ -206,6 +246,8 @@
             }
         }
 
+        outputDirectoryCheck.check(outputDir);
+
         ByteArrayOutputStream stdout = new ByteArrayOutputStream();
         PrintStream prevOut = System.out;
         System.setOut(new PrintStream(stdout));
@@ -232,6 +274,15 @@
     }
 
     /**
+     * Set a filter to check the initial contents of the output directory
+     * before javadoc is run.
+     * The filter should return true for files that should <b>not</b> appear.
+     */
+    public void setCheckOutputDirectoryCheck(DirectoryCheck c) {
+        outputDirectoryCheck = c;
+    }
+
+    /**
      * Create new string writer buffers
      */
     private void initOutputBuffers() {