changeset 5330:2979c174803e

Improvements to template and combo test frameworks: - Add 'debug' mode to combo test layer - Add support for separate compilation to template test layer - Add test cases for separate compilation
author briangoetz
date Thu, 17 May 2012 15:57:54 -0400
parents ef9074ded24b
children 7e8f57b88c4f
files combo-tests/tests/tools/javac/combo/ComboTestBase.java combo-tests/tests/tools/javac/combo/Diagnostics.java combo-tests/tests/tools/javac/combo/JavacTemplateTestBase.java combo-tests/tests/tools/javac/combo/SeparateCompilationTest.java combo-tests/tests/tools/javac/combo/TemplateTest.java
diffstat 5 files changed, 150 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/combo-tests/tests/tools/javac/combo/ComboTestBase.java	Thu May 10 11:48:56 2012 -0700
+++ b/combo-tests/tests/tools/javac/combo/ComboTestBase.java	Thu May 17 15:57:54 2012 -0400
@@ -43,6 +43,7 @@
 
     protected final ComboTestMetadata<T> metadata;
     protected final Object[] comboArgs;
+    private boolean debugOnly = false;
 
     // This class cannot have a constructor; it interferes with testng's ability to find the factory method
     //in subclasses.  Anything that would be done by a constructor should insteady be done by init blocks
@@ -82,6 +83,16 @@
         }
     }
 
+    /** For debugging of test cases -- sets tests into debug-only mode, where we print out the instantiations of
+     * the dimension variables and the templates.
+     */
+    public static<T extends ComboTestBase> Object[] debugFactory(Class<T> clazz) throws ReflectiveOperationException {
+        Object[] results = factory(clazz);
+        for (Object o : results)
+            ((ComboTestBase) o).debugOnly = true;
+        return results;
+    }
+
     private static<T extends ComboTestBase<T>> ComboTestMetadata<T> getMetadata(Class<T> clazz) {
         try {
             ComboTestMetadata<T> md = metadataMap.get(clazz);
@@ -142,12 +153,19 @@
 
     @Test
     public void test() throws Exception {
-        preCompile();
-        File dest = compile(shouldRun());
-        postCompile();
+        if (debugOnly) {
+            System.out.println("Test case : " + getTestCaseDescription());
+            for (Map.Entry e : sourceFiles.entrySet())
+                System.out.println("Source file " + e.getKey() + ": " + e.getValue());
+        }
+        else {
+            preCompile();
+            File dest = compile(shouldRun());
+            postCompile();
 
-        if (shouldRun() && !diags.errorsFound()) {
-            run(loadClass("Main", dest));
+            if (shouldRun() && !diags.errorsFound()) {
+                run(loadClass("Main", dest));
+            }
         }
     }
 
--- a/combo-tests/tests/tools/javac/combo/Diagnostics.java	Thu May 10 11:48:56 2012 -0700
+++ b/combo-tests/tests/tools/javac/combo/Diagnostics.java	Thu May 17 15:57:54 2012 -0400
@@ -65,4 +65,9 @@
     }
 
     public String toString() { return errors.toString(); }
+
+    public void reset() {
+        errors.clear();
+        warnings.clear();
+    }
 }
--- a/combo-tests/tests/tools/javac/combo/JavacTemplateTestBase.java	Thu May 10 11:48:56 2012 -0700
+++ b/combo-tests/tests/tools/javac/combo/JavacTemplateTestBase.java	Thu May 17 15:57:54 2012 -0400
@@ -74,6 +74,18 @@
         Collections.addAll(compileOptions, opts);
     }
 
