# HG changeset patch # User Jaroslav Tulach # Date 1398709424 -7200 # Node ID daa5f903b529f4ffc42daadd7b9c61225fb117fc # Parent d3df935aff70677d065edcdaffcc838d1fe6724f Testing loading of resource from an extension and preparing for loading them from multiple extensions diff -r d3df935aff70 -r daa5f903b529 rt/vm/src/test/java/org/apidesign/vm4brwsr/NumbersAsExtensionTest.java --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/NumbersAsExtensionTest.java Mon Apr 28 17:31:29 2014 +0200 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/NumbersAsExtensionTest.java Mon Apr 28 20:23:44 2014 +0200 @@ -17,6 +17,7 @@ */ package org.apidesign.vm4brwsr; +import javax.script.ScriptEngine; import static org.testng.Assert.*; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; @@ -209,7 +210,8 @@ @BeforeClass public static void compileTheCode() throws Exception { - code = TestVM.compileClassAsExtension(null, null, "org/apidesign/vm4brwsr/Numbers"); + ScriptEngine[] eng = { null }; + code = TestVM.compileClassAsExtension(null, eng, "org/apidesign/vm4brwsr/Numbers", null, null); } @AfterClass public static void releaseTheCode() { diff -r d3df935aff70 -r daa5f903b529 rt/vm/src/test/java/org/apidesign/vm4brwsr/Resources.java --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/Resources.java Mon Apr 28 17:31:29 2014 +0200 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/Resources.java Mon Apr 28 20:23:44 2014 +0200 @@ -19,6 +19,8 @@ import java.io.IOException; import java.io.InputStream; +import java.net.URL; +import java.util.Enumeration; /** * @@ -27,26 +29,26 @@ public class Resources { public static String loadKO() throws IOException { InputStream is = Resources.class.getResourceAsStream("ko.js"); + return readIS(is, false); + } + + static String loadClazz() throws IOException { + InputStream is = Resources.class.getResourceAsStream("Bck2BrwsrToolkit.class"); + return readIS(is, false); + } + + private static String readIS(InputStream is, boolean asString) throws IOException { if (is == null) { return "No resource found!"; } byte[] arr = new byte[4092]; int len = is.read(arr); - if (len == -1) { - return "No data read!"; + if (len < 5) { + return "No data read! Len: " + len; } - final Object str = new String(arr, 0, len, "UTF-8"); - return str.toString(); - } - static String loadClazz() throws IOException { - InputStream is = Resources.class.getResourceAsStream("Bck2BrwsrToolkit.class"); - if (is == null) { - return "No resource found!"; - } - byte[] arr = new byte[4092]; - int len = is.read(arr); - if (len < 10) { - return "No data read! Len: " + len; + + if (asString) { + return new String(arr, 0, len, "UTF-8").toString().toString(); } StringBuilder sb = new StringBuilder(); @@ -57,4 +59,25 @@ return sb.toString().toString(); } + + static String loadHello() throws IOException { + Enumeration en; + try { + en = Resources.class.getClassLoader().getResources("META-INF/ahoj"); + } catch (SecurityException ex) { + return "SecurityException"; + } + StringBuilder sb = new StringBuilder(); + while (en.hasMoreElements()) { + URL url = en.nextElement(); + sb.append(readIS(url.openStream(), true)); + } + return sb.toString().toString(); + } + static String loadJustHello() throws IOException { + URL url = Resources.class.getResource("/META-INF/ahoj"); + StringBuilder sb = new StringBuilder(); + sb.append(readIS(url.openStream(), true)); + return sb.toString().toString(); + } } diff -r d3df935aff70 -r daa5f903b529 rt/vm/src/test/java/org/apidesign/vm4brwsr/ResourcesWithExtensionsTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/ResourcesWithExtensionsTest.java Mon Apr 28 20:23:44 2014 +0200 @@ -0,0 +1,86 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.vm4brwsr; + +import java.io.UnsupportedEncodingException; +import javax.script.ScriptEngine; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +/** Tests related to loading resources from the VM. + * + * @author Jaroslav Tulach + */ +public class ResourcesWithExtensionsTest { + @Test public void checkHello() throws Exception { + String exp = "Hello "; + + assertExec("Loading a precompiled resource:", + Resources.class, "loadJustHello__Ljava_lang_String_2", + exp + ); + } + /* + @Test public void checkHelloWorld() throws Exception { + String exp = "Hello World!"; + + assertExec("Loading precompiled resources:", + Resources.class, "loadHello__Ljava_lang_String_2", + exp + ); + } + */ + + private static TestVM code; + + @BeforeClass + public static void compileTheCode() throws Exception { + StringBuilder sb = new StringBuilder(); + ScriptEngine[] eng = { null }; + code = TestVM.compileClassAsExtension(sb, eng, + "org/apidesign/vm4brwsr/Resources", + "META-INF/ahoj", "Hello " + ); + code = TestVM.compileClassAsExtension(sb, eng, + "org/apidesign/vm4brwsr/Resources", + "META-INF/ahoj", "World!" + ); + } + @AfterClass + public static void releaseTheCode() { + code = null; + } + + private void assertExec( + String msg, Class clazz, String method, + Object ret, Object... args + ) throws Exception { + code.assertExec(msg, clazz, method, ret, args); + } + + public static String parseBase64Binary(String s) throws UnsupportedEncodingException { + final byte[] arr = javax.xml.bind.DatatypeConverter.parseBase64Binary(s); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < arr.length; i++) { + int ch = arr[i]; + sb.append((char)ch); + } + return sb.toString(); + } +} diff -r d3df935aff70 -r daa5f903b529 rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java Mon Apr 28 17:31:29 2014 +0200 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java Mon Apr 28 20:23:44 2014 +0200 @@ -17,6 +17,7 @@ */ package org.apidesign.vm4brwsr; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileWriter; import java.io.IOException; @@ -126,24 +127,39 @@ } } - static TestVM compileClassAsExtension(StringBuilder sb, ScriptEngine[] eng, String... names) throws ScriptException, IOException { + static TestVM compileClassAsExtension( + StringBuilder sb, ScriptEngine[] eng, + String name, final String resourceName, final String resourceContent + ) throws ScriptException, IOException { if (sb == null) { sb = new StringBuilder(); } - Bck2Brwsr.generate(sb, new EmulationResources()); + if (eng[0] == null) { + ScriptEngineManager sem = new ScriptEngineManager(); + ScriptEngine js = sem.getEngineByExtension("js"); + eng[0] = js; + Bck2Brwsr.generate(sb, new EmulationResources()); + } Bck2Brwsr b2b = Bck2Brwsr.newCompiler(). - resources(new EmulationResources()). - addRootClasses(names).library(true); + resources(new EmulationResources() { + @Override + public InputStream get(String name) throws IOException { + if (name.equals(resourceName)) { + return new ByteArrayInputStream(resourceContent.getBytes("UTF-8")); + } + return super.get(name); + } + }). + addRootClasses(name).library(true); + if (resourceName != null) { + b2b = b2b.addResources(resourceName); + } b2b.generate(sb); - ScriptEngineManager sem = new ScriptEngineManager(); - ScriptEngine js = sem.getEngineByExtension("js"); - if (eng != null) { - eng[0] = js; - } try { - Object res = js.eval(sb.toString()); - assertTrue(js instanceof Invocable, "It is invocable object: " + res); - return new TestVM((Invocable) js, sb); + defineAtoB(eng[0]); + Object res = eng[0].eval(sb.toString()); + assertTrue(eng[0] instanceof Invocable, "It is invocable object: " + res); + return new TestVM((Invocable) eng[0], sb); } catch (Exception ex) { if (sb.length() > 2000) { sb = dumpJS(sb); @@ -169,10 +185,7 @@ eng[0] = js; } try { - js.eval("atob = function(s) { return org.apidesign.vm4brwsr.ResourcesTest.parseBase64Binary(s); }"); - - Object ahoj = js.eval("atob('QWhvag==')"); - assertEquals(ahoj, "Ahoj", "Verify the atob function behaves properly"); + defineAtoB(js); Object res = js.eval(sb.toString()); assertTrue(js instanceof Invocable, "It is invocable object: " + res); @@ -186,6 +199,13 @@ } } + private static void defineAtoB(ScriptEngine js) throws ScriptException { + js.eval("atob = function(s) { return org.apidesign.vm4brwsr.ResourcesTest.parseBase64Binary(s); }"); + + Object ahoj = js.eval("atob('QWhvag==')"); + assertEquals(ahoj, "Ahoj", "Verify the atob function behaves properly"); + } + Object loadClass(String loadClass, String name) throws ScriptException, NoSuchMethodException { return code.invokeMethod(bck2brwsr, "loadClass", Exceptions.class.getName()); }