Skip to content

Commit 0fb9469

Browse files
8290126: Add a check in JavadocTester for "javadoc should not crash"
Reviewed-by: prappo
1 parent 0a4d0ce commit 0fb9469

File tree

5 files changed

+148
-10
lines changed

5 files changed

+148
-10
lines changed

test/langtools/jdk/javadoc/doclet/InheritDocForUserTags/DocTest.java

-4
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@ public void test() {
5151
testSrc("DocTest.java")
5252
);
5353
checkExit(Exit.OK);
54-
55-
// javadoc does not report an exit code for an internal exception (!)
56-
// so monitor stderr for stack dumps.
57-
checkOutput(Output.STDERR, false, "at com.sun");
5854
}
5955

6056
/**

test/langtools/jdk/javadoc/lib/javadoc/tester/JavadocTester.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public enum DirectoryCheck {
214214
NONE(null) { @Override void check(Path dir) { } };
215215

216216
/** The filter used to detect that files should <i>not</i> be present. */
217-
DirectoryStream.Filter<Path> filter;
217+
private final DirectoryStream.Filter<Path> filter;
218218

219219
DirectoryCheck(DirectoryStream.Filter<Path> f) {
220220
filter = f;
@@ -246,6 +246,7 @@ void check(Path dir) {
246246
private boolean automaticCheckAccessibility = true;
247247
private boolean automaticCheckLinks = true;
248248
private boolean automaticCheckUniqueOUT = true;
249+
private boolean automaticCheckNoStacktrace = true;
249250
private boolean useStandardStreams = false;
250251
private StandardJavaFileManager fileManager = null;
251252

@@ -488,6 +489,11 @@ public void javadoc(String... args) {
488489
}
489490
});
490491

