Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/langtools/jdk/javadoc/tool/doclint/DocLintTest.java
40974 views
1
/*
2
* Copyright (c) 2012, 2021, 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 8004834 8007610 8129909 8182765 8247815
27
* @summary Add doclint support into javadoc
28
* @modules jdk.compiler/com.sun.tools.javac.main
29
*/
30
31
import java.io.File;
32
import java.io.PrintWriter;
33
import java.io.StringWriter;
34
import java.net.URI;
35
import java.util.Arrays;
36
import java.util.Collections;
37
import java.util.EnumSet;
38
import java.util.List;
39
import java.util.Set;
40
import java.util.regex.Matcher;
41
import java.util.regex.Pattern;
42
43
import javax.tools.Diagnostic;
44
import javax.tools.DocumentationTool;
45
import javax.tools.DocumentationTool.DocumentationTask;
46
import javax.tools.JavaFileObject;
47
import javax.tools.SimpleJavaFileObject;
48
import javax.tools.StandardJavaFileManager;
49
import javax.tools.StandardLocation;
50
import javax.tools.ToolProvider;
51
import static javax.tools.Diagnostic.Kind.*;
52
53
import com.sun.tools.javac.main.Main;
54
55
public class DocLintTest {
56
public static void main(String... args) throws Exception {
57
new DocLintTest().run();
58
}
59
60
DocumentationTool javadoc;
61
StandardJavaFileManager fm;
62
Iterable<? extends JavaFileObject> files;
63
64
final String code =
65
/* 01 */ "/** Class comment. */\n" +
66
/* 02 */ "public class Test {\n" +
67
/* 03 */ " /** Method comment. */\n" +
68
/* 04 */ " public void method() { }\n" +
69
/* 05 */ "\n" +
70
/* 06 */ " /** Syntax < error. */\n" +
71
/* 07 */ """
72
\s private void syntaxError() { }
73
""" +
74
/* 08 */ "\n" +
75
/* 09 */ " /** @see DoesNotExist */\n" +
76
/* 10 */ """
77
\s protected void referenceError() { }
78
""" +
79
/* 11 */ "\n" +
80
/* 12 */ " /** @return */\n" +
81
/* 13 */ """
82
\s public int emptyReturn() { return 0; }
83
""" +
84
/* 14 */ "}\n";
85
86
final String p1Code =
87
/* 01 */ "package p1;\n" +
88
/* 02 */ "public class P1Test {\n" +
89
/* 03 */ " /** Syntax < error. */\n" +
90
/* 04 */ " public void method() { }\n" +
91
/* 05 */ "}\n";
92
93
final String p2Code =
94
/* 01 */ "package p2;\n" +
95
/* 02 */ "public class P2Test {\n" +
96
/* 03 */ " /** Syntax < error. */\n" +
97
/* 04 */ " public void method() { }\n" +
98
/* 05 */ "}\n";
99
100
private final String rawDiags = "-XDrawDiagnostics";
101
private final String htmlVersion = "-html5";
102
103
private enum Message {
104
// doclint messages
105
DL_ERR6(ERROR, "Test.java:6:16: compiler.err.proc.messager: malformed HTML"),
106
DL_ERR9(ERROR, "Test.java:9:14: compiler.err.proc.messager: reference not found"),
107
DL_WRN12(WARNING, "Test.java:12:9: compiler.warn.proc.messager: no description for @return"),
108
109
DL_ERR_P1TEST(ERROR, "P1Test.java:3:16: compiler.err.proc.messager: malformed HTML"),
110
DL_ERR_P2TEST(ERROR, "P2Test.java:3:16: compiler.err.proc.messager: malformed HTML"),
111
DL_WARN_P1TEST(WARNING, "P1Test.java:2:8: compiler.warn.proc.messager: no comment"),
112
DL_WARN_P2TEST(WARNING, "P2Test.java:2:8: compiler.warn.proc.messager: no comment"),
113
114
// doclint messages when -XDrawDiagnostics is not in effect
115
DL_ERR9A(ERROR, "Test.java:9: error: reference not found"),
116
DL_WRN12A(WARNING, "Test.java:12: warning: no description for @return"),
117
118
// javadoc messages about bad content: these should only appear when doclint is disabled
119
JD_WRN10(WARNING, "Test.java:10: warning: Tag @see: reference not found: DoesNotExist"),
120
JD_WRN13(WARNING, "Test.java:13: warning: @return tag has no arguments."),
121
122
// javadoc messages for bad options
123
OPT_BADARG(ERROR, "error: Invalid argument for -Xdoclint option"),
124
OPT_BADQUAL(ERROR, "error: Access qualifiers not permitted for -Xdoclint arguments"),
125
OPT_BADPACKAGEARG(ERROR, "error: Invalid argument for -Xdoclint/package option");
126
127
final Diagnostic.Kind kind;
128
final String text;
129
130
static Message get(String text) {
131
for (Message m: values()) {
132
if (m.text.equals(text))
133
return m;
134
}
135
return null;
136
}
137
138
Message(Diagnostic.Kind kind, String text) {
139
this.kind = kind;
140
this.text = text;
141
}
142
143
@Override
144
public String toString() {
145
return "[" + kind + ",\"" + text + "\"]";
146
}
147
}
148
149
void run() throws Exception {
150
javadoc = ToolProvider.getSystemDocumentationTool();
151
fm = javadoc.getStandardFileManager(null, null, null);
152
try {
153
fm.setLocation(StandardLocation.CLASS_OUTPUT, List.of(new File(".")));
154
fm.setLocation(StandardLocation.CLASS_PATH, Collections.<File>emptyList());
155
files = List.of(new TestJFO("Test.java", code));
156
157
test(List.of(htmlVersion),
158
Main.Result.ERROR,
159
EnumSet.of(Message.DL_ERR9A, Message.DL_WRN12A));
160
161
test(List.of(htmlVersion, rawDiags),
162
Main.Result.ERROR,
163
EnumSet.of(Message.DL_ERR9, Message.DL_WRN12));
164
165
// test(List.of("-Xdoclint:none"),
166
// Main.Result.OK,
167
// EnumSet.of(Message.JD_WRN10, Message.JD_WRN13));
168
169
test(List.of(htmlVersion, rawDiags, "-Xdoclint"),
170
Main.Result.ERROR,
171
EnumSet.of(Message.DL_ERR9, Message.DL_WRN12));
172
173
test(List.of(htmlVersion, rawDiags, "-Xdoclint:all/public"),
174
Main.Result.ERROR,
175
EnumSet.of(Message.OPT_BADQUAL));
176
177
test(List.of(htmlVersion, rawDiags, "-Xdoclint:all", "-public"),
178
Main.Result.OK,
179
EnumSet.of(Message.DL_WRN12));
180
181
test(List.of(htmlVersion, rawDiags, "-Xdoclint:missing"),
182
Main.Result.OK,
183
EnumSet.of(Message.DL_WRN12));
184
185
test(List.of(htmlVersion, rawDiags, "-private"),
186
Main.Result.ERROR,
187
EnumSet.of(Message.DL_ERR6, Message.DL_ERR9, Message.DL_WRN12));
188
189
test(List.of(htmlVersion, rawDiags, "-Xdoclint:missing,syntax", "-private"),
190
Main.Result.ERROR,
191
EnumSet.of(Message.DL_ERR6, Message.DL_WRN12));
192
193
test(List.of(htmlVersion, rawDiags, "-Xdoclint:reference"),
194
Main.Result.ERROR,
195
EnumSet.of(Message.DL_ERR9));
196
197
test(List.of(htmlVersion, rawDiags, "-Xdoclint:badarg"),
198
Main.Result.ERROR,
199
EnumSet.of(Message.OPT_BADARG));
200
201
files = List.of(new TestJFO("p1/P1Test.java", p1Code),
202
new TestJFO("p2/P2Test.java", p2Code));
203
204
test(List.of(htmlVersion, rawDiags),
205
Main.Result.ERROR,
206
EnumSet.of(Message.DL_ERR_P1TEST, Message.DL_ERR_P2TEST,
207
Message.DL_WARN_P1TEST, Message.DL_WARN_P2TEST));
208
209
test(List.of(htmlVersion, rawDiags, "-Xdoclint/package:p1"),
210
Main.Result.ERROR,
211
EnumSet.of(Message.DL_ERR_P1TEST,
212
Message.DL_WARN_P1TEST));
213
214
test(List.of(htmlVersion, rawDiags, "-Xdoclint/package:*p"),
215
Main.Result.ERROR,
216
EnumSet.of(Message.OPT_BADPACKAGEARG));
217
218
if (errors > 0)
219
throw new Exception(errors + " errors occurred");
220
} finally {
221
fm.close();
222
}
223
}
224
225
void test(List<String> opts, Main.Result expectResult, Set<Message> expectMessages) {
226
System.err.println("test: " + opts);
227
StringWriter sw = new StringWriter();
228
PrintWriter pw = new PrintWriter(sw);
229
try {
230
DocumentationTask t = javadoc.getTask(pw, fm, null, null, opts, files);
231
boolean ok = t.call();
232
pw.close();
233
String out = sw.toString().replaceAll("[\r\n]+", "\n");
234
if (!out.isEmpty())
235
System.err.println(out);
236
if (ok && expectResult != Main.Result.OK) {
237
error("Compilation succeeded unexpectedly");
238
} else if (!ok && expectResult != Main.Result.ERROR) {
239
error("Compilation failed unexpectedly");
240
} else
241
check(out, expectMessages);
242
} catch (IllegalArgumentException e) {
243
System.err.println(e);
244
String expectOut = expectMessages.iterator().next().text;
245
if (expectResult != Main.Result.CMDERR)
246
error("unexpected exception caught");
247
else if (!e.getMessage().equals(expectOut)) {
248
error("unexpected exception message: "
249
+ e.getMessage()
250
+ " expected: " + expectOut);
251
}
252
}
253
254
// if (errors > 0)
255
// throw new Error("stop");
256
}
257
258
private void check(String out, Set<Message> expect) {
259
Pattern ignore = Pattern.compile("^(Building|Constructing|Generating|Loading|Standard|Starting| ) .*");
260
Pattern stats = Pattern.compile("^([1-9]+) (error|warning)(s?)");
261
Set<Message> found = EnumSet.noneOf(Message.class);
262
int e = 0, w = 0;
263
for (String line: out.split("[\r\n]+")) {
264
if (ignore.matcher(line).matches())
265
continue;
266
267
Matcher s = stats.matcher(line);
268
if (s.matches()) {
269
int i = Integer.valueOf(s.group(1));
270
if (s.group(2).equals("error"))
271
e++;
272
else
273
w++;
274
continue;
275
}
276
277
Message m = Message.get(line);
278
if (m == null)
279
error("Unexpected line: " + line);
280
else
281
found.add(m);
282
}
283
for (Message m: expect) {
284
if (!found.contains(m))
285
error("expected message not found: " + m.text);
286
}
287
for (Message m: found) {
288
if (!expect.contains(m))
289
error("unexpected message found: " + m.text);
290
}
291
}
292
293
void error(String msg) {
294
System.err.println("Error: " + msg);
295
errors++;
296
}
297
298
int errors;
299
300
class TestJFO extends SimpleJavaFileObject {
301
302
private final String content;
303
304
public TestJFO(String fileName, String content) {
305
super(URI.create(fileName), JavaFileObject.Kind.SOURCE);
306
this.content = content;
307
}
308
309
@Override
310
public CharSequence getCharContent(boolean ignoreEncoding) {
311
return content;
312
}
313
};
314
}
315
316