Tue, 20 Dec 2016 15:24:55 -0800
Added tag jdk8u152-b00 for changeset 03e31cfcd499
1 /*
2 * Copyright (c) 2012, 2013, 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 */
24 import java.io.File;
25 import java.util.ArrayList;
26 import java.util.List;
28 import com.sun.tools.doclint.DocLint;
29 import com.sun.tools.doclint.DocLint.BadArgs;
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;
40 public class DocLintTester {
42 public static void main(String... args) throws Exception {
43 new DocLintTester().run(args);
44 }
46 public void run(String... args) throws Exception {
47 String testSrc = System.getProperty("test.src");
49 boolean badArgs = false;
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]);
57 } else if (arg.equals("-badargs")) {
58 badArgs = true;
59 } else if (arg.startsWith("-Xmsgs")) {
60 opts.add(arg);
61 } else if (arg.startsWith("-XcustomTags")) {
62 opts.add(arg);
63 } else if (arg.startsWith("-")) {
64 opts.add(arg);
65 if (i < args.length - 1 && !args[i+1].startsWith("-"))
66 opts.add(args[++i]);
67 } else
68 files.add(new File(testSrc, arg));
69 }
71 check(opts, files, badArgs, refFile);
73 if (errors > 0)
74 throw new Exception(errors + " errors occurred");
75 }
77 void check(List<String> opts, List<File> files, boolean expectBadArgs, File refFile) throws Exception {
78 List<String> args = new ArrayList<String>();
79 args.addAll(opts);
80 for (File file: files)
81 args.add(file.getPath());
83 StringWriter sw = new StringWriter();
84 PrintWriter pw = new PrintWriter(sw);
85 try {
86 new DocLint().run(pw, args.toArray(new String[args.size()]));
87 if (expectBadArgs)
88 error("expected exception not thrown");
89 } catch (BadArgs e) {
90 if (!expectBadArgs)
91 error("unexpected exception caught: " + e);
92 }
93 pw.flush();
94 String out = normalizeNewlines(removeFileNames(sw.toString())).trim();
95 if (out != null)
96 System.err.println("Output:\n" + out);
98 if (refFile == null) {
99 if (!out.isEmpty())
100 error("unexpected output");
101 } else {
102 String expect = readFile(refFile);
103 if (!expect.equals(out)) {
104 error("expected output not found");
105 System.err.println("EXPECT>>" + expect + "<<");
106 System.err.println(" FOUND>>" + out + "<<");
107 }
108 }
109 }
111 String readFile(File file) throws IOException {
112 StringBuilder sb = new StringBuilder();
113 Reader in = new BufferedReader(new FileReader(file));
114 try {
115 char[] buf = new char[1024];
116 int n;
117 while ((n = in.read(buf)) != -1)
118 sb.append(buf, 0, n);
119 } finally {
120 in.close();
121 }
122 return sb.toString().trim();
123 }
125 private static final Pattern dirFileLine = Pattern.compile(
126 "(?m)" // multi-line mode
127 + "^(.*?)" // directory part of file name
128 + "([-A-Za-z0-9.]+:[0-9]+:)"); // file name and line number
130 String removeFileNames(String s) {
131 Matcher m = dirFileLine.matcher(s);
132 StringBuffer sb = new StringBuffer();
133 while (m.find()) {
134 m.appendReplacement(sb, "$2");
135 }
136 m.appendTail(sb);
137 return sb.toString();
138 }
140 private static final String nl = System.getProperty("line.separator");
141 String normalizeNewlines(String s) {
142 return (nl.equals("\n") ? s : s.replace(nl, "\n"));
143 }
146 void error(String msg) {
147 System.err.println("Error: " + msg);
148 errors++;
149 }
151 int errors;
152 }