Thu, 31 Aug 2017 15:17:03 +0800
merge
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 8007610
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, "-private"),
161 Main.Result.ERROR,
162 EnumSet.of(Message.DL_ERR6, Message.DL_ERR9, Message.DL_WRN12));
164 test(Arrays.asList(rawDiags, "-Xdoclint:syntax", "-private"),
165 Main.Result.ERROR,
166 EnumSet.of(Message.DL_ERR6, Message.DL_WRN12));
168 test(Arrays.asList(rawDiags, "-Xdoclint:reference"),
169 Main.Result.ERROR,
170 EnumSet.of(Message.DL_ERR9));
172 test(Arrays.asList(rawDiags, "-Xdoclint:badarg"),
173 Main.Result.ERROR,
174 EnumSet.of(Message.OPT_BADARG));
176 if (errors > 0)
177 throw new Exception(errors + " errors occurred");
178 }
180 void test(List<String> opts, Main.Result expectResult, Set<Message> expectMessages) {
181 System.err.println("test: " + opts);
182 StringWriter sw = new StringWriter();
183 PrintWriter pw = new PrintWriter(sw);
184 List<JavaFileObject> files = Arrays.asList(file);
185 try {
186 DocumentationTask t = javadoc.getTask(pw, fm, null, null, opts, files);
187 boolean ok = t.call();
188 pw.close();
189 String out = sw.toString().replaceAll("[\r\n]+", "\n");
190 if (!out.isEmpty())
191 System.err.println(out);
192 if (ok && expectResult != Main.Result.OK) {
193 error("Compilation succeeded unexpectedly");
194 } else if (!ok && expectResult != Main.Result.ERROR) {
195 error("Compilation failed unexpectedly");
196 } else
197 check(out, expectMessages);
198 } catch (IllegalArgumentException e) {
199 System.err.println(e);
200 String expectOut = expectMessages.iterator().next().text;
201 if (expectResult != Main.Result.CMDERR)
202 error("unexpected exception caught");
203 else if (!e.getMessage().equals(expectOut)) {
204 error("unexpected exception message: "
205 + e.getMessage()
206 + " expected: " + expectOut);
207 }
208 }
210 // if (errors > 0)
211 // throw new Error("stop");
212 }
214 private void check(String out, Set<Message> expect) {
215 Pattern ignore = Pattern.compile("^(Building|Constructing|Generating|Loading|Standard|Starting| ) .*");
216 Pattern stats = Pattern.compile("^([1-9]+) (error|warning)(s?)");
217 Set<Message> found = EnumSet.noneOf(Message.class);
218 int e = 0, w = 0;
219 for (String line: out.split("[\r\n]+")) {
220 if (ignore.matcher(line).matches())
221 continue;
223 Matcher s = stats.matcher(line);
224 if (s.matches()) {
225 int i = Integer.valueOf(s.group(1));
226 if (s.group(2).equals("error"))
227 e++;
228 else
229 w++;
230 continue;
231 }
233 Message m = Message.get(line);
234 if (m == null)
235 error("Unexpected line: " + line);
236 else
237 found.add(m);
238 }
239 for (Message m: expect) {
240 if (!found.contains(m))
241 error("expected message not found: " + m.text);
242 }
243 for (Message m: found) {
244 if (!expect.contains(m))
245 error("unexpected message found: " + m.text);
246 }
247 }
249 void error(String msg) {
250 System.err.println("Error: " + msg);
251 errors++;
252 }
254 int errors;
255 }