jjg@1455: /* jjg@1506: * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. jjg@1455: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@1455: * jjg@1455: * This code is free software; you can redistribute it and/or modify it jjg@1455: * under the terms of the GNU General Public License version 2 only, as jjg@1455: * published by the Free Software Foundation. jjg@1455: * jjg@1455: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@1455: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@1455: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@1455: * version 2 for more details (a copy is included in the LICENSE file that jjg@1455: * accompanied this code). jjg@1455: * jjg@1455: * You should have received a copy of the GNU General Public License version jjg@1455: * 2 along with this work; if not, write to the Free Software Foundation, jjg@1455: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@1455: * jjg@1455: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jjg@1455: * or visit www.oracle.com if you need additional information or have any jjg@1455: * questions. jjg@1455: */ jjg@1455: jjg@1455: import java.io.File; jjg@1455: import java.util.ArrayList; jjg@1455: import java.util.List; jjg@1455: jjg@1455: import com.sun.tools.doclint.DocLint; jjg@1506: import com.sun.tools.doclint.DocLint.BadArgs; jjg@1455: import java.io.BufferedReader; jjg@1455: import java.io.FileReader; jjg@1455: import java.io.IOException; jjg@1455: import java.io.PrintWriter; jjg@1455: import java.io.Reader; jjg@1455: import java.io.StringWriter; jjg@1455: import java.util.regex.Matcher; jjg@1455: import java.util.regex.Pattern; jjg@1455: jjg@1455: jjg@1455: public class DocLintTester { jjg@1455: jjg@1455: public static void main(String... args) throws Exception { jjg@1455: new DocLintTester().run(args); jjg@1455: } jjg@1455: jjg@1455: public void run(String... args) throws Exception { jjg@1455: String testSrc = System.getProperty("test.src"); jjg@1455: jjg@1506: boolean badArgs = false; jjg@1455: File refFile = null; jjg@1455: List opts = new ArrayList(); jjg@1455: List files = new ArrayList(); jjg@1455: for (int i = 0; i < args.length; i++) { jjg@1455: String arg = args[i]; jjg@1455: if (arg.equals("-ref")) { jjg@1455: refFile = new File(testSrc, args[++i]); jjg@1506: } else if (arg.equals("-badargs")) { jjg@1506: badArgs = true; jjg@1455: } else if (arg.startsWith("-Xmsgs")) { jjg@1455: opts.add(arg); jjg@1506: } else if (arg.startsWith("-")) { jjg@1506: opts.add(arg); jjg@1506: if (i < args.length - 1 && !args[i+1].startsWith("-")) jjg@1506: opts.add(args[++i]); jjg@1455: } else jjg@1455: files.add(new File(testSrc, arg)); jjg@1455: } jjg@1455: jjg@1506: check(opts, files, badArgs, refFile); jjg@1455: jjg@1455: if (errors > 0) jjg@1455: throw new Exception(errors + " errors occurred"); jjg@1455: } jjg@1455: jjg@1506: void check(List opts, List files, boolean expectBadArgs, File refFile) throws Exception { jjg@1455: List args = new ArrayList(); jjg@1455: args.addAll(opts); jjg@1455: for (File file: files) jjg@1455: args.add(file.getPath()); jjg@1455: jjg@1455: StringWriter sw = new StringWriter(); jjg@1455: PrintWriter pw = new PrintWriter(sw); jjg@1506: try { jjg@1506: new DocLint().run(pw, args.toArray(new String[args.size()])); jjg@1506: if (expectBadArgs) jjg@1506: error("expected exception not thrown"); jjg@1506: } catch (BadArgs e) { jjg@1506: if (!expectBadArgs) jjg@1506: error("unexpected exception caught: " + e); jjg@1506: } jjg@1455: pw.flush(); jjg@1455: String out = normalizeNewlines(removeFileNames(sw.toString())).trim(); jjg@1455: if (out != null) jjg@1455: System.err.println("Output:\n" + out); jjg@1455: jjg@1455: if (refFile == null) { jjg@1455: if (!out.isEmpty()) jjg@1455: error("unexpected output"); jjg@1455: } else { jjg@1455: String expect = readFile(refFile); jjg@1455: if (!expect.equals(out)) { jjg@1455: error("expected output not found"); jjg@1455: System.err.println("EXPECT>>" + expect + "<<"); jjg@1455: System.err.println(" FOUND>>" + out + "<<"); jjg@1455: } jjg@1455: } jjg@1455: } jjg@1455: jjg@1455: String readFile(File file) throws IOException { jjg@1455: StringBuilder sb = new StringBuilder(); jjg@1455: Reader in = new BufferedReader(new FileReader(file)); jjg@1455: try { jjg@1455: char[] buf = new char[1024]; jjg@1455: int n; jjg@1455: while ((n = in.read(buf)) != -1) jjg@1455: sb.append(buf, 0, n); jjg@1455: } finally { jjg@1455: in.close(); jjg@1455: } jjg@1455: return sb.toString().trim(); jjg@1455: } jjg@1455: jjg@1455: private static final Pattern dirFileLine = Pattern.compile( jjg@1455: "(?m)" // multi-line mode jjg@1474: + "^(.*?)" // directory part of file name jjg@1913: + "([-A-Za-z0-9.]+:[0-9]+:)"); // file name and line number jjg@1455: jjg@1455: String removeFileNames(String s) { jjg@1455: Matcher m = dirFileLine.matcher(s); jjg@1455: StringBuffer sb = new StringBuffer(); jjg@1455: while (m.find()) { jjg@1455: m.appendReplacement(sb, "$2"); jjg@1455: } jjg@1455: m.appendTail(sb); jjg@1455: return sb.toString(); jjg@1455: } jjg@1455: jjg@1455: private static final String nl = System.getProperty("line.separator"); jjg@1455: String normalizeNewlines(String s) { jjg@1455: return (nl.equals("\n") ? s : s.replace(nl, "\n")); jjg@1455: } jjg@1455: jjg@1455: jjg@1455: void error(String msg) { jjg@1455: System.err.println("Error: " + msg); jjg@1455: errors++; jjg@1455: } jjg@1455: jjg@1455: int errors; jjg@1455: }