test/com/sun/javadoc/lib/JavadocTester.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

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

mercurial