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

Tue, 22 Sep 2009 14:06:29 -0700

author
xdono
date
Tue, 22 Sep 2009 14:06:29 -0700
changeset 404
14735c7932d7
parent 389
0ba956343648
child 554
9d9f26857129
permissions
-rw-r--r--

6884624: Update copyright year
Summary: Update copyright for files that have been modified in 2009 through Septermber
Reviewed-by: tbell, ohair

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

mercurial