8040904: Ensure javadoc tests do not overwrite results within tests

Reviewed-by: ksrini
This commit is contained in:
Jonathan Gibbons 2014-04-25 13:08:41 -07:00
parent e5a91499f0
commit 6c24c7e153
13 changed files with 384 additions and 293 deletions

View file

@ -113,6 +113,46 @@ public abstract class JavadocTester {
*/
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.
*/
@ -206,6 +246,8 @@ public abstract class JavadocTester {
}
}
outputDirectoryCheck.check(outputDir);
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
PrintStream prevOut = System.out;
System.setOut(new PrintStream(stdout));
@ -231,6 +273,15 @@ public abstract class JavadocTester {
return returnCode;
}
/**
* 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
*/