Path: blob/master/test/langtools/jdk/javadoc/tool/doclint/DocLintTest.java
40974 views
/*1* Copyright (c) 2012, 2021, 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*/2223/*24* @test25* @bug 8004834 8007610 8129909 8182765 824781526* @summary Add doclint support into javadoc27* @modules jdk.compiler/com.sun.tools.javac.main28*/2930import java.io.File;31import java.io.PrintWriter;32import java.io.StringWriter;33import java.net.URI;34import java.util.Arrays;35import java.util.Collections;36import java.util.EnumSet;37import java.util.List;38import java.util.Set;39import java.util.regex.Matcher;40import java.util.regex.Pattern;4142import javax.tools.Diagnostic;43import javax.tools.DocumentationTool;44import javax.tools.DocumentationTool.DocumentationTask;45import javax.tools.JavaFileObject;46import javax.tools.SimpleJavaFileObject;47import javax.tools.StandardJavaFileManager;48import javax.tools.StandardLocation;49import javax.tools.ToolProvider;50import static javax.tools.Diagnostic.Kind.*;5152import com.sun.tools.javac.main.Main;5354public class DocLintTest {55public static void main(String... args) throws Exception {56new DocLintTest().run();57}5859DocumentationTool javadoc;60StandardJavaFileManager fm;61Iterable<? extends JavaFileObject> files;6263final String code =64/* 01 */ "/** Class comment. */\n" +65/* 02 */ "public class Test {\n" +66/* 03 */ " /** Method comment. */\n" +67/* 04 */ " public void method() { }\n" +68/* 05 */ "\n" +69/* 06 */ " /** Syntax < error. */\n" +70/* 07 */ """71\s private void syntaxError() { }72""" +73/* 08 */ "\n" +74/* 09 */ " /** @see DoesNotExist */\n" +75/* 10 */ """76\s protected void referenceError() { }77""" +78/* 11 */ "\n" +79/* 12 */ " /** @return */\n" +80/* 13 */ """81\s public int emptyReturn() { return 0; }82""" +83/* 14 */ "}\n";8485final String p1Code =86/* 01 */ "package p1;\n" +87/* 02 */ "public class P1Test {\n" +88/* 03 */ " /** Syntax < error. */\n" +89/* 04 */ " public void method() { }\n" +90/* 05 */ "}\n";9192final String p2Code =93/* 01 */ "package p2;\n" +94/* 02 */ "public class P2Test {\n" +95/* 03 */ " /** Syntax < error. */\n" +96/* 04 */ " public void method() { }\n" +97/* 05 */ "}\n";9899private final String rawDiags = "-XDrawDiagnostics";100private final String htmlVersion = "-html5";101102private enum Message {103// doclint messages104DL_ERR6(ERROR, "Test.java:6:16: compiler.err.proc.messager: malformed HTML"),105DL_ERR9(ERROR, "Test.java:9:14: compiler.err.proc.messager: reference not found"),106DL_WRN12(WARNING, "Test.java:12:9: compiler.warn.proc.messager: no description for @return"),107108DL_ERR_P1TEST(ERROR, "P1Test.java:3:16: compiler.err.proc.messager: malformed HTML"),109DL_ERR_P2TEST(ERROR, "P2Test.java:3:16: compiler.err.proc.messager: malformed HTML"),110DL_WARN_P1TEST(WARNING, "P1Test.java:2:8: compiler.warn.proc.messager: no comment"),111DL_WARN_P2TEST(WARNING, "P2Test.java:2:8: compiler.warn.proc.messager: no comment"),112113// doclint messages when -XDrawDiagnostics is not in effect114DL_ERR9A(ERROR, "Test.java:9: error: reference not found"),115DL_WRN12A(WARNING, "Test.java:12: warning: no description for @return"),116117// javadoc messages about bad content: these should only appear when doclint is disabled118JD_WRN10(WARNING, "Test.java:10: warning: Tag @see: reference not found: DoesNotExist"),119JD_WRN13(WARNING, "Test.java:13: warning: @return tag has no arguments."),120121// javadoc messages for bad options122OPT_BADARG(ERROR, "error: Invalid argument for -Xdoclint option"),123OPT_BADQUAL(ERROR, "error: Access qualifiers not permitted for -Xdoclint arguments"),124OPT_BADPACKAGEARG(ERROR, "error: Invalid argument for -Xdoclint/package option");125126final Diagnostic.Kind kind;127final String text;128129static Message get(String text) {130for (Message m: values()) {131if (m.text.equals(text))132return m;133}134return null;135}136137Message(Diagnostic.Kind kind, String text) {138this.kind = kind;139this.text = text;140}141142@Override143public String toString() {144return "[" + kind + ",\"" + text + "\"]";145}146}147148void run() throws Exception {149javadoc = ToolProvider.getSystemDocumentationTool();150fm = javadoc.getStandardFileManager(null, null, null);151try {152fm.setLocation(StandardLocation.CLASS_OUTPUT, List.of(new File(".")));153fm.setLocation(StandardLocation.CLASS_PATH, Collections.<File>emptyList());154files = List.of(new TestJFO("Test.java", code));155156test(List.of(htmlVersion),157Main.Result.ERROR,158EnumSet.of(Message.DL_ERR9A, Message.DL_WRN12A));159160test(List.of(htmlVersion, rawDiags),161Main.Result.ERROR,162EnumSet.of(Message.DL_ERR9, Message.DL_WRN12));163164// test(List.of("-Xdoclint:none"),165// Main.Result.OK,166// EnumSet.of(Message.JD_WRN10, Message.JD_WRN13));167168test(List.of(htmlVersion, rawDiags, "-Xdoclint"),169Main.Result.ERROR,170EnumSet.of(Message.DL_ERR9, Message.DL_WRN12));171172test(List.of(htmlVersion, rawDiags, "-Xdoclint:all/public"),173Main.Result.ERROR,174EnumSet.of(Message.OPT_BADQUAL));175176test(List.of(htmlVersion, rawDiags, "-Xdoclint:all", "-public"),177Main.Result.OK,178EnumSet.of(Message.DL_WRN12));179180test(List.of(htmlVersion, rawDiags, "-Xdoclint:missing"),181Main.Result.OK,182EnumSet.of(Message.DL_WRN12));183184test(List.of(htmlVersion, rawDiags, "-private"),185Main.Result.ERROR,186EnumSet.of(Message.DL_ERR6, Message.DL_ERR9, Message.DL_WRN12));187188test(List.of(htmlVersion, rawDiags, "-Xdoclint:missing,syntax", "-private"),189Main.Result.ERROR,190EnumSet.of(Message.DL_ERR6, Message.DL_WRN12));191192test(List.of(htmlVersion, rawDiags, "-Xdoclint:reference"),193Main.Result.ERROR,194EnumSet.of(Message.DL_ERR9));195196test(List.of(htmlVersion, rawDiags, "-Xdoclint:badarg"),197Main.Result.ERROR,198EnumSet.of(Message.OPT_BADARG));199200files = List.of(new TestJFO("p1/P1Test.java", p1Code),201new TestJFO("p2/P2Test.java", p2Code));202203test(List.of(htmlVersion, rawDiags),204Main.Result.ERROR,205EnumSet.of(Message.DL_ERR_P1TEST, Message.DL_ERR_P2TEST,206Message.DL_WARN_P1TEST, Message.DL_WARN_P2TEST));207208test(List.of(htmlVersion, rawDiags, "-Xdoclint/package:p1"),209Main.Result.ERROR,210EnumSet.of(Message.DL_ERR_P1TEST,211Message.DL_WARN_P1TEST));212213test(List.of(htmlVersion, rawDiags, "-Xdoclint/package:*p"),214Main.Result.ERROR,215EnumSet.of(Message.OPT_BADPACKAGEARG));216217if (errors > 0)218throw new Exception(errors + " errors occurred");219} finally {220fm.close();221}222}223224void test(List<String> opts, Main.Result expectResult, Set<Message> expectMessages) {225System.err.println("test: " + opts);226StringWriter sw = new StringWriter();227PrintWriter pw = new PrintWriter(sw);228try {229DocumentationTask t = javadoc.getTask(pw, fm, null, null, opts, files);230boolean ok = t.call();231pw.close();232String out = sw.toString().replaceAll("[\r\n]+", "\n");233if (!out.isEmpty())234System.err.println(out);235if (ok && expectResult != Main.Result.OK) {236error("Compilation succeeded unexpectedly");237} else if (!ok && expectResult != Main.Result.ERROR) {238error("Compilation failed unexpectedly");239} else240check(out, expectMessages);241} catch (IllegalArgumentException e) {242System.err.println(e);243String expectOut = expectMessages.iterator().next().text;244if (expectResult != Main.Result.CMDERR)245error("unexpected exception caught");246else if (!e.getMessage().equals(expectOut)) {247error("unexpected exception message: "248+ e.getMessage()249+ " expected: " + expectOut);250}251}252253// if (errors > 0)254// throw new Error("stop");255}256257private void check(String out, Set<Message> expect) {258Pattern ignore = Pattern.compile("^(Building|Constructing|Generating|Loading|Standard|Starting| ) .*");259Pattern stats = Pattern.compile("^([1-9]+) (error|warning)(s?)");260Set<Message> found = EnumSet.noneOf(Message.class);261int e = 0, w = 0;262for (String line: out.split("[\r\n]+")) {263if (ignore.matcher(line).matches())264continue;265266Matcher s = stats.matcher(line);267if (s.matches()) {268int i = Integer.valueOf(s.group(1));269if (s.group(2).equals("error"))270e++;271else272w++;273continue;274}275276Message m = Message.get(line);277if (m == null)278error("Unexpected line: " + line);279else280found.add(m);281}282for (Message m: expect) {283if (!found.contains(m))284error("expected message not found: " + m.text);285}286for (Message m: found) {287if (!expect.contains(m))288error("unexpected message found: " + m.text);289}290}291292void error(String msg) {293System.err.println("Error: " + msg);294errors++;295}296297int errors;298299class TestJFO extends SimpleJavaFileObject {300301private final String content;302303public TestJFO(String fileName, String content) {304super(URI.create(fileName), JavaFileObject.Kind.SOURCE);305this.content = content;306}307308@Override309public CharSequence getCharContent(boolean ignoreEncoding) {310return content;311}312};313}314315316