test/tools/javap/output/Tester.java

Fri, 03 May 2013 09:56:56 -0700

author
jjg
date
Fri, 03 May 2013 09:56:56 -0700
changeset 1721
abd153854f16
parent 1643
1f8c28134ffc
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8012728: Normalize @ignore comments on langtools tests
Reviewed-by: vromero, mcimadamore

jjg@1643 1 /*
jjg@1643 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
jjg@1643 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1643 4 *
jjg@1643 5 * This code is free software; you can redistribute it and/or modify it
jjg@1643 6 * under the terms of the GNU General Public License version 2 only, as
jjg@1643 7 * published by the Free Software Foundation.
jjg@1643 8 *
jjg@1643 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1643 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1643 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1643 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@1643 13 * accompanied this code).
jjg@1643 14 *
jjg@1643 15 * You should have received a copy of the GNU General Public License version
jjg@1643 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1643 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1643 18 *
jjg@1643 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1643 20 * or visit www.oracle.com if you need additional information or have any
jjg@1643 21 * questions.
jjg@1643 22 */
jjg@1643 23
jjg@1643 24 import java.io.*;
jjg@1643 25 import java.util.*;
jjg@1643 26 import java.lang.annotation.*;
jjg@1643 27 import java.lang.reflect.InvocationTargetException;
jjg@1643 28
jjg@1643 29 /**
jjg@1643 30 * {@code Tester} is an abstract test-driver that provides the logic
jjg@1643 31 * to execute test-cases, grouped by test classes.
jjg@1643 32 * A test class is a main class extending this class, that instantiate
jjg@1643 33 * itself, and calls the {@link run} method, passing any command line
jjg@1643 34 * arguments.
jjg@1643 35 * <p>
jjg@1643 36 * The {@code run} method, expects arguments to identify test-case classes.
jjg@1643 37 * A test-case class is a class extending the test class, and annotated
jjg@1643 38 * with {@code TestCase}.
jjg@1643 39 * <p>
jjg@1643 40 * If no test-cases are specified, the test class directory is searched for
jjg@1643 41 * co-located test-case classes (i.e. any class extending the test class,
jjg@1643 42 * annotated with {@code TestCase}).
jjg@1643 43 * <p>
jjg@1643 44 * Besides serving to group test-cases, extending the driver allow
jjg@1643 45 * setting up a test-case template, and possibly overwrite default
jjg@1643 46 * test-driver behaviour.
jjg@1643 47 */
jjg@1643 48 public abstract class Tester {
jjg@1643 49
jjg@1643 50 private static boolean debug = false;
jjg@1643 51 private static final PrintStream out = System.err;
jjg@1643 52 private static final PrintStream err = System.err;
jjg@1643 53
jjg@1643 54
jjg@1643 55 protected void run(String... args) throws Exception {
jjg@1643 56
jjg@1643 57 final File classesdir = new File(System.getProperty("test.classes", "."));
jjg@1643 58
jjg@1643 59 String[] classNames = args;
jjg@1643 60
jjg@1643 61 // If no test-cases are specified, we regard all co-located classes
jjg@1643 62 // as potential test-cases.
jjg@1643 63 if (args.length == 0) {
jjg@1643 64 final String pattern = ".*\\.class";
jjg@1643 65 final File classFiles[] = classesdir.listFiles(new FileFilter() {
jjg@1643 66 public boolean accept(File f) {
jjg@1643 67 return f.getName().matches(pattern);
jjg@1643 68 }
jjg@1643 69 });
jjg@1643 70 ArrayList<String> names = new ArrayList<String>(classFiles.length);
jjg@1643 71 for (File f : classFiles) {
jjg@1643 72 String fname = f.getName();
jjg@1643 73 names.add(fname.substring(0, fname.length() -6));
jjg@1643 74 }
jjg@1643 75 classNames = names.toArray(new String[names.size()]);
jjg@1643 76 } else {
jjg@1643 77 debug = true;
jjg@1643 78 }
jjg@1643 79 // Test-cases must extend the driver type, and be marked
jjg@1643 80 // @TestCase. Other arguments (classes) are ignored.
jjg@1643 81 // Test-cases are instantiated, and thereby executed.
jjg@1643 82 for (String clname : classNames) {
jjg@1643 83 try {
jjg@1643 84 final Class tclass = Class.forName(clname);
jjg@1643 85 if (!getClass().isAssignableFrom(tclass)) continue;
jjg@1643 86 TestCase anno = (TestCase) tclass.getAnnotation(TestCase.class);
jjg@1643 87 if (anno == null) continue;
jjg@1643 88 if (!debug) {
jjg@1643 89 ignore i = (ignore) tclass.getAnnotation(ignore.class);
jjg@1643 90 if (i != null) {
jjg@1643 91 out.println("Ignore: " + clname);
jjg@1643 92 ignored++;
jjg@1643 93 continue;
jjg@1643 94 }
jjg@1643 95 }
jjg@1643 96 out.println("TestCase: " + clname);
jjg@1643 97 cases++;
jjg@1643 98 Tester tc = (Tester) tclass.getConstructor().newInstance();
jjg@1643 99 if (tc.errors > 0) {
jjg@1643 100 error("" + tc.errors + " test points failed in " + clname);
jjg@1643 101 errors += tc.errors - 1;
jjg@1643 102 fcases++;
jjg@1643 103 }
jjg@1643 104 } catch(ReflectiveOperationException roe) {
jjg@1643 105 error("Warning: " + clname + " - ReflectiveOperationException");
jjg@1643 106 roe.printStackTrace(err);
jjg@1643 107 } catch(Exception unknown) {
jjg@1643 108 error("Warning: " + clname + " - uncaught exception");
jjg@1643 109 unknown.printStackTrace(err);
jjg@1643 110 }
jjg@1643 111 }
jjg@1643 112
jjg@1643 113 String imsg = ignored > 0 ? " (" + ignored + " ignored)" : "";
jjg@1643 114 if (errors > 0)
jjg@1643 115 throw new Error(errors + " error, in " + fcases + " of " + cases + " test-cases" + imsg);
jjg@1643 116 else
jjg@1643 117 err.println("" + cases + " test-cases executed" + imsg + ", no errors");
jjg@1643 118 }
jjg@1643 119
jjg@1643 120
jjg@1643 121 /**
jjg@1643 122 * Test-cases must be marked with the {@code TestCase} annotation,
jjg@1643 123 * as well as extend {@code Tester} (or an driver extension
jjg@1643 124 * specified as the first argument to the {@code main()} method.
jjg@1643 125 */
jjg@1643 126 @Retention(RetentionPolicy.RUNTIME)
jjg@1643 127 @interface TestCase { }
jjg@1643 128
jjg@1643 129 /**
jjg@1643 130 * Individual test-cases failing due to product bugs, may temporarily
jjg@1721 131 * be excluded by marking them like this, (where "at-" is replaced by "@")
jjg@1721 132 * at-ignore // 1234567: bug synopsis
jjg@1643 133 */
jjg@1643 134 @Retention(RetentionPolicy.RUNTIME)
jjg@1643 135 @interface ignore { }
jjg@1643 136
jjg@1643 137 /**
jjg@1643 138 * Test-cases are classes extending {@code Tester}, and
jjg@1643 139 * calling {@link setSrc}, followed by one or more invocations
jjg@1643 140 * of {@link verify} in the body of the constructor.
jjg@1643 141 * <p>
jjg@1643 142 * Sets a default test-case template, which is empty except
jjg@1643 143 * for a key of {@code "TESTCASE"}.
jjg@1643 144 * Subclasses will typically call {@code setSrc(TestSource)}
jjg@1643 145 * to setup a useful test-case template.
jjg@1643 146 */
jjg@1643 147 public Tester() {
jjg@1643 148 this.testCase = this.getClass().getName();
jjg@1643 149 src = new TestSource("TESTCASE");
jjg@1643 150 }
jjg@1643 151
jjg@1643 152 /**
jjg@1643 153 * Set the top-level source template.
jjg@1643 154 */
jjg@1643 155 protected Tester setSrc(TestSource src) {
jjg@1643 156 this.src = src;
jjg@1643 157 return this;
jjg@1643 158 }
jjg@1643 159
jjg@1643 160 /**
jjg@1643 161 * Convenience method for calling {@code innerSrc("TESTCASE", ...)}.
jjg@1643 162 */
jjg@1643 163 protected Tester setSrc(String... lines) {
jjg@1643 164 return innerSrc("TESTCASE", lines);
jjg@1643 165 }
jjg@1643 166
jjg@1643 167 /**
jjg@1643 168 * Convenience method for calling {@code innerSrc(key, new TestSource(...))}.
jjg@1643 169 */
jjg@1643 170 protected Tester innerSrc(String key, String... lines) {
jjg@1643 171 return innerSrc(key, new TestSource(lines));
jjg@1643 172 }
jjg@1643 173
jjg@1643 174 /**
jjg@1643 175 * Specialize the testcase template, setting replacement content
jjg@1643 176 * for the specified key.
jjg@1643 177 */
jjg@1643 178 protected Tester innerSrc(String key, TestSource content) {
jjg@1643 179 if (src == null) {
jjg@1643 180 src = new TestSource(key);
jjg@1643 181 }
jjg@1643 182 src.setInner(key, content);
jjg@1643 183 return this;
jjg@1643 184 }
jjg@1643 185
jjg@1643 186 /**
jjg@1643 187 * On the first invocation, call {@code execute()} to compile
jjg@1643 188 * the test-case source and process the resulting class(se)
jjg@1643 189 * into verifiable output.
jjg@1643 190 * <p>
jjg@1643 191 * Verify that the output matches each of the regular expressions
jjg@1643 192 * given as argument.
jjg@1643 193 * <p>
jjg@1643 194 * Any failure to match constitutes a test failure, but doesn't
jjg@1643 195 * abort the test-case.
jjg@1643 196 * <p>
jjg@1643 197 * Any exception (e.g. bad regular expression syntax) results in
jjg@1643 198 * a test failure, and aborts the test-case.
jjg@1643 199 */
jjg@1643 200 protected void verify(String... expect) {
jjg@1643 201 if (!didExecute) {
jjg@1643 202 try {
jjg@1643 203 execute();
jjg@1643 204 } catch(Exception ue) {
jjg@1643 205 throw new Error(ue);
jjg@1643 206 } finally {
jjg@1643 207 didExecute = true;
jjg@1643 208 }
jjg@1643 209 }
jjg@1643 210 if (output == null) {
jjg@1643 211 error("output is null");
jjg@1643 212 return;
jjg@1643 213 }
jjg@1643 214 for (String e: expect) {
jjg@1643 215 // Escape regular expressions (to allow input to be literals).
jjg@1643 216 // Notice, characters to be escaped are themselves identified
jjg@1643 217 // using regular expressions
jjg@1643 218 String rc[] = { "(", ")", "[", "]", "{", "}", "$" };
jjg@1643 219 for (String c : rc) {
jjg@1643 220 e = e.replace(c, "\\" + c);
jjg@1643 221 }
jjg@1643 222 // DEBUG: Uncomment this to test modulo constant pool index.
jjg@1643 223 // e = e.replaceAll("#[0-9]{2}", "#[0-9]{2}");
jjg@1643 224 if (!output.matches("(?s).*" + e + ".*")) {
jjg@1643 225 if (!didPrint) {
jjg@1643 226 out.println(output);
jjg@1643 227 didPrint = true;
jjg@1643 228 }
jjg@1643 229 error("not matched: '" + e + "'");
jjg@1643 230 } else if(debug) {
jjg@1643 231 out.println("matched: '" + e + "'");
jjg@1643 232 }
jjg@1643 233 }
jjg@1643 234 }
jjg@1643 235
jjg@1643 236 /**
jjg@1643 237 * Calls {@code writeTestFile()} to write out the test-case source
jjg@1643 238 * content to a file, then call {@code compileTestFile()} to
jjg@1643 239 * compile it, and finally run the {@link process} method to produce
jjg@1643 240 * verifiable output. The default {@code process} method runs javap.
jjg@1643 241 * <p>
jjg@1643 242 * If an exception occurs, it results in a test failure, and
jjg@1643 243 * aborts the test-case.
jjg@1643 244 */
jjg@1643 245 protected void execute() throws IOException {
jjg@1643 246 err.println("TestCase: " + testCase);
jjg@1643 247 writeTestFile();
jjg@1643 248 compileTestFile();
jjg@1643 249 process();
jjg@1643 250 }
jjg@1643 251
jjg@1643 252 /**
jjg@1643 253 * Generate java source from test-case.
jjg@1643 254 * TBD: change to use javaFileObject, possibly make
jjg@1643 255 * this class extend JavaFileObject.
jjg@1643 256 */
jjg@1643 257 protected void writeTestFile() throws IOException {
jjg@1643 258 javaFile = new File("Test.java");
jjg@1643 259 FileWriter fw = new FileWriter(javaFile);
jjg@1643 260 BufferedWriter bw = new BufferedWriter(fw);
jjg@1643 261 PrintWriter pw = new PrintWriter(bw);
jjg@1643 262 for (String line : src) {
jjg@1643 263 pw.println(line);
jjg@1643 264 if (debug) out.println(line);
jjg@1643 265 }
jjg@1643 266 pw.close();
jjg@1643 267 }
jjg@1643 268
jjg@1643 269 /**
jjg@1643 270 * Compile the Java source code.
jjg@1643 271 */
jjg@1643 272 protected void compileTestFile() {
jjg@1643 273 String path = javaFile.getPath();
jjg@1643 274 String params[] = { "-source", "1.8", "-g", path };
jjg@1643 275 int rc = com.sun.tools.javac.Main.compile(params);
jjg@1643 276 if (rc != 0)
jjg@1643 277 throw new Error("compilation failed. rc=" + rc);
jjg@1643 278 classFile = new File(path.substring(0, path.length() - 5) + ".class");
jjg@1643 279 }
jjg@1643 280
jjg@1643 281
jjg@1643 282 /**
jjg@1643 283 * Process class file to generate output for verification.
jjg@1643 284 * The default implementation simply runs javap. This might be
jjg@1643 285 * overwritten to generate output in a different manner.
jjg@1643 286 */
jjg@1643 287 protected void process() {
jjg@1643 288 String testClasses = "."; //System.getProperty("test.classes", ".");
jjg@1643 289 StringWriter sw = new StringWriter();
jjg@1643 290 PrintWriter pw = new PrintWriter(sw);
jjg@1643 291 String[] args = { "-v", "-classpath", testClasses, "Test" };
jjg@1643 292 int rc = com.sun.tools.javap.Main.run(args, pw);
jjg@1643 293 if (rc != 0)
jjg@1643 294 throw new Error("javap failed. rc=" + rc);
jjg@1643 295 pw.close();
jjg@1643 296 output = sw.toString();
jjg@1643 297 if (debug) {
jjg@1643 298 out.println(output);
jjg@1643 299 didPrint = true;
jjg@1643 300 }
jjg@1643 301
jjg@1643 302 }
jjg@1643 303
jjg@1643 304
jjg@1643 305 private String testCase;
jjg@1643 306 private TestSource src;
jjg@1643 307 private File javaFile = null;
jjg@1643 308 private File classFile = null;
jjg@1643 309 private String output = null;
jjg@1643 310 private boolean didExecute = false;
jjg@1643 311 private boolean didPrint = false;
jjg@1643 312
jjg@1643 313
jjg@1643 314 protected void error(String msg) {
jjg@1643 315 err.println("Error: " + msg);
jjg@1643 316 errors++;
jjg@1643 317 }
jjg@1643 318
jjg@1643 319 private int cases;
jjg@1643 320 private int fcases;
jjg@1643 321 private int errors;
jjg@1643 322 private int ignored;
jjg@1643 323
jjg@1643 324 /**
jjg@1643 325 * The TestSource class provides a simple container for
jjg@1643 326 * test cases. It contains an array of source code lines,
jjg@1643 327 * where zero or more lines may be markers for nested lines.
jjg@1643 328 * This allows representing templates, with specialization.
jjg@1643 329 * <P>
jjg@1643 330 * This may be generalized to support more advance combo
jjg@1643 331 * tests, but presently it's only used with a static template,
jjg@1643 332 * and one level of specialization.
jjg@1643 333 */
jjg@1643 334 public class TestSource implements Iterable<String> {
jjg@1643 335
jjg@1643 336 private String[] lines;
jjg@1643 337 private Hashtable<String, TestSource> innerSrc;
jjg@1643 338
jjg@1643 339 public TestSource(String... lines) {
jjg@1643 340 this.lines = lines;
jjg@1643 341 innerSrc = new Hashtable<String, TestSource>();
jjg@1643 342 }
jjg@1643 343
jjg@1643 344 public void setInner(String key, TestSource inner) {
jjg@1643 345 innerSrc.put(key, inner);
jjg@1643 346 }
jjg@1643 347
jjg@1643 348 public void setInner(String key, String... lines) {
jjg@1643 349 innerSrc.put(key, new TestSource(lines));
jjg@1643 350 }
jjg@1643 351
jjg@1643 352 public Iterator<String> iterator() {
jjg@1643 353 return new LineIterator();
jjg@1643 354 }
jjg@1643 355
jjg@1643 356 private class LineIterator implements Iterator<String> {
jjg@1643 357
jjg@1643 358 int nextLine = 0;
jjg@1643 359 Iterator<String> innerIt = null;
jjg@1643 360
jjg@1643 361 public boolean hasNext() {
jjg@1643 362 return nextLine < lines.length;
jjg@1643 363 }
jjg@1643 364
jjg@1643 365 public String next() {
jjg@1643 366 if (!hasNext()) throw new NoSuchElementException();
jjg@1643 367 String str = lines[nextLine];
jjg@1643 368 TestSource inner = innerSrc.get(str);
jjg@1643 369 if (inner == null) {
jjg@1643 370 nextLine++;
jjg@1643 371 return str;
jjg@1643 372 }
jjg@1643 373 if (innerIt == null) {
jjg@1643 374 innerIt = inner.iterator();
jjg@1643 375 }
jjg@1643 376 if (innerIt.hasNext()) {
jjg@1643 377 return innerIt.next();
jjg@1643 378 }
jjg@1643 379 innerIt = null;
jjg@1643 380 nextLine++;
jjg@1643 381 return next();
jjg@1643 382 }
jjg@1643 383
jjg@1643 384 public void remove() {
jjg@1643 385 throw new UnsupportedOperationException();
jjg@1643 386 }
jjg@1643 387 }
jjg@1643 388 }
jjg@1643 389 }

mercurial