author | jjg |
Fri, 25 Apr 2014 13:08:41 -0700 | |
changeset 24217 | 25b12d4d4192 |
parent 24072 | e7549dcbc4af |
child 24399 | af1a0220d0fa |
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 java.io.*; |
|
25 |
||
26 |
||
27 |
/** |
|
28 |
* Runs javadoc and then runs regression tests on the resulting output. |
|
29 |
* This class currently contains three tests: |
|
30 |
* <ul> |
|
31 |
* <li> String search: Reads each file, complete with newlines, |
|
32 |
* into a string. Lets you search for strings that contain |
|
33 |
* newlines. String matching is case-sensitive. |
|
34 |
* You can run javadoc multiple times with different arguments, |
|
35 |
* generating output into different destination directories, and |
|
36 |
* then perform a different array of tests on each one. |
|
37 |
* To do this, the run method accepts a test array for testing |
|
38 |
* that a string is found, and a negated test array for testing |
|
39 |
* that a string is not found. |
|
40 |
* <li> Run diffs: Iterate through the list of given file pairs |
|
41 |
* and diff the pairs. |
|
42 |
* <li> Check exit code: Check the exit code of Javadoc and |
|
43 |
* record whether the test passed or failed. |
|
44 |
* </ul> |
|
45 |
* |
|
46 |
* @author Doug Kramer |
|
47 |
* @author Jamie Ho |
|
48 |
* @since 1.4.2 |
|
49 |
*/ |
|
50 |
public abstract class JavadocTester { |
|
51 |
||
23971 | 52 |
protected static final String NL = System.getProperty("line.separator"); |
10 | 53 |
protected static final String FS = System.getProperty("file.separator"); |
23971 | 54 |
|
10 | 55 |
protected static final String SRC_DIR = System.getProperty("test.src", "."); |
56 |
protected static final String JAVA_VERSION = System.getProperty("java.version"); |
|
24072 | 57 |
protected static final String OUTPUT_DIR = "out"; |
10 | 58 |
protected static final String[][] NO_TEST = new String[][] {}; |
20238
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
59 |
protected static final String[] NO_FILE_TEST = new String[] {}; |
10 | 60 |
|
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"; |
|
66 |
||
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"; |
|
72 |
||
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"; |
|
78 |
||
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"; |
|
84 |
||
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"; |
|
90 |
||
91 |
/** |
|
92 |
* The writer to write error messages. |
|
93 |
*/ |
|
94 |
public StringWriter errors; |
|
95 |
||
96 |
/** |
|
97 |
* The writer to write notices. |
|
98 |
*/ |
|
99 |
public StringWriter notices; |
|
100 |
||
101 |
/** |
|
102 |
* The writer to write warnings. |
|
103 |
*/ |
|
104 |
public StringWriter warnings; |
|
105 |
||
106 |
/** |
|
24072 | 107 |
* The buffer of warning output. |
10 | 108 |
*/ |
109 |
public StringBuffer standardOut; |
|
110 |
||
111 |
/** |
|
24072 | 112 |
* The output directory. |
113 |
*/ |
|
114 |
private File outputDir; |
|
115 |
||
116 |
/** |
|
24217
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
117 |
* Alternatives for checking the contents of a directory. |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
118 |
*/ |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
119 |
enum DirectoryCheck { |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
120 |
/** |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
121 |
* Check that the directory is empty. |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
122 |
*/ |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
123 |
EMPTY((file, name) -> true), |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
124 |
/** |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
125 |
* Check that the directory does not contain any HTML files, |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
126 |
* such as may have been generated by a prior run of javadoc |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
127 |
* using this directory. |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
128 |
* For now, the check is only performed on the top level directory. |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
129 |
*/ |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
130 |
NO_HTML_FILES((file, name) -> name.endsWith(".html")), |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
131 |
/** |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
132 |
* No check is performed on the directory contents. |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
133 |
*/ |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
134 |
NONE(null) { @Override void check(File dir) { } }; |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
135 |
|
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
136 |
/** The filter used to detect that files should <i>not</i> be present. */ |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
137 |
FilenameFilter filter; |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
138 |
|
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
139 |
DirectoryCheck(FilenameFilter f) { |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
140 |
filter = f; |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
141 |
} |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
142 |
|
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
143 |
void check(File dir) { |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
144 |
if (dir.isDirectory()) { |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
145 |
String[] contents = dir.list(filter); |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
146 |
if (contents == null) |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
147 |
throw new Error("cannot list directory: " + dir); |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
148 |
if (contents.length > 0) |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
149 |
throw new Error("directory has unexpected content: " + dir); |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
150 |
} |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
151 |
} |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
152 |
} |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
153 |
|
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
154 |
private DirectoryCheck outputDirectoryCheck = DirectoryCheck.EMPTY; |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
155 |
|
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
156 |
/** |
10 | 157 |
* The current subtest number. |
158 |
*/ |
|
159 |
private static int numTestsRun = 0; |
|
160 |
||
161 |
/** |
|
162 |
* The number of subtests passed. |
|
163 |
*/ |
|
164 |
private static int numTestsPassed = 0; |
|
165 |
||
166 |
/** |
|
167 |
* The current run of javadoc |
|
168 |
*/ |
|
169 |
private static int javadocRunNum = 0; |
|
170 |
||
171 |
/** |
|
172 |
* Construct a JavadocTester. |
|
173 |
*/ |
|
174 |
public JavadocTester() { |
|
175 |
} |
|
176 |
||
177 |
/** |
|
178 |
* Execute the tests. |
|
179 |
* |
|
180 |
* @param args the arguments to pass to Javadoc |
|
181 |
* @param testArray the array of tests |
|
182 |
* @param negatedTestArray the array of negated tests |
|
183 |
* @return the return code for the execution of Javadoc |
|
184 |
*/ |
|
24065
fc4022e50129
8041150: Avoid silly use of static methods in JavadocTester
jjg
parents:
23971
diff
changeset
|
185 |
public int run(String[] args, |
10 | 186 |
String[][] testArray, String[][] negatedTestArray) { |
24065
fc4022e50129
8041150: Avoid silly use of static methods in JavadocTester
jjg
parents:
23971
diff
changeset
|
187 |
int returnCode = runJavadoc(args); |
fc4022e50129
8041150: Avoid silly use of static methods in JavadocTester
jjg
parents:
23971
diff
changeset
|
188 |
runTestsOnHTML(testArray, negatedTestArray); |
10 | 189 |
return returnCode; |
190 |
} |
|
191 |
||
192 |
/** |
|
20238
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
193 |
* Execute the tests. |
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
194 |
* |
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
195 |
* @param args the arguments to pass to Javadoc |
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
196 |
* @param testArray the array of tests |
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
197 |
* @param negatedTestArray the array of negated tests |
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
198 |
* @param fileTestArray the array of file tests |
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
199 |
* @param negatedFileTestArray the array of negated file tests |
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
200 |
* @return the return code for the execution of Javadoc |
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
201 |
*/ |
24065
fc4022e50129
8041150: Avoid silly use of static methods in JavadocTester
jjg
parents:
23971
diff
changeset
|
202 |
public int run(String[] args, |
fc4022e50129
8041150: Avoid silly use of static methods in JavadocTester
jjg
parents:
23971
diff
changeset
|
203 |
String[][] testArray, String[][] negatedTestArray, |
fc4022e50129
8041150: Avoid silly use of static methods in JavadocTester
jjg
parents:
23971
diff
changeset
|
204 |
String[] fileTestArray, String[] negatedFileTestArray) { |
fc4022e50129
8041150: Avoid silly use of static methods in JavadocTester
jjg
parents:
23971
diff
changeset
|
205 |
int returnCode = runJavadoc(args); |
fc4022e50129
8041150: Avoid silly use of static methods in JavadocTester
jjg
parents:
23971
diff
changeset
|
206 |
runTestsOnHTML(testArray, negatedTestArray); |
fc4022e50129
8041150: Avoid silly use of static methods in JavadocTester
jjg
parents:
23971
diff
changeset
|
207 |
runTestsOnFile(fileTestArray, negatedFileTestArray); |
20238
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
208 |
return returnCode; |
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
209 |
} |
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
210 |
|
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
211 |
/** |
10 | 212 |
* Execute Javadoc using the default doclet. |
213 |
* |
|
214 |
* @param args the arguments to pass to Javadoc |
|
215 |
* @return the return code from the execution of Javadoc |
|
216 |
*/ |
|
217 |
public int runJavadoc(String[] args) { |
|
218 |
float javaVersion = Float.parseFloat(JAVA_VERSION.substring(0,3)); |
|
219 |
String docletClass = javaVersion < 1.5 ? |
|
220 |
DEFAULT_DOCLET_CLASS_OLD : DEFAULT_DOCLET_CLASS; |
|
221 |
return runJavadoc(docletClass, args); |
|
222 |
} |
|
223 |
||
224 |
||
225 |
/** |
|
226 |
* Execute Javadoc. |
|
227 |
* |
|
228 |
* @param docletClass the doclet being tested. |
|
229 |
* @param args the arguments to pass to Javadoc |
|
230 |
* @return the return code from the execution of Javadoc |
|
231 |
*/ |
|
232 |
public int runJavadoc(String docletClass, String[] args) { |
|
233 |
javadocRunNum++; |
|
234 |
if (javadocRunNum == 1) { |
|
235 |
System.out.println("\n" + "Running javadoc..."); |
|
236 |
} else { |
|
237 |
System.out.println("\n" + "Running javadoc (run " |
|
238 |
+ javadocRunNum + ")..."); |
|
239 |
} |
|
240 |
initOutputBuffers(); |
|
24072 | 241 |
outputDir = new File("."); |
242 |
for (int i = 0; i < args.length - 2; i++) { |
|
243 |
if (args[i].equals("-d")) { |
|
244 |
outputDir = new File(args[++i]); |
|
245 |
break; |
|
246 |
} |
|
247 |
} |
|
10 | 248 |
|
24217
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
249 |
outputDirectoryCheck.check(outputDir); |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
250 |
|
10 | 251 |
ByteArrayOutputStream stdout = new ByteArrayOutputStream(); |
10190
11c701650189
6735320: StringIndexOutOfBoundsException for empty @serialField tag
ksrini
parents:
5520
diff
changeset
|
252 |
PrintStream prevOut = System.out; |
10 | 253 |
System.setOut(new PrintStream(stdout)); |
10190
11c701650189
6735320: StringIndexOutOfBoundsException for empty @serialField tag
ksrini
parents:
5520
diff
changeset
|
254 |
|
11c701650189
6735320: StringIndexOutOfBoundsException for empty @serialField tag
ksrini
parents:
5520
diff
changeset
|
255 |
ByteArrayOutputStream stderr = new ByteArrayOutputStream(); |
11c701650189
6735320: StringIndexOutOfBoundsException for empty @serialField tag
ksrini
parents:
5520
diff
changeset
|
256 |
PrintStream prevErr = System.err; |
11c701650189
6735320: StringIndexOutOfBoundsException for empty @serialField tag
ksrini
parents:
5520
diff
changeset
|
257 |
System.setErr(new PrintStream(stderr)); |
11c701650189
6735320: StringIndexOutOfBoundsException for empty @serialField tag
ksrini
parents:
5520
diff
changeset
|
258 |
|
10 | 259 |
int returnCode = com.sun.tools.javadoc.Main.execute( |
24072 | 260 |
"javadoc", |
10 | 261 |
new PrintWriter(errors, true), |
262 |
new PrintWriter(warnings, true), |
|
263 |
new PrintWriter(notices, true), |
|
264 |
docletClass, |
|
1475 | 265 |
getClass().getClassLoader(), |
10 | 266 |
args); |
10190
11c701650189
6735320: StringIndexOutOfBoundsException for empty @serialField tag
ksrini
parents:
5520
diff
changeset
|
267 |
System.setOut(prevOut); |
10 | 268 |
standardOut = new StringBuffer(stdout.toString()); |
10190
11c701650189
6735320: StringIndexOutOfBoundsException for empty @serialField tag
ksrini
parents:
5520
diff
changeset
|
269 |
System.setErr(prevErr); |
11c701650189
6735320: StringIndexOutOfBoundsException for empty @serialField tag
ksrini
parents:
5520
diff
changeset
|
270 |
errors.write(NL + stderr.toString()); |
11c701650189
6735320: StringIndexOutOfBoundsException for empty @serialField tag
ksrini
parents:
5520
diff
changeset
|
271 |
|
10 | 272 |
printJavadocOutput(); |
273 |
return returnCode; |
|
274 |
} |
|
275 |
||
276 |
/** |
|
24217
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
277 |
* Set a filter to check the initial contents of the output directory |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
278 |
* before javadoc is run. |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
279 |
* The filter should return true for files that should <b>not</b> appear. |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
280 |
*/ |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
281 |
public void setCheckOutputDirectoryCheck(DirectoryCheck c) { |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
282 |
outputDirectoryCheck = c; |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
283 |
} |
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
284 |
|
25b12d4d4192
8040904: Ensure javadoc tests do not overwrite results within tests
jjg
parents:
24072
diff
changeset
|
285 |
/** |
10 | 286 |
* Create new string writer buffers |
287 |
*/ |
|
288 |
private void initOutputBuffers() { |
|
289 |
errors = new StringWriter(); |
|
290 |
notices = new StringWriter(); |
|
291 |
warnings = new StringWriter(); |
|
292 |
} |
|
293 |
||
294 |
/** |
|
295 |
* Run array of tests on the resulting HTML. |
|
296 |
* This method accepts a testArray for testing that a string is found |
|
297 |
* and a negatedTestArray for testing that a string is not found. |
|
298 |
* |
|
299 |
* @param testArray the array of tests |
|
300 |
* @param negatedTestArray the array of negated tests |
|
301 |
*/ |
|
302 |
public void runTestsOnHTML(String[][] testArray, String[][] negatedTestArray) { |
|
303 |
runTestsOnHTML(testArray, false); |
|
304 |
runTestsOnHTML(negatedTestArray, true); |
|
305 |
} |
|
306 |
||
307 |
/** |
|
20238
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
308 |
* Run array of tests on the generated files. |
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
309 |
* 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
|
310 |
* and a negatedFileTestArray for testing if a file is not found. |
24072 | 311 |
* The files are relative to the most recent output directory specified |
312 |
* with -d. |
|
20238
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
313 |
* |
24072 | 314 |
* @param fileTestArray the array of file tests |
315 |
* @param negatedFileTestArray the array of negated file tests |
|
20238
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
316 |
*/ |
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
317 |
public void runTestsOnFile(String[] fileTestArray, String[] negatedFileTestArray) { |
24072 | 318 |
runTestsOnFile(outputDir, fileTestArray, false); |
319 |
runTestsOnFile(outputDir, negatedFileTestArray, true); |
|
20238
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
320 |
} |
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 |
/** |
10 | 323 |
* Run the array of tests on the resulting HTML. |
24072 | 324 |
* The files are relative to the most recent output directory specified |
325 |
* with -d. |
|
10 | 326 |
* |
327 |
* @param testArray the array of tests |
|
328 |
* @param isNegated true if test is negated; false otherwise |
|
329 |
*/ |
|
330 |
private void runTestsOnHTML(String[][] testArray , boolean isNegated) { |
|
24072 | 331 |
for (String[] test : testArray) { |
10 | 332 |
numTestsRun++; |
333 |
System.out.print("Running subtest #" + numTestsRun + "... "); |
|
334 |
// Get string to find |
|
24072 | 335 |
String stringToFind = test[1]; |
10 | 336 |
// Read contents of file into a string |
337 |
String fileString; |
|
338 |
try { |
|
24072 | 339 |
fileString = readFileToString(outputDir, test[0]); |
10 | 340 |
} catch (Error e) { |
341 |
if (isNegated) { |
|
24072 | 342 |
System.out.println( "FAILED, due to " + e + "\n"); |
343 |
continue; |
|
10 | 344 |
} |
345 |
throw e; |
|
346 |
} |
|
347 |
// Find string in file's contents |
|
348 |
boolean isFound = findString(fileString, stringToFind); |
|
24072 | 349 |
if ((isNegated && !isFound) || (!isNegated && isFound)) { |
10 | 350 |
numTestsPassed += 1; |
24072 | 351 |
System.out.println("Passed" + "\n" |
352 |
+ (isNegated ? "not found:" : "found:") + "\n" |
|
353 |
+ stringToFind + " in " + test[0] + "\n"); |
|
10 | 354 |
} else { |
24072 | 355 |
System.out.println("FAILED, when searching for:" + "\n" |
356 |
+ stringToFind |
|
357 |
+ " in " + test[0] + "\n"); |
|
10 | 358 |
} |
359 |
} |
|
360 |
} |
|
361 |
||
362 |
/** |
|
20238
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
363 |
* 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
|
364 |
* |
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
365 |
* @param testArray the array of file tests |
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
366 |
* @param isNegated true if test is negated; false otherwise |
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
367 |
*/ |
24072 | 368 |
private void runTestsOnFile(File baseDir, String[] testArray, boolean isNegated) { |
369 |
for (String fileName : testArray) { |
|
20238
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
370 |
numTestsRun++; |
24072 | 371 |
String failedString = "FAILED: file (" + fileName + ") found" + "\n"; |
372 |
String passedString = "Passed" + "\n" + |
|
373 |
"file (" + fileName + ") not found" + "\n"; |
|
20238
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
374 |
System.out.print("Running subtest #" + numTestsRun + "... "); |
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
375 |
try { |
24072 | 376 |
File file = new File(baseDir, fileName); |
20238
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
377 |
if ((file.exists() && !isNegated) || (!file.exists() && isNegated)) { |
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
378 |
numTestsPassed += 1; |
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
379 |
System.out.println(passedString); |
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
380 |
} else { |
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
381 |
System.out.println(failedString); |
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
382 |
} |
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
383 |
} catch (Error e) { |
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
384 |
System.err.println(e); |
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
385 |
} |
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
386 |
} |
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
387 |
} |
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
388 |
|
a08610368936
8024096: some javadoc tests may contain false positive results
bpatel
parents:
10190
diff
changeset
|
389 |
/** |
10 | 390 |
* Iterate through the list of given file pairs and diff each file. |
391 |
* |
|
24072 | 392 |
* @param baseDir1 the directory containing the first set of files |
393 |
* @param baseDir2 the directory containing the second set of files |
|
394 |
* @param files the set of files to be compared |
|
395 |
* @throws Error if any differences are found between |
|
10 | 396 |
* file pairs. |
397 |
*/ |
|
24072 | 398 |
public void runDiffs(String baseDir1, String baseDir2, String[] files) throws Error { |
399 |
runDiffs(baseDir1, baseDir2, files, true); |
|
10 | 400 |
} |
401 |
||
402 |
/** |
|
403 |
* Iterate through the list of given file pairs and diff each file. |
|
404 |
* |
|
24072 | 405 |
* @param baseDir1 the directory containing the first set of files |
406 |
* @param baseDir2 the directory containing the second set of files |
|
407 |
* @param files the set of files to be compared |
|
408 |
* @param throwErrorIfNoMatch flag to indicate whether or not to throw |
|
10 | 409 |
* an error if the files do not match. |
410 |
* |
|
24072 | 411 |
* @throws Error if any differences are found between |
412 |
* file pairs and throwErrorIfNoMatch is true. |
|
10 | 413 |
*/ |
24072 | 414 |
public void runDiffs(String baseDir1, String baseDir2, String[] files, boolean throwErrorIfNoMatch) throws Error { |
415 |
File bd1 = new File(baseDir1); |
|
416 |
File bd2 = new File(baseDir2); |
|
417 |
for (String file : files) { |
|
418 |
diff(bd1, bd2, file, throwErrorIfNoMatch); |
|
10 | 419 |
} |
420 |
} |
|
421 |
||
422 |
/** |
|
423 |
* Check the exit code of Javadoc and record whether the test passed |
|
424 |
* or failed. |
|
425 |
* |
|
426 |
* @param expectedExitCode The exit code that is required for the test |
|
427 |
* to pass. |
|
428 |
* @param actualExitCode The actual exit code from the previous run of |
|
429 |
* Javadoc. |
|
430 |
*/ |
|
431 |
public void checkExitCode(int expectedExitCode, int actualExitCode) { |
|
432 |
numTestsRun++; |
|
433 |
if (expectedExitCode == actualExitCode) { |
|
434 |
System.out.println( "Passed" + "\n" + " got return code " + |
|
435 |
actualExitCode); |
|
436 |
numTestsPassed++; |
|
437 |
} else { |
|
24072 | 438 |
System.out.println( "FAILED: expected return code " + |
10 | 439 |
expectedExitCode + " but got " + actualExitCode); |
440 |
} |
|
441 |
} |
|
442 |
||
443 |
/** |
|
444 |
* Print a summary of the test results. |
|
445 |
*/ |
|
446 |
protected void printSummary() { |
|
447 |
if ( numTestsRun != 0 && numTestsPassed == numTestsRun ) { |
|
448 |
// Test passed |
|
449 |
System.out.println("\n" + "All " + numTestsPassed |
|
450 |
+ " subtests passed"); |
|
451 |
} else { |
|
452 |
// Test failed |
|
453 |
throw new Error("\n" + (numTestsRun - numTestsPassed) |
|
454 |
+ " of " + (numTestsRun) |
|
24072 | 455 |
+ " subtests failed\n"); |
10 | 456 |
} |
457 |
} |
|
458 |
||
459 |
/** |
|
460 |
* Print the output stored in the buffers. |
|
461 |
*/ |
|
462 |
protected void printJavadocOutput() { |
|
463 |
System.out.println(STANDARD_OUTPUT + " : \n" + getStandardOutput()); |
|
464 |
System.err.println(ERROR_OUTPUT + " : \n" + getErrorOutput()); |
|
465 |
System.err.println(WARNING_OUTPUT + " : \n" + getWarningOutput()); |
|
466 |
System.out.println(NOTICE_OUTPUT + " : \n" + getNoticeOutput()); |
|
467 |
} |
|
468 |
||
469 |
/** |
|
470 |
* Read the file and return it as a string. |
|
471 |
* |
|
472 |
* @param fileName the name of the file to read |
|
473 |
* @return the file in string format |
|
474 |
*/ |
|
475 |
public String readFileToString(String fileName) throws Error { |
|
24072 | 476 |
return readFileToString(outputDir, fileName); |
477 |
} |
|
478 |
||
479 |
/** |
|
480 |
* Read the file and return it as a string. |
|
481 |
* |
|
482 |
* @param baseDir the directory in which to locate the file |
|
483 |
* @param fileName the name of the file to read |
|
484 |
* @return the file in string format |
|
485 |
*/ |
|
486 |
private String readFileToString(File baseDir, String fileName) throws Error { |
|
487 |
switch (fileName) { |
|
488 |
case ERROR_OUTPUT: |
|
489 |
return getErrorOutput(); |
|
490 |
case NOTICE_OUTPUT: |
|
491 |
return getNoticeOutput(); |
|
492 |
case WARNING_OUTPUT: |
|
493 |
return getWarningOutput(); |
|
494 |
case STANDARD_OUTPUT: |
|
495 |
return getStandardOutput(); |
|
10 | 496 |
} |
497 |
try { |
|
24072 | 498 |
File file = new File(baseDir, fileName); |
10 | 499 |
if ( !file.exists() ) { |
500 |
System.out.println("\n" + "FILE DOES NOT EXIST: " + fileName); |
|
501 |
} |
|
24072 | 502 |
char[] allChars; |
503 |
try (BufferedReader in = new BufferedReader(new FileReader(file))) { |
|
504 |
// Create an array of characters the size of the file |
|
505 |
allChars = new char[(int)file.length()]; |
|
506 |
// Read the characters into the allChars array |
|
507 |
in.read(allChars, 0, (int)file.length()); |
|
508 |
} |
|
10 | 509 |
|
510 |
// Convert to a string |
|
511 |
String allCharsString = new String(allChars); |
|
512 |
return allCharsString; |
|
513 |
} catch (FileNotFoundException e) { |
|
514 |
System.err.println(e); |
|
515 |
throw new Error("File not found: " + fileName); |
|
516 |
} catch (IOException e) { |
|
517 |
System.err.println(e); |
|
518 |
throw new Error("Error reading file: " + fileName); |
|
519 |
} |
|
520 |
} |
|
521 |
||
522 |
/** |
|
523 |
* Compare the two given files. |
|
524 |
* |
|
24072 | 525 |
* @param baseDir1 the directory in which to locate the first file |
526 |
* @param baseDir2 the directory in which to locate the second file |
|
527 |
* @param file the file to compare in the two base directories |
|
10 | 528 |
* @param throwErrorIFNoMatch flag to indicate whether or not to throw |
529 |
* an error if the files do not match. |
|
530 |
* @return true if the files are the same and false otherwise. |
|
531 |
*/ |
|
24072 | 532 |
private boolean diff(File baseDir1, File baseDir2, String file, |
533 |
boolean throwErrorIFNoMatch) throws Error { |
|
534 |
String file1Contents = readFileToString(baseDir1, file); |
|
535 |
String file2Contents = readFileToString(baseDir2, file); |
|
10 | 536 |
numTestsRun++; |
537 |
if (file1Contents.trim().compareTo(file2Contents.trim()) == 0) { |
|
24072 | 538 |
System.out.println("Diff successful: " + new File(baseDir1, file) + ", " + new File(baseDir2, file)); |
10 | 539 |
numTestsPassed++; |
540 |
return true; |
|
541 |
} else if (throwErrorIFNoMatch) { |
|
24072 | 542 |
throw new Error("Diff failed: " + new File(baseDir1, file) + ", " + new File(baseDir2, file)); |
10 | 543 |
} else { |
544 |
return false; |
|
545 |
} |
|
546 |
} |
|
547 |
||
548 |
/** |
|
549 |
* Search for the string in the given file and return true |
|
550 |
* if the string was found. |
|
551 |
* |
|
552 |
* @param fileString the contents of the file to search through |
|
553 |
* @param stringToFind the string to search for |
|
554 |
* @return true if the string was found |
|
555 |
*/ |
|
556 |
private boolean findString(String fileString, String stringToFind) { |
|
23971 | 557 |
return fileString.contains(stringToFind.replace("\n", NL)); |
10 | 558 |
} |
559 |
||
3771 | 560 |
|
10 | 561 |
/** |
562 |
* Return the standard output. |
|
563 |
* @return the standard output |
|
564 |
*/ |
|
565 |
public String getStandardOutput() { |
|
566 |
return standardOut.toString(); |
|
567 |
} |
|
568 |
||
569 |
/** |
|
570 |
* Return the error output. |
|
571 |
* @return the error output |
|
572 |
*/ |
|
573 |
public String getErrorOutput() { |
|
574 |
return errors.getBuffer().toString(); |
|
575 |
} |
|
576 |
||
577 |
/** |
|
578 |
* Return the notice output. |
|
579 |
* @return the notice output |
|
580 |
*/ |
|
581 |
public String getNoticeOutput() { |
|
582 |
return notices.getBuffer().toString(); |
|
583 |
} |
|
584 |
||
585 |
/** |
|
586 |
* Return the warning output. |
|
587 |
* @return the warning output |
|
588 |
*/ |
|
589 |
public String getWarningOutput() { |
|
590 |
return warnings.getBuffer().toString(); |
|
591 |
} |
|
592 |
||
593 |
/** |
|
594 |
* A utility to copy a directory from one place to another. |
|
595 |
* We may possibly want to move this to our doclet toolkit in |
|
596 |
* the near future and maintain it from there. |
|
597 |
* |
|
598 |
* @param targetDir the directory to copy. |
|
599 |
* @param destDir the destination to copy the directory to. |
|
600 |
*/ |
|
601 |
public static void copyDir(String targetDir, String destDir) { |
|
602 |
if (targetDir.endsWith("SCCS")) { |
|
603 |
return; |
|
604 |
} |
|
605 |
try { |
|
606 |
File targetDirObj = new File(targetDir); |
|
607 |
File destDirParentObj = new File(destDir); |
|
608 |
File destDirObj = new File(destDirParentObj, targetDirObj.getName()); |
|
609 |
if (! destDirParentObj.exists()) { |
|
610 |
destDirParentObj.mkdir(); |
|
611 |
} |
|
612 |
if (! destDirObj.exists()) { |
|
613 |
destDirObj.mkdir(); |
|
614 |
} |
|
615 |
String[] files = targetDirObj.list(); |
|
24072 | 616 |
for (String file : files) { |
617 |
File srcFile = new File(targetDirObj, file); |
|
618 |
File destFile = new File(destDirObj, file); |
|
10 | 619 |
if (srcFile.isFile()) { |
620 |
System.out.println("Copying " + srcFile + " to " + destFile); |
|
24072 | 621 |
copyFile(destFile, srcFile); |
10 | 622 |
} else if(srcFile.isDirectory()) { |
623 |
copyDir(srcFile.getAbsolutePath(), destDirObj.getAbsolutePath()); |
|
624 |
} |
|
625 |
} |
|
626 |
} catch (IOException exc) { |
|
627 |
throw new Error("Could not copy " + targetDir + " to " + destDir); |
|
628 |
} |
|
629 |
} |
|
630 |
||
631 |
/** |
|
632 |
* Copy source file to destination file. |
|
633 |
* |
|
24072 | 634 |
* @param destfile the destination file |
635 |
* @param srcfile the source file |
|
10 | 636 |
* @throws SecurityException |
637 |
* @throws IOException |
|
638 |
*/ |
|
639 |
public static void copyFile(File destfile, File srcfile) |
|
640 |
throws IOException { |
|
641 |
byte[] bytearr = new byte[512]; |
|
24072 | 642 |
int len; |
10 | 643 |
FileInputStream input = new FileInputStream(srcfile); |
644 |
File destDir = destfile.getParentFile(); |
|
645 |
destDir.mkdirs(); |
|
646 |
FileOutputStream output = new FileOutputStream(destfile); |
|
647 |
try { |
|
648 |
while ((len = input.read(bytearr)) != -1) { |
|
649 |
output.write(bytearr, 0, len); |
|
650 |
} |
|
24072 | 651 |
} catch (FileNotFoundException | SecurityException exc) { |
10 | 652 |
} finally { |
653 |
input.close(); |
|
654 |
output.close(); |
|
655 |
} |
|
656 |
} |
|
657 |
} |