+    protected void resetCompileOptions() { compileOptions.clear(); }
+    protected void resetTemplates() { templates.clear(); }
+    protected void resetDiagnostics() { diags.reset(); }
+    protected void resetSourceFiles() { sourceFiles.clear(); }
+
+    protected void reset() {
+        resetCompileOptions();
+        resetDiagnostics();
+        resetSourceFiles();
+        resetTemplates();
+    }
+
     @AfterMethod
     public void copyErrors(ITestResult result) {
         if (!result.isSuccess()) {
@@ -156,6 +168,7 @@
         JavacTask ct = (JavacTask) systemJavaCompiler.getTask(null, fm, diags, compileOptions, null, files);
         if (generate) {
             File destDir = new File(root, Integer.toString(counter.incrementAndGet()));
+            // @@@ Assert that this directory didn't exist, or start counter at max+1
             destDir.mkdirs();
             fm.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(destDir));
             ct.generate();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/combo-tests/tests/tools/javac/combo/SeparateCompilationTest.java	Thu May 17 15:57:54 2012 -0400
@@ -0,0 +1,104 @@
+/*
+ * Copyright (c) 2012, 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.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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.
+ */
+package tools.javac.combo;
+
+import org.testng.annotations.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.Method;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+
+/**
+ * SeparateCompilationTest -- test of test framework to do separate compilation
+ */
+@Test
+public class SeparateCompilationTest extends JavacTemplateTestBase {
+
+    public void testCompletelySeparate() throws ReflectiveOperationException, IOException {
+        addSourceFile("A.java", new StringTemplate("public class A { public String a() throws Throwable { return Class.forName(\"B\").getName(); } }"));
+        File aFile = compile(true);
+        assertCompileSucceeded();
+        reset();
+
+        addSourceFile("B.java", new StringTemplate("public class B {  }"));
+        File bFile = compile(true);
+        assertCompileSucceeded();
+
+        Class clazz = loadClass("A", aFile, bFile);
+        Method m = clazz.getMethod("a");
+        Object instance = clazz.newInstance();
+        String x = (String) m.invoke(instance);
+        assertEquals("B", x);
+    }
+
+    public void testIdenticalRecompile() throws ReflectiveOperationException, IOException {
+        addSourceFile("A.java", new StringTemplate("public class A { public String a() { return B.b(); } }"));
+        addSourceFile("B.java", new StringTemplate("public class B { public static String b() { return \"1\"; } }"));
+        File aFile = compile(true);
+        assertCompileSucceeded();
+        // Deleting B.class is not generally needed for test cases that simply replace B, but here we're testing the framework's ability to find B elsewhere
+        File bClass = new File(aFile, "B.class");
+        assertTrue(bClass.exists());
+        boolean deleted = bClass.delete();
+        assertTrue(deleted);
+        reset();
+
+        addSourceFile("B.java", new StringTemplate("public class B { public static String b() { return \"1\"; } }"));
+        File bFile = compile(true);
+        assertCompileSucceeded();
+
+        Class clazz = loadClass("A", bFile, aFile);
+        Method m = clazz.getMethod("a");
+        Object instance = clazz.newInstance();
+        String x = (String) m.invoke(instance);
+        assertEquals("1", x);
+    }
+
+    public void testConsistentRecompile() throws ReflectiveOperationException, IOException {
+        addSourceFile("A.java", new StringTemplate("public class A { public String a() { return B.b(); } }"));
+        addSourceFile("B.java", new StringTemplate("public class B { public static String b() { return \"1\"; } }"));
+        File aFile = compile(true);
+        assertCompileSucceeded();
+        File bClass = new File(aFile, "B.class");
+        assertTrue(bClass.exists());
+        boolean deleted = bClass.delete();
+        assertTrue(deleted);
+        reset();
+
+        addSourceFile("B.java", new StringTemplate("public class B { public static String b() { return \"2\"; } }"));
+        File bFile = compile(true);
+        assertCompileSucceeded();
+
+        Class clazz = loadClass("A", bFile, aFile);
+        Method m = clazz.getMethod("a");
+        Object instance = clazz.newInstance();
+        String x = (String) m.invoke(instance);
+        assertEquals("2", x);
+    }
+
+}
--- a/combo-tests/tests/tools/javac/combo/TemplateTest.java	Thu May 10 11:48:56 2012 -0700
+++ b/combo-tests/tests/tools/javac/combo/TemplateTest.java	Thu May 17 15:57:54 2012 -0400
@@ -83,6 +83,11 @@
         assertTemplate("c", "#{A[2]}");
     }
 
+    public void testAngleBrackets() {
+        vars.put("X", s -> "xyz");
+        assertTemplate("List<String> ls = xyz;", "List<String> ls = #{X};");
+    }
+
     @Test(expectedExceptions = IllegalStateException.class )
     public void testUnknownKey() {
         assertTemplate("#{Q}", "#{Q}");