1/* 2 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * @test 26 * @bug 8006263 27 * @summary Supplementary test cases needed for doclint 28 * @modules jdk.compiler/com.sun.tools.doclint 29 * jdk.compiler/com.sun.tools.javac.api 30 */ 31 32import com.sun.source.util.JavacTask; 33import com.sun.tools.doclint.DocLint; 34import com.sun.tools.doclint.DocLint.BadArgs; 35import com.sun.tools.javac.api.JavacTool; 36import java.io.ByteArrayOutputStream; 37import java.io.IOException; 38import java.io.PrintStream; 39import java.io.PrintWriter; 40import java.io.StringWriter; 41import java.net.URI; 42import java.security.Permission; 43import java.util.Arrays; 44import java.util.List; 45import java.util.Objects; 46import javax.tools.JavaFileObject; 47import javax.tools.SimpleJavaFileObject; 48 49public class RunTest { 50 static class SimpleSecurityManager extends SecurityManager { 51 boolean allowExit = false; 52 53 @Override 54 public void checkExit(int status) { 55 if (!allowExit) 56 throw new SecurityException("System.exit(" + status + ")"); 57 } 58 @Override 59 public void checkPermission(Permission perm) { } 60 61 } 62 63 public static void main(String... args) throws Exception { 64 // if no security manager already installed, install one to 65 // prevent System.exit 66 SimpleSecurityManager secmgr = null; 67 if (System.getSecurityManager() == null) { 68 System.setSecurityManager(secmgr = new SimpleSecurityManager() { }); 69 } 70 71 try { 72 new RunTest().run(); 73 } finally { 74 if (secmgr != null) 75 secmgr.allowExit = true; 76 } 77 } 78 79 void run() throws Exception { 80 testMain(); 81 testRun(); 82 testInit(); 83 testArgsNoFiles(); 84 85 if (errors > 0) 86 throw new Exception(errors + " errors found"); 87 } 88 89 void testMain() { 90 System.err.println("test main(String[])"); 91 testMain(true, "-help"); 92 testMain(false, "-unknownOption"); 93 } 94 95 void testMain(boolean expectOK, String... args) { 96 try { 97 DocLint.main(args); 98 if (!expectOK) 99 error("expected SecurityException (from System.exit) not thrown"); 100 } catch (SecurityException e) { 101 System.err.println(e); 102 if (expectOK) 103 error("unexpected SecurityException caught"); 104 } 105 } 106 107 void testRun() throws BadArgs, IOException { 108 System.err.println("test run(String[])"); 109 DocLint dl = new DocLint(); 110 String[] args = { "-help" }; 111 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 112 PrintStream ps = new PrintStream(baos); 113 PrintStream prev = System.out; 114 try { 115 System.setOut(ps); 116 dl.run(args); 117 } finally { 118 System.setOut(prev); 119 } 120 ps.close(); 121 String stdout = baos.toString(); 122 123 StringWriter sw = new StringWriter(); 124 PrintWriter pw = new PrintWriter(sw); 125 dl.run(pw, args); 126 pw.close(); 127 String direct = sw.toString(); 128 129 if (!stdout.equals(direct)) { 130 error("unexpected output"); 131 System.err.println("EXPECT>>" + direct + "<<"); 132 System.err.println("FOUND>>" + stdout + "<<"); 133 } 134 } 135 136 void testInit() { 137 System.err.println("test init"); 138 DocLint dl = new DocLint(); 139 String name = dl.getName(); 140 if (!Objects.equals(name, "doclint")) 141 error("unexpected result for DocLint.getName()"); 142 143 List<? extends JavaFileObject> files = 144 Arrays.asList(createFile("Test.java", "/** &0; */ class Test{ }")); 145 String[] goodArgs = { "-Xmsgs" }; 146 testInit(true, goodArgs, files); 147 148 String[] badArgs = { "-unknown" }; 149 testInit(false, badArgs, files); 150 } 151 152 void testInit(boolean expectOK, String[] args, List<? extends JavaFileObject> files) { 153 JavacTool javac = JavacTool.create(); 154 JavacTask task = javac.getTask(null, null, null, null, null, files); 155 try { 156 DocLint dl = new DocLint(); 157 dl.init(task, args, true); 158 if (!expectOK) 159 error("expected IllegalArgumentException not thrown"); 160 task.call(); 161 } catch (IllegalArgumentException e) { 162 System.err.println(e); 163 if (expectOK) 164 error("unexpected IllegalArgumentException caught"); 165 } 166 } 167 168 void testArgsNoFiles() throws BadArgs, IOException { 169 System.err.println("test args, no files"); 170 DocLint dl = new DocLint(); 171 172 StringWriter sw = new StringWriter(); 173 PrintWriter pw = new PrintWriter(sw); 174 dl.run(pw, "-Xmsgs"); 175 pw.close(); 176 String out = sw.toString(); 177 178 String expect = "No files given"; 179 if (!Objects.equals(out.trim(), expect)) { 180 error("unexpected output"); 181 System.err.println("EXPECT>>" + expect + "<<"); 182 System.err.println("FOUND>>" + out + "<<"); 183 } 184 185 } 186 187 JavaFileObject createFile(String name, final String body) { 188 return new SimpleJavaFileObject(URI.create(name), JavaFileObject.Kind.SOURCE) { 189 @Override 190 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { 191 return body; 192 } 193 }; 194 } 195 196 void error(String msg) { 197 System.err.println("Error: " + msg); 198 errors++; 199 } 200 201 int errors; 202} 203