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