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