vm/src/test/java/org/apidesign/vm4brwsr/StringTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 11 Oct 2012 10:43:17 -0700
changeset 103 e8438996d406
parent 94 19497b4312bb
child 104 1376481f15e7
permissions -rw-r--r--
Less compilation during test execution
     1 package org.apidesign.vm4brwsr;
     2 
     3 import javax.script.Invocable;
     4 import javax.script.ScriptException;
     5 import org.testng.annotations.Test;
     6 import static org.testng.Assert.*;
     7 import org.testng.annotations.BeforeClass;
     8 
     9 /**
    10  *
    11  * @author Jaroslav Tulach <jtulach@netbeans.org>
    12  */
    13 public class StringTest {
    14     @Test public void firstChar() throws Exception {
    15         assertExec(
    16             "First char in Hello is H",
    17             "org_apidesign_vm4brwsr_StringSample_sayHelloCI",
    18             "H", 0
    19         );
    20     }
    21 
    22     @Test public void fromChars() throws Exception {
    23         assertExec(
    24             "First char in Hello is ABC",
    25             "org_apidesign_vm4brwsr_StringSample_fromCharsLjava_lang_StringCCC",
    26             "ABC", 'A', 'B', 'C'
    27         );
    28     }
    29 
    30     @Test(timeOut=10000) public void toStringConcatenation() throws Exception {
    31         assertExec(
    32             "Five executions should generate 5Hello World!",
    33             "org_apidesign_vm4brwsr_StringSample_toStringTestLjava_lang_StringI",
    34             "Hello World!5", 5
    35         );
    36     }
    37     @Test public void toStringConcatenationJava() throws Exception {
    38         assertEquals("Hello World!5", StringSample.toStringTest(5));
    39     }
    40     private static CharSequence codeSeq;
    41     private static Invocable code;
    42     
    43     @BeforeClass 
    44     public void compileTheCode() throws Exception {
    45         StringBuilder sb = new StringBuilder();
    46         code = StaticMethodTest.compileClass(sb, 
    47             "org/apidesign/vm4brwsr/StringSample",
    48             "java/lang/String"
    49         );
    50         codeSeq = sb;
    51     }
    52     
    53     private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
    54         Object ret = null;
    55         try {
    56             ret = code.invokeFunction(methodName, args);
    57         } catch (ScriptException ex) {
    58             fail("Execution failed in\n" + codeSeq, ex);
    59         } catch (NoSuchMethodException ex) {
    60             fail("Cannot find method in\n" + codeSeq, ex);
    61         }
    62         if (ret == null && expRes == null) {
    63             return;
    64         }
    65         if (expRes.equals(ret)) {
    66             return;
    67         }
    68         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
    69         
    70     }
    71     
    72 }