author | naoto |
Tue, 29 Mar 2016 17:06:33 -0700 | |
changeset 36739 | 145210aba850 |
parent 29957 | 7740f9657f56 |
permissions | -rw-r--r-- |
14952 | 1 |
/* |
15370 | 2 |
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
14952 | 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 |
||
29957
7740f9657f56
8072945: Javadoc should generate valid and compliant HTML5 output
bpatel
parents:
21500
diff
changeset
|
24 |
import java.io.BufferedReader; |
14952 | 25 |
import java.io.File; |
26 |
import java.io.FileReader; |
|
27 |
import java.io.IOException; |
|
28 |
import java.io.PrintWriter; |
|
29 |
import java.io.Reader; |
|
30 |
import java.io.StringWriter; |
|
29957
7740f9657f56
8072945: Javadoc should generate valid and compliant HTML5 output
bpatel
parents:
21500
diff
changeset
|
31 |
import java.util.ArrayList; |
7740f9657f56
8072945: Javadoc should generate valid and compliant HTML5 output
bpatel
parents:
21500
diff
changeset
|
32 |
import java.util.List; |
14952 | 33 |
import java.util.regex.Matcher; |
34 |
import java.util.regex.Pattern; |
|
35 |
||
29957
7740f9657f56
8072945: Javadoc should generate valid and compliant HTML5 output
bpatel
parents:
21500
diff
changeset
|
36 |
import com.sun.tools.doclint.DocLint; |
7740f9657f56
8072945: Javadoc should generate valid and compliant HTML5 output
bpatel
parents:
21500
diff
changeset
|
37 |
import com.sun.tools.doclint.DocLint.BadArgs; |
14952 | 38 |
|
39 |
public class DocLintTester { |
|
40 |
||
41 |
public static void main(String... args) throws Exception { |
|
42 |
new DocLintTester().run(args); |
|
43 |
} |
|
44 |
||
45 |
public void run(String... args) throws Exception { |
|
46 |
String testSrc = System.getProperty("test.src"); |
|
47 |
||
15370 | 48 |
boolean badArgs = false; |
14952 | 49 |
File refFile = null; |
50 |
List<String> opts = new ArrayList<String>(); |
|
51 |
List<File> files = new ArrayList<File>(); |
|
52 |
for (int i = 0; i < args.length; i++) { |
|
53 |
String arg = args[i]; |
|
54 |
if (arg.equals("-ref")) { |
|
55 |
refFile = new File(testSrc, args[++i]); |
|
15370 | 56 |
} else if (arg.equals("-badargs")) { |
57 |
badArgs = true; |
|
14952 | 58 |
} else if (arg.startsWith("-Xmsgs")) { |
59 |
opts.add(arg); |
|
21500
475e59d3b40c
8006248: Since addition of -Xdoclint, javadoc ignores unknown tags
bpatel
parents:
19121
diff
changeset
|
60 |
} else if (arg.startsWith("-XcustomTags")) { |
475e59d3b40c
8006248: Since addition of -Xdoclint, javadoc ignores unknown tags
bpatel
parents:
19121
diff
changeset
|
61 |
opts.add(arg); |
29957
7740f9657f56
8072945: Javadoc should generate valid and compliant HTML5 output
bpatel
parents:
21500
diff
changeset
|
62 |
} else if (arg.startsWith("-XhtmlVersion")) { |
7740f9657f56
8072945: Javadoc should generate valid and compliant HTML5 output
bpatel
parents:
21500
diff
changeset
|
63 |
opts.add(arg); |
15370 | 64 |
} else if (arg.startsWith("-")) { |
65 |
opts.add(arg); |
|
66 |
if (i < args.length - 1 && !args[i+1].startsWith("-")) |
|
67 |
opts.add(args[++i]); |
|
14952 | 68 |
} else |
69 |
files.add(new File(testSrc, arg)); |
|
70 |
} |
|
71 |
||
15370 | 72 |
check(opts, files, badArgs, refFile); |
14952 | 73 |
|
74 |
if (errors > 0) |
|
75 |
throw new Exception(errors + " errors occurred"); |
|
76 |
} |
|
77 |
||
15370 | 78 |
void check(List<String> opts, List<File> files, boolean expectBadArgs, File refFile) throws Exception { |
14952 | 79 |
List<String> args = new ArrayList<String>(); |
80 |
args.addAll(opts); |
|
81 |
for (File file: files) |
|
82 |
args.add(file.getPath()); |
|
83 |
||
84 |
StringWriter sw = new StringWriter(); |
|
85 |
PrintWriter pw = new PrintWriter(sw); |
|
15370 | 86 |
try { |
87 |
new DocLint().run(pw, args.toArray(new String[args.size()])); |
|
88 |
if (expectBadArgs) |
|
89 |
error("expected exception not thrown"); |
|
90 |
} catch (BadArgs e) { |
|
91 |
if (!expectBadArgs) |
|
92 |
error("unexpected exception caught: " + e); |
|
93 |
} |
|
14952 | 94 |
pw.flush(); |
95 |
String out = normalizeNewlines(removeFileNames(sw.toString())).trim(); |
|
96 |
if (out != null) |
|
97 |
System.err.println("Output:\n" + out); |
|
98 |
||
99 |
if (refFile == null) { |
|
100 |
if (!out.isEmpty()) |
|
101 |
error("unexpected output"); |
|
102 |
} else { |
|
103 |
String expect = readFile(refFile); |
|
104 |
if (!expect.equals(out)) { |
|
105 |
error("expected output not found"); |
|
106 |
System.err.println("EXPECT>>" + expect + "<<"); |
|
107 |
System.err.println(" FOUND>>" + out + "<<"); |
|
108 |
} |
|
109 |
} |
|
110 |
} |
|
111 |
||
112 |
String readFile(File file) throws IOException { |
|
113 |
StringBuilder sb = new StringBuilder(); |
|
114 |
Reader in = new BufferedReader(new FileReader(file)); |
|
115 |
try { |
|
116 |
char[] buf = new char[1024]; |
|
117 |
int n; |
|
118 |
while ((n = in.read(buf)) != -1) |
|
119 |
sb.append(buf, 0, n); |
|
120 |
} finally { |
|
121 |
in.close(); |
|
122 |
} |
|
123 |
return sb.toString().trim(); |
|
124 |
} |
|
125 |
||
126 |
private static final Pattern dirFileLine = Pattern.compile( |
|
127 |
"(?m)" // multi-line mode |
|
15032 | 128 |
+ "^(.*?)" // directory part of file name |
19121
c626ed0c6ab0
8020664: doclint gives incorrect warnings on normal package statements
jjg
parents:
15370
diff
changeset
|
129 |
+ "([-A-Za-z0-9.]+:[0-9]+:)"); // file name and line number |
14952 | 130 |
|
131 |
String removeFileNames(String s) { |
|
132 |
Matcher m = dirFileLine.matcher(s); |
|
133 |
StringBuffer sb = new StringBuffer(); |
|
134 |
while (m.find()) { |
|
135 |
m.appendReplacement(sb, "$2"); |
|
136 |
} |
|
137 |
m.appendTail(sb); |
|
138 |
return sb.toString(); |
|
139 |
} |
|
140 |
||
141 |
private static final String nl = System.getProperty("line.separator"); |
|
142 |
String normalizeNewlines(String s) { |
|
143 |
return (nl.equals("\n") ? s : s.replace(nl, "\n")); |
|
144 |
} |
|
145 |
||
146 |
||
147 |
void error(String msg) { |
|
148 |
System.err.println("Error: " + msg); |
|
149 |
errors++; |
|
150 |
} |
|
151 |
||
152 |
int errors; |
|
153 |
} |