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