Path: blob/master/test/langtools/tools/doclint/DocLintTester.java
41144 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.BufferedReader;24import java.io.File;25import java.io.FileReader;26import java.io.IOException;27import java.io.PrintWriter;28import java.io.Reader;29import java.io.StringWriter;30import java.util.ArrayList;31import java.util.List;32import java.util.regex.Matcher;33import java.util.regex.Pattern;3435import jdk.javadoc.internal.doclint.DocLint;36import jdk.javadoc.internal.doclint.DocLint.BadArgs;3738public class DocLintTester {3940public static void main(String... args) throws Exception {41new DocLintTester().run(args);42}4344public void run(String... args) throws Exception {45String testSrc = System.getProperty("test.src");4647boolean badArgs = false;48File refFile = null;49List<String> opts = new ArrayList<String>();50List<File> files = new ArrayList<File>();51for (int i = 0; i < args.length; i++) {52String arg = args[i];53if (arg.equals("-ref")) {54refFile = new File(testSrc, args[++i]);55} else if (arg.equals("-badargs")) {56badArgs = true;57} else if (arg.startsWith("-Xmsgs")) {58opts.add(arg);59} else if (arg.startsWith("-XcustomTags")) {60opts.add(arg);61} else if (arg.startsWith("-XhtmlVersion")) {62opts.add(arg);63} else if (arg.startsWith("-")) {64opts.add(arg);65if (i < args.length - 1 && !args[i+1].startsWith("-"))66opts.add(args[++i]);67} else68files.add(new File(testSrc, arg));69}7071check(opts, files, badArgs, refFile);7273if (errors > 0)74throw new Exception(errors + " errors occurred");75}7677void check(List<String> opts, List<File> files, boolean expectBadArgs, File refFile) throws Exception {78List<String> args = new ArrayList<String>();79args.addAll(opts);80for (File file: files)81args.add(file.getPath());8283StringWriter sw = new StringWriter();84PrintWriter pw = new PrintWriter(sw);85try {86new DocLint().run(pw, args.toArray(new String[args.size()]));87if (expectBadArgs)88error("expected exception not thrown");89} catch (BadArgs e) {90if (!expectBadArgs)91error("unexpected exception caught: " + e);92}93pw.flush();94String out = normalizeNewlines(removeFileNames(sw.toString())).trim();95if (out != null)96System.err.println("Output:\n" + out);9798if (refFile == null) {99if (!out.isEmpty())100error("unexpected output");101} else {102String expect = readFile(refFile);103if (!expect.equals(out)) {104error("expected output not found");105System.err.println("EXPECT>>" + expect + "<<");106System.err.println(" FOUND>>" + out + "<<");107}108}109}110111String readFile(File file) throws IOException {112StringBuilder sb = new StringBuilder();113Reader in = new BufferedReader(new FileReader(file));114try {115char[] buf = new char[1024];116int n;117while ((n = in.read(buf)) != -1)118sb.append(buf, 0, n);119} finally {120in.close();121}122return sb.toString().trim();123}124125private static final Pattern dirFileLine = Pattern.compile(126"(?m)" // multi-line mode127+ "^(.*?)" // directory part of file name128+ "([-A-Za-z0-9.]+:[0-9]+:)"); // file name and line number129130String removeFileNames(String s) {131Matcher m = dirFileLine.matcher(s);132StringBuffer sb = new StringBuffer();133while (m.find()) {134m.appendReplacement(sb, "$2");135}136m.appendTail(sb);137return sb.toString();138}139140private static final String nl = System.getProperty("line.separator");141String normalizeNewlines(String s) {142return (nl.equals("\n") ? s : s.replace(nl, "\n"));143}144145146void error(String msg) {147System.err.println("Error: " + msg);148errors++;149}150151int errors;152}153154155