Fri, 14 Jun 2013 18:01:52 +0100
8008023: Get rid of utf8 chars in two tests
Reviewed-by: jjg
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("-")) {
62 opts.add(arg);
63 if (i < args.length - 1 && !args[i+1].startsWith("-"))
64 opts.add(args[++i]);
65 } else
66 files.add(new File(testSrc, arg));
67 }
69 check(opts, files, badArgs, refFile);
71 if (errors > 0)
72 throw new Exception(errors + " errors occurred");
73 }
75 void check(List<String> opts, List<File> files, boolean expectBadArgs, File refFile) throws Exception {
76 List<String> args = new ArrayList<String>();
77 args.addAll(opts);
78 for (File file: files)
79 args.add(file.getPath());
81 StringWriter sw = new StringWriter();
82 PrintWriter pw = new PrintWriter(sw);
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 }
91 pw.flush();
92 String out = normalizeNewlines(removeFileNames(sw.toString())).trim();
93 if (out != null)
94 System.err.println("Output:\n" + out);
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 }
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 }
123 private static final Pattern dirFileLine = Pattern.compile(
124 "(?m)" // multi-line mode
125 + "^(.*?)" // directory part of file name
126 + "([A-Za-z0-9.]+:[0-9]+:)"); // file name and line number
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 }
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 }
144 void error(String msg) {
145 System.err.println("Error: " + msg);
146 errors++;
147 }
149 int errors;
150 }