test/tools/doclint/tool/RunTest.java

Wed, 24 Sep 2014 11:38:26 -0700

author
katleman
date
Wed, 24 Sep 2014 11:38:26 -0700
changeset 2562
ed1a48bedfa8
parent 1796
242bcad5be74
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8u40-b07 for changeset 2fa3858a281f

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

mercurial