langtools/test/com/sun/javadoc/lib/JavadocTester.java
author jjg
Wed, 15 Oct 2008 08:07:59 -0700
changeset 1475 19c0851667ca
parent 10 06bc494ca11e
child 1652 1324f96f3883
permissions -rw-r--r--
6748541: javadoc should be reusable Reviewed-by: bpatel
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 2002-2004 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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[][] {};
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
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
     * The buffer of warning output..
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
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     * The current subtest number.
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    private static int numTestsRun = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
     * The number of subtests passed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
    private static int numTestsPassed = 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 current run of javadoc
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
    private static int javadocRunNum = 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
     * Construct a JavadocTester.
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
    public JavadocTester() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
     * Return the bug id.
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
     * @return the bug id
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
    public abstract String getBugId();
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
     * Return the name of the bug.
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
     * @return the name of the bug
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
    public abstract String getBugName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
     * Execute the tests.
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
     * @param tester           the tester to execute
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
     * @param args             the arguments to pass to Javadoc
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
     * @param testArray        the array of tests
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
     * @param negatedTestArray the array of negated tests
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
     * @return                 the return code for the execution of Javadoc
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
    public static int run(JavadocTester tester, String[] args,
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
            String[][] testArray, String[][] negatedTestArray) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
        int returnCode = tester.runJavadoc(args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
        tester.runTestsOnHTML(testArray, negatedTestArray);
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        return returnCode;
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
     * Execute Javadoc using the default doclet.
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
     * @param args  the arguments to pass to Javadoc
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
     * @return      the return code from the execution of Javadoc
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
    public int runJavadoc(String[] args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
        float javaVersion = Float.parseFloat(JAVA_VERSION.substring(0,3));
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
        String docletClass = javaVersion < 1.5 ?
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
            DEFAULT_DOCLET_CLASS_OLD : DEFAULT_DOCLET_CLASS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
        return runJavadoc(docletClass, args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
     * Execute Javadoc.
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
     * @param docletClass the doclet being tested.
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
     * @param args  the arguments to pass to Javadoc
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
     * @return      the return code from the execution of Javadoc
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
    public int runJavadoc(String docletClass, String[] args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
        javadocRunNum++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        if (javadocRunNum == 1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
            System.out.println("\n" + "Running javadoc...");
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
            System.out.println("\n" + "Running javadoc (run "
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
                                    + javadocRunNum + ")...");
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
        initOutputBuffers();
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
        ByteArrayOutputStream stdout = new ByteArrayOutputStream();
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
        PrintStream prev = System.out;
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
        System.setOut(new PrintStream(stdout));
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
        int returnCode = com.sun.tools.javadoc.Main.execute(
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
                getBugName(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
                new PrintWriter(errors, true),
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
                new PrintWriter(warnings, true),
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
                new PrintWriter(notices, true),
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
                docletClass,
1475
19c0851667ca 6748541: javadoc should be reusable
jjg
parents: 10
diff changeset
   200
                getClass().getClassLoader(),
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
                args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
        System.setOut(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
        standardOut = new StringBuffer(stdout.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
        printJavadocOutput();
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
        return returnCode;
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
     * Create new string writer buffers
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
    private void initOutputBuffers() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
        errors   = new StringWriter();
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
        notices  = new StringWriter();
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
        warnings = new StringWriter();
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
     * Run array of tests on the resulting HTML.
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
     * This method accepts a testArray for testing that a string is found
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
     * and a negatedTestArray for testing that a string is not found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
     * @param testArray         the array of tests
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
     * @param negatedTestArray  the array of negated tests
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
    public void runTestsOnHTML(String[][] testArray, String[][] negatedTestArray) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
        runTestsOnHTML(testArray, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
        runTestsOnHTML(negatedTestArray, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
     * Run the array of tests on the resulting HTML.
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
     * @param testArray the array of tests
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
     * @param isNegated true if test is negated; false otherwise
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
    private void runTestsOnHTML(String[][] testArray , boolean isNegated) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
        for (int i = 0; i < testArray.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
            numTestsRun++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
            System.out.print("Running subtest #" + numTestsRun + "... ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
            // Get string to find
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
            String stringToFind = testArray[i][1];
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
            // Read contents of file into a string
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
            String fileString;
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
                fileString = readFileToString(testArray[i][0]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
            } catch (Error e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
                if (isNegated) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
                  numTestsPassed += 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
                  System.out.println("Passed\n not found:\n"
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
                    + stringToFind + " in non-existent " + testArray[i][0] + "\n");
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
                  continue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
                throw e;
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
            // Find string in file's contents
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
            boolean isFound = findString(fileString, stringToFind);
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
            if ((isNegated && !isFound) || (!isNegated && isFound) ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
                numTestsPassed += 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
                System.out.println( "Passed" + "\n"
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
                                    + (isNegated ? "not found:" : "found:") + "\n"
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
                                    + stringToFind + " in " + testArray[i][0] + "\n");
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
                System.out.println( "FAILED" + "\n"
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
                                    + "for bug " + getBugId()
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
                                    + " (" + getBugName() + ")" + "\n"
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
                                    + "when searching for:" + "\n"
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
                                    + stringToFind
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
                                    + " in " + testArray[i][0] + "\n");
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
     * Iterate through the list of given file pairs and diff each file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
     * @param filePairs the pairs of files to diff.
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
     * @throws an Error is thrown if any differences are found between
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
     * file pairs.
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
    public void runDiffs(String[][] filePairs) throws Error {
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
        runDiffs(filePairs, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
     * Iterate through the list of given file pairs and diff each file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
     * @param filePairs the pairs of files to diff.
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
     * @param throwErrorIFNoMatch flag to indicate whether or not to throw
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
     * an error if the files do not match.
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
     * @throws an Error is thrown if any differences are found between
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
     * file pairs and throwErrorIFNoMatch is true.
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
    public void runDiffs(String[][] filePairs, boolean throwErrorIfNoMatch) throws Error {
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
        for (int i = 0; i < filePairs.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
            diff(filePairs[i][0], filePairs[i][1], throwErrorIfNoMatch);
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
     * Check the exit code of Javadoc and record whether the test passed
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
     * or failed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
     * @param expectedExitCode The exit code that is required for the test
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
     * to pass.
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
     * @param actualExitCode The actual exit code from the previous run of
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
     * Javadoc.
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
    public void checkExitCode(int expectedExitCode, int actualExitCode) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
        numTestsRun++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
        if (expectedExitCode == actualExitCode) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
            System.out.println( "Passed" + "\n" + " got return code " +
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
                actualExitCode);
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
            numTestsPassed++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
            System.out.println( "FAILED" + "\n" + "for bug " + getBugId()
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
                + " (" + getBugName() + ")" + "\n" + "Expected return code " +
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
                expectedExitCode + " but got " + actualExitCode);
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
     * Print a summary of the test results.
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
    protected void printSummary() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
        if ( numTestsRun != 0 && numTestsPassed == numTestsRun ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
            // Test passed
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
            System.out.println("\n" + "All " + numTestsPassed
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
                                             + " subtests passed");
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
            // Test failed
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
            throw new Error("\n" + (numTestsRun - numTestsPassed)
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
                                    + " of " + (numTestsRun)
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
                                    + " subtests failed for bug " + getBugId()
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
                                    + " (" + getBugName() + ")" + "\n");
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
     * Print the output stored in the buffers.
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
    protected void printJavadocOutput() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
        System.out.println(STANDARD_OUTPUT + " : \n" + getStandardOutput());
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
        System.err.println(ERROR_OUTPUT + " : \n" + getErrorOutput());
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
        System.err.println(WARNING_OUTPUT + " : \n" + getWarningOutput());
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
        System.out.println(NOTICE_OUTPUT + " : \n" + getNoticeOutput());
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
     * Read the file and return it as a string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
     * @param fileName  the name of the file to read
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
     * @return          the file in string format
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
    public String readFileToString(String fileName) throws Error {
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
        if (fileName.equals(ERROR_OUTPUT)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
            return getErrorOutput();
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
        } else if (fileName.equals(NOTICE_OUTPUT)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
            return getNoticeOutput();
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
        } else if (fileName.equals(WARNING_OUTPUT)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
            return getWarningOutput();
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
        } else if (fileName.equals(STANDARD_OUTPUT)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
            return getStandardOutput();
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
            File file = new File(fileName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
            if ( !file.exists() ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
                System.out.println("\n" + "FILE DOES NOT EXIST: " + fileName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
            BufferedReader in = new BufferedReader(new FileReader(file));
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
            // Create an array of characters the size of the file
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
            char[] allChars = new char[(int)file.length()];
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
            // Read the characters into the allChars array
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
            in.read(allChars, 0, (int)file.length());
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
            in.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
            // Convert to a string
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
            String allCharsString = new String(allChars);
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
            return allCharsString;
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
        } catch (FileNotFoundException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
            System.err.println(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
            throw new Error("File not found: " + fileName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
        } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
            System.err.println(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
            throw new Error("Error reading file: " + fileName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
     * Compare the two given files.
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
     * @param file1 the first file to compare.
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
     * @param file2 the second file to compare.
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
     * @param throwErrorIFNoMatch flag to indicate whether or not to throw
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
     * an error if the files do not match.
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
     * @return true if the files are the same and false otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
    public boolean diff(String file1, String file2, boolean throwErrorIFNoMatch) throws Error {
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
        String file1Contents = readFileToString(file1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
        String file2Contents = readFileToString(file2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
        numTestsRun++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
        if (file1Contents.trim().compareTo(file2Contents.trim()) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
            System.out.println("Diff successful: " + file1 + ", " + file2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
            numTestsPassed++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
        } else if (throwErrorIFNoMatch) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
            throw new Error("Diff failed: " + file1 + ", " + file2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
            return false;
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
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
     * Search for the string in the given file and return true
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
     * if the string was found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
     * @param fileString    the contents of the file to search through
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
     * @param stringToFind  the string to search for
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
     * @return              true if the string was found
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
    private boolean findString(String fileString, String stringToFind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
        return fileString.indexOf(stringToFind) >= 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
     * Return the standard output.
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
     * @return the standard output
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
    public String getStandardOutput() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
        return standardOut.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
     * Return the error output.
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
     * @return the error output
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
    public String getErrorOutput() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
        return errors.getBuffer().toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
     * Return the notice output.
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
     * @return the notice output
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
    public String getNoticeOutput() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
        return notices.getBuffer().toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
     * Return the warning output.
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
     * @return the warning output
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
    public String getWarningOutput() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
        return warnings.getBuffer().toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
     * A utility to copy a directory from one place to another.
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
     * We may possibly want to move this to our doclet toolkit in
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
     * the near future and maintain it from there.
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
     * @param targetDir the directory to copy.
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
     * @param destDir the destination to copy the directory to.
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
    public static void copyDir(String targetDir, String destDir) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
        if (targetDir.endsWith("SCCS")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
            File targetDirObj = new File(targetDir);
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
            File destDirParentObj = new File(destDir);
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
            File destDirObj = new File(destDirParentObj, targetDirObj.getName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
            if (! destDirParentObj.exists()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
                destDirParentObj.mkdir();
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
            if (! destDirObj.exists()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
                destDirObj.mkdir();
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
            String[] files = targetDirObj.list();
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
            for (int i = 0; i < files.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
                File srcFile = new File(targetDirObj, files[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
                File destFile = new File(destDirObj, files[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
                if (srcFile.isFile()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
                    System.out.println("Copying " + srcFile + " to " + destFile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
                        copyFile(destFile, srcFile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
                } else if(srcFile.isDirectory()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
                    copyDir(srcFile.getAbsolutePath(), destDirObj.getAbsolutePath());
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
        } catch (IOException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
            throw new Error("Could not copy " + targetDir + " to " + destDir);
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
     * Copy source file to destination file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
     * @throws SecurityException
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
     * @throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
    public static void copyFile(File destfile, File srcfile)
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
        throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
        byte[] bytearr = new byte[512];
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
        int len = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
        FileInputStream input = new FileInputStream(srcfile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
        File destDir = destfile.getParentFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
        destDir.mkdirs();
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
        FileOutputStream output = new FileOutputStream(destfile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
            while ((len = input.read(bytearr)) != -1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
                output.write(bytearr, 0, len);
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
        } catch (FileNotFoundException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
        } catch (SecurityException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
            input.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
            output.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
}