8274172: Convert JavadocTester to use NIO
Reviewed-by: prappo
This commit is contained in:
parent
2657bcbd99
commit
5b0c9ccc49
test/langtools
jdk/javadoc
doclet
testDocFileDir
testMetadata
testRelativeLinks
testSearchScript
testSingletonLists
testStylesheet
lib/javadoc/tester
tools/lib/toolbox
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2002, 2021, 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
|
||||
@ -28,13 +28,14 @@
|
||||
* get overwritten when the sourcepath is equal to the destination
|
||||
* directory.
|
||||
* Also test that -docfilessubdirs and -excludedocfilessubdir both work.
|
||||
* @library ../../lib
|
||||
* @library /tools/lib ../../lib
|
||||
* @modules jdk.javadoc/jdk.javadoc.internal.tool
|
||||
* @build javadoc.tester.*
|
||||
* @build toolbox.ToolBox javadoc.tester.*
|
||||
* @run main TestDocFileDir
|
||||
*/
|
||||
|
||||
import javadoc.tester.JavadocTester;
|
||||
import toolbox.ToolBox;
|
||||
|
||||
public class TestDocFileDir extends JavadocTester {
|
||||
|
||||
@ -43,10 +44,12 @@ public class TestDocFileDir extends JavadocTester {
|
||||
tester.runTests();
|
||||
}
|
||||
|
||||
ToolBox tb = new ToolBox();
|
||||
|
||||
// Output dir = "", Input dir = ""
|
||||
@Test
|
||||
public void test1() {
|
||||
copyDir(testSrc("pkg"), ".");
|
||||
tb.copyDir(testSrc("pkg"), "pkg");
|
||||
setOutputDirectoryCheck(DirectoryCheck.NO_HTML_FILES);
|
||||
javadoc("pkg/C.java");
|
||||
checkExit(Exit.OK);
|
||||
@ -58,7 +61,7 @@ public class TestDocFileDir extends JavadocTester {
|
||||
@Test
|
||||
public void test2() {
|
||||
String outdir = "out2";
|
||||
copyDir(testSrc("pkg"), outdir);
|
||||
tb.copyDir(testSrc("pkg"), outdir + "/pkg");
|
||||
setOutputDirectoryCheck(DirectoryCheck.NO_HTML_FILES);
|
||||
javadoc("-d", outdir,
|
||||
"-sourcepath", "blah" + PS + outdir + PS + "blah",
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2019, 2021, 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
|
||||
@ -165,9 +165,8 @@ public class TestMetadata extends JavadocTester {
|
||||
);
|
||||
|
||||
void checkBodyClasses() throws IOException {
|
||||
Path outputDirPath = outputDir.toPath();
|
||||
for (Path p : tb.findFiles(".html", outputDirPath)) {
|
||||
checkBodyClass(outputDirPath.relativize(p));
|
||||
for (Path p : tb.findFiles(".html", outputDir)) {
|
||||
checkBodyClass(outputDir.relativize(p));
|
||||
}
|
||||
}
|
||||
|
||||
@ -231,9 +230,8 @@ public class TestMetadata extends JavadocTester {
|
||||
);
|
||||
|
||||
void checkMetadata() throws IOException {
|
||||
Path outputDirPath = outputDir.toPath();
|
||||
for (Path p : tb.findFiles(".html", outputDirPath)) {
|
||||
checkMetadata(outputDirPath.relativize(p));
|
||||
for (Path p : tb.findFiles(".html", outputDir)) {
|
||||
checkMetadata(outputDir.relativize(p));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -35,6 +35,9 @@
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import javadoc.tester.JavadocTester;
|
||||
|
||||
@ -200,9 +203,9 @@ public class TestRelativeLinks extends JavadocTester {
|
||||
}
|
||||
|
||||
private void touch(String file) {
|
||||
File f = new File(outputDir, file);
|
||||
Path f = outputDir.resolve(file);
|
||||
out.println("touch " + f);
|
||||
try (FileOutputStream fos = new FileOutputStream(f)) {
|
||||
try (OutputStream fos = Files.newOutputStream(f)) {
|
||||
} catch (IOException e) {
|
||||
checking("Touch file");
|
||||
failed("Error creating file: " + e);
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2019, 2021, 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
|
||||
@ -41,10 +41,9 @@ import javax.script.ScriptContext;
|
||||
import javax.script.ScriptEngine;
|
||||
import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import jtreg.SkippedException;
|
||||
@ -71,9 +70,9 @@ public class TestSearchScript extends JavadocTester {
|
||||
// see https://github.com/graalvm/graaljs/blob/master/docs/user/ScriptEngine.md
|
||||
Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
|
||||
bindings.put("polyglot.js.nashorn-compat", true);
|
||||
engine.eval(new BufferedReader(new FileReader(new File(testSrc, "javadoc-search.js"))));
|
||||
engine.eval(Files.newBufferedReader(Path.of(testSrc).resolve("javadoc-search.js")));
|
||||
Invocable inv = (Invocable) engine;
|
||||
inv.invokeFunction("loadIndexFiles", outputDir.getAbsolutePath());
|
||||
inv.invokeFunction("loadIndexFiles", outputDir.toAbsolutePath().toString());
|
||||
return inv;
|
||||
}
|
||||
|
||||
|
@ -206,7 +206,7 @@ public class TestSingletonLists extends JavadocTester {
|
||||
checking("Check lists");
|
||||
ListChecker c = new ListChecker(out, this::readFile);
|
||||
try {
|
||||
c.checkDirectory(outputDir.toPath());
|
||||
c.checkDirectory(outputDir);
|
||||
c.report();
|
||||
int errors = c.getErrorCount();
|
||||
if (errors == 0) {
|
||||
|
@ -381,7 +381,7 @@ public class TestStylesheet extends JavadocTester {
|
||||
checking("Check CSS class names");
|
||||
CSSClassChecker c = new CSSClassChecker(out, this::readFile, styles);
|
||||
try {
|
||||
c.checkDirectory(outputDir.toPath());
|
||||
c.checkDirectory(outputDir);
|
||||
c.report();
|
||||
int errors = c.getErrorCount();
|
||||
if (errors == 0) {
|
||||
|
@ -25,10 +25,7 @@ package javadoc.tester;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileWriter;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.io.PrintWriter;
|
||||
@ -41,13 +38,18 @@ import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.UnsupportedCharsetException;
|
||||
import java.nio.file.DirectoryStream;
|
||||
import java.nio.file.FileVisitResult;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.SimpleFileVisitor;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.EnumMap;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@ -157,7 +159,7 @@ public abstract class JavadocTester {
|
||||
}
|
||||
|
||||
/** The output directory used in the most recent call of javadoc. */
|
||||
protected File outputDir;
|
||||
protected Path outputDir;
|
||||
|
||||
/** The output charset used in the most recent call of javadoc. */
|
||||
protected Charset charset = Charset.defaultCharset();
|
||||
@ -169,7 +171,7 @@ public abstract class JavadocTester {
|
||||
private final Map<Output, String> outputMap = new EnumMap<>(Output.class);
|
||||
|
||||
/** A cache of file content, to avoid reading files unnecessarily. */
|
||||
private final Map<File,SoftReference<String>> fileContentCache = new HashMap<>();
|
||||
private final Map<Path,SoftReference<String>> fileContentCache = new HashMap<>();
|
||||
/** The charset used for files in the fileContentCache. */
|
||||
private Charset fileContentCacheCharset = null;
|
||||
|
||||
@ -185,7 +187,7 @@ public abstract class JavadocTester {
|
||||
* @return the full path of the specified file
|
||||
*/
|
||||
public static String testSrc(String path) {
|
||||
return new File(testSrc, path).getPath();
|
||||
return Path.of(testSrc).resolve(path).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -195,35 +197,40 @@ public abstract class JavadocTester {
|
||||
/**
|
||||
* Check that the directory is empty.
|
||||
*/
|
||||
EMPTY((file, name) -> true),
|
||||
EMPTY(p -> 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_HTML_FILES(p -> p.getFileName().toString().endsWith(".html")),
|
||||
/**
|
||||
* No check is performed on the directory contents.
|
||||
*/
|
||||
NONE(null) { @Override void check(File dir) { } };
|
||||
NONE(null) { @Override void check(Path dir) { } };
|
||||
|
||||
/** The filter used to detect that files should <i>not</i> be present. */
|
||||
FilenameFilter filter;
|
||||
DirectoryStream.Filter<Path> filter;
|
||||
|
||||
DirectoryCheck(FilenameFilter f) {
|
||||
DirectoryCheck(DirectoryStream.Filter<Path> 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) {
|
||||
System.err.println("Found extraneous files in dir:" + dir.getAbsolutePath());
|
||||
for (String x : contents) {
|
||||
System.err.println(x);
|
||||
void check(Path dir) {
|
||||
if (Files.isDirectory(dir)) {
|
||||
List<Path> contents = new ArrayList<>();
|
||||
try (var ds = Files.newDirectoryStream(dir, filter)) {
|
||||
for (Path p : ds) {
|
||||
contents.add(p);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new Error("cannot list directory: " + dir + "; " + e, e);
|
||||
}
|
||||
if (!contents.isEmpty()) {
|
||||
System.err.println("Found extraneous files in dir:" + dir.toAbsolutePath());
|
||||
for (Path p : contents) {
|
||||
System.err.println(p);
|
||||
}
|
||||
throw new Error("directory has unexpected content: " + dir);
|
||||
}
|
||||
@ -316,13 +323,13 @@ public abstract class JavadocTester {
|
||||
out.println("Running javadoc (run "+ javadocRunNum + ")...");
|
||||
}
|
||||
|
||||
outputDir = new File(".");
|
||||
outputDir = Path.of(".");
|
||||
String charsetArg = null;
|
||||
String docencodingArg = null;
|
||||
String encodingArg = null;
|
||||
for (int i = 0; i < args.length - 2; i++) {
|
||||
switch (args[i]) {
|
||||
case "-d" -> outputDir = new File(args[++i]);
|
||||
case "-d" -> outputDir = Path.of(args[++i]);
|
||||
case "-charset" -> charsetArg = args[++i];
|
||||
case "-docencoding" -> docencodingArg = args[++i];
|
||||
case "-encoding" -> encodingArg = args[++i];
|
||||
@ -377,7 +384,7 @@ public abstract class JavadocTester {
|
||||
}
|
||||
});
|
||||
|
||||
if (exitCode == Exit.OK.code && outputDir.exists()) {
|
||||
if (exitCode == Exit.OK.code && Files.exists(outputDir)) {
|
||||
if (automaticCheckLinks) {
|
||||
checkLinks();
|
||||
}
|
||||
@ -502,8 +509,8 @@ public abstract class JavadocTester {
|
||||
public void checkOutput(String path, boolean expectedFound, String... strings) {
|
||||
// Read contents of file
|
||||
try {
|
||||
String fileString = readFile(outputDir, path);
|
||||
checkOutput(new File(outputDir, path).getPath(), fileString, expectedFound, strings);
|
||||
String fileString = readFile(outputDir, Path.of(path));
|
||||
checkOutput(outputDir.resolve(path).toString(), fileString, expectedFound, strings);
|
||||
} catch (Error e) {
|
||||
checking("Read file");
|
||||
failed("Error reading file: " + e);
|
||||
@ -594,7 +601,7 @@ public abstract class JavadocTester {
|
||||
checking("Check accessibility");
|
||||
A11yChecker c = new A11yChecker(out, this::readFile);
|
||||
try {
|
||||
c.checkDirectory(outputDir.toPath());
|
||||
c.checkDirectory(outputDir);
|
||||
c.report();
|
||||
int errors = c.getErrorCount();
|
||||
if (errors == 0) {
|
||||
@ -616,7 +623,7 @@ public abstract class JavadocTester {
|
||||
checking("Check links");
|
||||
LinkChecker c = new LinkChecker(out, this::readFile);
|
||||
try {
|
||||
c.checkDirectory(outputDir.toPath());
|
||||
c.checkDirectory(outputDir);
|
||||
c.report();
|
||||
int errors = c.getErrorCount();
|
||||
if (errors == 0) {
|
||||
@ -641,8 +648,8 @@ public abstract class JavadocTester {
|
||||
ShowHeadings s = new ShowHeadings(out, this::readFile);
|
||||
for (String p : paths) {
|
||||
try {
|
||||
File f = new File(outputDir, p);
|
||||
s.checkFiles(List.of(f.toPath()), false, Collections.emptySet());
|
||||
Path f = outputDir.resolve(p);
|
||||
s.checkFiles(List.of(f), false, Collections.emptySet());
|
||||
} catch (IOException e) {
|
||||
checking("Read file");
|
||||
failed("Error reading file: " + e);
|
||||
@ -691,8 +698,8 @@ public abstract class JavadocTester {
|
||||
for (String path: paths) {
|
||||
// log.logCheckFile(path, expectedFound);
|
||||
checking("checkFile");
|
||||
File file = new File(outputDir, path);
|
||||
boolean isFound = file.exists();
|
||||
Path file = outputDir.resolve(path);
|
||||
boolean isFound = Files.exists(file);
|
||||
if (isFound == expectedFound) {
|
||||
passed(file, "file " + (isFound ? "found:" : "not found:") + "\n");
|
||||
} else {
|
||||
@ -708,7 +715,7 @@ public abstract class JavadocTester {
|
||||
* @param strings the strings whose order to check
|
||||
*/
|
||||
public void checkOrder(String path, String... strings) {
|
||||
File file = new File(outputDir, path);
|
||||
Path file = outputDir.resolve(path);
|
||||
String fileString = readOutputFile(path);
|
||||
int prevIndex = -1;
|
||||
for (String s : strings) {
|
||||
@ -736,7 +743,7 @@ public abstract class JavadocTester {
|
||||
* @param strings ensure each are unique
|
||||
*/
|
||||
public void checkUnique(String path, String... strings) {
|
||||
File file = new File(outputDir, path);
|
||||
Path file = outputDir.resolve(path);
|
||||
String fileString = readOutputFile(path);
|
||||
for (String s : strings) {
|
||||
int currentIndex = fileString.indexOf(s);
|
||||
@ -762,58 +769,13 @@ public abstract class JavadocTester {
|
||||
* @param files the set of files to be compared
|
||||
*/
|
||||
public void diff(String baseDir1, String baseDir2, String... files) {
|
||||
File bd1 = new File(baseDir1);
|
||||
File bd2 = new File(baseDir2);
|
||||
Path bd1 = Path.of(baseDir1);
|
||||
Path bd2 = Path.of(baseDir2);
|
||||
for (String file : files) {
|
||||
diff(bd1, bd2, file);
|
||||
diff(bd1, bd2, Path.of(file));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies a directory from one place to another.
|
||||
*
|
||||
* @param targetDir the directory to copy.
|
||||
* @param destDir the destination to copy the directory to.
|
||||
*/
|
||||
// TODO: convert to using java.nio.Files.walkFileTree
|
||||
public void copyDir(String targetDir, String destDir) {
|
||||
try {
|
||||
File targetDirObj = new File(targetDir);
|
||||
File destDirParentObj = new File(destDir);
|
||||
File destDirObj = new File(destDirParentObj, targetDirObj.getName());
|
||||
if (! destDirParentObj.exists()) {
|
||||
destDirParentObj.mkdir();
|
||||
}
|
||||
if (! destDirObj.exists()) {
|
||||
destDirObj.mkdir();
|
||||
}
|
||||
String[] files = targetDirObj.list();
|
||||
for (String file : files) {
|
||||
File srcFile = new File(targetDirObj, file);
|
||||
File destFile = new File(destDirObj, file);
|
||||
if (srcFile.isFile()) {
|
||||
out.println("Copying " + srcFile + " to " + destFile);
|
||||
copyFile(destFile, srcFile);
|
||||
} else if(srcFile.isDirectory()) {
|
||||
copyDir(srcFile.getAbsolutePath(), destDirObj.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
} catch (IOException exc) {
|
||||
throw new Error("Could not copy " + targetDir + " to " + destDir);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies a file.
|
||||
*
|
||||
* @param destfile the destination file
|
||||
* @param srcfile the source file
|
||||
* @throws IOException
|
||||
*/
|
||||
public void copyFile(File destfile, File srcfile) throws IOException {
|
||||
Files.copy(srcfile.toPath(), destfile.toPath());
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a file from the output directory.
|
||||
*
|
||||
@ -821,27 +783,27 @@ public abstract class JavadocTester {
|
||||
* @return the file in string format
|
||||
*/
|
||||
public String readOutputFile(String fileName) throws Error {
|
||||
return readFile(outputDir, fileName);
|
||||
return readFile(outputDir, Path.of(fileName));
|
||||
}
|
||||
|
||||
protected String readFile(String fileName) throws Error {
|
||||
return readFile(outputDir, fileName);
|
||||
return readFile(outputDir, Path.of(fileName));
|
||||
}
|
||||
|
||||
protected String readFile(String baseDir, String fileName) throws Error {
|
||||
return readFile(new File(baseDir), fileName);
|
||||
return readFile(Path.of(baseDir), Path.of(fileName));
|
||||
}
|
||||
|
||||
protected String readFile(Path file) {
|
||||
File baseDir;
|
||||
if (file.startsWith(outputDir.toPath())) {
|
||||
Path baseDir;
|
||||
if (file.startsWith(outputDir)) {
|
||||
baseDir = outputDir;
|
||||
} else if (file.startsWith(currDir)) {
|
||||
baseDir = currDir.toFile();
|
||||
baseDir = currDir;
|
||||
} else {
|
||||
baseDir = file.getParent().toFile();
|
||||
baseDir = file.getParent();
|
||||
}
|
||||
String fileName = baseDir.toPath().relativize(file).toString();
|
||||
Path fileName = baseDir.relativize(file);
|
||||
return readFile(baseDir, fileName);
|
||||
}
|
||||
|
||||
@ -852,20 +814,20 @@ public abstract class JavadocTester {
|
||||
* @param fileName the name of the file to read
|
||||
* @return the file in string format
|
||||
*/
|
||||
private String readFile(File baseDir, String fileName) throws Error {
|
||||
private String readFile(Path baseDir, Path fileName) throws Error {
|
||||
if (!Objects.equals(fileContentCacheCharset, charset)) {
|
||||
fileContentCache.clear();
|
||||
fileContentCacheCharset = charset;
|
||||
}
|
||||
try {
|
||||
File file = new File(baseDir, fileName);
|
||||
Path file = baseDir.resolve(fileName);
|
||||
SoftReference<String> ref = fileContentCache.get(file);
|
||||
String content = (ref == null) ? null : ref.get();
|
||||
if (content != null)
|
||||
return content;
|
||||
|
||||
// charset defaults to a value inferred from latest javadoc run
|
||||
content = new String(Files.readAllBytes(file.toPath()), charset);
|
||||
content = new String(Files.readAllBytes(file), charset);
|
||||
fileContentCache.put(file, new SoftReference<>(content));
|
||||
return content;
|
||||
} catch (FileNotFoundException e) {
|
||||
@ -897,7 +859,7 @@ public abstract class JavadocTester {
|
||||
* @param file the file that was the focus of the check
|
||||
* @param message a short description of the outcome
|
||||
*/
|
||||
protected void passed(File file, String message) {
|
||||
protected void passed(Path file, String message) {
|
||||
passed(file + ": " + message);
|
||||
}
|
||||
|
||||
@ -922,7 +884,7 @@ public abstract class JavadocTester {
|
||||
* @param file the file that was the focus of the check
|
||||
* @param message a short description of the outcome
|
||||
*/
|
||||
protected void failed(File file, String message) {
|
||||
protected void failed(Path file, String message) {
|
||||
failed(file + ": " + message);
|
||||
}
|
||||
|
||||
@ -1002,10 +964,10 @@ public abstract class JavadocTester {
|
||||
* @param baseDir2 the directory in which to locate the second file
|
||||
* @param file the file to compare in the two base directories
|
||||
*/
|
||||
private void diff(File baseDir1, File baseDir2, String file) {
|
||||
private void diff(Path baseDir1, Path baseDir2, Path file) {
|
||||
String file1Contents = readFile(baseDir1, file);
|
||||
String file2Contents = readFile(baseDir2, file);
|
||||
checking("diff " + new File(baseDir1, file) + ", " + new File(baseDir2, file));
|
||||
checking("diff " + baseDir1.resolve(file) + ", " + baseDir2.resolve(file));
|
||||
if (file1Contents.trim().compareTo(file2Contents.trim()) == 0) {
|
||||
passed("files are equal");
|
||||
} else {
|
||||
@ -1068,44 +1030,46 @@ public abstract class JavadocTester {
|
||||
private static final int SUFFIX = 20;
|
||||
private static final int MAX = PREFIX + SUFFIX;
|
||||
List<String> tests = new ArrayList<>();
|
||||
String outDir;
|
||||
String rootDir = rootDir();
|
||||
Path outDir;
|
||||
Path rootDir = rootDir();
|
||||
|
||||
static String rootDir() {
|
||||
File f = new File(".").getAbsoluteFile();
|
||||
while (!new File(f, ".hg").exists())
|
||||
f = f.getParentFile();
|
||||
return f.getPath();
|
||||
static Path rootDir() {
|
||||
Path f = Path.of(".").toAbsolutePath();
|
||||
while (f != null && !Files.exists(f.resolve(".git")))
|
||||
f = f.getParent();
|
||||
return f;
|
||||
}
|
||||
|
||||
void setOutDir(File outDir) {
|
||||
this.outDir = outDir.getPath();
|
||||
void setOutDir(Path outDir) {
|
||||
this.outDir = outDir;
|
||||
}
|
||||
|
||||
void logCheckFile(String file, boolean positive) {
|
||||
// Strip the outdir because that will typically not be the same
|
||||
if (file.startsWith(outDir + "/"))
|
||||
file = file.substring(outDir.length() + 1);
|
||||
tests.add(file + " " + positive);
|
||||
Path p = Path.of(file);
|
||||
if (p.startsWith(outDir))
|
||||
p = p.relativize(outDir);
|
||||
tests.add(p + " " + positive);
|
||||
}
|
||||
|
||||
void logCheckOutput(String file, boolean positive, String text) {
|
||||
// Compress the string to be displayed in the log file
|
||||
String simpleText = text.replaceAll("\\s+", " ").replace(rootDir, "[ROOT]");
|
||||
String simpleText = text.replaceAll("\\s+", " ").replace(rootDir.toString(), "[ROOT]");
|
||||
if (simpleText.length() > MAX)
|
||||
simpleText = simpleText.substring(0, PREFIX)
|
||||
+ "..." + simpleText.substring(simpleText.length() - SUFFIX);
|
||||
// Strip the outdir because that will typically not be the same
|
||||
if (file.startsWith(outDir + "/"))
|
||||
file = file.substring(outDir.length() + 1);
|
||||
Path p = Path.of(file);
|
||||
if (p.startsWith(outDir))
|
||||
p = p.relativize(outDir);
|
||||
// The use of text.hashCode ensure that all of "text" is taken into account
|
||||
tests.add(file + " " + positive + " " + text.hashCode() + " " + simpleText);
|
||||
tests.add(p + " " + positive + " " + text.hashCode() + " " + simpleText);
|
||||
}
|
||||
|
||||
void write() {
|
||||
// sort the log entries because the subtests may not be executed in the same order
|
||||
tests.sort((a, b) -> a.compareTo(b));
|
||||
try (BufferedWriter bw = new BufferedWriter(new FileWriter("tester.log"))) {
|
||||
tests.sort(Comparator.naturalOrder());
|
||||
try (var bw = Files.newBufferedWriter(Path.of("tester.log"))) {
|
||||
for (String t: tests) {
|
||||
bw.write(t);
|
||||
bw.newLine();
|
||||
@ -1116,6 +1080,4 @@ public abstract class JavadocTester {
|
||||
}
|
||||
}
|
||||
|
||||
// Support classes for checkLinks
|
||||
|
||||
}
|
@ -34,6 +34,8 @@ import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.FileAlreadyExistsException;
|
||||
import java.nio.file.FileVisitOption;
|
||||
import java.nio.file.FileVisitResult;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
@ -46,6 +48,7 @@ import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Deque;
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
@ -249,7 +252,51 @@ public class ToolBox {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates one of more directories.
|
||||
* Copies the contents of a directory to another directory.
|
||||
* <p>Similar to the shell command: {@code rsync fromDir/ toDir/}.
|
||||
*
|
||||
* @param fromDir the directory containing the files to be copied
|
||||
* @param toDir the destination to which to copy the files
|
||||
*/
|
||||
public void copyDir(String fromDir, String toDir) {
|
||||
copyDir(Path.of(fromDir), Path.of(toDir));
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies the contents of a directory to another directory.
|
||||
* The destination direction should not already exist.
|
||||
* <p>Similar to the shell command: {@code rsync fromDir/ toDir/}.
|
||||
*
|
||||
* @param fromDir the directory containing the files to be copied
|
||||
* @param toDir the destination to which to copy the files
|
||||
*/
|
||||
public void copyDir(Path fromDir, Path toDir) {
|
||||
try {
|
||||
if (toDir.getParent() != null) {
|
||||
Files.createDirectories(toDir.getParent());
|
||||
}
|
||||
Files.walkFileTree(fromDir, new SimpleFileVisitor<Path>() {
|
||||
@Override
|
||||
public FileVisitResult preVisitDirectory(Path fromSubdir, BasicFileAttributes attrs)
|
||||
throws IOException {
|
||||
Files.copy(fromSubdir, toDir.resolve(fromDir.relativize(fromSubdir)));
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path fromFile, BasicFileAttributes attrs)
|
||||
throws IOException {
|
||||
Files.copy(fromFile, toDir.resolve(fromDir.relativize(fromFile)));
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
});
|
||||
} catch (IOException e) {
|
||||
throw new Error("Could not copy " + fromDir + " to " + toDir + ": " + e, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates one or more directories.
|
||||
* For each of the series of paths, a directory will be created,
|
||||
* including any necessary parent directories.
|
||||
* <p>Similar to the shell command: {@code mkdir -p paths}.
|
||||
|
Loading…
x
Reference in New Issue
Block a user