vm/src/test/java/org/apidesign/vm4brwsr/StringTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 16 Oct 2012 11:55:56 +0200
changeset 104 1376481f15e7
parent 103 e8438996d406
child 106 346633cd13d6
permissions -rw-r--r--
Concatenation of strings works
     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     
    41     @Test(timeOut=10000) public void stringStringConcat() throws Exception {
    42         assertExec(
    43             "Composes strings OK",
    44             "org_apidesign_vm4brwsr_StringSample_concatStringsLjava_lang_String",
    45             "Hello World!1Ahoj"
    46         );
    47     }
    48     
    49     private static CharSequence codeSeq;
    50     private static Invocable code;
    51     
    52     @BeforeClass 
    53     public void compileTheCode() throws Exception {
    54         StringBuilder sb = new StringBuilder();
    55         code = StaticMethodTest.compileClass(sb, 
    56             "org/apidesign/vm4brwsr/StringSample",
    57             "java/lang/String"
    58         );
    59         codeSeq = sb;
    60     }
    61     
    62     private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
    63         Object ret = null;
    64         try {
    65             ret = code.invokeFunction(methodName, args);
    66         } catch (ScriptException ex) {
    67             fail("Execution failed in\n" + codeSeq, ex);
    68         } catch (NoSuchMethodException ex) {
    69             fail("Cannot find method in\n" + codeSeq, ex);
    70         }
    71         if (ret == null && expRes == null) {
    72             return;
    73         }
    74         if (expRes.equals(ret)) {
    75             return;
    76         }
    77         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
    78         
    79     }
    80     
    81 }