Tue, 14 May 2013 10:14:56 -0700
8012311: Cleanup names and duplicatre code in TagletManager
Reviewed-by: darcy
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); |
jjg@1506 | 61 | } else if (arg.startsWith("-")) { |
jjg@1506 | 62 | opts.add(arg); |
jjg@1506 | 63 | if (i < args.length - 1 && !args[i+1].startsWith("-")) |
jjg@1506 | 64 | opts.add(args[++i]); |
jjg@1455 | 65 | } else |
jjg@1455 | 66 | files.add(new File(testSrc, arg)); |
jjg@1455 | 67 | } |
jjg@1455 | 68 | |
jjg@1506 | 69 | check(opts, files, badArgs, refFile); |
jjg@1455 | 70 | |
jjg@1455 | 71 | if (errors > 0) |
jjg@1455 | 72 | throw new Exception(errors + " errors occurred"); |
jjg@1455 | 73 | } |
jjg@1455 | 74 | |
jjg@1506 | 75 | void check(List<String> opts, List<File> files, boolean expectBadArgs, File refFile) throws Exception { |
jjg@1455 | 76 | List<String> args = new ArrayList<String>(); |
jjg@1455 | 77 | args.addAll(opts); |
jjg@1455 | 78 | for (File file: files) |
jjg@1455 | 79 | args.add(file.getPath()); |
jjg@1455 | 80 | |
jjg@1455 | 81 | StringWriter sw = new StringWriter(); |
jjg@1455 | 82 | PrintWriter pw = new PrintWriter(sw); |
jjg@1506 | 83 | try { |
jjg@1506 | 84 | new DocLint().run(pw, args.toArray(new String[args.size()])); |
jjg@1506 | 85 | if (expectBadArgs) |
jjg@1506 | 86 | error("expected exception not thrown"); |
jjg@1506 | 87 | } catch (BadArgs e) { |
jjg@1506 | 88 | if (!expectBadArgs) |
jjg@1506 | 89 | error("unexpected exception caught: " + e); |
jjg@1506 | 90 | } |
jjg@1455 | 91 | pw.flush(); |
jjg@1455 | 92 | String out = normalizeNewlines(removeFileNames(sw.toString())).trim(); |
jjg@1455 | 93 | if (out != null) |
jjg@1455 | 94 | System.err.println("Output:\n" + out); |
jjg@1455 | 95 | |
jjg@1455 | 96 | if (refFile == null) { |
jjg@1455 | 97 | if (!out.isEmpty()) |
jjg@1455 | 98 | error("unexpected output"); |
jjg@1455 | 99 | } else { |
jjg@1455 | 100 | String expect = readFile(refFile); |
jjg@1455 | 101 | if (!expect.equals(out)) { |
jjg@1455 | 102 | error("expected output not found"); |
jjg@1455 | 103 | System.err.println("EXPECT>>" + expect + "<<"); |
jjg@1455 | 104 | System.err.println(" FOUND>>" + out + "<<"); |
jjg@1455 | 105 | } |
jjg@1455 | 106 | } |
jjg@1455 | 107 | } |
jjg@1455 | 108 | |
jjg@1455 | 109 | String readFile(File file) throws IOException { |
jjg@1455 | 110 | StringBuilder sb = new StringBuilder(); |
jjg@1455 | 111 | Reader in = new BufferedReader(new FileReader(file)); |
jjg@1455 | 112 | try { |
jjg@1455 | 113 | char[] buf = new char[1024]; |
jjg@1455 | 114 | int n; |
jjg@1455 | 115 | while ((n = in.read(buf)) != -1) |
jjg@1455 | 116 | sb.append(buf, 0, n); |
jjg@1455 | 117 | } finally { |
jjg@1455 | 118 | in.close(); |
jjg@1455 | 119 | } |
jjg@1455 | 120 | return sb.toString().trim(); |
jjg@1455 | 121 | } |
jjg@1455 | 122 | |
jjg@1455 | 123 | private static final Pattern dirFileLine = Pattern.compile( |
jjg@1455 | 124 | "(?m)" // multi-line mode |
jjg@1474 | 125 | + "^(.*?)" // directory part of file name |
jjg@1455 | 126 | + "([A-Za-z0-9.]+:[0-9]+:)"); // file name and line number |
jjg@1455 | 127 | |
jjg@1455 | 128 | String removeFileNames(String s) { |
jjg@1455 | 129 | Matcher m = dirFileLine.matcher(s); |
jjg@1455 | 130 | StringBuffer sb = new StringBuffer(); |
jjg@1455 | 131 | while (m.find()) { |
jjg@1455 | 132 | m.appendReplacement(sb, "$2"); |
jjg@1455 | 133 | } |
jjg@1455 | 134 | m.appendTail(sb); |
jjg@1455 | 135 | return sb.toString(); |
jjg@1455 | 136 | } |
jjg@1455 | 137 | |
jjg@1455 | 138 | private static final String nl = System.getProperty("line.separator"); |
jjg@1455 | 139 | String normalizeNewlines(String s) { |
jjg@1455 | 140 | return (nl.equals("\n") ? s : s.replace(nl, "\n")); |
jjg@1455 | 141 | } |
jjg@1455 | 142 | |
jjg@1455 | 143 | |
jjg@1455 | 144 | void error(String msg) { |
jjg@1455 | 145 | System.err.println("Error: " + msg); |
jjg@1455 | 146 | errors++; |
jjg@1455 | 147 | } |
jjg@1455 | 148 | |
jjg@1455 | 149 | int errors; |
jjg@1455 | 150 | } |