test/tools/javadoc/doclint/DocLintTest.java

Wed, 14 Nov 2018 10:18:25 -0800

author
diazhou
date
Wed, 14 Nov 2018 10:18:25 -0800
changeset 3762
7909abb85562
parent 1558
60caf53b98e2
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8u201-b04 for changeset a7f48b9dfb82

jjg@1490 1 /*
jjg@1490 2 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
jjg@1490 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1490 4 *
jjg@1490 5 * This code is free software; you can redistribute it and/or modify it
jjg@1490 6 * under the terms of the GNU General Public License version 2 only, as
jjg@1490 7 * published by the Free Software Foundation.
jjg@1490 8 *
jjg@1490 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1490 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1490 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1490 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@1490 13 * accompanied this code).
jjg@1490 14 *
jjg@1490 15 * You should have received a copy of the GNU General Public License version
jjg@1490 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1490 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1490 18 *
jjg@1490 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1490 20 * or visit www.oracle.com if you need additional information or have any
jjg@1490 21 * questions.
jjg@1490 22 */
jjg@1490 23
jjg@1490 24 /*
jjg@1490 25 * @test
jjg@1558 26 * @bug 8004834 8007610
jjg@1490 27 * @summary Add doclint support into javadoc
jjg@1490 28 */
jjg@1490 29
jjg@1490 30 import java.io.File;
jjg@1490 31 import java.io.PrintWriter;
jjg@1490 32 import java.io.StringWriter;
jjg@1490 33 import java.net.URI;
jjg@1490 34 import java.util.Arrays;
jjg@1490 35 import java.util.Collections;
jjg@1490 36 import java.util.EnumSet;
jjg@1490 37 import java.util.List;
jjg@1490 38 import java.util.Set;
jjg@1490 39 import java.util.regex.Matcher;
jjg@1490 40 import java.util.regex.Pattern;
jjg@1490 41
jjg@1490 42 import javax.tools.Diagnostic;
jjg@1490 43 import javax.tools.DocumentationTool;
jjg@1490 44 import javax.tools.DocumentationTool.DocumentationTask;
jjg@1490 45 import javax.tools.JavaFileObject;
jjg@1490 46 import javax.tools.SimpleJavaFileObject;
jjg@1490 47 import javax.tools.StandardJavaFileManager;
jjg@1490 48 import javax.tools.StandardLocation;
jjg@1490 49 import javax.tools.ToolProvider;
jjg@1490 50 import static javax.tools.Diagnostic.Kind.*;
jjg@1490 51
jjg@1490 52 import com.sun.tools.javac.main.Main;
jjg@1490 53
jjg@1490 54 public class DocLintTest {
jjg@1490 55 public static void main(String... args) throws Exception {
jjg@1490 56 new DocLintTest().run();
jjg@1490 57 }
jjg@1490 58
jjg@1490 59 DocumentationTool javadoc;
jjg@1490 60 StandardJavaFileManager fm;
jjg@1490 61 JavaFileObject file;
jjg@1490 62
jjg@1490 63 final String code =
jjg@1490 64 /* 01 */ "/** Class comment. */\n" +
jjg@1490 65 /* 02 */ "public class Test {\n" +
jjg@1490 66 /* 03 */ " /** Method comment. */\n" +
jjg@1490 67 /* 04 */ " public void method() { }\n" +
jjg@1490 68 /* 05 */ "\n" +
jjg@1490 69 /* 06 */ " /** Syntax < error. */\n" +
jjg@1490 70 /* 07 */ " private void syntaxError() { }\n" +
jjg@1490 71 /* 08 */ "\n" +
jjg@1490 72 /* 09 */ " /** @see DoesNotExist */\n" +
jjg@1490 73 /* 10 */ " protected void referenceError() { }\n" +
jjg@1490 74 /* 11 */ "\n" +
jjg@1490 75 /* 12 */ " /** @return */\n" +
jjg@1490 76 /* 13 */ " public int emptyReturn() { return 0; }\n" +
jjg@1490 77 /* 14 */ "}\n";
jjg@1490 78
jjg@1490 79 private final String rawDiags = "-XDrawDiagnostics";
jjg@1490 80
jjg@1490 81 private enum Message {
jjg@1490 82 // doclint messages
jjg@1490 83 DL_ERR6(ERROR, "Test.java:6:16: compiler.err.proc.messager: malformed HTML"),
jjg@1490 84 DL_ERR9(ERROR, "Test.java:9:14: compiler.err.proc.messager: reference not found"),
jjg@1490 85 DL_WRN12(WARNING, "Test.java:12:9: compiler.warn.proc.messager: no description for @return"),
jjg@1490 86
jjg@1490 87 // doclint messages when -XDrawDiagnostics is not in effect
jjg@1490 88 DL_ERR9A(ERROR, "Test.java:9: error: reference not found"),
jjg@1490 89 DL_WRN12A(WARNING, "Test.java:12: warning: no description for @return"),
jjg@1490 90
jjg@1490 91 // javadoc messages about bad content: these should only appear when doclint is disabled
jjg@1490 92 JD_WRN10(WARNING, "Test.java:10: warning - Tag @see: reference not found: DoesNotExist"),
jjg@1490 93 JD_WRN13(WARNING, "Test.java:13: warning - @return tag has no arguments."),
jjg@1490 94
jjg@1490 95 // javadoc messages for bad options
jjg@1490 96 OPT_BADARG(ERROR, "javadoc: error - Invalid argument for -Xdoclint option"),
jjg@1490 97 OPT_BADQUAL(ERROR, "javadoc: error - Access qualifiers not permitted for -Xdoclint arguments");
jjg@1490 98
jjg@1490 99 final Diagnostic.Kind kind;
jjg@1490 100 final String text;
jjg@1490 101
jjg@1490 102 static Message get(String text) {
jjg@1490 103 for (Message m: values()) {
jjg@1490 104 if (m.text.equals(text))
jjg@1490 105 return m;
jjg@1490 106 }
jjg@1490 107 return null;
jjg@1490 108 }
jjg@1490 109
jjg@1490 110 Message(Diagnostic.Kind kind, String text) {
jjg@1490 111 this.kind = kind;
jjg@1490 112 this.text = text;
jjg@1490 113 }
jjg@1490 114
jjg@1490 115 @Override
jjg@1490 116 public String toString() {
jjg@1490 117 return "[" + kind + ",\"" + text + "\"]";
jjg@1490 118 }
jjg@1490 119 }
jjg@1490 120
jjg@1490 121 void run() throws Exception {
jjg@1490 122 javadoc = ToolProvider.getSystemDocumentationTool();
jjg@1490 123 fm = javadoc.getStandardFileManager(null, null, null);
jjg@1490 124 fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(".")));
jjg@1490 125 file = new SimpleJavaFileObject(URI.create("Test.java"), JavaFileObject.Kind.SOURCE) {
jjg@1490 126 @Override
jjg@1490 127 public CharSequence getCharContent(boolean ignoreEncoding) {
jjg@1490 128 return code;
jjg@1490 129 }
jjg@1490 130 };
jjg@1490 131
jjg@1490 132 test(Collections.<String>emptyList(),
jjg@1490 133 Main.Result.ERROR,
jjg@1490 134 EnumSet.of(Message.DL_ERR9A, Message.DL_WRN12A));
jjg@1490 135
jjg@1490 136 test(Arrays.asList(rawDiags),
jjg@1490 137 Main.Result.ERROR,
jjg@1490 138 EnumSet.of(Message.DL_ERR9, Message.DL_WRN12));
jjg@1490 139
jjg@1490 140 test(Arrays.asList("-Xdoclint:none"),
jjg@1490 141 Main.Result.OK,
jjg@1490 142 EnumSet.of(Message.JD_WRN10, Message.JD_WRN13));
jjg@1490 143
jjg@1490 144 test(Arrays.asList(rawDiags, "-Xdoclint"),
jjg@1490 145 Main.Result.ERROR,
jjg@1490 146 EnumSet.of(Message.DL_ERR9, Message.DL_WRN12));
jjg@1490 147
jjg@1490 148 test(Arrays.asList(rawDiags, "-Xdoclint:all/public"),
jjg@1490 149 Main.Result.ERROR,
jjg@1490 150 EnumSet.of(Message.OPT_BADQUAL));
jjg@1490 151
jjg@1490 152 test(Arrays.asList(rawDiags, "-Xdoclint:all", "-public"),
jjg@1490 153 Main.Result.OK,
jjg@1490 154 EnumSet.of(Message.DL_WRN12));
jjg@1490 155
jjg@1490 156 test(Arrays.asList(rawDiags, "-Xdoclint:syntax"),
jjg@1490 157 Main.Result.OK,
jjg@1490 158 EnumSet.of(Message.DL_WRN12));
jjg@1490 159
jjg@1558 160 test(Arrays.asList(rawDiags, "-private"),
jjg@1558 161 Main.Result.ERROR,
jjg@1558 162 EnumSet.of(Message.DL_ERR6, Message.DL_ERR9, Message.DL_WRN12));
jjg@1558 163
jjg@1490 164 test(Arrays.asList(rawDiags, "-Xdoclint:syntax", "-private"),
jjg@1490 165 Main.Result.ERROR,
jjg@1490 166 EnumSet.of(Message.DL_ERR6, Message.DL_WRN12));
jjg@1490 167
jjg@1490 168 test(Arrays.asList(rawDiags, "-Xdoclint:reference"),
jjg@1490 169 Main.Result.ERROR,
jjg@1490 170 EnumSet.of(Message.DL_ERR9));
jjg@1490 171
jjg@1490 172 test(Arrays.asList(rawDiags, "-Xdoclint:badarg"),
jjg@1490 173 Main.Result.ERROR,
jjg@1490 174 EnumSet.of(Message.OPT_BADARG));
jjg@1490 175
jjg@1490 176 if (errors > 0)
jjg@1490 177 throw new Exception(errors + " errors occurred");
jjg@1490 178 }
jjg@1490 179
jjg@1490 180 void test(List<String> opts, Main.Result expectResult, Set<Message> expectMessages) {
jjg@1490 181 System.err.println("test: " + opts);
jjg@1490 182 StringWriter sw = new StringWriter();
jjg@1490 183 PrintWriter pw = new PrintWriter(sw);
jjg@1490 184 List<JavaFileObject> files = Arrays.asList(file);
jjg@1490 185 try {
jjg@1490 186 DocumentationTask t = javadoc.getTask(pw, fm, null, null, opts, files);
jjg@1490 187 boolean ok = t.call();
jjg@1490 188 pw.close();
jjg@1490 189 String out = sw.toString().replaceAll("[\r\n]+", "\n");
jjg@1490 190 if (!out.isEmpty())
jjg@1490 191 System.err.println(out);
jjg@1490 192 if (ok && expectResult != Main.Result.OK) {
jjg@1490 193 error("Compilation succeeded unexpectedly");
jjg@1490 194 } else if (!ok && expectResult != Main.Result.ERROR) {
jjg@1490 195 error("Compilation failed unexpectedly");
jjg@1490 196 } else
jjg@1490 197 check(out, expectMessages);
jjg@1490 198 } catch (IllegalArgumentException e) {
jjg@1490 199 System.err.println(e);
jjg@1490 200 String expectOut = expectMessages.iterator().next().text;
jjg@1490 201 if (expectResult != Main.Result.CMDERR)
jjg@1490 202 error("unexpected exception caught");
jjg@1490 203 else if (!e.getMessage().equals(expectOut)) {
jjg@1490 204 error("unexpected exception message: "
jjg@1490 205 + e.getMessage()
jjg@1490 206 + " expected: " + expectOut);
jjg@1490 207 }
jjg@1490 208 }
jjg@1490 209
jjg@1490 210 // if (errors > 0)
jjg@1490 211 // throw new Error("stop");
jjg@1490 212 }
jjg@1490 213
jjg@1490 214 private void check(String out, Set<Message> expect) {
jjg@1490 215 Pattern ignore = Pattern.compile("^(Building|Constructing|Generating|Loading|Standard|Starting| ) .*");
jjg@1490 216 Pattern stats = Pattern.compile("^([1-9]+) (error|warning)(s?)");
jjg@1490 217 Set<Message> found = EnumSet.noneOf(Message.class);
jjg@1490 218 int e = 0, w = 0;
jjg@1490 219 for (String line: out.split("[\r\n]+")) {
jjg@1490 220 if (ignore.matcher(line).matches())
jjg@1490 221 continue;
jjg@1490 222
jjg@1490 223 Matcher s = stats.matcher(line);
jjg@1490 224 if (s.matches()) {
jjg@1490 225 int i = Integer.valueOf(s.group(1));
jjg@1490 226 if (s.group(2).equals("error"))
jjg@1490 227 e++;
jjg@1490 228 else
jjg@1490 229 w++;
jjg@1490 230 continue;
jjg@1490 231 }
jjg@1490 232
jjg@1490 233 Message m = Message.get(line);
jjg@1490 234 if (m == null)
jjg@1490 235 error("Unexpected line: " + line);
jjg@1490 236 else
jjg@1490 237 found.add(m);
jjg@1490 238 }
jjg@1490 239 for (Message m: expect) {
jjg@1490 240 if (!found.contains(m))
jjg@1490 241 error("expected message not found: " + m.text);
jjg@1490 242 }
jjg@1490 243 for (Message m: found) {
jjg@1490 244 if (!expect.contains(m))
jjg@1490 245 error("unexpected message found: " + m.text);
jjg@1490 246 }
jjg@1490 247 }
jjg@1490 248
jjg@1490 249 void error(String msg) {
jjg@1490 250 System.err.println("Error: " + msg);
jjg@1490 251 errors++;
jjg@1490 252 }
jjg@1490 253
jjg@1490 254 int errors;
jjg@1490 255 }

mercurial