author | jjg |
Wed, 29 Oct 2014 17:25:23 -0700 | |
changeset 27319 | 030080f03e4f |
parent 15713 | 55f55d4f5f9f |
child 30730 | d3ce7619db2c |
permissions | -rw-r--r-- |
15354 | 1 |
/* |
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
2 |
* Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved. |
15354 | 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 |
|
15713 | 26 |
* @bug 8004834 8007610 |
15354 | 27 |
* @summary Add doclint support into javadoc |
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.EnumSet; |
|
37 |
import java.util.List; |
|
38 |
import java.util.Set; |
|
39 |
import java.util.regex.Matcher; |
|
40 |
import java.util.regex.Pattern; |
|
41 |
||
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.*; |
|
51 |
||
52 |
import com.sun.tools.javac.main.Main; |
|
53 |
||
54 |
public class DocLintTest { |
|
55 |
public static void main(String... args) throws Exception { |
|
56 |
new DocLintTest().run(); |
|
57 |
} |
|
58 |
||
59 |
DocumentationTool javadoc; |
|
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 |
/* 11 */ "\n" + |
|
75 |
/* 12 */ " /** @return */\n" + |
|
76 |
/* 13 */ " public int emptyReturn() { return 0; }\n" + |
|
77 |
/* 14 */ "}\n"; |
|
78 |
||
79 |
private final String rawDiags = "-XDrawDiagnostics"; |
|
80 |
||
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"), |
|
86 |
||
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"), |
|
90 |
||
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."), |
|
94 |
||
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"); |
|
98 |
||
99 |
final Diagnostic.Kind kind; |
|
100 |
final String text; |
|
101 |
||
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 |
} |
|
109 |
||
110 |
Message(Diagnostic.Kind kind, String text) { |
|
111 |
this.kind = kind; |
|
112 |
this.text = text; |
|
113 |
} |
|
114 |
||
115 |
@Override |
|
116 |
public String toString() { |
|
117 |
return "[" + kind + ",\"" + text + "\"]"; |
|
118 |
} |
|
119 |
} |
|
120 |
||
121 |
void run() throws Exception { |
|
122 |
javadoc = ToolProvider.getSystemDocumentationTool(); |
|
123 |
fm = javadoc.getStandardFileManager(null, null, null); |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
124 |
try { |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
125 |
fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(new File("."))); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
126 |
file = new SimpleJavaFileObject(URI.create("Test.java"), JavaFileObject.Kind.SOURCE) { |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
127 |
@Override |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
128 |
public CharSequence getCharContent(boolean ignoreEncoding) { |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
129 |
return code; |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
130 |
} |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
131 |
}; |
15354 | 132 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
133 |
test(Collections.<String>emptyList(), |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
134 |
Main.Result.ERROR, |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
135 |
EnumSet.of(Message.DL_ERR9A, Message.DL_WRN12A)); |
15354 | 136 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
137 |
test(Arrays.asList(rawDiags), |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
138 |
Main.Result.ERROR, |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
139 |
EnumSet.of(Message.DL_ERR9, Message.DL_WRN12)); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
140 |
|
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
141 |
test(Arrays.asList("-Xdoclint:none"), |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
142 |
Main.Result.OK, |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
143 |
EnumSet.of(Message.JD_WRN10, Message.JD_WRN13)); |
15354 | 144 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
145 |
test(Arrays.asList(rawDiags, "-Xdoclint"), |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
146 |
Main.Result.ERROR, |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
147 |
EnumSet.of(Message.DL_ERR9, Message.DL_WRN12)); |
15354 | 148 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
149 |
test(Arrays.asList(rawDiags, "-Xdoclint:all/public"), |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
150 |
Main.Result.ERROR, |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
151 |
EnumSet.of(Message.OPT_BADQUAL)); |
15354 | 152 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
153 |
test(Arrays.asList(rawDiags, "-Xdoclint:all", "-public"), |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
154 |
Main.Result.OK, |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
155 |
EnumSet.of(Message.DL_WRN12)); |
15354 | 156 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
157 |
test(Arrays.asList(rawDiags, "-Xdoclint:syntax"), |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
158 |
Main.Result.OK, |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
159 |
EnumSet.of(Message.DL_WRN12)); |
15354 | 160 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
161 |
test(Arrays.asList(rawDiags, "-private"), |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
162 |
Main.Result.ERROR, |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
163 |
EnumSet.of(Message.DL_ERR6, Message.DL_ERR9, Message.DL_WRN12)); |
15354 | 164 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
165 |
test(Arrays.asList(rawDiags, "-Xdoclint:syntax", "-private"), |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
166 |
Main.Result.ERROR, |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
167 |
EnumSet.of(Message.DL_ERR6, Message.DL_WRN12)); |
15713 | 168 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
169 |
test(Arrays.asList(rawDiags, "-Xdoclint:reference"), |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
170 |
Main.Result.ERROR, |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
171 |
EnumSet.of(Message.DL_ERR9)); |
15354 | 172 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
173 |
test(Arrays.asList(rawDiags, "-Xdoclint:badarg"), |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
174 |
Main.Result.ERROR, |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
175 |
EnumSet.of(Message.OPT_BADARG)); |
15354 | 176 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
177 |
if (errors > 0) |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
178 |
throw new Exception(errors + " errors occurred"); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
179 |
} finally { |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
180 |
fm.close(); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
15713
diff
changeset
|
181 |
} |
15354 | 182 |
} |
183 |
||
184 |
void test(List<String> opts, Main.Result expectResult, Set<Message> expectMessages) { |
|
185 |
System.err.println("test: " + opts); |
|
186 |
StringWriter sw = new StringWriter(); |
|
187 |
PrintWriter pw = new PrintWriter(sw); |
|
188 |
List<JavaFileObject> files = Arrays.asList(file); |
|
189 |
try { |
|
190 |
DocumentationTask t = javadoc.getTask(pw, fm, null, null, opts, files); |
|
191 |
boolean ok = t.call(); |
|
192 |
pw.close(); |
|
193 |
String out = sw.toString().replaceAll("[\r\n]+", "\n"); |
|
194 |
if (!out.isEmpty()) |
|
195 |
System.err.println(out); |
|
196 |
if (ok && expectResult != Main.Result.OK) { |
|
197 |
error("Compilation succeeded unexpectedly"); |
|
198 |
} else if (!ok && expectResult != Main.Result.ERROR) { |
|
199 |
error("Compilation failed unexpectedly"); |
|
200 |
} else |
|
201 |
check(out, expectMessages); |
|
202 |
} catch (IllegalArgumentException e) { |
|
203 |
System.err.println(e); |
|
204 |
String expectOut = expectMessages.iterator().next().text; |
|
205 |
if (expectResult != Main.Result.CMDERR) |
|
206 |
error("unexpected exception caught"); |
|
207 |
else if (!e.getMessage().equals(expectOut)) { |
|
208 |
error("unexpected exception message: " |
|
209 |
+ e.getMessage() |
|
210 |
+ " expected: " + expectOut); |
|
211 |
} |
|
212 |
} |
|
213 |
||
214 |
// if (errors > 0) |
|
215 |
// throw new Error("stop"); |
|
216 |
} |
|
217 |
||
218 |
private void check(String out, Set<Message> expect) { |
|
219 |
Pattern ignore = Pattern.compile("^(Building|Constructing|Generating|Loading|Standard|Starting| ) .*"); |
|
220 |
Pattern stats = Pattern.compile("^([1-9]+) (error|warning)(s?)"); |
|
221 |
Set<Message> found = EnumSet.noneOf(Message.class); |
|
222 |
int e = 0, w = 0; |
|
223 |
for (String line: out.split("[\r\n]+")) { |
|
224 |
if (ignore.matcher(line).matches()) |
|
225 |
continue; |
|
226 |
||
227 |
Matcher s = stats.matcher(line); |
|
228 |
if (s.matches()) { |
|
229 |
int i = Integer.valueOf(s.group(1)); |
|
230 |
if (s.group(2).equals("error")) |
|
231 |
e++; |
|
232 |
else |
|
233 |
w++; |
|
234 |
continue; |
|
235 |
} |
|
236 |
||
237 |
Message m = Message.get(line); |
|
238 |
if (m == null) |
|
239 |
error("Unexpected line: " + line); |
|
240 |
else |
|
241 |
found.add(m); |
|
242 |
} |
|
243 |
for (Message m: expect) { |
|
244 |
if (!found.contains(m)) |
|
245 |
error("expected message not found: " + m.text); |
|
246 |
} |
|
247 |
for (Message m: found) { |
|
248 |
if (!expect.contains(m)) |
|
249 |
error("unexpected message found: " + m.text); |
|
250 |
} |
|
251 |
} |
|
252 |
||
253 |
void error(String msg) { |
|
254 |
System.err.println("Error: " + msg); |
|
255 |
errors++; |
|
256 |
} |
|
257 |
||
258 |
int errors; |
|
259 |
} |