/*
* Copyright (c) 2002, 2017, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
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;
import java.io.StringWriter;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.ref.SoftReference;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
/**
* Test framework for running javadoc and performing tests on the resulting output.
*
*
* Tests are typically written as subtypes of JavadocTester, with a main
* method that creates an instance of the test class and calls the runTests()
* method. The runTests() methods calls all the test methods declared in the class,
* and then calls a method to print a summary, and throw an exception if
* any of the test methods reported a failure.
*
*
* Test methods are identified with a @Test annotation. They have no parameters.
* The name of the method is not important, but if you have more than one, it is
* recommended that the names be meaningful and suggestive of the test case
* contained therein.
*
*
* Typically, a test method will invoke javadoc, and then perform various
* checks on the results. The standard checks are:
*
*
* - checkExitCode
*
- Check the exit code returned from javadoc.
*
- checkOutput
*
- Perform a series of checks on the contents on a file or output stream
* generated by javadoc.
* The checks can be either that a series of strings are found or are not found.
*
- checkFiles
*
- Perform a series of checks on the files generated by javadoc.
* The checks can be that a series of files are found or are not found.
*
*
*
* public class MyTester extends JavadocTester {
* public static void main(String... args) throws Exception {
* MyTester tester = new MyTester();
* tester.runTests();
* }
*
* // test methods...
* @Test
* void test() {
* javadoc(args);
* checkExit(Exit.OK);
* checkOutput(file, true,
* strings-to-find);
* checkOutput(file, false,
* strings-to-not-find);
* }
* }
*
*
*
* If javadoc is run more than once in a test method, you can compare the
* results that are generated with the diff method. Since files written by
* javadoc typically contain a timestamp, you may want to use the -notimestamp
* option if you are going to compare the results from two runs of javadoc.
*
*
* If you have many calls of checkOutput that are very similar, you can write
* your own check... method to reduce the amount of duplication. For example,
* if you want to check that many files contain the same string, you could
* write a method that takes a varargs list of files and calls checkOutput
* on each file in turn with the string to be checked.
*
*
* You can also write you own custom check methods, which can use
* readFile to get the contents of a file generated by javadoc,
* and then use pass(...) or fail(...) to report whether the check
* succeeded or not.
*
*
* You can have many separate test methods, each identified with a @Test
* annotation. However, you should not assume they will be called
* in the order declared in your source file. If the order of a series
* of javadoc invocations is important, do that within a single method.
* If the invocations are independent, for better clarity, use separate
* test methods, each with their own set of checks on the results.
*
* @author Doug Kramer
* @author Jamie Ho
* @author Jonathan Gibbons (rewrite)
*/
public abstract class JavadocTester {
public static final String FS = System.getProperty("file.separator");
public static final String PS = System.getProperty("path.separator");
public static final String NL = System.getProperty("line.separator");
public enum Output {
/** The name of the output stream from javadoc. */
OUT,
/** The name for any output written to System.out. */
STDOUT,
/** The name for any output written to System.err. */
STDERR
}
/** The output directory used in the most recent call of javadoc. */
protected File outputDir;
/** The exit code of the most recent call of javadoc. */
private int exitCode;
/** The output generated by javadoc to the various writers and streams. */
private final Map