test/tools/doclint/tool/RunTest.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
equal deleted inserted replaced
-1:000000000000 0:959103a6100f
1 /*
2 * Copyright (c) 2013, 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 */
29
30 import com.sun.source.util.JavacTask;
31 import com.sun.tools.doclint.DocLint;
32 import com.sun.tools.doclint.DocLint.BadArgs;
33 import com.sun.tools.javac.api.JavacTool;
34 import java.io.ByteArrayOutputStream;
35 import java.io.IOException;
36 import java.io.PrintStream;
37 import java.io.PrintWriter;
38 import java.io.StringWriter;
39 import java.net.URI;
40 import java.security.Permission;
41 import java.util.Arrays;
42 import java.util.List;
43 import java.util.Objects;
44 import javax.tools.JavaFileObject;
45 import javax.tools.SimpleJavaFileObject;
46
47 public class RunTest {
48 static class SimpleSecurityManager extends SecurityManager {
49 boolean allowExit = false;
50
51 @Override
52 public void checkExit(int status) {
53 if (!allowExit)
54 throw new SecurityException("System.exit(" + status + ")");
55 }
56 @Override
57 public void checkPermission(Permission perm) { }
58
59 }
60
61 public static void main(String... args) throws Exception {
62 // if no security manager already installed, install one to
63 // prevent System.exit
64 SimpleSecurityManager secmgr = null;
65 if (System.getSecurityManager() == null) {
66 System.setSecurityManager(secmgr = new SimpleSecurityManager() { });
67 }
68
69 try {
70 new RunTest().run();
71 } finally {
72 if (secmgr != null)
73 secmgr.allowExit = true;
74 }
75 }
76
77 void run() throws Exception {
78 testMain();
79 testRun();
80 testInit();
81 testArgsNoFiles();
82
83 if (errors > 0)
84 throw new Exception(errors + " errors found");
85 }
86
87 void testMain() {
88 System.err.println("test main(String[])");
89 testMain(true, "-help");
90 testMain(false, "-unknownOption");
91 }
92
93 void testMain(boolean expectOK, String... args) {
94 try {
95 DocLint.main(args);
96 if (!expectOK)
97 error("expected SecurityException (from System.exit) not thrown");
98 } catch (SecurityException e) {
99 System.err.println(e);
100 if (expectOK)
101 error("unexpected SecurityException caught");
102 }
103 }
104
105 void testRun() throws BadArgs, IOException {
106 System.err.println("test run(String[])");
107 DocLint dl = new DocLint();
108 String[] args = { "-help" };
109 ByteArrayOutputStream baos = new ByteArrayOutputStream();
110 PrintStream ps = new PrintStream(baos);
111 PrintStream prev = System.out;
112 try {
113 System.setOut(ps);
114 dl.run(args);
115 } finally {
116 System.setOut(prev);
117 }
118 ps.close();
119 String stdout = baos.toString();
120
121 StringWriter sw = new StringWriter();
122 PrintWriter pw = new PrintWriter(sw);
123 dl.run(pw, args);
124 pw.close();
125 String direct = sw.toString();
126
127 if (!stdout.equals(direct)) {
128 error("unexpected output");
129 System.err.println("EXPECT>>" + direct + "<<");
130 System.err.println("FOUND>>" + stdout + "<<");
131 }
132 }
133
134 void testInit() {
135 System.err.println("test init");
136 DocLint dl = new DocLint();
137 String name = dl.getName();
138 if (!Objects.equals(name, "doclint"))
139 error("unexpected result for DocLint.getName()");
140
141 List<? extends JavaFileObject> files =
142 Arrays.asList(createFile("Test.java", "/** &0; */ class Test{ }"));
143 String[] goodArgs = { "-Xmsgs" };
144 testInit(true, goodArgs, files);
145
146 String[] badArgs = { "-unknown" };
147 testInit(false, badArgs, files);
148 }
149
150 void testInit(boolean expectOK, String[] args, List<? extends JavaFileObject> files) {
151 JavacTool javac = JavacTool.create();
152 JavacTask task = javac.getTask(null, null, null, null, null, files);
153 try {
154 DocLint dl = new DocLint();
155 dl.init(task, args, true);
156 if (!expectOK)
157 error("expected IllegalArgumentException not thrown");
158 task.call();
159 } catch (IllegalArgumentException e) {
160 System.err.println(e);
161 if (expectOK)
162 error("unexpected IllegalArgumentException caught");
163 }
164 }
165
166 void testArgsNoFiles() throws BadArgs, IOException {
167 System.err.println("test args, no files");
168 DocLint dl = new DocLint();
169
170 StringWriter sw = new StringWriter();
171 PrintWriter pw = new PrintWriter(sw);
172 dl.run(pw, "-Xmsgs");
173 pw.close();
174 String out = sw.toString();
175
176 String expect = "No files given";
177 if (!Objects.equals(out.trim(), expect)) {
178 error("unexpected output");
179 System.err.println("EXPECT>>" + expect + "<<");
180 System.err.println("FOUND>>" + out + "<<");
181 }
182
183 }
184
185 JavaFileObject createFile(String name, final String body) {
186 return new SimpleJavaFileObject(URI.create(name), JavaFileObject.Kind.SOURCE) {
187 @Override
188 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
189 return body;
190 }
191 };
192 }
193
194 void error(String msg) {
195 System.err.println("Error: " + msg);
196 errors++;
197 }
198
199 int errors;
200 }

mercurial