jaroslav@106: /** jaroslav@106: * Back 2 Browser Bytecode Translator jaroslav@106: * Copyright (C) 2012 Jaroslav Tulach jaroslav@106: * jaroslav@106: * This program is free software: you can redistribute it and/or modify jaroslav@106: * it under the terms of the GNU General Public License as published by jaroslav@106: * the Free Software Foundation, version 2 of the License. jaroslav@106: * jaroslav@106: * This program is distributed in the hope that it will be useful, jaroslav@106: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@106: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@106: * GNU General Public License for more details. jaroslav@106: * jaroslav@106: * You should have received a copy of the GNU General Public License jaroslav@106: * along with this program. Look for COPYING file in the top folder. jaroslav@106: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@106: */ jaroslav@34: package org.apidesign.vm4brwsr; jaroslav@34: jaroslav@34: import javax.script.Invocable; jaroslav@34: import javax.script.ScriptException; jaroslav@34: import org.testng.annotations.Test; jaroslav@34: import static org.testng.Assert.*; jaroslav@103: import org.testng.annotations.BeforeClass; jaroslav@34: jaroslav@34: /** jaroslav@34: * jaroslav@34: * @author Jaroslav Tulach jaroslav@34: */ jaroslav@34: public class StringTest { jaroslav@34: @Test public void firstChar() throws Exception { jaroslav@34: assertExec( jaroslav@34: "First char in Hello is H", jaroslav@34: "org_apidesign_vm4brwsr_StringSample_sayHelloCI", jaroslav@35: "H", 0 jaroslav@34: ); jaroslav@34: } jaroslav@36: jaroslav@45: @Test public void fromChars() throws Exception { jaroslav@45: assertExec( jaroslav@45: "First char in Hello is ABC", jaroslav@45: "org_apidesign_vm4brwsr_StringSample_fromCharsLjava_lang_StringCCC", jaroslav@45: "ABC", 'A', 'B', 'C' jaroslav@45: ); jaroslav@45: } jaroslav@45: jaroslav@93: @Test(timeOut=10000) public void toStringConcatenation() throws Exception { jaroslav@36: assertExec( jaroslav@36: "Five executions should generate 5Hello World!", jaroslav@36: "org_apidesign_vm4brwsr_StringSample_toStringTestLjava_lang_StringI", jaroslav@94: "Hello World!5", 5 jaroslav@36: ); jaroslav@36: } jaroslav@94: @Test public void toStringConcatenationJava() throws Exception { jaroslav@94: assertEquals("Hello World!5", StringSample.toStringTest(5)); jaroslav@94: } jaroslav@104: jaroslav@104: @Test(timeOut=10000) public void stringStringConcat() throws Exception { jaroslav@104: assertExec( jaroslav@104: "Composes strings OK", jaroslav@104: "org_apidesign_vm4brwsr_StringSample_concatStringsLjava_lang_String", jaroslav@104: "Hello World!1Ahoj" jaroslav@104: ); jaroslav@104: } jaroslav@104: jaroslav@103: private static CharSequence codeSeq; jaroslav@103: private static Invocable code; jaroslav@34: jaroslav@103: @BeforeClass jaroslav@103: public void compileTheCode() throws Exception { jaroslav@34: StringBuilder sb = new StringBuilder(); jaroslav@103: code = StaticMethodTest.compileClass(sb, jaroslav@34: "org/apidesign/vm4brwsr/StringSample", jaroslav@34: "java/lang/String" jaroslav@34: ); jaroslav@103: codeSeq = sb; jaroslav@103: } jaroslav@103: jaroslav@103: private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception { jaroslav@34: Object ret = null; jaroslav@34: try { jaroslav@103: ret = code.invokeFunction(methodName, args); jaroslav@34: } catch (ScriptException ex) { jaroslav@103: fail("Execution failed in\n" + codeSeq, ex); jaroslav@34: } catch (NoSuchMethodException ex) { jaroslav@103: fail("Cannot find method in\n" + codeSeq, ex); jaroslav@34: } jaroslav@34: if (ret == null && expRes == null) { jaroslav@34: return; jaroslav@34: } jaroslav@34: if (expRes.equals(ret)) { jaroslav@34: return; jaroslav@34: } jaroslav@103: assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq); jaroslav@34: jaroslav@34: } jaroslav@34: jaroslav@34: }