1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/doclint/tool/RunTest.java Mon Jan 21 10:00:46 2013 -0800 1.3 @@ -0,0 +1,200 @@ 1.4 +/* 1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 8006263 1.30 + * @summary Supplementary test cases needed for doclint 1.31 + */ 1.32 + 1.33 +import com.sun.source.util.JavacTask; 1.34 +import com.sun.tools.doclint.DocLint; 1.35 +import com.sun.tools.doclint.DocLint.BadArgs; 1.36 +import com.sun.tools.javac.api.JavacTool; 1.37 +import java.io.ByteArrayOutputStream; 1.38 +import java.io.IOException; 1.39 +import java.io.PrintStream; 1.40 +import java.io.PrintWriter; 1.41 +import java.io.StringWriter; 1.42 +import java.net.URI; 1.43 +import java.security.Permission; 1.44 +import java.util.Arrays; 1.45 +import java.util.List; 1.46 +import java.util.Objects; 1.47 +import javax.tools.JavaFileObject; 1.48 +import javax.tools.SimpleJavaFileObject; 1.49 + 1.50 +public class RunTest { 1.51 + static class SimpleSecurityManager extends SecurityManager { 1.52 + boolean allowExit = false; 1.53 + 1.54 + @Override 1.55 + public void checkExit(int status) { 1.56 + if (!allowExit) 1.57 + throw new SecurityException("System.exit(" + status + ")"); 1.58 + } 1.59 + @Override 1.60 + public void checkPermission(Permission perm) { } 1.61 + 1.62 + } 1.63 + 1.64 + public static void main(String... args) throws Exception { 1.65 + // if no security manager already installed, install one to 1.66 + // prevent System.exit 1.67 + SimpleSecurityManager secmgr = null; 1.68 + if (System.getSecurityManager() == null) { 1.69 + System.setSecurityManager(secmgr = new SimpleSecurityManager() { }); 1.70 + } 1.71 + 1.72 + try { 1.73 + new RunTest().run(); 1.74 + } finally { 1.75 + if (secmgr != null) 1.76 + secmgr.allowExit = true; 1.77 + } 1.78 + } 1.79 + 1.80 + void run() throws Exception { 1.81 + testMain(); 1.82 + testRun(); 1.83 + testInit(); 1.84 + testArgsNoFiles(); 1.85 + 1.86 + if (errors > 0) 1.87 + throw new Exception(errors + " errors found"); 1.88 + } 1.89 + 1.90 + void testMain() { 1.91 + System.err.println("test main(String[])"); 1.92 + testMain(true, "-help"); 1.93 + testMain(false, "-unknownOption"); 1.94 + } 1.95 + 1.96 + void testMain(boolean expectOK, String... args) { 1.97 + try { 1.98 + DocLint.main(args); 1.99 + if (!expectOK) 1.100 + error("expected SecurityException (from System.exit) not thrown"); 1.101 + } catch (SecurityException e) { 1.102 + System.err.println(e); 1.103 + if (expectOK) 1.104 + error("unexpected SecurityException caught"); 1.105 + } 1.106 + } 1.107 + 1.108 + void testRun() throws BadArgs, IOException { 1.109 + System.err.println("test run(String[])"); 1.110 + DocLint dl = new DocLint(); 1.111 + String[] args = { "-help" }; 1.112 + ByteArrayOutputStream baos = new ByteArrayOutputStream(); 1.113 + PrintStream ps = new PrintStream(baos); 1.114 + PrintStream prev = System.out; 1.115 + try { 1.116 + System.setOut(ps); 1.117 + dl.run(args); 1.118 + } finally { 1.119 + System.setOut(prev); 1.120 + } 1.121 + ps.close(); 1.122 + String stdout = baos.toString(); 1.123 + 1.124 + StringWriter sw = new StringWriter(); 1.125 + PrintWriter pw = new PrintWriter(sw); 1.126 + dl.run(pw, args); 1.127 + pw.close(); 1.128 + String direct = sw.toString(); 1.129 + 1.130 + if (!stdout.equals(direct)) { 1.131 + error("unexpected output"); 1.132 + System.err.println("EXPECT>>" + direct + "<<"); 1.133 + System.err.println("FOUND>>" + stdout + "<<"); 1.134 + } 1.135 + } 1.136 + 1.137 + void testInit() { 1.138 + System.err.println("test init"); 1.139 + DocLint dl = new DocLint(); 1.140 + String name = dl.getName(); 1.141 + if (!Objects.equals(name, "doclint")) 1.142 + error("unexpected result for DocLint.getName()"); 1.143 + 1.144 + List<? extends JavaFileObject> files = 1.145 + Arrays.asList(createFile("Test.java", "/** &0; */ class Test{ }")); 1.146 + String[] goodArgs = { "-Xmsgs" }; 1.147 + testInit(true, goodArgs, files); 1.148 + 1.149 + String[] badArgs = { "-unknown" }; 1.150 + testInit(false, badArgs, files); 1.151 + } 1.152 + 1.153 + void testInit(boolean expectOK, String[] args, List<? extends JavaFileObject> files) { 1.154 + JavacTool javac = JavacTool.create(); 1.155 + JavacTask task = javac.getTask(null, null, null, null, null, files); 1.156 + try { 1.157 + DocLint dl = new DocLint(); 1.158 + dl.init(task, args, true); 1.159 + if (!expectOK) 1.160 + error("expected IllegalArgumentException not thrown"); 1.161 + task.call(); 1.162 + } catch (IllegalArgumentException e) { 1.163 + System.err.println(e); 1.164 + if (expectOK) 1.165 + error("unexpected IllegalArgumentException caught"); 1.166 + } 1.167 + } 1.168 + 1.169 + void testArgsNoFiles() throws BadArgs, IOException { 1.170 + System.err.println("test args, no files"); 1.171 + DocLint dl = new DocLint(); 1.172 + 1.173 + StringWriter sw = new StringWriter(); 1.174 + PrintWriter pw = new PrintWriter(sw); 1.175 + dl.run(pw, "-Xmsgs"); 1.176 + pw.close(); 1.177 + String out = sw.toString(); 1.178 + 1.179 + String expect = "no files given"; 1.180 + if (!Objects.equals(out.trim(), expect)) { 1.181 + error("unexpected output"); 1.182 + System.err.println("EXPECT>>" + expect + "<<"); 1.183 + System.err.println("FOUND>>" + out + "<<"); 1.184 + } 1.185 + 1.186 + } 1.187 + 1.188 + JavaFileObject createFile(String name, final String body) { 1.189 + return new SimpleJavaFileObject(URI.create(name), JavaFileObject.Kind.SOURCE) { 1.190 + @Override 1.191 + public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { 1.192 + return body; 1.193 + } 1.194 + }; 1.195 + } 1.196 + 1.197 + void error(String msg) { 1.198 + System.err.println("Error: " + msg); 1.199 + errors++; 1.200 + } 1.201 + 1.202 + int errors; 1.203 +}