492+
if (automaticCheckNoStacktrace) {
493+
// Any stacktrace will have javadoc near the bottom of the stack
494+
checkOutput(Output.STDERR, false, "at jdk.javadoc/jdk.javadoc.internal.");
495+
}
496+
491497
if (exitCode == Exit.OK.code && Files.exists(outputDir)) {
492498
if (automaticCheckLinks) {
493499
checkLinks();
@@ -533,6 +539,13 @@ public void setAutomaticCheckUniqueOUT(boolean b) {
533539
automaticCheckUniqueOUT = b;
534540
}
535541

542+
/**
543+
* Sets whether or not to check for stacktraces.
544+
*/
545+
public void setAutomaticCheckNoStacktrace(boolean b) {
546+
automaticCheckNoStacktrace = b;
547+
}
548+
536549
/**
537550
* Sets whether to use standard output streams (stdout and stderr)
538551
* instead of a single temporary stream.
@@ -1147,7 +1160,7 @@ public OutputChecker setAllowOverlaps(boolean allowOverlaps) {
11471160
public OutputChecker check(String... strings) {
11481161
if (name == null) {
11491162
out.println("Skipping checks for:" + NL
1150-
+ List.of(strings).stream()
1163+
+ Stream.of(strings)
11511164
.map(s -> " " + toShortString(s))
11521165
.collect(Collectors.joining(NL)));
11531166
return this;
@@ -1169,7 +1182,7 @@ public OutputChecker check(String... strings) {
11691182
public OutputChecker check(Pattern... patterns) {
11701183
if (name == null) {
11711184
out.println("Skipping checks for:" + NL
1172-
+ List.of(patterns).stream()
1185+
+ Stream.of(patterns)
11731186
.map(p -> " " + toShortString(p.pattern()))
11741187
.collect(Collectors.joining(NL)));
11751188
return this;

test/langtools/jdk/javadoc/testJavadocTester/TestJavadocTester.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static void main(String... args) throws Exception {
6363
tester.setup().runTests();
6464
}
6565

66-
private final List<String> messages = new ArrayList<>();
66+
protected final List<String> messages = new ArrayList<>();
6767
private int testErrors = 0;
6868

6969
/**
@@ -158,7 +158,7 @@ private void report(String message) {
158158

159159
//-------------------------------------------------
160160

161-
private final ToolBox tb = new ToolBox();
161+
protected final ToolBox tb = new ToolBox();
162162

163163
TestJavadocTester setup() throws IOException {
164164
Path src = Path.of("src");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright (c) 2022, 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+
*/
23+
24+
25+
/*
26+
* @test
27+
* @bug 8290126
28+
* @summary Add a check in JavadocTester for "javadoc should not crash"
29+
* @library /tools/lib/ ../lib
30+
* @modules jdk.javadoc/jdk.javadoc.internal.tool
31+
* @build toolbox.ToolBox javadoc.tester.* TestJavadocTester
32+
* @run main TestJavadocTesterCrash
33+
*/
34+
35+
import com.sun.source.doctree.DocTree;
36+
import jdk.javadoc.doclet.Taglet;
37+
38+
import javax.lang.model.element.Element;
39+
import java.nio.file.Path;
40+
import java.util.EnumSet;
41+
import java.util.List;
42+
import java.util.Set;
43+
44+
/**
45+
* Tests that {@code JavadocTester} detects and reports exceptions.
46+
*
47+
* It is not a direct test of the javadoc tool or the output generated by the
48+
* Standard Doclet, although both are indirectly used as part of this test.
49+
*
50+
* The test uses the infrastructure of TestJavadocTester, but cannot be
51+
* added to the tests there, which rely on checking aspects of the output
52+
* from a single run on javadoc. This test forces a crash to occur in
53+
* javadoc, and verifies that JavadocTester detects and reports the crash.
54+
*
55+
* Arguably, a crash in a user-provided taglet should not cause a full stack
56+
* trace. If that is ever fixed, we would need to revisit the goals and mechanism
57+
* of this test.
58+
*/
59+
public class TestJavadocTesterCrash extends TestJavadocTester {
60+
public static void main(String... args) throws Exception {
61+
TestJavadocTesterCrash tester = new TestJavadocTesterCrash();
62+
tester.runTests();
63+
}
64+
65+
/** A taglet that can throw an exception. */
66+
public static class TestTaglet implements Taglet {
67+
public TestTaglet() { }
68+
69+
@Override
70+
public Set<Location> getAllowedLocations() {
71+
return EnumSet.allOf(Location.class);
72+
}
73+
74+
@Override
75+
public boolean isInlineTag() {
76+
return true;
77+
}
78+
79+
@Override
80+
public String getName() {
81+
return "test-taglet";
82+
}
83+
84+
@Override
85+
public String toString(List<? extends DocTree> tags, Element element) {
86+
String s = tags.toString();
87+
if (s.contains("test")) {
88+
throw new Error("demo error");
89+
};
90+
return s;
91+
}
92+
}
93+
94+
@Test
95+
public void testDetectException(Path base) throws Exception {
96+
messages.clear();
97+
Path src = base.resolve("src");
98+
tb.writeJavaFiles(src,
99+
"""
100+
package p;
101+
/** Comment.
102+
abc {@test-taglet simple} {@test-taglet test} xyz
103+
*/
104+
public class C { }""");
105+
javadoc("-d", base.resolve("api").toString(),
106+
"-tagletpath", System.getProperty("test.class.path"),
107+
"-taglet", TestTaglet.class.getName(),
108+
"-sourcepath", src.toString(),
109+
"p");
110+
checkExit(Exit.ERROR);
111+
112+
// verify that the taglet threw an exception as intended
113+
new OutputChecker(Output.OUT)
114+
.setExpectOrdered(true)
115+
.check("Generating testDetectException/api/p/C.html...",
116+
"error: An internal exception has occurred.",
117+
"(java.lang.Error: demo error)",
118+
"1 error");
119+
120+
// verify that JavadocTester detected the crash
121+
checkMessages("FAILED: STDERR: following text found:");
122+
}
123+
}

test/langtools/jdk/javadoc/testTFMBuilder/TestTFMBuilder.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -161,6 +161,7 @@ public void testFileManagerAccess(Path base) throws Exception {
161161

162162
try {
163163
setFileManager(tfm);
164+
setAutomaticCheckNoStacktrace(false);
164165
javadoc("-d", base.resolve("api").toString(),
165166
"-sourcepath", srcDir.toString(),
166167
"p");
@@ -173,6 +174,7 @@ public void testFileManagerAccess(Path base) throws Exception {
173174
.replace("##EXC##", TestException.class.getName()));
174175
} finally {
175176
setFileManager(null);
177+
setAutomaticCheckNoStacktrace(true);
176178
}
177179
}
178180
}
@@ -190,6 +192,7 @@ public void testFileObjectRead(Path base) throws Exception {
190192

191193
try {
192194
setFileManager(tfm);
195+
setAutomaticCheckNoStacktrace(false);
193196
javadoc("-d", base.resolve("api").toString(),
194197
"-sourcepath", srcDir.toString(),
195198
"p");
@@ -203,6 +206,7 @@ public void testFileObjectRead(Path base) throws Exception {
203206
.replace("##FILE##", srcDir.resolve("p").resolve("C.java").toString()));
204207
} finally {
205208
setFileManager(null);
209+
setAutomaticCheckNoStacktrace(true);
206210
}
207211
}
208212
}
@@ -222,6 +226,7 @@ public void testFileObjectWrite(Path base) throws Exception {
222226

223227
try {
224228
setFileManager(tfm);
229+
setAutomaticCheckNoStacktrace(false);
225230
javadoc("-d", outDir.toString(),
226231
"-sourcepath", srcDir.toString(),
227232
"p");
@@ -236,6 +241,7 @@ public void testFileObjectWrite(Path base) throws Exception {
236241
.replace("##FILE##", outDir.resolve("p").resolve("C.html").toString()));
237242
} finally {
238243
setFileManager(null);
244+
setAutomaticCheckNoStacktrace(true);
239245
}
240246
}
241247
}

0 commit comments

Comments
 (0)