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