|
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 import java.io.File; |
|
25 import java.util.ArrayList; |
|
26 import java.util.List; |
|
27 |
|
28 import com.sun.tools.doclint.DocLint; |
|
29 import java.io.BufferedReader; |
|
30 import java.io.FileReader; |
|
31 import java.io.IOException; |
|
32 import java.io.PrintWriter; |
|
33 import java.io.Reader; |
|
34 import java.io.StringWriter; |
|
35 import java.util.regex.Matcher; |
|
36 import java.util.regex.Pattern; |
|
37 |
|
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 |
|
48 File refFile = null; |
|
49 List<String> opts = new ArrayList<String>(); |
|
50 List<File> files = new ArrayList<File>(); |
|
51 for (int i = 0; i < args.length; i++) { |
|
52 String arg = args[i]; |
|
53 if (arg.equals("-ref")) { |
|
54 refFile = new File(testSrc, args[++i]); |
|
55 } else if (arg.startsWith("-Xmsgs")) { |
|
56 opts.add(arg); |
|
57 } else |
|
58 files.add(new File(testSrc, arg)); |
|
59 } |
|
60 |
|
61 check(opts, files, refFile); |
|
62 |
|
63 if (errors > 0) |
|
64 throw new Exception(errors + " errors occurred"); |
|
65 } |
|
66 |
|
67 void check(List<String> opts, List<File> files, File refFile) throws Exception { |
|
68 List<String> args = new ArrayList<String>(); |
|
69 args.addAll(opts); |
|
70 for (File file: files) |
|
71 args.add(file.getPath()); |
|
72 |
|
73 StringWriter sw = new StringWriter(); |
|
74 PrintWriter pw = new PrintWriter(sw); |
|
75 new DocLint().run(pw, args.toArray(new String[args.size()])); |
|
76 pw.flush(); |
|
77 String out = normalizeNewlines(removeFileNames(sw.toString())).trim(); |
|
78 if (out != null) |
|
79 System.err.println("Output:\n" + out); |
|
80 |
|
81 if (refFile == null) { |
|
82 if (!out.isEmpty()) |
|
83 error("unexpected output"); |
|
84 } else { |
|
85 String expect = readFile(refFile); |
|
86 if (!expect.equals(out)) { |
|
87 error("expected output not found"); |
|
88 System.err.println("EXPECT>>" + expect + "<<"); |
|
89 System.err.println(" FOUND>>" + out + "<<"); |
|
90 } |
|
91 } |
|
92 } |
|
93 |
|
94 String readFile(File file) throws IOException { |
|
95 StringBuilder sb = new StringBuilder(); |
|
96 Reader in = new BufferedReader(new FileReader(file)); |
|
97 try { |
|
98 char[] buf = new char[1024]; |
|
99 int n; |
|
100 while ((n = in.read(buf)) != -1) |
|
101 sb.append(buf, 0, n); |
|
102 } finally { |
|
103 in.close(); |
|
104 } |
|
105 return sb.toString().trim(); |
|
106 } |
|
107 |
|
108 private static final Pattern dirFileLine = Pattern.compile( |
|
109 "(?m)" // multi-line mode |
|
110 + "^([^: ]+?)" // directory part of file name |
|
111 + "([A-Za-z0-9.]+:[0-9]+:)"); // file name and line number |
|
112 |
|
113 String removeFileNames(String s) { |
|
114 Matcher m = dirFileLine.matcher(s); |
|
115 StringBuffer sb = new StringBuffer(); |
|
116 while (m.find()) { |
|
117 m.appendReplacement(sb, "$2"); |
|
118 } |
|
119 m.appendTail(sb); |
|
120 return sb.toString(); |
|
121 } |
|
122 |
|
123 private static final String nl = System.getProperty("line.separator"); |
|
124 String normalizeNewlines(String s) { |
|
125 return (nl.equals("\n") ? s : s.replace(nl, "\n")); |
|
126 } |
|
127 |
|
128 |
|
129 void error(String msg) { |
|
130 System.err.println("Error: " + msg); |
|
131 errors++; |
|
132 } |
|
133 |
|
134 int errors; |
|
135 } |