vm/src/test/java/org/apidesign/vm4brwsr/StringTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 08 Oct 2012 17:10:27 -0700
branchemul
changeset 93 a236a9f137ac
parent 45 bc9a4c28d1a3
child 94 19497b4312bb
permissions -rw-r--r--
Concatanation of strings sort of works (but produces wrong result)
     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 
     8 /**
     9  *
    10  * @author Jaroslav Tulach <jtulach@netbeans.org>
    11  */
    12 public class StringTest {
    13     @Test public void firstChar() throws Exception {
    14         assertExec(
    15             "First char in Hello is H",
    16             "org_apidesign_vm4brwsr_StringSample_sayHelloCI",
    17             "H", 0
    18         );
    19     }
    20 
    21     @Test public void fromChars() throws Exception {
    22         assertExec(
    23             "First char in Hello is ABC",
    24             "org_apidesign_vm4brwsr_StringSample_fromCharsLjava_lang_StringCCC",
    25             "ABC", 'A', 'B', 'C'
    26         );
    27     }
    28 
    29     @Test(timeOut=10000) public void toStringConcatenation() throws Exception {
    30         assertExec(
    31             "Five executions should generate 5Hello World!",
    32             "org_apidesign_vm4brwsr_StringSample_toStringTestLjava_lang_StringI",
    33             "5Hello World!", 5
    34         );
    35     }
    36     
    37     private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
    38         StringBuilder sb = new StringBuilder();
    39         Invocable i = StaticMethodTest.compileClass(sb, 
    40             "org/apidesign/vm4brwsr/StringSample",
    41             "java/lang/String"
    42         );
    43         
    44         Object ret = null;
    45         try {
    46             ret = i.invokeFunction(methodName, args);
    47         } catch (ScriptException ex) {
    48             fail("Execution failed in " + sb, ex);
    49         } catch (NoSuchMethodException ex) {
    50             fail("Cannot find method in " + sb, ex);
    51         }
    52         if (ret == null && expRes == null) {
    53             return;
    54         }
    55         if (expRes.equals(ret)) {
    56             return;
    57         }
    58         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
    59         
    60     }
    61     
    62 }