test/tools/doclint/DocLintTester.java

Wed, 16 Jan 2013 20:41:14 -0800

author
jjg
date
Wed, 16 Jan 2013 20:41:14 -0800
changeset 1502
916143318f10
parent 1474
383bc0fbd759
child 1506
4a3cfc970c6f
permissions
-rw-r--r--

8006228: Doclint doesn't detect <code> {@code nested inline} </code>
Reviewed-by: darcy

jjg@1455 1 /*
jjg@1455 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
jjg@1455 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1455 4 *
jjg@1455 5 * This code is free software; you can redistribute it and/or modify it
jjg@1455 6 * under the terms of the GNU General Public License version 2 only, as
jjg@1455 7 * published by the Free Software Foundation.
jjg@1455 8 *
jjg@1455 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1455 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1455 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1455 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@1455 13 * accompanied this code).
jjg@1455 14 *
jjg@1455 15 * You should have received a copy of the GNU General Public License version
jjg@1455 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1455 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1455 18 *
jjg@1455 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1455 20 * or visit www.oracle.com if you need additional information or have any
jjg@1455 21 * questions.
jjg@1455 22 */
jjg@1455 23
jjg@1455 24 import java.io.File;
jjg@1455 25 import java.util.ArrayList;
jjg@1455 26 import java.util.List;
jjg@1455 27
jjg@1455 28 import com.sun.tools.doclint.DocLint;
jjg@1455 29 import java.io.BufferedReader;
jjg@1455 30 import java.io.FileReader;
jjg@1455 31 import java.io.IOException;
jjg@1455 32 import java.io.PrintWriter;
jjg@1455 33 import java.io.Reader;
jjg@1455 34 import java.io.StringWriter;
jjg@1455 35 import java.util.regex.Matcher;
jjg@1455 36 import java.util.regex.Pattern;
jjg@1455 37
jjg@1455 38
jjg@1455 39 public class DocLintTester {
jjg@1455 40
jjg@1455 41 public static void main(String... args) throws Exception {
jjg@1455 42 new DocLintTester().run(args);
jjg@1455 43 }
jjg@1455 44
jjg@1455 45 public void run(String... args) throws Exception {
jjg@1455 46 String testSrc = System.getProperty("test.src");
jjg@1455 47
jjg@1455 48 File refFile = null;
jjg@1455 49 List<String> opts = new ArrayList<String>();
jjg@1455 50 List<File> files = new ArrayList<File>();
jjg@1455 51 for (int i = 0; i < args.length; i++) {
jjg@1455 52 String arg = args[i];
jjg@1455 53 if (arg.equals("-ref")) {
jjg@1455 54 refFile = new File(testSrc, args[++i]);
jjg@1455 55 } else if (arg.startsWith("-Xmsgs")) {
jjg@1455 56 opts.add(arg);
jjg@1455 57 } else
jjg@1455 58 files.add(new File(testSrc, arg));
jjg@1455 59 }
jjg@1455 60
jjg@1455 61 check(opts, files, refFile);
jjg@1455 62
jjg@1455 63 if (errors > 0)
jjg@1455 64 throw new Exception(errors + " errors occurred");
jjg@1455 65 }
jjg@1455 66
jjg@1455 67 void check(List<String> opts, List<File> files, File refFile) throws Exception {
jjg@1455 68 List<String> args = new ArrayList<String>();
jjg@1455 69 args.addAll(opts);
jjg@1455 70 for (File file: files)
jjg@1455 71 args.add(file.getPath());
jjg@1455 72
jjg@1455 73 StringWriter sw = new StringWriter();
jjg@1455 74 PrintWriter pw = new PrintWriter(sw);
jjg@1455 75 new DocLint().run(pw, args.toArray(new String[args.size()]));
jjg@1455 76 pw.flush();
jjg@1455 77 String out = normalizeNewlines(removeFileNames(sw.toString())).trim();
jjg@1455 78 if (out != null)
jjg@1455 79 System.err.println("Output:\n" + out);
jjg@1455 80
jjg@1455 81 if (refFile == null) {
jjg@1455 82 if (!out.isEmpty())
jjg@1455 83 error("unexpected output");
jjg@1455 84 } else {
jjg@1455 85 String expect = readFile(refFile);
jjg@1455 86 if (!expect.equals(out)) {
jjg@1455 87 error("expected output not found");
jjg@1455 88 System.err.println("EXPECT>>" + expect + "<<");
jjg@1455 89 System.err.println(" FOUND>>" + out + "<<");
jjg@1455 90 }
jjg@1455 91 }
jjg@1455 92 }
jjg@1455 93
jjg@1455 94 String readFile(File file) throws IOException {
jjg@1455 95 StringBuilder sb = new StringBuilder();
jjg@1455 96 Reader in = new BufferedReader(new FileReader(file));
jjg@1455 97 try {
jjg@1455 98 char[] buf = new char[1024];
jjg@1455 99 int n;
jjg@1455 100 while ((n = in.read(buf)) != -1)
jjg@1455 101 sb.append(buf, 0, n);
jjg@1455 102 } finally {
jjg@1455 103 in.close();
jjg@1455 104 }
jjg@1455 105 return sb.toString().trim();
jjg@1455 106 }
jjg@1455 107
jjg@1455 108 private static final Pattern dirFileLine = Pattern.compile(
jjg@1455 109 "(?m)" // multi-line mode
jjg@1474 110 + "^(.*?)" // directory part of file name
jjg@1455 111 + "([A-Za-z0-9.]+:[0-9]+:)"); // file name and line number
jjg@1455 112
jjg@1455 113 String removeFileNames(String s) {
jjg@1455 114 Matcher m = dirFileLine.matcher(s);
jjg@1455 115 StringBuffer sb = new StringBuffer();
jjg@1455 116 while (m.find()) {
jjg@1455 117 m.appendReplacement(sb, "$2");
jjg@1455 118 }
jjg@1455 119 m.appendTail(sb);
jjg@1455 120 return sb.toString();
jjg@1455 121 }
jjg@1455 122
jjg@1455 123 private static final String nl = System.getProperty("line.separator");
jjg@1455 124 String normalizeNewlines(String s) {
jjg@1455 125 return (nl.equals("\n") ? s : s.replace(nl, "\n"));
jjg@1455 126 }
jjg@1455 127
jjg@1455 128
jjg@1455 129 void error(String msg) {
jjg@1455 130 System.err.println("Error: " + msg);
jjg@1455 131 errors++;
jjg@1455 132 }
jjg@1455 133
jjg@1455 134 int errors;
jjg@1455 135 }

mercurial