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