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@22: package org.apidesign.vm4brwsr; jaroslav@0: jaroslav@1495: import java.io.UnsupportedEncodingException; jaroslav@1497: import javax.script.ScriptEngine; jaroslav@789: import org.testng.annotations.AfterClass; jaroslav@103: import org.testng.annotations.BeforeClass; jaroslav@0: import org.testng.annotations.Test; jaroslav@0: jaroslav@1493: /** Tests related to loading resources from the VM. jaroslav@0: * jaroslav@0: * @author Jaroslav Tulach jaroslav@0: */ jaroslav@1497: public class ResourcesWithExtensionsTest { jaroslav@1497: @Test public void checkHello() throws Exception { jaroslav@1497: String exp = "Hello "; jaroslav@1493: jaroslav@1493: assertExec("Loading a precompiled resource:", jaroslav@1497: Resources.class, "loadJustHello__Ljava_lang_String_2", jaroslav@1493: exp jaroslav@291: ); jaroslav@291: } jaroslav@1519: jaroslav@1497: @Test public void checkHelloWorld() throws Exception { jaroslav@1497: String exp = "Hello World!"; jaroslav@1709: exp = exp + exp.hashCode(); jaroslav@1496: jaroslav@1497: assertExec("Loading precompiled resources:", jaroslav@1497: Resources.class, "loadHello__Ljava_lang_String_2", jaroslav@1496: exp jaroslav@1496: ); jaroslav@1496: } jaroslav@1587: jaroslav@1587: @Test public void objJSResourceIsNotFound() throws Exception { jaroslav@1587: assertExec("Objects from @JavaScriptResource resources are available", jaroslav@1587: Resources.class, "isObj__Z", 1.0 jaroslav@1587: ); jaroslav@1587: } jaroslav@1587: @Test public void objJSIsFound() throws Exception { jaroslav@1587: assertExec("The resources used as @JavaScriptResource aren't available", jaroslav@1722: Resources.class, "isResource__ZLjava_lang_String_2", 0.0, "obj" jaroslav@1722: ); jaroslav@1722: } jaroslav@1722: jaroslav@1722: @Test public void thisObjJSIsFound() throws Exception { jaroslav@1722: assertExec("The resources used as @JavaScriptResource aren't available", jaroslav@1722: Resources.class, "isResource__ZLjava_lang_String_2", 0.0, "thisObj" jaroslav@1587: ); jaroslav@1587: } jaroslav@789: jaroslav@708: private static TestVM code; jaroslav@103: jaroslav@103: @BeforeClass jaroslav@789: public static void compileTheCode() throws Exception { jaroslav@103: StringBuilder sb = new StringBuilder(); jaroslav@1497: ScriptEngine[] eng = { null }; jaroslav@1497: code = TestVM.compileClassAsExtension(sb, eng, jaroslav@1496: "org/apidesign/vm4brwsr/Resources", jaroslav@1497: "META-INF/ahoj", "Hello " jaroslav@1497: ); jaroslav@1497: code = TestVM.compileClassAsExtension(sb, eng, jaroslav@1497: "org/apidesign/vm4brwsr/Resources", jaroslav@1497: "META-INF/ahoj", "World!" jaroslav@1496: ); jaroslav@2: } jaroslav@789: @AfterClass jaroslav@789: public static void releaseTheCode() { jaroslav@789: code = null; jaroslav@789: } jaroslav@2: jaroslav@708: private void assertExec( jaroslav@708: String msg, Class clazz, String method, jaroslav@708: Object ret, Object... args jaroslav@708: ) throws Exception { jaroslav@708: code.assertExec(msg, clazz, method, ret, args); jaroslav@298: } jaroslav@1495: jaroslav@1495: public static String parseBase64Binary(String s) throws UnsupportedEncodingException { jaroslav@1496: final byte[] arr = javax.xml.bind.DatatypeConverter.parseBase64Binary(s); jaroslav@1496: StringBuilder sb = new StringBuilder(); jaroslav@1496: for (int i = 0; i < arr.length; i++) { jaroslav@1496: int ch = arr[i]; jaroslav@1496: sb.append((char)ch); jaroslav@1496: } jaroslav@1496: return sb.toString(); jaroslav@1495: } jaroslav@0: }