test/tools/doclint/DocLintTester.java

Thu, 26 Jul 2018 15:47:44 +0800

author
aoqi
date
Thu, 26 Jul 2018 15:47:44 +0800
changeset 3607
73e417207c0f
parent 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag mips64el-jdk8u172-b13 for changeset a5ddc08adcc0

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

mercurial