1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
|
/*
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import com.sun.tools.javac.api.JavacTool;
import com.sun.tools.javac.file.JavacFileManager;
import com.sun.tools.javac.util.Context;
import java.io.ByteArrayOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UncheckedIOException;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
public class OptionModesTester {
/** Marker annotation for test methods to be invoked by runTests. */
@Retention(RetentionPolicy.RUNTIME)
@interface Test { }
/**
* Run all methods annotated with @Test, and throw an exception if any
* errors are reported..
* Typically called on a tester object in main()
* @throws Exception if any errors occurred
*/
void runTests() throws Exception {
for (Method m: getClass().getDeclaredMethods()) {
Annotation a = m.getAnnotation(Test.class);
if (a != null) {
try {
out.println("Running test " + m.getName());
m.invoke(this);
} catch (InvocationTargetException e) {
Throwable cause = e.getCause();
throw (cause instanceof Exception) ? ((Exception) cause) : e;
}
out.println();
}
}
if (errors > 0)
throw new Exception(errors + " errors occurred");
}
TestResult runMain(String[] opts, String[] files) {
out.println("Main " + Arrays.toString(opts) + " " + Arrays.toString(files));
return run(new TestResult(opts), (tr, c, pw) -> {
com.sun.tools.javac.main.Main compiler =
new com.sun.tools.javac.main.Main("javac", pw);
int rc = compiler.compile(join(opts, files), c).exitCode;
tr.setResult(rc);
});
}
TestResult runCall(String[] opts, String[] files) {
out.println("Call " + Arrays.toString(opts) + " " + Arrays.toString(files));
return run(new TestResult(opts), (tr, c, pw) -> {
boolean ok = JavacTool.create()
.getTask(pw, null, null, Arrays.asList(opts), null, getFiles(files), c)
.call();
tr.setResult(ok);
});
}
TestResult runParse(String[] opts, String[] files) {
out.println("Parse " + Arrays.toString(opts) + " " + Arrays.toString(files));
return run(new TestResult(opts), (tr, c, pw) -> {
JavacTool.create()
.getTask(pw, null, null, Arrays.asList(opts), null, getFiles(files), c)
.parse();
tr.setResult(true);
});
}
TestResult runAnalyze(String[] opts, String[] files) {
out.println("Analyze " + Arrays.toString(opts) + " " + Arrays.toString(files));
return run(new TestResult(opts), (tr, c, pw) -> {
JavacTool.create()
.getTask(pw, null, null, Arrays.asList(opts), null, getFiles(files), c)
.analyze();
tr.setResult(true);
});
}
interface Runnable {
void run(TestResult tr, Context c, PrintWriter pw) throws IOException;
}
TestResult run(TestResult tr, Runnable r) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
StreamOutput sysOut = new StreamOutput(System.out, System::setOut);
StreamOutput sysErr = new StreamOutput(System.err, System::setErr);
Context context = new Context();
JavacFileManager.preRegister(context);
try {
r.run(tr, context, pw);
} catch (IllegalArgumentException | IllegalStateException | IOException e) {
tr.setThrown(e);
} finally {
try {
((JavacFileManager) context.get(JavaFileManager.class)).close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
tr.setLogs(sw.toString(), sysOut.close(), sysErr.close());
}
tr.setContext(context);
tr.show();
return tr;
}
enum Log { DIRECT, STDOUT, STDERR };
class TestResult {
final List<String> args;
Throwable thrown;
List<Throwable> suppressed = new ArrayList<>();
Object rc; // Number or Boolean
Map<Log, String> logs;
Context context;
TestResult(String... args) {
this.args = Arrays.asList(args);
}
TestResult(List<String> args, Iterable<? extends JavaFileObject> files) {
this.args = new ArrayList<>();
this.args.addAll(args);
for (JavaFileObject f: files)
this.args.add(f.getName());
}
void setResult(int rc) {
this.rc = rc;
}
void setResult(boolean ok) {
this.rc = ok ? 0 : 1;
}
void setSuppressed(Throwable thrown) {
this.suppressed.add(thrown);
}
void setThrown(Throwable thrown) {
this.thrown = thrown;
}
void setLogs(String direct, String stdOut, String stdErr) {
logs = new EnumMap<>(Log.class);
logs.put(Log.DIRECT, direct);
logs.put(Log.STDOUT, stdOut);
logs.put(Log.STDERR, stdErr);
}
void setContext(Context context) {
this.context = context;
}
final void show() {
String NL = System.getProperty("line.separator");
boolean needSep = false;
if (rc != null) {
out.print("rc:" + rc);
needSep = true;
}
if (thrown != null) {
if (needSep) out.print("; ");
out.print("thrown:" + thrown);
needSep = true;
}
if (!suppressed.isEmpty()) {
if (needSep) out.print("; ");
out.print("suppressed:" + suppressed);
needSep = true;
}
if (needSep)
out.println();
logs.forEach((k, v) -> {
if (!v.isEmpty()) {
out.println("javac/" + k + ":");
if (v.endsWith(NL))
out.print(v);
else
out.println(v);
}
});
}
TestResult checkOK() {
if (thrown != null) {
error("unexpected exception thrown: " + thrown);
} else if (rc == null) {
error("no result set");
} else if (rc != (Integer) 0 && rc != (Boolean) true) {
error("compilation failed unexpectedly; rc=" + rc);
}
return this;
}
TestResult checkResult(int expect) {
if (thrown != null) {
error("unexpected exception thrown: " + thrown);
} else if (rc != (Integer) expect) {
error("unexpected result: " + rc +", expected:" + expect);
}
return this;
}
TestResult checkResult(boolean expect) {
if (thrown != null) {
error("unexpected exception thrown: " + thrown);
} else if (rc != (Integer) (expect ? 0 : 1)) {
error("unexpected result: " + rc +", expected:" + expect);
}
return this;
}
TestResult checkLog(String... expects) {
return checkLog(Log.DIRECT, expects);
}
TestResult checkLog(Log l, String... expects) {
for (String e: expects) {
if (!logs.get(l).contains(e))
error("expected string not found: " + e);
}
return this;
}
TestResult checkIllegalArgumentException() {
return checkThrown(IllegalArgumentException.class);
}
TestResult checkIllegalStateException() {
return checkThrown(IllegalStateException.class);
}
TestResult checkThrown(Class<? extends Throwable> t) {
if (thrown == null)
error("expected exception not thrown: " + t);
else if (!t.isAssignableFrom(thrown.getClass()))
error("unexpected exception thrown: " + thrown + "; expected: " + t);
return this;
}
TestResult checkClass(String name) {
Path p = getOutDir().resolve(name.replace(".", "/") + ".class");
if (!Files.exists(p))
error("expected class not found: " + name + " (" + p + ")");
return this;
}
Path getOutDir() {
Iterator<String> iter = args.iterator();
while (iter.hasNext()) {
if (iter.next().equals("-d")) {
return Paths.get(iter.next());
}
}
return null;
}
}
/**
* Utility class to simplify the handling of temporarily setting a
* new stream for System.out or System.err.
*/
private static class StreamOutput {
// functional interface to set a stream.
private interface Initializer {
void set(PrintStream s);
}
private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
private final PrintStream ps = new PrintStream(baos);
private final PrintStream prev;
private final Initializer init;
StreamOutput(PrintStream s, Initializer init) {
prev = s;
init.set(ps);
this.init = init;
}
String close() {
init.set(prev);
ps.close();
return baos.toString();
}
}
List<JavaFileObject> getFiles(String... paths) {
List<JavaFileObject> files = new ArrayList<>();
for (JavaFileObject f : fm.getJavaFileObjects(paths))
files.add(f);
return files;
}
String toString(List<JavaFileObject> files) {
return files.stream().map(f -> f.getName()).collect(Collectors.toList()).toString();
}
void mkdirs(String path) throws IOException {
Files.createDirectories(Paths.get(path));
}
void writeFile(String path, String body) throws IOException {
Path p = Paths.get(path);
if (p.getParent() != null)
Files.createDirectories(p.getParent());
try (FileWriter w = new FileWriter(path)) {
w.write(body);
}
}
String[] join(String[] a, String[] b) {
String[] result = new String[a.length + b.length];
System.arraycopy(a, 0, result, 0, a.length);
System.arraycopy(b, 0, result, a.length, b.length);
return result;
}
void error(String message) {
out.print(">>>>> ");
out.println(message);
errors++;
}
StandardJavaFileManager fm =
ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null);
PrintStream out = System.err;
int errors;
}
|