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