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