Thu, 10 Jan 2013 19:38:57 -0800
8004834: Add doclint support into javadoc
Reviewed-by: darcy
1 /*
2 * Copyright (c) 2012, 2013, 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 */
24 /*
25 * @test
26 * @bug 8004834
27 * @summary Add doclint support into javadoc
28 */
30 import java.io.File;
31 import java.io.PrintWriter;
32 import java.io.StringWriter;
33 import java.net.URI;
34 import java.util.Arrays;
35 import java.util.Collections;
36 import java.util.EnumSet;
37 import java.util.List;
38 import java.util.Set;
39 import java.util.regex.Matcher;
40 import java.util.regex.Pattern;
42 import javax.tools.Diagnostic;
43 import javax.tools.DocumentationTool;
44 import javax.tools.DocumentationTool.DocumentationTask;
45 import javax.tools.JavaFileObject;
46 import javax.tools.SimpleJavaFileObject;
47 import javax.tools.StandardJavaFileManager;
48 import javax.tools.StandardLocation;
49 import javax.tools.ToolProvider;
50 import static javax.tools.Diagnostic.Kind.*;
52 import com.sun.tools.javac.main.Main;
54 public class DocLintTest {
55 public static void main(String... args) throws Exception {
56 new DocLintTest().run();
57 }
59 DocumentationTool javadoc;
60 StandardJavaFileManager fm;
61 JavaFileObject file;
63 final 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 */ " private void syntaxError() { }\n" +
71 /* 08 */ "\n" +
72 /* 09 */ " /** @see DoesNotExist */\n" +
73 /* 10 */ " protected void referenceError() { }\n" +
74 /* 11 */ "\n" +
75 /* 12 */ " /** @return */\n" +
76 /* 13 */ " public int emptyReturn() { return 0; }\n" +
77 /* 14 */ "}\n";
79 private final String rawDiags = "-XDrawDiagnostics";
81 private enum Message {
82 // doclint messages
83 DL_ERR6(ERROR, "Test.java:6:16: compiler.err.proc.messager: malformed HTML"),
84 DL_ERR9(ERROR, "Test.java:9:14: compiler.err.proc.messager: reference not found"),
85 DL_WRN12(WARNING, "Test.java:12:9: compiler.warn.proc.messager: no description for @return"),
87 // doclint messages when -XDrawDiagnostics is not in effect
88 DL_ERR9A(ERROR, "Test.java:9: error: reference not found"),
89 DL_WRN12A(WARNING, "Test.java:12: warning: no description for @return"),
91 // javadoc messages about bad content: these should only appear when doclint is disabled
92 JD_WRN10(WARNING, "Test.java:10: warning - Tag @see: reference not found: DoesNotExist"),
93 JD_WRN13(WARNING, "Test.java:13: warning - @return tag has no arguments."),
95 // javadoc messages for bad options
96 OPT_BADARG(ERROR, "javadoc: error - Invalid argument for -Xdoclint option"),
97 OPT_BADQUAL(ERROR, "javadoc: error - Access qualifiers not permitted for -Xdoclint arguments");
99 final Diagnostic.Kind kind;
100 final String text;
102 static Message get(String text) {
103 for (Message m: values()) {
104 if (m.text.equals(text))
105 return m;
106 }
107 return null;
108 }
110 Message(Diagnostic.Kind kind, String text) {
111 this.kind = kind;
112 this.text = text;
113 }
115 @Override
116 public String toString() {
117 return "[" + kind + ",\"" + text + "\"]";
118 }
119 }
121 void run() throws Exception {
122 javadoc = ToolProvider.getSystemDocumentationTool();
123 fm = javadoc.getStandardFileManager(null, null, null);
124 fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(".")));
125 file = new SimpleJavaFileObject(URI.create("Test.java"), JavaFileObject.Kind.SOURCE) {
126 @Override
127 public CharSequence getCharContent(boolean ignoreEncoding) {
128 return code;
129 }
130 };
132 test(Collections.<String>emptyList(),
133 Main.Result.ERROR,
134 EnumSet.of(Message.DL_ERR9A, Message.DL_WRN12A));
136 test(Arrays.asList(rawDiags),
137 Main.Result.ERROR,
138 EnumSet.of(Message.DL_ERR9, Message.DL_WRN12));
140 test(Arrays.asList("-Xdoclint:none"),
141 Main.Result.OK,
142 EnumSet.of(Message.JD_WRN10, Message.JD_WRN13));
144 test(Arrays.asList(rawDiags, "-Xdoclint"),
145 Main.Result.ERROR,
146 EnumSet.of(Message.DL_ERR9, Message.DL_WRN12));
148 test(Arrays.asList(rawDiags, "-Xdoclint:all/public"),
149 Main.Result.ERROR,
150 EnumSet.of(Message.OPT_BADQUAL));
152 test(Arrays.asList(rawDiags, "-Xdoclint:all", "-public"),
153 Main.Result.OK,
154 EnumSet.of(Message.DL_WRN12));
156 test(Arrays.asList(rawDiags, "-Xdoclint:syntax"),
157 Main.Result.OK,
158 EnumSet.of(Message.DL_WRN12));
160 test(Arrays.asList(rawDiags, "-Xdoclint:syntax", "-private"),
161 Main.Result.ERROR,
162 EnumSet.of(Message.DL_ERR6, Message.DL_WRN12));
164 test(Arrays.asList(rawDiags, "-Xdoclint:reference"),
165 Main.Result.ERROR,
166 EnumSet.of(Message.DL_ERR9));
168 test(Arrays.asList(rawDiags, "-Xdoclint:badarg"),
169 Main.Result.ERROR,
170 EnumSet.of(Message.OPT_BADARG));
172 if (errors > 0)
173 throw new Exception(errors + " errors occurred");
174 }
176 void test(List<String> opts, Main.Result expectResult, Set<Message> expectMessages) {
177 System.err.println("test: " + opts);
178 StringWriter sw = new StringWriter();
179 PrintWriter pw = new PrintWriter(sw);
180 List<JavaFileObject> files = Arrays.asList(file);
181 try {
182 DocumentationTask t = javadoc.getTask(pw, fm, null, null, opts, files);
183 boolean ok = t.call();
184 pw.close();
185 String out = sw.toString().replaceAll("[\r\n]+", "\n");
186 if (!out.isEmpty())
187 System.err.println(out);
188 if (ok && expectResult != Main.Result.OK) {
189 error("Compilation succeeded unexpectedly");
190 } else if (!ok && expectResult != Main.Result.ERROR) {
191 error("Compilation failed unexpectedly");
192 } else
193 check(out, expectMessages);
194 } catch (IllegalArgumentException e) {
195 System.err.println(e);
196 String expectOut = expectMessages.iterator().next().text;
197 if (expectResult != Main.Result.CMDERR)
198 error("unexpected exception caught");
199 else if (!e.getMessage().equals(expectOut)) {
200 error("unexpected exception message: "
201 + e.getMessage()
202 + " expected: " + expectOut);
203 }
204 }
206 // if (errors > 0)
207 // throw new Error("stop");
208 }
210 private void check(String out, Set<Message> expect) {
211 Pattern ignore = Pattern.compile("^(Building|Constructing|Generating|Loading|Standard|Starting| ) .*");
212 Pattern stats = Pattern.compile("^([1-9]+) (error|warning)(s?)");
213 Set<Message> found = EnumSet.noneOf(Message.class);
214 int e = 0, w = 0;
215 for (String line: out.split("[\r\n]+")) {
216 if (ignore.matcher(line).matches())
217 continue;
219 Matcher s = stats.matcher(line);
220 if (s.matches()) {
221 int i = Integer.valueOf(s.group(1));
222 if (s.group(2).equals("error"))
223 e++;
224 else
225 w++;
226 continue;
227 }
229 Message m = Message.get(line);
230 if (m == null)
231 error("Unexpected line: " + line);
232 else
233 found.add(m);
234 }
235 for (Message m: expect) {
236 if (!found.contains(m))
237 error("expected message not found: " + m.text);
238 }
239 for (Message m: found) {
240 if (!expect.contains(m))
241 error("unexpected message found: " + m.text);
242 }
243 }
245 void error(String msg) {
246 System.err.println("Error: " + msg);
247 errors++;
248 }
250 int errors;
251 }