1 package org.apidesign.vm4brwsr;
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;
11 * @author Jaroslav Tulach <jtulach@netbeans.org>
13 public class StringTest {
14 @Test public void firstChar() throws Exception {
16 "First char in Hello is H",
17 "org_apidesign_vm4brwsr_StringSample_sayHelloCI",
22 @Test public void fromChars() throws Exception {
24 "First char in Hello is ABC",
25 "org_apidesign_vm4brwsr_StringSample_fromCharsLjava_lang_StringCCC",
30 @Test(timeOut=10000) public void toStringConcatenation() throws Exception {
32 "Five executions should generate 5Hello World!",
33 "org_apidesign_vm4brwsr_StringSample_toStringTestLjava_lang_StringI",
37 @Test public void toStringConcatenationJava() throws Exception {
38 assertEquals("Hello World!5", StringSample.toStringTest(5));
40 private static CharSequence codeSeq;
41 private static Invocable code;
44 public void compileTheCode() throws Exception {
45 StringBuilder sb = new StringBuilder();
46 code = StaticMethodTest.compileClass(sb,
47 "org/apidesign/vm4brwsr/StringSample",
53 private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
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);
62 if (ret == null && expRes == null) {
65 if (expRes.equals(ret)) {
68 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);