langtools/test/com/sun/javadoc/lib/JavadocTester.java
author jjg
Tue, 22 Apr 2014 17:57:40 -0700
changeset 24072 e7549dcbc4af
parent 24065 fc4022e50129
child 24217 25b12d4d4192
permissions -rw-r--r--
8040903: Clean up use of BUG_ID in javadoc tests Reviewed-by: ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
23971
f5ff1f5a8dee 8031649: Clean up javadoc tests
jjg
parents: 20238
diff changeset
     2
 * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3890
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3890
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3890
diff changeset
    21
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
 * Runs javadoc and then runs regression tests on the resulting output.
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
 * This class currently contains three tests:
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
 * <ul>
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
 * <li> String search: Reads each file, complete with newlines,
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
 *      into a string.  Lets you search for strings that contain
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
 *      newlines.  String matching is case-sensitive.
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
 *      You can run javadoc multiple times with different arguments,
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
 *      generating output into different destination directories, and
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 *      then perform a different array of tests on each one.
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 *      To do this, the run method accepts a test array for testing
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 *      that a string is found, and a negated test array for testing
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 *      that a string is not found.
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * <li> Run diffs: Iterate through the list of given file pairs
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 *      and diff the pairs.
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 * <li> Check exit code: Check the exit code of Javadoc and
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 *      record whether the test passed or failed.
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 * </ul>
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 * @author Doug Kramer
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 * @author Jamie Ho
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 * @since 1.4.2
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
public abstract class JavadocTester {
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
23971
f5ff1f5a8dee 8031649: Clean up javadoc tests
jjg
parents: 20238
diff changeset
    52
    protected static final String NL = System.getProperty("line.separator");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    protected static final String FS = System.getProperty("file.separator");
23971
f5ff1f5a8dee 8031649: Clean up javadoc tests
jjg
parents: 20238
diff changeset
    54
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    protected static final String SRC_DIR = System.getProperty("test.src", ".");
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    protected static final String JAVA_VERSION = System.getProperty("java.version");
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
    57
    protected static final String OUTPUT_DIR = "out";
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    protected static final String[][] NO_TEST = new String[][] {};
20238
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
    59
    protected static final String[] NO_FILE_TEST = new String[] {};
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
     * Use this as the file name in the test array when you want to search
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     * for a string in the error output.
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    public static final String ERROR_OUTPUT = "ERROR_OUTPUT";
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
     * Use this as the file name in the test array when you want to search
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
     * for a string in the notice output.
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    public static final String NOTICE_OUTPUT = "NOTICE_OUTPUT";
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
     * Use this as the file name in the test array when you want to search
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
     * for a string in the warning output.
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    public static final String WARNING_OUTPUT = "WARNING_OUTPUT";
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
     * Use this as the file name in the test array when you want to search
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
     * for a string in standard output.
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    public static final String STANDARD_OUTPUT = "STANDARD_OUTPUT";
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
     * The default doclet.
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    public static final String DEFAULT_DOCLET_CLASS = "com.sun.tools.doclets.formats.html.HtmlDoclet";
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    public static final String DEFAULT_DOCLET_CLASS_OLD = "com.sun.tools.doclets.standard.Standard";
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     * The writer to write error messages.
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
    public StringWriter errors;
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
     * The writer to write notices.
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
    public StringWriter notices;
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
     * The writer to write warnings.
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    public StringWriter warnings;
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    /**
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   107
     * The buffer of warning output.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
    public StringBuffer standardOut;
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
    /**
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   112
     * The output directory.
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   113
     */
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   114
    private File outputDir;
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   115
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   116
    /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
     * The current subtest number.
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
    private static int numTestsRun = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
     * The number of subtests passed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
    private static int numTestsPassed = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
     * The current run of javadoc
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
    private static int javadocRunNum = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
     * Construct a JavadocTester.
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    public JavadocTester() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
     * Execute the tests.
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
     * @param args             the arguments to pass to Javadoc
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
     * @param testArray        the array of tests
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
     * @param negatedTestArray the array of negated tests
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
     * @return                 the return code for the execution of Javadoc
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
     */
24065
fc4022e50129 8041150: Avoid silly use of static methods in JavadocTester
jjg
parents: 23971
diff changeset
   145
    public int run(String[] args,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
            String[][] testArray, String[][] negatedTestArray) {
24065
fc4022e50129 8041150: Avoid silly use of static methods in JavadocTester
jjg
parents: 23971
diff changeset
   147
        int returnCode = runJavadoc(args);
fc4022e50129 8041150: Avoid silly use of static methods in JavadocTester
jjg
parents: 23971
diff changeset
   148
        runTestsOnHTML(testArray, negatedTestArray);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        return returnCode;
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
    /**
20238
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   153
     * Execute the tests.
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   154
     *
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   155
     * @param args                 the arguments to pass to Javadoc
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   156
     * @param testArray            the array of tests
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   157
     * @param negatedTestArray     the array of negated tests
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   158
     * @param fileTestArray        the array of file tests
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   159
     * @param negatedFileTestArray the array of negated file tests
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   160
     * @return                     the return code for the execution of Javadoc
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   161
     */
24065
fc4022e50129 8041150: Avoid silly use of static methods in JavadocTester
jjg
parents: 23971
diff changeset
   162
    public int run(String[] args,
fc4022e50129 8041150: Avoid silly use of static methods in JavadocTester
jjg
parents: 23971
diff changeset
   163
            String[][] testArray, String[][] negatedTestArray,
fc4022e50129 8041150: Avoid silly use of static methods in JavadocTester
jjg
parents: 23971
diff changeset
   164
            String[] fileTestArray, String[] negatedFileTestArray) {
fc4022e50129 8041150: Avoid silly use of static methods in JavadocTester
jjg
parents: 23971
diff changeset
   165
        int returnCode = runJavadoc(args);
fc4022e50129 8041150: Avoid silly use of static methods in JavadocTester
jjg
parents: 23971
diff changeset
   166
        runTestsOnHTML(testArray, negatedTestArray);
fc4022e50129 8041150: Avoid silly use of static methods in JavadocTester
jjg
parents: 23971
diff changeset
   167
        runTestsOnFile(fileTestArray, negatedFileTestArray);
20238
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   168
        return returnCode;
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   169
    }
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   170
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   171
    /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
     * Execute Javadoc using the default doclet.
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
     * @param args  the arguments to pass to Javadoc
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
     * @return      the return code from the execution of Javadoc
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
    public int runJavadoc(String[] args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
        float javaVersion = Float.parseFloat(JAVA_VERSION.substring(0,3));
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
        String docletClass = javaVersion < 1.5 ?
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
            DEFAULT_DOCLET_CLASS_OLD : DEFAULT_DOCLET_CLASS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        return runJavadoc(docletClass, args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
     * Execute Javadoc.
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
     * @param docletClass the doclet being tested.
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
     * @param args  the arguments to pass to Javadoc
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
     * @return      the return code from the execution of Javadoc
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
    public int runJavadoc(String docletClass, String[] args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
        javadocRunNum++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
        if (javadocRunNum == 1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
            System.out.println("\n" + "Running javadoc...");
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
            System.out.println("\n" + "Running javadoc (run "
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
                                    + javadocRunNum + ")...");
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
        initOutputBuffers();
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   201
        outputDir = new File(".");
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   202
        for (int i = 0; i < args.length - 2; i++) {
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   203
            if (args[i].equals("-d")) {
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   204
                outputDir = new File(args[++i]);
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   205
                break;
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   206
            }
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   207
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
        ByteArrayOutputStream stdout = new ByteArrayOutputStream();
10190
11c701650189 6735320: StringIndexOutOfBoundsException for empty @serialField tag
ksrini
parents: 5520
diff changeset
   210
        PrintStream prevOut = System.out;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
        System.setOut(new PrintStream(stdout));
10190
11c701650189 6735320: StringIndexOutOfBoundsException for empty @serialField tag
ksrini
parents: 5520
diff changeset
   212
11c701650189 6735320: StringIndexOutOfBoundsException for empty @serialField tag
ksrini
parents: 5520
diff changeset
   213
        ByteArrayOutputStream stderr = new ByteArrayOutputStream();
11c701650189 6735320: StringIndexOutOfBoundsException for empty @serialField tag
ksrini
parents: 5520
diff changeset
   214
        PrintStream prevErr = System.err;
11c701650189 6735320: StringIndexOutOfBoundsException for empty @serialField tag
ksrini
parents: 5520
diff changeset
   215
        System.setErr(new PrintStream(stderr));
11c701650189 6735320: StringIndexOutOfBoundsException for empty @serialField tag
ksrini
parents: 5520
diff changeset
   216
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
        int returnCode = com.sun.tools.javadoc.Main.execute(
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   218
                "javadoc",
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
                new PrintWriter(errors, true),
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
                new PrintWriter(warnings, true),
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
                new PrintWriter(notices, true),
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
                docletClass,
1475
19c0851667ca 6748541: javadoc should be reusable
jjg
parents: 10
diff changeset
   223
                getClass().getClassLoader(),
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
                args);
10190
11c701650189 6735320: StringIndexOutOfBoundsException for empty @serialField tag
ksrini
parents: 5520
diff changeset
   225
        System.setOut(prevOut);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
        standardOut = new StringBuffer(stdout.toString());
10190
11c701650189 6735320: StringIndexOutOfBoundsException for empty @serialField tag
ksrini
parents: 5520
diff changeset
   227
        System.setErr(prevErr);
11c701650189 6735320: StringIndexOutOfBoundsException for empty @serialField tag
ksrini
parents: 5520
diff changeset
   228
        errors.write(NL + stderr.toString());
11c701650189 6735320: StringIndexOutOfBoundsException for empty @serialField tag
ksrini
parents: 5520
diff changeset
   229
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
        printJavadocOutput();
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
        return returnCode;
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
     * Create new string writer buffers
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
    private void initOutputBuffers() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
        errors   = new StringWriter();
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
        notices  = new StringWriter();
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
        warnings = new StringWriter();
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
     * Run array of tests on the resulting HTML.
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
     * This method accepts a testArray for testing that a string is found
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
     * and a negatedTestArray for testing that a string is not found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
     * @param testArray         the array of tests
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
     * @param negatedTestArray  the array of negated tests
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
    public void runTestsOnHTML(String[][] testArray, String[][] negatedTestArray) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
        runTestsOnHTML(testArray, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
        runTestsOnHTML(negatedTestArray, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
    /**
20238
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   257
     * Run array of tests on the generated files.
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   258
     * This method accepts a fileTestArray for testing if a file is generated
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   259
     * and a negatedFileTestArray for testing if a file is not found.
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   260
     * The files are relative to the most recent output directory specified
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   261
     * with -d.
20238
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   262
     *
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   263
     * @param fileTestArray         the array of file tests
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   264
     * @param negatedFileTestArray  the array of negated file tests
20238
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   265
     */
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   266
    public void runTestsOnFile(String[] fileTestArray, String[] negatedFileTestArray) {
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   267
        runTestsOnFile(outputDir, fileTestArray, false);
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   268
        runTestsOnFile(outputDir, negatedFileTestArray, true);
20238
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   269
    }
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   270
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   271
    /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
     * Run the array of tests on the resulting HTML.
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   273
     * The files are relative to the most recent output directory specified
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   274
     * with -d.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
     * @param testArray the array of tests
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
     * @param isNegated true if test is negated; false otherwise
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
    private void runTestsOnHTML(String[][] testArray , boolean isNegated) {
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   280
        for (String[] test : testArray) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
            numTestsRun++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
            System.out.print("Running subtest #" + numTestsRun + "... ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
            // Get string to find
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   284
            String stringToFind = test[1];
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
            // Read contents of file into a string
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
            String fileString;
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
            try {
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   288
                fileString = readFileToString(outputDir, test[0]);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
            } catch (Error e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
                if (isNegated) {
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   291
                    System.out.println( "FAILED, due to " + e + "\n");
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   292
                    continue;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
                throw e;
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
            // Find string in file's contents
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
            boolean isFound = findString(fileString, stringToFind);
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   298
            if ((isNegated && !isFound) || (!isNegated && isFound)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
                numTestsPassed += 1;
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   300
                System.out.println("Passed" + "\n"
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   301
                        + (isNegated ? "not found:" : "found:") + "\n"
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   302
                        + stringToFind + " in " + test[0] + "\n");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
            } else {
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   304
                System.out.println("FAILED, when searching for:" + "\n"
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   305
                        + stringToFind
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   306
                        + " in " + test[0] + "\n");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
    /**
20238
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   312
     * Run the array of file tests on the generated files.
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   313
     *
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   314
     * @param testArray the array of file tests
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   315
     * @param isNegated true if test is negated; false otherwise
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   316
     */
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   317
    private void runTestsOnFile(File baseDir, String[] testArray, boolean isNegated) {
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   318
        for (String fileName : testArray) {
20238
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   319
            numTestsRun++;
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   320
            String failedString = "FAILED: file (" + fileName + ") found" + "\n";
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   321
            String passedString = "Passed" + "\n" +
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   322
                    "file (" + fileName + ") not found" + "\n";
20238
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   323
            System.out.print("Running subtest #" + numTestsRun + "... ");
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   324
            try {
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   325
                File file = new File(baseDir, fileName);
20238
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   326
                if ((file.exists() && !isNegated) || (!file.exists() && isNegated)) {
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   327
                    numTestsPassed += 1;
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   328
                    System.out.println(passedString);
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   329
                } else {
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   330
                    System.out.println(failedString);
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   331
                }
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   332
            } catch (Error e) {
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   333
                System.err.println(e);
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   334
            }
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   335
        }
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   336
    }
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   337
a08610368936 8024096: some javadoc tests may contain false positive results
bpatel
parents: 10190
diff changeset
   338
    /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
     * Iterate through the list of given file pairs and diff each file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
     *
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   341
     * @param baseDir1 the directory containing the first set of files
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   342
     * @param baseDir2 the directory containing the second set of files
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   343
     * @param files the set of files to be compared
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   344
     * @throws Error if any differences are found between
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
     * file pairs.
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
     */
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   347
    public void runDiffs(String baseDir1, String baseDir2, String[] files) throws Error {
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   348
        runDiffs(baseDir1, baseDir2, files, true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
     * Iterate through the list of given file pairs and diff each file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
     *
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   354
     * @param baseDir1 the directory containing the first set of files
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   355
     * @param baseDir2 the directory containing the second set of files
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   356
     * @param files the set of files to be compared
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   357
     * @param throwErrorIfNoMatch flag to indicate whether or not to throw
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
     * an error if the files do not match.
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
     *
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   360
     * @throws Error if any differences are found between
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   361
     * file pairs and throwErrorIfNoMatch is true.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
     */
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   363
    public void runDiffs(String baseDir1, String baseDir2, String[] files, boolean throwErrorIfNoMatch) throws Error {
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   364
        File bd1 = new File(baseDir1);
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   365
        File bd2 = new File(baseDir2);
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   366
        for (String file : files) {
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   367
            diff(bd1, bd2, file, throwErrorIfNoMatch);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
     * Check the exit code of Javadoc and record whether the test passed
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
     * or failed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
     * @param expectedExitCode The exit code that is required for the test
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
     * to pass.
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
     * @param actualExitCode The actual exit code from the previous run of
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
     * Javadoc.
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
    public void checkExitCode(int expectedExitCode, int actualExitCode) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
        numTestsRun++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
        if (expectedExitCode == actualExitCode) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
            System.out.println( "Passed" + "\n" + " got return code " +
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
                actualExitCode);
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
            numTestsPassed++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
        } else {
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   387
            System.out.println( "FAILED: expected return code " +
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
                expectedExitCode + " but got " + actualExitCode);
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
     * Print a summary of the test results.
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
    protected void printSummary() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
        if ( numTestsRun != 0 && numTestsPassed == numTestsRun ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
            // Test passed
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
            System.out.println("\n" + "All " + numTestsPassed
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
                                             + " subtests passed");
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
            // Test failed
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
            throw new Error("\n" + (numTestsRun - numTestsPassed)
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
                                    + " of " + (numTestsRun)
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   404
                                    + " subtests failed\n");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
     * Print the output stored in the buffers.
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
    protected void printJavadocOutput() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
        System.out.println(STANDARD_OUTPUT + " : \n" + getStandardOutput());
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
        System.err.println(ERROR_OUTPUT + " : \n" + getErrorOutput());
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
        System.err.println(WARNING_OUTPUT + " : \n" + getWarningOutput());
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
        System.out.println(NOTICE_OUTPUT + " : \n" + getNoticeOutput());
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
     * Read the file and return it as a string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
     * @param fileName  the name of the file to read
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
     * @return          the file in string format
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
    public String readFileToString(String fileName) throws Error {
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   425
        return readFileToString(outputDir, fileName);
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   426
    }
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   427
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   428
    /**
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   429
     * Read the file and return it as a string.
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   430
     *
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   431
     * @param baseDir   the directory in which to locate the file
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   432
     * @param fileName  the name of the file to read
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   433
     * @return          the file in string format
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   434
     */
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   435
    private String readFileToString(File baseDir, String fileName) throws Error {
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   436
        switch (fileName) {
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   437
            case ERROR_OUTPUT:
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   438
                return getErrorOutput();
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   439
            case NOTICE_OUTPUT:
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   440
                return getNoticeOutput();
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   441
            case WARNING_OUTPUT:
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   442
                return getWarningOutput();
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   443
            case STANDARD_OUTPUT:
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   444
                return getStandardOutput();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
        try {
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   447
            File file = new File(baseDir, fileName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
            if ( !file.exists() ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
                System.out.println("\n" + "FILE DOES NOT EXIST: " + fileName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
            }
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   451
            char[] allChars;
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   452
            try (BufferedReader in = new BufferedReader(new FileReader(file))) {
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   453
                // Create an array of characters the size of the file
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   454
                allChars = new char[(int)file.length()];
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   455
                // Read the characters into the allChars array
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   456
                in.read(allChars, 0, (int)file.length());
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   457
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
            // Convert to a string
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
            String allCharsString = new String(allChars);
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
            return allCharsString;
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
        } catch (FileNotFoundException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
            System.err.println(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
            throw new Error("File not found: " + fileName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
            System.err.println(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
            throw new Error("Error reading file: " + fileName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
     * Compare the two given files.
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
     *
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   474
     * @param baseDir1 the directory in which to locate the first file
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   475
     * @param baseDir2 the directory in which to locate the second file
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   476
     * @param file the file to compare in the two base directories
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
     * @param throwErrorIFNoMatch flag to indicate whether or not to throw
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
     * an error if the files do not match.
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
     * @return true if the files are the same and false otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
     */
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   481
    private boolean diff(File baseDir1, File baseDir2, String file,
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   482
            boolean throwErrorIFNoMatch) throws Error {
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   483
        String file1Contents = readFileToString(baseDir1, file);
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   484
        String file2Contents = readFileToString(baseDir2, file);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
        numTestsRun++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
        if (file1Contents.trim().compareTo(file2Contents.trim()) == 0) {
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   487
            System.out.println("Diff successful: " + new File(baseDir1, file) + ", " + new File(baseDir2, file));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
            numTestsPassed++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
        } else if (throwErrorIFNoMatch) {
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   491
            throw new Error("Diff failed: " + new File(baseDir1, file) + ", " + new File(baseDir2, file));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
     * Search for the string in the given file and return true
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
     * if the string was found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
     * @param fileString    the contents of the file to search through
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
     * @param stringToFind  the string to search for
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
     * @return              true if the string was found
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
    private boolean findString(String fileString, String stringToFind) {
23971
f5ff1f5a8dee 8031649: Clean up javadoc tests
jjg
parents: 20238
diff changeset
   506
        return fileString.contains(stringToFind.replace("\n", NL));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
3771
6e7b16ecca30 6876782: two javadoc tests fail on Windows
jjg
parents: 1652
diff changeset
   509
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
     * Return the standard output.
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
     * @return the standard output
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
    public String getStandardOutput() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
        return standardOut.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
     * Return the error output.
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
     * @return the error output
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
    public String getErrorOutput() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
        return errors.getBuffer().toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
     * Return the notice output.
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
     * @return the notice output
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
    public String getNoticeOutput() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
        return notices.getBuffer().toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
     * Return the warning output.
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
     * @return the warning output
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
    public String getWarningOutput() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
        return warnings.getBuffer().toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
     * A utility to copy a directory from one place to another.
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
     * We may possibly want to move this to our doclet toolkit in
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
     * the near future and maintain it from there.
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
     * @param targetDir the directory to copy.
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
     * @param destDir the destination to copy the directory to.
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
    public static void copyDir(String targetDir, String destDir) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
        if (targetDir.endsWith("SCCS")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
            File targetDirObj = new File(targetDir);
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
            File destDirParentObj = new File(destDir);
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
            File destDirObj = new File(destDirParentObj, targetDirObj.getName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
            if (! destDirParentObj.exists()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
                destDirParentObj.mkdir();
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
            if (! destDirObj.exists()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
                destDirObj.mkdir();
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
            String[] files = targetDirObj.list();
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   565
            for (String file : files) {
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   566
                File srcFile = new File(targetDirObj, file);
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   567
                File destFile = new File(destDirObj, file);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
                if (srcFile.isFile()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
                    System.out.println("Copying " + srcFile + " to " + destFile);
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   570
                    copyFile(destFile, srcFile);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
                } else if(srcFile.isDirectory()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
                    copyDir(srcFile.getAbsolutePath(), destDirObj.getAbsolutePath());
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
        } catch (IOException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
            throw new Error("Could not copy " + targetDir + " to " + destDir);
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
     * Copy source file to destination file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
     *
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   583
     * @param destfile the destination file
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   584
     * @param srcfile the source file
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
     * @throws SecurityException
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
     * @throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
    public static void copyFile(File destfile, File srcfile)
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
        throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
        byte[] bytearr = new byte[512];
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   591
        int len;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
        FileInputStream input = new FileInputStream(srcfile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
        File destDir = destfile.getParentFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
        destDir.mkdirs();
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
        FileOutputStream output = new FileOutputStream(destfile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
            while ((len = input.read(bytearr)) != -1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
                output.write(bytearr, 0, len);
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
            }
24072
e7549dcbc4af 8040903: Clean up use of BUG_ID in javadoc tests
jjg
parents: 24065
diff changeset
   600
        } catch (FileNotFoundException | SecurityException exc) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
            input.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
            output.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
}