Less compilation during test execution
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 11 Oct 2012 10:43:17 -0700
changeset 103e8438996d406
parent 102 2354255a1844
child 104 1376481f15e7
Less compilation during test execution
vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
vm/src/test/java/org/apidesign/vm4brwsr/ArrayTest.java
vm/src/test/java/org/apidesign/vm4brwsr/InstanceTest.java
vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java
vm/src/test/java/org/apidesign/vm4brwsr/StringTest.java
     1.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Thu Oct 11 10:03:21 2012 -0700
     1.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Thu Oct 11 10:43:17 2012 -0700
     1.3 @@ -886,7 +886,8 @@
     1.4          return i;
     1.5      }
     1.6      
     1.7 -    private void addReference(String cn) {
     1.8 +    private void addReference(String cn) throws IOException {
     1.9 +        out.append(" /* needs ").append(cn).append(" */");
    1.10          if (references != null) {
    1.11              references.add(cn);
    1.12          }
     2.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/ArrayTest.java	Thu Oct 11 10:03:21 2012 -0700
     2.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/ArrayTest.java	Thu Oct 11 10:43:17 2012 -0700
     2.3 @@ -21,6 +21,7 @@
     2.4  import javax.script.ScriptException;
     2.5  import org.testng.annotations.Test;
     2.6  import static org.testng.Assert.*;
     2.7 +import org.testng.annotations.BeforeClass;
     2.8  
     2.9  /**
    2.10   *
    2.11 @@ -28,7 +29,7 @@
    2.12   */
    2.13  public class ArrayTest {
    2.14      @Test public void verifySimpleIntOperation() throws Exception {
    2.15 -        assertExec("CheckTheSum", "org_apidesign_vm4brwsr_Array_simpleI", 
    2.16 +            assertExec("CheckTheSum", "org_apidesign_vm4brwsr_Array_simpleI", 
    2.17              Double.valueOf(15)
    2.18          );
    2.19      }
    2.20 @@ -38,19 +39,25 @@
    2.21          );
    2.22      }
    2.23      
    2.24 -    private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
    2.25 +    private static CharSequence codeSeq;
    2.26 +    private static Invocable code;
    2.27 +    
    2.28 +    @BeforeClass 
    2.29 +    public void compileTheCode() throws Exception {
    2.30          StringBuilder sb = new StringBuilder();
    2.31 -        Invocable i = StaticMethodTest.compileClass(sb, 
    2.32 +        code = StaticMethodTest.compileClass(sb, 
    2.33              "org/apidesign/vm4brwsr/Array"
    2.34          );
    2.35 -        
    2.36 +        codeSeq = sb;
    2.37 +    }
    2.38 +    private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
    2.39          Object ret = null;
    2.40          try {
    2.41 -            ret = i.invokeFunction(methodName, args);
    2.42 +            ret = code.invokeFunction(methodName, args);
    2.43          } catch (ScriptException ex) {
    2.44 -            fail("Execution failed in " + sb, ex);
    2.45 +            fail("Execution failed in\n" + codeSeq, ex);
    2.46          } catch (NoSuchMethodException ex) {
    2.47 -            fail("Cannot find method in " + sb, ex);
    2.48 +            fail("Cannot find method in\n" + codeSeq, ex);
    2.49          }
    2.50          if (ret == null && expRes == null) {
    2.51              return;
    2.52 @@ -58,6 +65,6 @@
    2.53          if (expRes.equals(ret)) {
    2.54              return;
    2.55          }
    2.56 -        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
    2.57 +        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
    2.58      }
    2.59  }
     3.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/InstanceTest.java	Thu Oct 11 10:03:21 2012 -0700
     3.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/InstanceTest.java	Thu Oct 11 10:43:17 2012 -0700
     3.3 @@ -21,6 +21,8 @@
     3.4  import javax.script.ScriptException;
     3.5  import org.testng.annotations.Test;
     3.6  import static org.testng.Assert.*;
     3.7 +import org.testng.annotations.BeforeClass;
     3.8 +import org.testng.annotations.BeforeTest;
     3.9  
    3.10  /**
    3.11   *
    3.12 @@ -113,19 +115,29 @@
    3.13          return "org/apidesign/vm4brwsr/Instance";
    3.14      }
    3.15      
    3.16 +    private static CharSequence codeSeq;
    3.17 +    private static Invocable code;
    3.18 +    
    3.19 +    @BeforeTest 
    3.20 +    public void compileTheCode() throws Exception {
    3.21 +        if (codeSeq == null) {
    3.22 +            StringBuilder sb = new StringBuilder();
    3.23 +            code = StaticMethodTest.compileClass(sb, startCompilationWith());
    3.24 +            codeSeq = sb;
    3.25 +        }
    3.26 +    }
    3.27 +    
    3.28      private void assertExec(
    3.29          String msg, String methodName, Object expRes, Object... args
    3.30      ) throws Exception {
    3.31 -        StringBuilder sb = new StringBuilder();
    3.32 -        Invocable i = StaticMethodTest.compileClass(sb, startCompilationWith());
    3.33 -        
    3.34 +
    3.35          Object ret = null;
    3.36          try {
    3.37 -            ret = i.invokeFunction(methodName, args);
    3.38 +            ret = code.invokeFunction(methodName, args);
    3.39          } catch (ScriptException ex) {
    3.40 -            fail("Execution failed in " + sb, ex);
    3.41 +            fail("Execution failed in\n" + codeSeq, ex);
    3.42          } catch (NoSuchMethodException ex) {
    3.43 -            fail("Cannot find method in " + sb, ex);
    3.44 +            fail("Cannot find method in\n" + codeSeq, ex);
    3.45          }
    3.46          if (ret == null && expRes == null) {
    3.47              return;
    3.48 @@ -133,7 +145,7 @@
    3.49          if (expRes.equals(ret)) {
    3.50              return;
    3.51          }
    3.52 -        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
    3.53 +        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
    3.54          
    3.55      }
    3.56      
     4.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java	Thu Oct 11 10:03:21 2012 -0700
     4.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java	Thu Oct 11 10:43:17 2012 -0700
     4.3 @@ -23,6 +23,7 @@
     4.4  import javax.script.ScriptEngineManager;
     4.5  import javax.script.ScriptException;
     4.6  import static org.testng.Assert.*;
     4.7 +import org.testng.annotations.BeforeClass;
     4.8  import org.testng.annotations.Test;
     4.9  
    4.10  /** Checks the basic behavior of the translator.
    4.11 @@ -193,17 +194,27 @@
    4.12          );
    4.13      }
    4.14      
    4.15 +    private static CharSequence codeSeq;
    4.16 +    private static Invocable code;
    4.17 +    
    4.18 +    @BeforeClass 
    4.19 +    public void compileTheCode() throws Exception {
    4.20 +        StringBuilder sb = new StringBuilder();
    4.21 +        code = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
    4.22 +        codeSeq = sb;
    4.23 +    }
    4.24 +    
    4.25 +    
    4.26      private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
    4.27          StringBuilder sb = new StringBuilder();
    4.28 -        Invocable i = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
    4.29          
    4.30          Object ret = null;
    4.31          try {
    4.32 -            ret = i.invokeFunction(methodName, args);
    4.33 +            ret = code.invokeFunction(methodName, args);
    4.34          } catch (ScriptException ex) {
    4.35 -            fail("Execution failed in " + sb, ex);
    4.36 +            fail("Execution failed in\n" + codeSeq, ex);
    4.37          } catch (NoSuchMethodException ex) {
    4.38 -            fail("Cannot find method in " + sb, ex);
    4.39 +            fail("Cannot find method in\n" + codeSeq, ex);
    4.40          }
    4.41          if (ret == null && expRes == null) {
    4.42              return;
    4.43 @@ -211,7 +222,7 @@
    4.44          if (expRes != null && expRes.equals(ret)) {
    4.45              return;
    4.46          }
    4.47 -        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
    4.48 +        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
    4.49          
    4.50      }
    4.51  
     5.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/StringTest.java	Thu Oct 11 10:03:21 2012 -0700
     5.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/StringTest.java	Thu Oct 11 10:43:17 2012 -0700
     5.3 @@ -4,6 +4,7 @@
     5.4  import javax.script.ScriptException;
     5.5  import org.testng.annotations.Test;
     5.6  import static org.testng.Assert.*;
     5.7 +import org.testng.annotations.BeforeClass;
     5.8  
     5.9  /**
    5.10   *
    5.11 @@ -36,21 +37,27 @@
    5.12      @Test public void toStringConcatenationJava() throws Exception {
    5.13          assertEquals("Hello World!5", StringSample.toStringTest(5));
    5.14      }
    5.15 +    private static CharSequence codeSeq;
    5.16 +    private static Invocable code;
    5.17      
    5.18 -    private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
    5.19 +    @BeforeClass 
    5.20 +    public void compileTheCode() throws Exception {
    5.21          StringBuilder sb = new StringBuilder();
    5.22 -        Invocable i = StaticMethodTest.compileClass(sb, 
    5.23 +        code = StaticMethodTest.compileClass(sb, 
    5.24              "org/apidesign/vm4brwsr/StringSample",
    5.25              "java/lang/String"
    5.26          );
    5.27 -        
    5.28 +        codeSeq = sb;
    5.29 +    }
    5.30 +    
    5.31 +    private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
    5.32          Object ret = null;
    5.33          try {
    5.34 -            ret = i.invokeFunction(methodName, args);
    5.35 +            ret = code.invokeFunction(methodName, args);
    5.36          } catch (ScriptException ex) {
    5.37 -            fail("Execution failed in " + sb, ex);
    5.38 +            fail("Execution failed in\n" + codeSeq, ex);
    5.39          } catch (NoSuchMethodException ex) {
    5.40 -            fail("Cannot find method in " + sb, ex);
    5.41 +            fail("Cannot find method in\n" + codeSeq, ex);
    5.42          }
    5.43          if (ret == null && expRes == null) {
    5.44              return;
    5.45 @@ -58,7 +65,7 @@
    5.46          if (expRes.equals(ret)) {
    5.47              return;
    5.48          }
    5.49 -        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
    5.50 +        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
    5.51          
    5.52      }
    5.53