Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javadoc/doclint/DocLintTest.java
38813 views
/*1* Copyright (c) 2012, 2013, 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 800761026* @summary Add doclint support into javadoc27*/2829import java.io.File;30import java.io.PrintWriter;31import java.io.StringWriter;32import java.net.URI;33import java.util.Arrays;34import java.util.Collections;35import java.util.EnumSet;36import java.util.List;37import java.util.Set;38import java.util.regex.Matcher;39import java.util.regex.Pattern;4041import javax.tools.Diagnostic;42import javax.tools.DocumentationTool;43import javax.tools.DocumentationTool.DocumentationTask;44import javax.tools.JavaFileObject;45import javax.tools.SimpleJavaFileObject;46import javax.tools.StandardJavaFileManager;47import javax.tools.StandardLocation;48import javax.tools.ToolProvider;49import static javax.tools.Diagnostic.Kind.*;5051import com.sun.tools.javac.main.Main;5253public class DocLintTest {54public static void main(String... args) throws Exception {55new DocLintTest().run();56}5758DocumentationTool javadoc;59StandardJavaFileManager fm;60JavaFileObject file;6162final String code =63/* 01 */ "/** Class comment. */\n" +64/* 02 */ "public class Test {\n" +65/* 03 */ " /** Method comment. */\n" +66/* 04 */ " public void method() { }\n" +67/* 05 */ "\n" +68/* 06 */ " /** Syntax < error. */\n" +69/* 07 */ " private void syntaxError() { }\n" +70/* 08 */ "\n" +71/* 09 */ " /** @see DoesNotExist */\n" +72/* 10 */ " protected void referenceError() { }\n" +73/* 11 */ "\n" +74/* 12 */ " /** @return */\n" +75/* 13 */ " public int emptyReturn() { return 0; }\n" +76/* 14 */ "}\n";7778private final String rawDiags = "-XDrawDiagnostics";7980private enum Message {81// doclint messages82DL_ERR6(ERROR, "Test.java:6:16: compiler.err.proc.messager: malformed HTML"),83DL_ERR9(ERROR, "Test.java:9:14: compiler.err.proc.messager: reference not found"),84DL_WRN12(WARNING, "Test.java:12:9: compiler.warn.proc.messager: no description for @return"),8586// doclint messages when -XDrawDiagnostics is not in effect87DL_ERR9A(ERROR, "Test.java:9: error: reference not found"),88DL_WRN12A(WARNING, "Test.java:12: warning: no description for @return"),8990// javadoc messages about bad content: these should only appear when doclint is disabled91JD_WRN10(WARNING, "Test.java:10: warning - Tag @see: reference not found: DoesNotExist"),92JD_WRN13(WARNING, "Test.java:13: warning - @return tag has no arguments."),9394// javadoc messages for bad options95OPT_BADARG(ERROR, "javadoc: error - Invalid argument for -Xdoclint option"),96OPT_BADQUAL(ERROR, "javadoc: error - Access qualifiers not permitted for -Xdoclint arguments");9798final Diagnostic.Kind kind;99final String text;100101static Message get(String text) {102for (Message m: values()) {103if (m.text.equals(text))104return m;105}106return null;107}108109Message(Diagnostic.Kind kind, String text) {110this.kind = kind;111this.text = text;112}113114@Override115public String toString() {116return "[" + kind + ",\"" + text + "\"]";117}118}119120void run() throws Exception {121javadoc = ToolProvider.getSystemDocumentationTool();122fm = javadoc.getStandardFileManager(null, null, null);123fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(".")));124file = new SimpleJavaFileObject(URI.create("Test.java"), JavaFileObject.Kind.SOURCE) {125@Override126public CharSequence getCharContent(boolean ignoreEncoding) {127return code;128}129};130131test(Collections.<String>emptyList(),132Main.Result.ERROR,133EnumSet.of(Message.DL_ERR9A, Message.DL_WRN12A));134135test(Arrays.asList(rawDiags),136Main.Result.ERROR,137EnumSet.of(Message.DL_ERR9, Message.DL_WRN12));138139test(Arrays.asList("-Xdoclint:none"),140Main.Result.OK,141EnumSet.of(Message.JD_WRN10, Message.JD_WRN13));142143test(Arrays.asList(rawDiags, "-Xdoclint"),144Main.Result.ERROR,145EnumSet.of(Message.DL_ERR9, Message.DL_WRN12));146147test(Arrays.asList(rawDiags, "-Xdoclint:all/public"),148Main.Result.ERROR,149EnumSet.of(Message.OPT_BADQUAL));150151test(Arrays.asList(rawDiags, "-Xdoclint:all", "-public"),152Main.Result.OK,153EnumSet.of(Message.DL_WRN12));154155test(Arrays.asList(rawDiags, "-Xdoclint:syntax"),156Main.Result.OK,157EnumSet.of(Message.DL_WRN12));158159test(Arrays.asList(rawDiags, "-private"),160Main.Result.ERROR,161EnumSet.of(Message.DL_ERR6, Message.DL_ERR9, Message.DL_WRN12));162163test(Arrays.asList(rawDiags, "-Xdoclint:syntax", "-private"),164Main.Result.ERROR,165EnumSet.of(Message.DL_ERR6, Message.DL_WRN12));166167test(Arrays.asList(rawDiags, "-Xdoclint:reference"),168Main.Result.ERROR,169EnumSet.of(Message.DL_ERR9));170171test(Arrays.asList(rawDiags, "-Xdoclint:badarg"),172Main.Result.ERROR,173EnumSet.of(Message.OPT_BADARG));174175if (errors > 0)176throw new Exception(errors + " errors occurred");177}178179void test(List<String> opts, Main.Result expectResult, Set<Message> expectMessages) {180System.err.println("test: " + opts);181StringWriter sw = new StringWriter();182PrintWriter pw = new PrintWriter(sw);183List<JavaFileObject> files = Arrays.asList(file);184try {185DocumentationTask t = javadoc.getTask(pw, fm, null, null, opts, files);186boolean ok = t.call();187pw.close();188String out = sw.toString().replaceAll("[\r\n]+", "\n");189if (!out.isEmpty())190System.err.println(out);191if (ok && expectResult != Main.Result.OK) {192error("Compilation succeeded unexpectedly");193} else if (!ok && expectResult != Main.Result.ERROR) {194error("Compilation failed unexpectedly");195} else196check(out, expectMessages);197} catch (IllegalArgumentException e) {198System.err.println(e);199String expectOut = expectMessages.iterator().next().text;200if (expectResult != Main.Result.CMDERR)201error("unexpected exception caught");202else if (!e.getMessage().equals(expectOut)) {203error("unexpected exception message: "204+ e.getMessage()205+ " expected: " + expectOut);206}207}208209// if (errors > 0)210// throw new Error("stop");211}212213private void check(String out, Set<Message> expect) {214Pattern ignore = Pattern.compile("^(Building|Constructing|Generating|Loading|Standard|Starting| ) .*");215Pattern stats = Pattern.compile("^([1-9]+) (error|warning)(s?)");216Set<Message> found = EnumSet.noneOf(Message.class);217int e = 0, w = 0;218for (String line: out.split("[\r\n]+")) {219if (ignore.matcher(line).matches())220continue;221222Matcher s = stats.matcher(line);223if (s.matches()) {224int i = Integer.valueOf(s.group(1));225if (s.group(2).equals("error"))226e++;227else228w++;229continue;230}231232Message m = Message.get(line);233if (m == null)234error("Unexpected line: " + line);235else236found.add(m);237}238for (Message m: expect) {239if (!found.contains(m))240error("expected message not found: " + m.text);241}242for (Message m: found) {243if (!expect.contains(m))244error("unexpected message found: " + m.text);245}246}247248void error(String msg) {249System.err.println("Error: " + msg);250errors++;251}252253int errors;254}255256257