vm/src/test/java/org/apidesign/vm4brwsr/StringTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 28 Sep 2012 07:43:53 +0200
branchstrings
changeset 36 95330dd02c47
parent 35 7cfa9b56f888
child 45 bc9a4c28d1a3
permissions -rw-r--r--
Implementation of two more methods for String - yet many are missing to make StringBuilder work
     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 toStringConcatenation() throws Exception {
    22         assertExec(
    23             "Five executions should generate 5Hello World!",
    24             "org_apidesign_vm4brwsr_StringSample_toStringTestLjava_lang_StringI",
    25             "5Hello World!", 5
    26         );
    27     }
    28     
    29     private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
    30         StringBuilder sb = new StringBuilder();
    31         Invocable i = StaticMethodTest.compileClass(sb, 
    32             "org/apidesign/vm4brwsr/StringSample",
    33             "java/lang/String"
    34         );
    35         
    36         Object ret = null;
    37         try {
    38             ret = i.invokeFunction(methodName, args);
    39         } catch (ScriptException ex) {
    40             fail("Execution failed in " + sb, ex);
    41         } catch (NoSuchMethodException ex) {
    42             fail("Cannot find method in " + sb, ex);
    43         }
    44         if (ret == null && expRes == null) {
    45             return;
    46         }
    47         if (expRes.equals(ret)) {
    48             return;
    49         }
    50         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + sb);
    51         
    52     }
    53     
    54 }