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