1.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java Mon Apr 28 10:59:12 2014 +0200
1.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java Mon Apr 28 20:23:44 2014 +0200
1.3 @@ -17,6 +17,7 @@
1.4 */
1.5 package org.apidesign.vm4brwsr;
1.6
1.7 +import java.io.ByteArrayInputStream;
1.8 import java.io.File;
1.9 import java.io.FileWriter;
1.10 import java.io.IOException;
1.11 @@ -126,24 +127,39 @@
1.12 }
1.13 }
1.14
1.15 - static TestVM compileClassAsExtension(StringBuilder sb, ScriptEngine[] eng, String... names) throws ScriptException, IOException {
1.16 + static TestVM compileClassAsExtension(
1.17 + StringBuilder sb, ScriptEngine[] eng,
1.18 + String name, final String resourceName, final String resourceContent
1.19 + ) throws ScriptException, IOException {
1.20 if (sb == null) {
1.21 sb = new StringBuilder();
1.22 }
1.23 - Bck2Brwsr.generate(sb, new EmulationResources());
1.24 + if (eng[0] == null) {
1.25 + ScriptEngineManager sem = new ScriptEngineManager();
1.26 + ScriptEngine js = sem.getEngineByExtension("js");
1.27 + eng[0] = js;
1.28 + Bck2Brwsr.generate(sb, new EmulationResources());
1.29 + }
1.30 Bck2Brwsr b2b = Bck2Brwsr.newCompiler().
1.31 - resources(new EmulationResources()).
1.32 - addRootClasses(names).library(true);
1.33 + resources(new EmulationResources() {
1.34 + @Override
1.35 + public InputStream get(String name) throws IOException {
1.36 + if (name.equals(resourceName)) {
1.37 + return new ByteArrayInputStream(resourceContent.getBytes("UTF-8"));
1.38 + }
1.39 + return super.get(name);
1.40 + }
1.41 + }).
1.42 + addRootClasses(name).library(true);
1.43 + if (resourceName != null) {
1.44 + b2b = b2b.addResources(resourceName);
1.45 + }
1.46 b2b.generate(sb);
1.47 - ScriptEngineManager sem = new ScriptEngineManager();
1.48 - ScriptEngine js = sem.getEngineByExtension("js");
1.49 - if (eng != null) {
1.50 - eng[0] = js;
1.51 - }
1.52 try {
1.53 - Object res = js.eval(sb.toString());
1.54 - assertTrue(js instanceof Invocable, "It is invocable object: " + res);
1.55 - return new TestVM((Invocable) js, sb);
1.56 + defineAtoB(eng[0]);
1.57 + Object res = eng[0].eval(sb.toString());
1.58 + assertTrue(eng[0] instanceof Invocable, "It is invocable object: " + res);
1.59 + return new TestVM((Invocable) eng[0], sb);
1.60 } catch (Exception ex) {
1.61 if (sb.length() > 2000) {
1.62 sb = dumpJS(sb);
1.63 @@ -169,10 +185,7 @@
1.64 eng[0] = js;
1.65 }
1.66 try {
1.67 - js.eval("atob = function(s) { return org.apidesign.vm4brwsr.ResourcesTest.parseBase64Binary(s); }");
1.68 -
1.69 - Object ahoj = js.eval("atob('QWhvag==')");
1.70 - assertEquals(ahoj, "Ahoj", "Verify the atob function behaves properly");
1.71 + defineAtoB(js);
1.72
1.73 Object res = js.eval(sb.toString());
1.74 assertTrue(js instanceof Invocable, "It is invocable object: " + res);
1.75 @@ -186,6 +199,13 @@
1.76 }
1.77 }
1.78
1.79 + private static void defineAtoB(ScriptEngine js) throws ScriptException {
1.80 + js.eval("atob = function(s) { return org.apidesign.vm4brwsr.ResourcesTest.parseBase64Binary(s); }");
1.81 +
1.82 + Object ahoj = js.eval("atob('QWhvag==')");
1.83 + assertEquals(ahoj, "Ahoj", "Verify the atob function behaves properly");
1.84 + }
1.85 +
1.86 Object loadClass(String loadClass, String name) throws ScriptException, NoSuchMethodException {
1.87 return code.invokeMethod(bck2brwsr, "loadClass", Exceptions.class.getName());
1.88 }