Path: blob/master/test/langtools/tools/doclint/EmptyHtmlTest.java
41144 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/222324/*25* @test26* @bug 824671227* @summary doclint incorrectly reports some HTML elements as empty28* @modules jdk.javadoc/jdk.javadoc.internal.doclint29* @library /tools/lib30* @build toolbox.TestRunner toolbox.ToolBox31* @run main EmptyHtmlTest32*/3334import java.io.PrintWriter;35import java.io.StringWriter;36import java.lang.reflect.Method;37import java.nio.file.Files;38import java.nio.file.Path;39import java.util.List;4041import com.sun.source.doctree.DocTreeVisitor;42import com.sun.source.doctree.InlineTagTree;43import jdk.javadoc.internal.doclint.DocLint;44import toolbox.TestRunner;45import toolbox.ToolBox;4647public class EmptyHtmlTest extends TestRunner {4849public static void main(String... args) throws Exception {50EmptyHtmlTest t = new EmptyHtmlTest();51t.runTests(m -> new Object[] { Path.of(m.getName()) });52}5354public EmptyHtmlTest() {55super(System.err);56}5758ToolBox tb = new ToolBox();5960/**61* This test is intended to be future-proof, and hence detect any62* problems in any inline tags added in the future.63* Since there is not yet any mapping between DocTree.Kind and64* the corresponding subtype DocTree (see javac Tree.Kind, Tree)65* the list of all current inline tag classes is determined by66* scanning DocTreeVisitor.67*68* @param base working directory for the test case69* @throws Exception if an error occurs70*/71@Test72public void testInlines(Path base) throws Exception {73Class<DocTreeVisitor> c = DocTreeVisitor.class;74for (Method m : c.getDeclaredMethods()) {75if (m.getName().startsWith("visit") && m.getParameterCount() == 2) {76Class<?>[] paramTypes = m.getParameterTypes();77Class<?> firstParamType = paramTypes[0];78if (InlineTagTree.class.isAssignableFrom(firstParamType)) {79testInline(base, firstParamType);80}81}82}83}8485void testInline(Path base, Class<?> type) throws Exception {86// the following can eventually be converted to instanceof pattern switch87Path d = Files.createDirectories(base.resolve(type.getSimpleName()));88switch (type.getSimpleName()) {89case "DocRootTree" ->90test(d, type, "{@docRoot}");9192case "IndexTree" ->93test(d, type, "{@index Object}");9495case "InheritDocTree" ->96test(d, type, "{@inheritDoc}");9798case "LinkTree" ->99test(d, type, "{@link Object}");100101case "LiteralTree" ->102test(d, type, "{@literal abc}");103104case "ReturnTree" ->105test(d, type, "{@return abc}");106107case "SummaryTree" ->108test(d, type, "{@summary First sentence.}");109110case "SystemPropertyTree" ->111test(d, type, "{@systemProperty file.separator}");112113case "UnknownInlineTagTree" ->114test(d, type, "{@unknown}");115116case "ValueTree" ->117test(d, type, "{@value Math.PI}");118119default ->120error("no test case provided for " + type);121}122}123124void test(Path base, Class<?> type, String tag) throws Exception {125System.err.println("test " + type.getSimpleName() + " " + tag);126Path src = base.resolve("src");127String text = """128/**129* This is a comment.130* <b>INSERT</b>131*/132public class C { }133""".replace("INSERT", tag);134tb.writeJavaFiles(src, text);135136List<String> cmdArgs = List.of(137"-Xmsgs:html",138"-XcustomTags:unknown",139src.resolve("C.java").toString()140);141142StringWriter sw = new StringWriter();143try (PrintWriter pw = new PrintWriter(sw)) {144new DocLint().run(pw, cmdArgs.toArray(new String[0]));145}146String log = sw.toString();147if (!log.isEmpty()) {148System.err.println("output:");149log.lines().forEach(System.err::println);150error("Unexpected output from doclint");151}152}153}154155156