aoqi@0: /* aoqi@0: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 8006263 aoqi@0: * @summary Supplementary test cases needed for doclint aoqi@0: */ aoqi@0: aoqi@0: import com.sun.source.util.JavacTask; aoqi@0: import com.sun.tools.doclint.DocLint; aoqi@0: import com.sun.tools.doclint.DocLint.BadArgs; aoqi@0: import com.sun.tools.javac.api.JavacTool; aoqi@0: import java.io.ByteArrayOutputStream; aoqi@0: import java.io.IOException; aoqi@0: import java.io.PrintStream; aoqi@0: import java.io.PrintWriter; aoqi@0: import java.io.StringWriter; aoqi@0: import java.net.URI; aoqi@0: import java.security.Permission; aoqi@0: import java.util.Arrays; aoqi@0: import java.util.List; aoqi@0: import java.util.Objects; aoqi@0: import javax.tools.JavaFileObject; aoqi@0: import javax.tools.SimpleJavaFileObject; aoqi@0: aoqi@0: public class RunTest { aoqi@0: static class SimpleSecurityManager extends SecurityManager { aoqi@0: boolean allowExit = false; aoqi@0: aoqi@0: @Override aoqi@0: public void checkExit(int status) { aoqi@0: if (!allowExit) aoqi@0: throw new SecurityException("System.exit(" + status + ")"); aoqi@0: } aoqi@0: @Override aoqi@0: public void checkPermission(Permission perm) { } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: public static void main(String... args) throws Exception { aoqi@0: // if no security manager already installed, install one to aoqi@0: // prevent System.exit aoqi@0: SimpleSecurityManager secmgr = null; aoqi@0: if (System.getSecurityManager() == null) { aoqi@0: System.setSecurityManager(secmgr = new SimpleSecurityManager() { }); aoqi@0: } aoqi@0: aoqi@0: try { aoqi@0: new RunTest().run(); aoqi@0: } finally { aoqi@0: if (secmgr != null) aoqi@0: secmgr.allowExit = true; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void run() throws Exception { aoqi@0: testMain(); aoqi@0: testRun(); aoqi@0: testInit(); aoqi@0: testArgsNoFiles(); aoqi@0: aoqi@0: if (errors > 0) aoqi@0: throw new Exception(errors + " errors found"); aoqi@0: } aoqi@0: aoqi@0: void testMain() { aoqi@0: System.err.println("test main(String[])"); aoqi@0: testMain(true, "-help"); aoqi@0: testMain(false, "-unknownOption"); aoqi@0: } aoqi@0: aoqi@0: void testMain(boolean expectOK, String... args) { aoqi@0: try { aoqi@0: DocLint.main(args); aoqi@0: if (!expectOK) aoqi@0: error("expected SecurityException (from System.exit) not thrown"); aoqi@0: } catch (SecurityException e) { aoqi@0: System.err.println(e); aoqi@0: if (expectOK) aoqi@0: error("unexpected SecurityException caught"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void testRun() throws BadArgs, IOException { aoqi@0: System.err.println("test run(String[])"); aoqi@0: DocLint dl = new DocLint(); aoqi@0: String[] args = { "-help" }; aoqi@0: ByteArrayOutputStream baos = new ByteArrayOutputStream(); aoqi@0: PrintStream ps = new PrintStream(baos); aoqi@0: PrintStream prev = System.out; aoqi@0: try { aoqi@0: System.setOut(ps); aoqi@0: dl.run(args); aoqi@0: } finally { aoqi@0: System.setOut(prev); aoqi@0: } aoqi@0: ps.close(); aoqi@0: String stdout = baos.toString(); aoqi@0: aoqi@0: StringWriter sw = new StringWriter(); aoqi@0: PrintWriter pw = new PrintWriter(sw); aoqi@0: dl.run(pw, args); aoqi@0: pw.close(); aoqi@0: String direct = sw.toString(); aoqi@0: aoqi@0: if (!stdout.equals(direct)) { aoqi@0: error("unexpected output"); aoqi@0: System.err.println("EXPECT>>" + direct + "<<"); aoqi@0: System.err.println("FOUND>>" + stdout + "<<"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void testInit() { aoqi@0: System.err.println("test init"); aoqi@0: DocLint dl = new DocLint(); aoqi@0: String name = dl.getName(); aoqi@0: if (!Objects.equals(name, "doclint")) aoqi@0: error("unexpected result for DocLint.getName()"); aoqi@0: aoqi@0: List files = aoqi@0: Arrays.asList(createFile("Test.java", "/** &0; */ class Test{ }")); aoqi@0: String[] goodArgs = { "-Xmsgs" }; aoqi@0: testInit(true, goodArgs, files); aoqi@0: aoqi@0: String[] badArgs = { "-unknown" }; aoqi@0: testInit(false, badArgs, files); aoqi@0: } aoqi@0: aoqi@0: void testInit(boolean expectOK, String[] args, List files) { aoqi@0: JavacTool javac = JavacTool.create(); aoqi@0: JavacTask task = javac.getTask(null, null, null, null, null, files); aoqi@0: try { aoqi@0: DocLint dl = new DocLint(); aoqi@0: dl.init(task, args, true); aoqi@0: if (!expectOK) aoqi@0: error("expected IllegalArgumentException not thrown"); aoqi@0: task.call(); aoqi@0: } catch (IllegalArgumentException e) { aoqi@0: System.err.println(e); aoqi@0: if (expectOK) aoqi@0: error("unexpected IllegalArgumentException caught"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void testArgsNoFiles() throws BadArgs, IOException { aoqi@0: System.err.println("test args, no files"); aoqi@0: DocLint dl = new DocLint(); aoqi@0: aoqi@0: StringWriter sw = new StringWriter(); aoqi@0: PrintWriter pw = new PrintWriter(sw); aoqi@0: dl.run(pw, "-Xmsgs"); aoqi@0: pw.close(); aoqi@0: String out = sw.toString(); aoqi@0: aoqi@0: String expect = "No files given"; aoqi@0: if (!Objects.equals(out.trim(), expect)) { aoqi@0: error("unexpected output"); aoqi@0: System.err.println("EXPECT>>" + expect + "<<"); aoqi@0: System.err.println("FOUND>>" + out + "<<"); aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: JavaFileObject createFile(String name, final String body) { aoqi@0: return new SimpleJavaFileObject(URI.create(name), JavaFileObject.Kind.SOURCE) { aoqi@0: @Override aoqi@0: public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { aoqi@0: return body; aoqi@0: } aoqi@0: }; aoqi@0: } aoqi@0: aoqi@0: void error(String msg) { aoqi@0: System.err.println("Error: " + msg); aoqi@0: errors++; aoqi@0: } aoqi@0: aoqi@0: int errors; aoqi@0: }