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

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1
9a66ca7c79fa
child 140
22c4c1143a3a
permissions
-rw-r--r--

Initial load

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

mercurial