aoqi@0: /* aoqi@0: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: import java.io.*; aoqi@0: import java.util.*; aoqi@0: import java.lang.annotation.*; aoqi@0: import java.lang.reflect.InvocationTargetException; aoqi@0: aoqi@0: /** aoqi@0: * {@code Tester} is an abstract test-driver that provides the logic aoqi@0: * to execute test-cases, grouped by test classes. aoqi@0: * A test class is a main class extending this class, that instantiate aoqi@0: * itself, and calls the {@link run} method, passing any command line aoqi@0: * arguments. aoqi@0: *
aoqi@0: * The {@code run} method, expects arguments to identify test-case classes. aoqi@0: * A test-case class is a class extending the test class, and annotated aoqi@0: * with {@code TestCase}. aoqi@0: *
aoqi@0: * If no test-cases are specified, the test class directory is searched for aoqi@0: * co-located test-case classes (i.e. any class extending the test class, aoqi@0: * annotated with {@code TestCase}). aoqi@0: *
aoqi@0: * Besides serving to group test-cases, extending the driver allow
aoqi@0: * setting up a test-case template, and possibly overwrite default
aoqi@0: * test-driver behaviour.
aoqi@0: */
aoqi@0: public abstract class Tester {
aoqi@0:
aoqi@0: private static boolean debug = false;
aoqi@0: private static final PrintStream out = System.err;
aoqi@0: private static final PrintStream err = System.err;
aoqi@0:
aoqi@0:
aoqi@0: protected void run(String... args) throws Exception {
aoqi@0:
aoqi@0: final File classesdir = new File(System.getProperty("test.classes", "."));
aoqi@0:
aoqi@0: String[] classNames = args;
aoqi@0:
aoqi@0: // If no test-cases are specified, we regard all co-located classes
aoqi@0: // as potential test-cases.
aoqi@0: if (args.length == 0) {
aoqi@0: final String pattern = ".*\\.class";
aoqi@0: final File classFiles[] = classesdir.listFiles(new FileFilter() {
aoqi@0: public boolean accept(File f) {
aoqi@0: return f.getName().matches(pattern);
aoqi@0: }
aoqi@0: });
aoqi@0: ArrayList
aoqi@0: * Sets a default test-case template, which is empty except
aoqi@0: * for a key of {@code "TESTCASE"}.
aoqi@0: * Subclasses will typically call {@code setSrc(TestSource)}
aoqi@0: * to setup a useful test-case template.
aoqi@0: */
aoqi@0: public Tester() {
aoqi@0: this.testCase = this.getClass().getName();
aoqi@0: src = new TestSource("TESTCASE");
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Set the top-level source template.
aoqi@0: */
aoqi@0: protected Tester setSrc(TestSource src) {
aoqi@0: this.src = src;
aoqi@0: return this;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Convenience method for calling {@code innerSrc("TESTCASE", ...)}.
aoqi@0: */
aoqi@0: protected Tester setSrc(String... lines) {
aoqi@0: return innerSrc("TESTCASE", lines);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Convenience method for calling {@code innerSrc(key, new TestSource(...))}.
aoqi@0: */
aoqi@0: protected Tester innerSrc(String key, String... lines) {
aoqi@0: return innerSrc(key, new TestSource(lines));
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Specialize the testcase template, setting replacement content
aoqi@0: * for the specified key.
aoqi@0: */
aoqi@0: protected Tester innerSrc(String key, TestSource content) {
aoqi@0: if (src == null) {
aoqi@0: src = new TestSource(key);
aoqi@0: }
aoqi@0: src.setInner(key, content);
aoqi@0: return this;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * On the first invocation, call {@code execute()} to compile
aoqi@0: * the test-case source and process the resulting class(se)
aoqi@0: * into verifiable output.
aoqi@0: *
aoqi@0: * Verify that the output matches each of the regular expressions
aoqi@0: * given as argument.
aoqi@0: *
aoqi@0: * Any failure to match constitutes a test failure, but doesn't
aoqi@0: * abort the test-case.
aoqi@0: *
aoqi@0: * Any exception (e.g. bad regular expression syntax) results in
aoqi@0: * a test failure, and aborts the test-case.
aoqi@0: */
aoqi@0: protected void verify(String... expect) {
aoqi@0: if (!didExecute) {
aoqi@0: try {
aoqi@0: execute();
aoqi@0: } catch(Exception ue) {
aoqi@0: throw new Error(ue);
aoqi@0: } finally {
aoqi@0: didExecute = true;
aoqi@0: }
aoqi@0: }
aoqi@0: if (output == null) {
aoqi@0: error("output is null");
aoqi@0: return;
aoqi@0: }
aoqi@0: for (String e: expect) {
aoqi@0: // Escape regular expressions (to allow input to be literals).
aoqi@0: // Notice, characters to be escaped are themselves identified
aoqi@0: // using regular expressions
aoqi@0: String rc[] = { "(", ")", "[", "]", "{", "}", "$" };
aoqi@0: for (String c : rc) {
aoqi@0: e = e.replace(c, "\\" + c);
aoqi@0: }
aoqi@0: // DEBUG: Uncomment this to test modulo constant pool index.
aoqi@0: // e = e.replaceAll("#[0-9]{2}", "#[0-9]{2}");
aoqi@0: if (!output.matches("(?s).*" + e + ".*")) {
aoqi@0: if (!didPrint) {
aoqi@0: out.println(output);
aoqi@0: didPrint = true;
aoqi@0: }
aoqi@0: error("not matched: '" + e + "'");
aoqi@0: } else if(debug) {
aoqi@0: out.println("matched: '" + e + "'");
aoqi@0: }
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Calls {@code writeTestFile()} to write out the test-case source
aoqi@0: * content to a file, then call {@code compileTestFile()} to
aoqi@0: * compile it, and finally run the {@link process} method to produce
aoqi@0: * verifiable output. The default {@code process} method runs javap.
aoqi@0: *
aoqi@0: * If an exception occurs, it results in a test failure, and
aoqi@0: * aborts the test-case.
aoqi@0: */
aoqi@0: protected void execute() throws IOException {
aoqi@0: err.println("TestCase: " + testCase);
aoqi@0: writeTestFile();
aoqi@0: compileTestFile();
aoqi@0: process();
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Generate java source from test-case.
aoqi@0: * TBD: change to use javaFileObject, possibly make
aoqi@0: * this class extend JavaFileObject.
aoqi@0: */
aoqi@0: protected void writeTestFile() throws IOException {
aoqi@0: javaFile = new File("Test.java");
aoqi@0: FileWriter fw = new FileWriter(javaFile);
aoqi@0: BufferedWriter bw = new BufferedWriter(fw);
aoqi@0: PrintWriter pw = new PrintWriter(bw);
aoqi@0: for (String line : src) {
aoqi@0: pw.println(line);
aoqi@0: if (debug) out.println(line);
aoqi@0: }
aoqi@0: pw.close();
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Compile the Java source code.
aoqi@0: */
aoqi@0: protected void compileTestFile() {
aoqi@0: String path = javaFile.getPath();
aoqi@0: String params[] = { "-source", "1.8", "-g", path };
aoqi@0: int rc = com.sun.tools.javac.Main.compile(params);
aoqi@0: if (rc != 0)
aoqi@0: throw new Error("compilation failed. rc=" + rc);
aoqi@0: classFile = new File(path.substring(0, path.length() - 5) + ".class");
aoqi@0: }
aoqi@0:
aoqi@0:
aoqi@0: /**
aoqi@0: * Process class file to generate output for verification.
aoqi@0: * The default implementation simply runs javap. This might be
aoqi@0: * overwritten to generate output in a different manner.
aoqi@0: */
aoqi@0: protected void process() {
aoqi@0: String testClasses = "."; //System.getProperty("test.classes", ".");
aoqi@0: StringWriter sw = new StringWriter();
aoqi@0: PrintWriter pw = new PrintWriter(sw);
aoqi@0: String[] args = { "-v", "-classpath", testClasses, "Test" };
aoqi@0: int rc = com.sun.tools.javap.Main.run(args, pw);
aoqi@0: if (rc != 0)
aoqi@0: throw new Error("javap failed. rc=" + rc);
aoqi@0: pw.close();
aoqi@0: output = sw.toString();
aoqi@0: if (debug) {
aoqi@0: out.println(output);
aoqi@0: didPrint = true;
aoqi@0: }
aoqi@0:
aoqi@0: }
aoqi@0:
aoqi@0:
aoqi@0: private String testCase;
aoqi@0: private TestSource src;
aoqi@0: private File javaFile = null;
aoqi@0: private File classFile = null;
aoqi@0: private String output = null;
aoqi@0: private boolean didExecute = false;
aoqi@0: private boolean didPrint = false;
aoqi@0:
aoqi@0:
aoqi@0: protected void error(String msg) {
aoqi@0: err.println("Error: " + msg);
aoqi@0: errors++;
aoqi@0: }
aoqi@0:
aoqi@0: private int cases;
aoqi@0: private int fcases;
aoqi@0: private int errors;
aoqi@0: private int ignored;
aoqi@0:
aoqi@0: /**
aoqi@0: * The TestSource class provides a simple container for
aoqi@0: * test cases. It contains an array of source code lines,
aoqi@0: * where zero or more lines may be markers for nested lines.
aoqi@0: * This allows representing templates, with specialization.
aoqi@0: *
aoqi@0: * This may be generalized to support more advance combo
aoqi@0: * tests, but presently it's only used with a static template,
aoqi@0: * and one level of specialization.
aoqi@0: */
aoqi@0: public class TestSource implements Iterable