test/tools/javac/doclint/DocLintTest.java

changeset 1463
67b01d295cd2
parent 0
959103a6100f
equal deleted inserted replaced
1462:573b38691a74 1463:67b01d295cd2
1 /*
2 * Copyright (c) 2012, 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 */
23
24 /*
25 * @test
26 * @bug 8004833
27 * @summary Integrate doclint support into javac
28 */
29
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.List;
37
38 import javax.tools.Diagnostic;
39 import javax.tools.JavaCompiler;
40 import javax.tools.JavaFileObject;
41 import javax.tools.SimpleJavaFileObject;
42 import javax.tools.StandardJavaFileManager;
43 import javax.tools.StandardLocation;
44 import javax.tools.ToolProvider;
45 import static javax.tools.Diagnostic.Kind.*;
46
47 import com.sun.source.util.JavacTask;
48 import com.sun.tools.javac.main.Main;
49 import java.util.EnumSet;
50 import java.util.Set;
51 import java.util.regex.Matcher;
52 import java.util.regex.Pattern;
53
54 public class DocLintTest {
55 public static void main(String... args) throws Exception {
56 new DocLintTest().run();
57 }
58
59 JavaCompiler javac;
60 StandardJavaFileManager fm;
61 JavaFileObject file;
62
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 /* 08 */ "\n" +
75 /* 09 */ " /** @return */\n" +
76 /* 10 */ " public int emptyReturn() { return 0; }\n" +
77 /* 11 */ "}\n";
78
79 final String rawDiags = "-XDrawDiagnostics";
80 private enum Message {
81 // doclint messages
82 DL_ERR6(ERROR, "Test.java:6:16: compiler.err.proc.messager: malformed HTML"),
83 DL_ERR9(ERROR, "Test.java:9:14: compiler.err.proc.messager: reference not found"),
84 DL_WRN12(WARNING, "Test.java:12:9: compiler.warn.proc.messager: no description for @return"),
85
86 OPT_BADARG(ERROR, "invalid flag: -Xdoclint:badarg");
87
88 final Diagnostic.Kind kind;
89 final String text;
90
91 static Message get(String text) {
92 for (Message m: values()) {
93 if (m.text.equals(text))
94 return m;
95 }
96 return null;
97 }
98
99 Message(Diagnostic.Kind kind, String text) {
100 this.kind = kind;
101 this.text = text;
102 }
103
104 @Override
105 public String toString() {
106 return "[" + kind + ",\"" + text + "\"]";
107 }
108 }
109 void run() throws Exception {
110 javac = ToolProvider.getSystemJavaCompiler();
111 fm = javac.getStandardFileManager(null, null, null);
112 fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File(".")));
113 file = new SimpleJavaFileObject(URI.create("Test.java"), JavaFileObject.Kind.SOURCE) {
114 @Override
115 public CharSequence getCharContent(boolean ignoreEncoding) {
116 return code;
117 }
118 };
119
120 test(Collections.<String>emptyList(),
121 Main.Result.OK,
122 EnumSet.noneOf(Message.class));
123
124 test(Arrays.asList("-Xdoclint:none"),
125 Main.Result.OK,
126 EnumSet.noneOf(Message.class));
127
128 test(Arrays.asList(rawDiags, "-Xdoclint"),
129 Main.Result.ERROR,
130 EnumSet.of(Message.DL_ERR6, Message.DL_ERR9, Message.DL_WRN12));
131
132 test(Arrays.asList(rawDiags, "-Xdoclint:all/public"),
133 Main.Result.OK,
134 EnumSet.of(Message.DL_WRN12));
135
136 test(Arrays.asList(rawDiags, "-Xdoclint:syntax"),
137 Main.Result.ERROR,
138 EnumSet.of(Message.DL_ERR6, Message.DL_WRN12));
139
140 test(Arrays.asList(rawDiags, "-Xdoclint:reference"),
141 Main.Result.ERROR,
142 EnumSet.of(Message.DL_ERR9));
143
144 test(Arrays.asList(rawDiags, "-Xdoclint:badarg"),
145 Main.Result.CMDERR,
146 EnumSet.of(Message.OPT_BADARG));
147
148 if (errors > 0)
149 throw new Exception(errors + " errors occurred");
150 }
151
152 void test(List<String> opts, Main.Result expectResult, Set<Message> expectMessages) {
153 System.err.println("test: " + opts);
154 StringWriter sw = new StringWriter();
155 PrintWriter pw = new PrintWriter(sw);
156 List<JavaFileObject> files = Arrays.asList(file);
157 try {
158 JavacTask t = (JavacTask) javac.getTask(pw, fm, null, opts, null, files);
159 boolean ok = t.call();
160 pw.close();
161 String out = sw.toString().replaceAll("[\r\n]+", "\n");
162 if (!out.isEmpty())
163 System.err.println(out);
164 if (ok && expectResult != Main.Result.OK) {
165 error("Compilation succeeded unexpectedly");
166 } else if (!ok && expectResult != Main.Result.ERROR) {
167 error("Compilation failed unexpectedly");
168 } else
169 check(out, expectMessages);
170 } catch (IllegalArgumentException e) {
171 System.err.println(e);
172 String expectOut = expectMessages.iterator().next().text;
173 if (expectResult != Main.Result.CMDERR)
174 error("unexpected exception caught");
175 else if (!e.getMessage().equals(expectOut)) {
176 error("unexpected exception message: "
177 + e.getMessage()
178 + " expected: " + expectOut);
179 }
180 }
181
182 // if (errors > 0)
183 // throw new Error("stop");
184 }
185
186 private void check(String out, Set<Message> expect) {
187 Pattern stats = Pattern.compile("^([1-9]+) (error|warning)(s?)");
188 Set<Message> found = EnumSet.noneOf(Message.class);
189 int e = 0, w = 0;
190 if (!out.isEmpty()) {
191 for (String line: out.split("[\r\n]+")) {
192 Matcher s = stats.matcher(line);
193 if (s.matches()) {
194 int i = Integer.valueOf(s.group(1));
195 if (s.group(2).equals("error"))
196 e++;
197 else
198 w++;
199 continue;
200 }
201
202 Message m = Message.get(line);
203 if (m == null)
204 error("Unexpected line: " + line);
205 else
206 found.add(m);
207 }
208 }
209 for (Message m: expect) {
210 if (!found.contains(m))
211 error("expected message not found: " + m.text);
212 }
213 for (Message m: found) {
214 if (!expect.contains(m))
215 error("unexpected message found: " + m.text);
216 }
217 }
218
219 void error(String msg) {
220 System.err.println("Error: " + msg);
221 errors++;
222 }
223
224 int errors;
225 }

mercurial