Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/doclint/DocLintTester.java
32285 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.File;24import java.util.ArrayList;25import java.util.List;2627import com.sun.tools.doclint.DocLint;28import com.sun.tools.doclint.DocLint.BadArgs;29import java.io.BufferedReader;30import java.io.FileReader;31import java.io.IOException;32import java.io.PrintWriter;33import java.io.Reader;34import java.io.StringWriter;35import java.util.regex.Matcher;36import java.util.regex.Pattern;373839public class DocLintTester {4041public static void main(String... args) throws Exception {42new DocLintTester().run(args);43}4445public void run(String... args) throws Exception {46String testSrc = System.getProperty("test.src");4748boolean badArgs = false;49File refFile = null;50List<String> opts = new ArrayList<String>();51List<File> files = new ArrayList<File>();52for (int i = 0; i < args.length; i++) {53String arg = args[i];54if (arg.equals("-ref")) {55refFile = new File(testSrc, args[++i]);56} else if (arg.equals("-badargs")) {57badArgs = true;58} else if (arg.startsWith("-Xmsgs")) {59opts.add(arg);60} else if (arg.startsWith("-XcustomTags")) {61opts.add(arg);62} else if (arg.startsWith("-")) {63opts.add(arg);64if (i < args.length - 1 && !args[i+1].startsWith("-"))65opts.add(args[++i]);66} else67files.add(new File(testSrc, arg));68}6970check(opts, files, badArgs, refFile);7172if (errors > 0)73throw new Exception(errors + " errors occurred");74}7576void check(List<String> opts, List<File> files, boolean expectBadArgs, File refFile) throws Exception {77List<String> args = new ArrayList<String>();78args.addAll(opts);79for (File file: files)80args.add(file.getPath());8182StringWriter sw = new StringWriter();83PrintWriter pw = new PrintWriter(sw);84try {85new DocLint().run(pw, args.toArray(new String[args.size()]));86if (expectBadArgs)87error("expected exception not thrown");88} catch (BadArgs e) {89if (!expectBadArgs)90error("unexpected exception caught: " + e);91}92pw.flush();93String out = normalizeNewlines(removeFileNames(sw.toString())).trim();94if (out != null)95System.err.println("Output:\n" + out);9697if (refFile == null) {98if (!out.isEmpty())99error("unexpected output");100} else {101String expect = readFile(refFile);102if (!expect.equals(out)) {103error("expected output not found");104System.err.println("EXPECT>>" + expect + "<<");105System.err.println(" FOUND>>" + out + "<<");106}107}108}109110String readFile(File file) throws IOException {111StringBuilder sb = new StringBuilder();112Reader in = new BufferedReader(new FileReader(file));113try {114char[] buf = new char[1024];115int n;116while ((n = in.read(buf)) != -1)117sb.append(buf, 0, n);118} finally {119in.close();120}121return sb.toString().trim();122}123124private static final Pattern dirFileLine = Pattern.compile(125"(?m)" // multi-line mode126+ "^(.*?)" // directory part of file name127+ "([-A-Za-z0-9.]+:[0-9]+:)"); // file name and line number128129String removeFileNames(String s) {130Matcher m = dirFileLine.matcher(s);131StringBuffer sb = new StringBuffer();132while (m.find()) {133m.appendReplacement(sb, "$2");134}135m.appendTail(sb);136return sb.toString();137}138139private static final String nl = System.getProperty("line.separator");140String normalizeNewlines(String s) {141return (nl.equals("\n") ? s : s.replace(nl, "\n"));142}143144145void error(String msg) {146System.err.println("Error: " + msg);147errors++;148}149150int errors;151}152153154