|
| 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 | +} |
0 commit comments