test/tools/doclint/tool/RunTest.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 1796
242bcad5be74
parent 0
959103a6100f
permissions
-rw-r--r--

merge

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

mercurial