Encapsulation. Moving shared code into TestVM instance.
1.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/ArrayTest.java Sun Feb 10 19:33:35 2013 +0100
1.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/ArrayTest.java Mon Feb 11 12:46:43 2013 +0100
1.3 @@ -17,7 +17,6 @@
1.4 */
1.5 package org.apidesign.vm4brwsr;
1.6
1.7 -import javax.script.Invocable;
1.8 import static org.testng.Assert.*;
1.9 import org.testng.annotations.BeforeClass;
1.10 import org.testng.annotations.Test;
1.11 @@ -76,18 +75,13 @@
1.12 assertExec("Returns 'false'", Array.class, "instanceOfArray__ZLjava_lang_Object_2", Double.valueOf(0), "non-array");
1.13 }
1.14
1.15 - private static CharSequence codeSeq;
1.16 - private static Invocable code;
1.17 + private static TestVM code;
1.18
1.19 @BeforeClass
1.20 public void compileTheCode() throws Exception {
1.21 - StringBuilder sb = new StringBuilder();
1.22 - code = StaticMethodTest.compileClass(sb,
1.23 - "org/apidesign/vm4brwsr/Array"
1.24 - );
1.25 - codeSeq = sb;
1.26 + code = TestVM.compileClass("org/apidesign/vm4brwsr/Array");
1.27 }
1.28 private static void assertExec(String msg, Class clazz, String method, Object expRes, Object... args) throws Exception {
1.29 - StaticMethodTest.assertExec(code, codeSeq, msg, clazz, method, expRes, args);
1.30 + code.assertExec(msg, clazz, method, expRes, args);
1.31 }
1.32 }
2.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/ClassTest.java Sun Feb 10 19:33:35 2013 +0100
2.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/ClassTest.java Mon Feb 11 12:46:43 2013 +0100
2.3 @@ -17,7 +17,6 @@
2.4 */
2.5 package org.apidesign.vm4brwsr;
2.6
2.7 -import javax.script.Invocable;
2.8 import org.testng.annotations.Test;
2.9 import static org.testng.Assert.*;
2.10 import org.testng.annotations.BeforeClass;
2.11 @@ -185,22 +184,17 @@
2.12 );
2.13 }
2.14
2.15 - private static CharSequence codeSeq;
2.16 - private static Invocable code;
2.17 + private static TestVM code;
2.18
2.19 @BeforeClass
2.20 public void compileTheCode() throws Exception {
2.21 - if (codeSeq == null) {
2.22 - StringBuilder sb = new StringBuilder();
2.23 - code = StaticMethodTest.compileClass(sb, "org/apidesign/vm4brwsr/Classes");
2.24 - codeSeq = sb;
2.25 - }
2.26 + code = TestVM.compileClass("org/apidesign/vm4brwsr/Classes");
2.27 }
2.28
2.29 private void assertExec(
2.30 String msg, Class clazz, String method, Object expRes, Object... args
2.31 ) throws Exception {
2.32 - StaticMethodTest.assertExec(code, codeSeq, msg, clazz, method, expRes, args);
2.33 + code.assertExec(msg, clazz, method, expRes, args);
2.34 }
2.35
2.36 }
3.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/ExceptionsTest.java Sun Feb 10 19:33:35 2013 +0100
3.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/ExceptionsTest.java Mon Feb 11 12:46:43 2013 +0100
3.3 @@ -17,7 +17,6 @@
3.4 */
3.5 package org.apidesign.vm4brwsr;
3.6
3.7 -import javax.script.Invocable;
3.8 import javax.script.ScriptException;
3.9 import static org.testng.Assert.*;
3.10 import org.testng.annotations.BeforeClass;
3.11 @@ -81,8 +80,7 @@
3.12 }
3.13
3.14 @Test public void testThreeCalls() throws Exception {
3.15 - Object vm = code.invokeFunction("bck2brwsr");
3.16 - Object clazz = code.invokeMethod(vm, "loadClass", Exceptions.class.getName());
3.17 + Object clazz = code.loadClass("loadClass", Exceptions.class.getName());
3.18
3.19 String method = "readCounter__ILjava_lang_String_2";
3.20
3.21 @@ -104,18 +102,13 @@
3.22 }
3.23 }
3.24
3.25 - private static CharSequence codeSeq;
3.26 - private static Invocable code;
3.27 + private static TestVM code;
3.28
3.29 @BeforeClass
3.30 public void compileTheCode() throws Exception {
3.31 - StringBuilder sb = new StringBuilder();
3.32 - code = StaticMethodTest.compileClass(sb,
3.33 - "org/apidesign/vm4brwsr/Exceptions"
3.34 - );
3.35 - codeSeq = sb;
3.36 + code = TestVM.compileClass("org/apidesign/vm4brwsr/Exceptions");
3.37 }
3.38 private static void assertExec(String msg, Class clazz, String method, Object expRes, Object... args) throws Exception {
3.39 - StaticMethodTest.assertExec(code, codeSeq, msg, clazz, method, expRes, args);
3.40 + code.assertExec(msg, clazz, method, expRes, args);
3.41 }
3.42 }
4.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/InstanceTest.java Sun Feb 10 19:33:35 2013 +0100
4.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/InstanceTest.java Mon Feb 11 12:46:43 2013 +0100
4.3 @@ -17,7 +17,6 @@
4.4 */
4.5 package org.apidesign.vm4brwsr;
4.6
4.7 -import javax.script.Invocable;
4.8 import org.testng.annotations.Test;
4.9 import org.testng.annotations.BeforeClass;
4.10
4.11 @@ -151,22 +150,17 @@
4.12 return "org/apidesign/vm4brwsr/Instance";
4.13 }
4.14
4.15 - private static CharSequence codeSeq;
4.16 - private static Invocable code;
4.17 + private static TestVM code;
4.18
4.19 @BeforeClass
4.20 public void compileTheCode() throws Exception {
4.21 - if (codeSeq == null) {
4.22 - StringBuilder sb = new StringBuilder();
4.23 - code = StaticMethodTest.compileClass(sb, startCompilationWith());
4.24 - codeSeq = sb;
4.25 - }
4.26 + code = TestVM.compileClass(startCompilationWith());
4.27 }
4.28
4.29 private void assertExec(
4.30 String msg, Class clazz, String method, Object expRes, Object... args
4.31 ) throws Exception {
4.32 - StaticMethodTest.assertExec(code, codeSeq, msg, clazz, method, expRes, args);
4.33 + code.assertExec(msg, clazz, method, expRes, args);
4.34 }
4.35
4.36 }
5.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java Sun Feb 10 19:33:35 2013 +0100
5.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java Mon Feb 11 12:46:43 2013 +0100
5.3 @@ -17,8 +17,6 @@
5.4 */
5.5 package org.apidesign.vm4brwsr;
5.6
5.7 -import javax.script.Invocable;
5.8 -import javax.script.ScriptException;
5.9 import static org.testng.Assert.*;
5.10 import org.testng.annotations.BeforeClass;
5.11 import org.testng.annotations.Test;
5.12 @@ -690,22 +688,17 @@
5.13 );
5.14 }
5.15
5.16 - private static CharSequence codeSeq;
5.17 - private static Invocable code;
5.18 + private static TestVM code;
5.19
5.20 @BeforeClass
5.21 public void compileTheCode() throws Exception {
5.22 - if (codeSeq == null) {
5.23 - StringBuilder sb = new StringBuilder();
5.24 - code = StaticMethodTest.compileClass(sb, "org/apidesign/vm4brwsr/Numbers");
5.25 - codeSeq = sb;
5.26 - }
5.27 + code = TestVM.compileClass("org/apidesign/vm4brwsr/Numbers");
5.28 }
5.29
5.30 private static void assertExec(
5.31 String msg, Class<?> clazz, String method, Object expRes, Object... args) throws Exception
5.32 {
5.33 - Object ret = TestUtils.execCode(code, codeSeq, msg, clazz, method, expRes, args);
5.34 + Object ret = code.execCode(msg, clazz, method, expRes, args);
5.35 if (ret == null) {
5.36 return;
5.37 }
5.38 @@ -713,10 +706,10 @@
5.39 double expD = ((Double)expRes).doubleValue();
5.40 double retD = ((Double)ret).doubleValue();
5.41 assertEquals(retD, expD, 0.000004, msg + " "
5.42 - + StaticMethodTest.dumpJS(codeSeq));
5.43 + + code.toString());
5.44 return;
5.45 }
5.46 - assertEquals(ret, expRes, msg + " " + StaticMethodTest.dumpJS(codeSeq));
5.47 + assertEquals(ret, expRes, msg + " " + code.toString());
5.48 }
5.49
5.50 }
6.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java Sun Feb 10 19:33:35 2013 +0100
6.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java Mon Feb 11 12:46:43 2013 +0100
6.3 @@ -17,16 +17,6 @@
6.4 */
6.5 package org.apidesign.vm4brwsr;
6.6
6.7 -import java.io.File;
6.8 -import java.io.FileWriter;
6.9 -import java.io.IOException;
6.10 -import java.io.InputStream;
6.11 -import java.net.URL;
6.12 -import java.util.Enumeration;
6.13 -import javax.script.Invocable;
6.14 -import javax.script.ScriptEngine;
6.15 -import javax.script.ScriptEngineManager;
6.16 -import javax.script.ScriptException;
6.17 import static org.testng.Assert.*;
6.18 import org.testng.annotations.BeforeClass;
6.19 import org.testng.annotations.Test;
6.20 @@ -331,88 +321,18 @@
6.21 );
6.22 }
6.23
6.24 - private static CharSequence codeSeq;
6.25 - private static Invocable code;
6.26 + private static TestVM code;
6.27
6.28 @BeforeClass
6.29 public void compileTheCode() throws Exception {
6.30 StringBuilder sb = new StringBuilder();
6.31 - code = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
6.32 - codeSeq = sb;
6.33 - }
6.34 -
6.35 -
6.36 - private static void assertExec(
6.37 - String msg, Class clazz, String method,
6.38 - Object expRes, Object... args
6.39 - ) throws Exception {
6.40 - assertExec(code, codeSeq, msg, clazz, method, expRes, args);
6.41 - }
6.42 - static void assertExec(
6.43 - Invocable toRun, CharSequence theCode,
6.44 - String msg, Class clazz, String method,
6.45 - Object expRes, Object... args
6.46 - ) throws Exception {
6.47 - Object ret = TestUtils.execCode(toRun, theCode, msg, clazz, method, expRes, args);
6.48 - if (ret == null) {
6.49 - return;
6.50 - }
6.51 - if (expRes != null && expRes.equals(ret)) {
6.52 - return;
6.53 - }
6.54 - assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(theCode));
6.55 -
6.56 + code = TestVM.compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
6.57 }
6.58
6.59 - static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
6.60 - return compileClass(sb, null, names);
6.61 - }
6.62 - static Invocable compileClass(
6.63 - StringBuilder sb, ScriptEngine[] eng, String... names
6.64 - ) throws ScriptException, IOException {
6.65 - if (sb == null) {
6.66 - sb = new StringBuilder();
6.67 - }
6.68 - Bck2Brwsr.generate(sb, new EmulationResources(), names);
6.69 - ScriptEngineManager sem = new ScriptEngineManager();
6.70 - ScriptEngine js = sem.getEngineByExtension("js");
6.71 - if (eng != null) {
6.72 - eng[0] = js;
6.73 - }
6.74 - try {
6.75 - Object res = js.eval(sb.toString());
6.76 - assertTrue(js instanceof Invocable, "It is invocable object: " + res);
6.77 - return (Invocable)js;
6.78 - } catch (Exception ex) {
6.79 - if (sb.length() > 2000) {
6.80 - sb = dumpJS(sb);
6.81 - }
6.82 - fail("Could not evaluate:\n" + sb, ex);
6.83 - return null;
6.84 - }
6.85 - }
6.86 - static StringBuilder dumpJS(CharSequence sb) throws IOException {
6.87 - File f = File.createTempFile("execution", ".js");
6.88 - FileWriter w = new FileWriter(f);
6.89 - w.append(sb);
6.90 - w.close();
6.91 - return new StringBuilder(f.getPath());
6.92 - }
6.93 - private static class EmulationResources implements Bck2Brwsr.Resources {
6.94 - @Override
6.95 - public InputStream get(String name) throws IOException {
6.96 - Enumeration<URL> en = StaticMethodTest.class.getClassLoader().getResources(name);
6.97 - URL u = null;
6.98 - while (en.hasMoreElements()) {
6.99 - u = en.nextElement();
6.100 - }
6.101 - if (u == null) {
6.102 - throw new IOException("Can't find " + name);
6.103 - }
6.104 - if (u.toExternalForm().contains("rt.jar!")) {
6.105 - throw new IOException("No emulation for " + u);
6.106 - }
6.107 - return u.openStream();
6.108 - }
6.109 + private void assertExec(
6.110 + String msg, Class<?> clazz, String method,
6.111 + Object ret, Object... args
6.112 + ) throws Exception {
6.113 + code.assertExec(msg, clazz, method, ret, args);
6.114 }
6.115 }
7.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/StringTest.java Sun Feb 10 19:33:35 2013 +0100
7.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/StringTest.java Mon Feb 11 12:46:43 2013 +0100
7.3 @@ -17,7 +17,6 @@
7.4 */
7.5 package org.apidesign.vm4brwsr;
7.6
7.7 -import javax.script.Invocable;
7.8 import org.testng.annotations.Test;
7.9 import static org.testng.Assert.*;
7.10 import org.testng.annotations.BeforeClass;
7.11 @@ -194,23 +193,20 @@
7.12
7.13 }
7.14
7.15 - private static CharSequence codeSeq;
7.16 - private static Invocable code;
7.17 + private static TestVM code;
7.18
7.19 @BeforeClass
7.20 public void compileTheCode() throws Exception {
7.21 - StringBuilder sb = new StringBuilder();
7.22 - code = StaticMethodTest.compileClass(sb,
7.23 + code = TestVM.compileClass(
7.24 "org/apidesign/vm4brwsr/StringSample",
7.25 "java/lang/String"
7.26 );
7.27 - codeSeq = sb;
7.28 }
7.29
7.30 private static void assertExec(String msg,
7.31 Class<?> clazz, String method, Object expRes, Object... args
7.32 ) throws Exception {
7.33 - StaticMethodTest.assertExec(code, codeSeq, msg, clazz, method, expRes, args);
7.34 + code.assertExec(msg, clazz, method, expRes, args);
7.35 }
7.36
7.37 }
8.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/TestUtils.java Sun Feb 10 19:33:35 2013 +0100
8.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
8.3 @@ -1,60 +0,0 @@
8.4 -/**
8.5 - * Back 2 Browser Bytecode Translator
8.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
8.7 - *
8.8 - * This program is free software: you can redistribute it and/or modify
8.9 - * it under the terms of the GNU General Public License as published by
8.10 - * the Free Software Foundation, version 2 of the License.
8.11 - *
8.12 - * This program is distributed in the hope that it will be useful,
8.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
8.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8.15 - * GNU General Public License for more details.
8.16 - *
8.17 - * You should have received a copy of the GNU General Public License
8.18 - * along with this program. Look for COPYING file in the top folder.
8.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
8.20 - */
8.21 -package org.apidesign.vm4brwsr;
8.22 -
8.23 -import javax.script.Invocable;
8.24 -import javax.script.ScriptException;
8.25 -import static org.testng.Assert.*;
8.26 -
8.27 -class TestUtils {
8.28 -
8.29 - static Object execCode(Invocable code, CharSequence codeSeq,
8.30 - String msg, Class<?> clazz, String method, Object expRes, Object... args)
8.31 - throws Exception
8.32 - {
8.33 - Object ret = null;
8.34 - try {
8.35 - ret = code.invokeFunction("bck2brwsr");
8.36 - ret = code.invokeMethod(ret, "loadClass", clazz.getName());
8.37 - ret = code.invokeMethod(ret, method, args);
8.38 - } catch (ScriptException ex) {
8.39 - fail("Execution failed in " + StaticMethodTest.dumpJS(codeSeq), ex);
8.40 - } catch (NoSuchMethodException ex) {
8.41 - fail("Cannot find method in " + StaticMethodTest.dumpJS(codeSeq), ex);
8.42 - }
8.43 - if (ret == null && expRes == null) {
8.44 - return null;
8.45 - }
8.46 - if (expRes.equals(ret)) {
8.47 - return null;
8.48 - }
8.49 - if (expRes instanceof Number) {
8.50 - // in case of Long it is necessary convert it to number
8.51 - // since the Long is represented by two numbers in JavaScript
8.52 - try {
8.53 - ret = code.invokeMethod(ret, "toFP");
8.54 - ret = code.invokeFunction("Number", ret);
8.55 - } catch (ScriptException ex) {
8.56 - fail("Conversion to number failed in " + StaticMethodTest.dumpJS(codeSeq), ex);
8.57 - } catch (NoSuchMethodException ex) {
8.58 - fail("Cannot find global Number(x) function in " + StaticMethodTest.dumpJS(codeSeq), ex);
8.59 - }
8.60 - }
8.61 - return ret;
8.62 - }
8.63 -}
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
9.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java Mon Feb 11 12:46:43 2013 +0100
9.3 @@ -0,0 +1,170 @@
9.4 +/**
9.5 + * Back 2 Browser Bytecode Translator
9.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
9.7 + *
9.8 + * This program is free software: you can redistribute it and/or modify
9.9 + * it under the terms of the GNU General Public License as published by
9.10 + * the Free Software Foundation, version 2 of the License.
9.11 + *
9.12 + * This program is distributed in the hope that it will be useful,
9.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9.15 + * GNU General Public License for more details.
9.16 + *
9.17 + * You should have received a copy of the GNU General Public License
9.18 + * along with this program. Look for COPYING file in the top folder.
9.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
9.20 + */
9.21 +package org.apidesign.vm4brwsr;
9.22 +
9.23 +import java.io.File;
9.24 +import java.io.FileWriter;
9.25 +import java.io.IOException;
9.26 +import java.io.InputStream;
9.27 +import java.net.URL;
9.28 +import java.util.Enumeration;
9.29 +import javax.script.Invocable;
9.30 +import javax.script.ScriptEngine;
9.31 +import javax.script.ScriptEngineManager;
9.32 +import javax.script.ScriptException;
9.33 +import static org.testng.Assert.*;
9.34 +
9.35 +final class TestVM {
9.36 + private final Invocable code;
9.37 + private final CharSequence codeSeq;
9.38 + private final Object bck2brwsr;
9.39 +
9.40 +
9.41 + private TestVM(Invocable code, CharSequence codeSeq) throws ScriptException, NoSuchMethodException {
9.42 + this.code = code;
9.43 + this.codeSeq = codeSeq;
9.44 + this.bck2brwsr = code.invokeFunction("bck2brwsr");
9.45 + }
9.46 +
9.47 +
9.48 + public Object execCode(
9.49 + String msg, Class<?> clazz, String method,
9.50 + Object expRes, Object... args
9.51 + ) throws Exception {
9.52 + Object ret = null;
9.53 + try {
9.54 + ret = code.invokeMethod(bck2brwsr, "loadClass", clazz.getName());
9.55 + ret = code.invokeMethod(ret, method, args);
9.56 + } catch (ScriptException ex) {
9.57 + fail("Execution failed in " + dumpJS(codeSeq), ex);
9.58 + } catch (NoSuchMethodException ex) {
9.59 + fail("Cannot find method in " + dumpJS(codeSeq), ex);
9.60 + }
9.61 + if (ret == null && expRes == null) {
9.62 + return null;
9.63 + }
9.64 + if (expRes.equals(ret)) {
9.65 + return null;
9.66 + }
9.67 + if (expRes instanceof Number) {
9.68 + // in case of Long it is necessary convert it to number
9.69 + // since the Long is represented by two numbers in JavaScript
9.70 + try {
9.71 + ret = code.invokeMethod(ret, "toFP");
9.72 + ret = code.invokeFunction("Number", ret);
9.73 + } catch (ScriptException ex) {
9.74 + fail("Conversion to number failed in " + dumpJS(codeSeq), ex);
9.75 + } catch (NoSuchMethodException ex) {
9.76 + fail("Cannot find global Number(x) function in " + dumpJS(codeSeq), ex);
9.77 + }
9.78 + }
9.79 + return ret;
9.80 + }
9.81 +
9.82 + void assertExec(
9.83 + String msg, Class clazz, String method, Object expRes, Object... args
9.84 + ) throws Exception {
9.85 + Object ret = execCode(msg, clazz, method, expRes, args);
9.86 + if (ret == null) {
9.87 + return;
9.88 + }
9.89 + if (expRes != null && expRes.equals(ret)) {
9.90 + return;
9.91 + }
9.92 + assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(codeSeq));
9.93 + }
9.94 +
9.95 + static TestVM compileClass(String... names) throws ScriptException, IOException {
9.96 + return compileClass(null, names);
9.97 + }
9.98 +
9.99 + static TestVM compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
9.100 + return compileClass(sb, null, names);
9.101 + }
9.102 +
9.103 + static TestVM compileClass(StringBuilder sb, ScriptEngine[] eng, String... names) throws ScriptException, IOException {
9.104 + if (sb == null) {
9.105 + sb = new StringBuilder();
9.106 + }
9.107 + Bck2Brwsr.generate(sb, new EmulationResources(), names);
9.108 + ScriptEngineManager sem = new ScriptEngineManager();
9.109 + ScriptEngine js = sem.getEngineByExtension("js");
9.110 + if (eng != null) {
9.111 + eng[0] = js;
9.112 + }
9.113 + try {
9.114 + Object res = js.eval(sb.toString());
9.115 + assertTrue(js instanceof Invocable, "It is invocable object: " + res);
9.116 + return new TestVM((Invocable) js, sb);
9.117 + } catch (Exception ex) {
9.118 + if (sb.length() > 2000) {
9.119 + sb = dumpJS(sb);
9.120 + }
9.121 + fail("Could not evaluate:\n" + sb, ex);
9.122 + return null;
9.123 + }
9.124 + }
9.125 +
9.126 + Object loadClass(String loadClass, String name) throws ScriptException, NoSuchMethodException {
9.127 + return code.invokeMethod(bck2brwsr, "loadClass", Exceptions.class.getName());
9.128 + }
9.129 +
9.130 + Object invokeMethod(Object obj, String method, Object... params) throws ScriptException, NoSuchMethodException {
9.131 + return code.invokeMethod(obj, method, params);
9.132 + }
9.133 +
9.134 + Object invokeFunction(String methodName, Object... args) throws ScriptException, NoSuchMethodException {
9.135 + return code.invokeFunction(methodName, args);
9.136 + }
9.137 +
9.138 + static StringBuilder dumpJS(CharSequence sb) throws IOException {
9.139 + File f = File.createTempFile("execution", ".js");
9.140 + FileWriter w = new FileWriter(f);
9.141 + w.append(sb);
9.142 + w.close();
9.143 + return new StringBuilder(f.getPath());
9.144 + }
9.145 +
9.146 + @Override
9.147 + public String toString() {
9.148 + try {
9.149 + return dumpJS(codeSeq).toString();
9.150 + } catch (IOException ex) {
9.151 + return ex.toString();
9.152 + }
9.153 + }
9.154 +
9.155 +
9.156 + private static class EmulationResources implements Bck2Brwsr.Resources {
9.157 + @Override
9.158 + public InputStream get(String name) throws IOException {
9.159 + Enumeration<URL> en = StaticMethodTest.class.getClassLoader().getResources(name);
9.160 + URL u = null;
9.161 + while (en.hasMoreElements()) {
9.162 + u = en.nextElement();
9.163 + }
9.164 + if (u == null) {
9.165 + throw new IOException("Can't find " + name);
9.166 + }
9.167 + if (u.toExternalForm().contains("rt.jar!")) {
9.168 + throw new IOException("No emulation for " + u);
9.169 + }
9.170 + return u.openStream();
9.171 + }
9.172 + }
9.173 +}
10.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/VMLazyTest.java Sun Feb 10 19:33:35 2013 +0100
10.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/VMLazyTest.java Mon Feb 11 12:46:43 2013 +0100
10.3 @@ -17,7 +17,6 @@
10.4 */
10.5 package org.apidesign.vm4brwsr;
10.6
10.7 -import javax.script.Invocable;
10.8 import javax.script.ScriptContext;
10.9 import javax.script.ScriptEngine;
10.10 import javax.script.ScriptException;
10.11 @@ -30,10 +29,7 @@
10.12 * @author Jaroslav Tulach <jtulach@netbeans.org>
10.13 */
10.14 public class VMLazyTest {
10.15 -
10.16 -
10.17 - private static CharSequence codeSeq;
10.18 - private static Invocable code;
10.19 + private static TestVM code;
10.20
10.21 @BeforeClass
10.22 public void compileTheCode() throws Exception {
10.23 @@ -50,11 +46,10 @@
10.24 sb.append("\n}");
10.25
10.26 ScriptEngine[] arr = { null };
10.27 - code = StaticMethodTest.compileClass(sb, arr,
10.28 + code = TestVM.compileClass(sb, arr,
10.29 new String[]{"org/apidesign/vm4brwsr/VM", "org/apidesign/vm4brwsr/StaticMethod"}
10.30 );
10.31 arr[0].getContext().setAttribute("loader", new BytesLoader(), ScriptContext.ENGINE_SCOPE);
10.32 - codeSeq = sb;
10.33 }
10.34
10.35 @Test public void invokeStaticMethod() throws Exception {
10.36 @@ -83,9 +78,9 @@
10.37 try {
10.38 ret = code.invokeFunction(methodName, args);
10.39 } catch (ScriptException ex) {
10.40 - fail("Execution failed in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
10.41 + fail("Execution failed in\n" + code.toString(), ex);
10.42 } catch (NoSuchMethodException ex) {
10.43 - fail("Cannot find method in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
10.44 + fail("Cannot find method in\n" + code.toString(), ex);
10.45 }
10.46 if (ret == null && expRes == null) {
10.47 return;
10.48 @@ -93,6 +88,6 @@
10.49 if (expRes.equals(ret)) {
10.50 return;
10.51 }
10.52 - assertEquals(ret, expRes, msg + "was: " + ret + "\n" + StaticMethodTest.dumpJS(codeSeq));
10.53 + assertEquals(ret, expRes, msg + "was: " + ret + "\n" + code.toString());
10.54 }
10.55 }
11.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/VMinVMTest.java Sun Feb 10 19:33:35 2013 +0100
11.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/VMinVMTest.java Mon Feb 11 12:46:43 2013 +0100
11.3 @@ -21,7 +21,6 @@
11.4 import java.io.FileWriter;
11.5 import java.io.IOException;
11.6 import static org.testng.Assert.*;
11.7 -import javax.script.Invocable;
11.8 import org.testng.annotations.BeforeClass;
11.9 import org.testng.annotations.Test;
11.10
11.11 @@ -30,9 +29,7 @@
11.12 * @author Jaroslav Tulach <jtulach@netbeans.org>
11.13 */
11.14 public class VMinVMTest {
11.15 -
11.16 - private static CharSequence codeSeq;
11.17 - private static Invocable code;
11.18 + private static TestVM code;
11.19
11.20 @Test public void compareGeneratedCodeForArrayClass() throws Exception {
11.21 compareCode("org/apidesign/vm4brwsr/Array.class");
11.22 @@ -44,11 +41,7 @@
11.23
11.24 @BeforeClass
11.25 public void compileTheCode() throws Exception {
11.26 - StringBuilder sb = new StringBuilder();
11.27 - code = StaticMethodTest.compileClass(sb,
11.28 - "org/apidesign/vm4brwsr/VMinVM"
11.29 - );
11.30 - codeSeq = sb;
11.31 + code = TestVM.compileClass("org/apidesign/vm4brwsr/VMinVM");
11.32 }
11.33
11.34 private void compareCode(final String nm) throws Exception, IOException {
11.35 @@ -73,7 +66,7 @@
11.36 }
11.37 }
11.38 w.append("\n];\n");
11.39 - w.append(codeSeq);
11.40 + w.append(code.toString());
11.41 w.close();
11.42 throw new Exception(ex.getMessage() + " file: " + f, ex);
11.43 }
11.44 @@ -83,11 +76,11 @@
11.45
11.46 if (!ret1.toString().equals(ret)) {
11.47 StringBuilder msg = new StringBuilder("Difference found between ");
11.48 - msg.append(StaticMethodTest.dumpJS(ret1));
11.49 + msg.append(TestVM.dumpJS(ret1));
11.50 msg.append(" ");
11.51 - msg.append(StaticMethodTest.dumpJS((CharSequence) ret));
11.52 + msg.append(TestVM.dumpJS((CharSequence) ret));
11.53 msg.append(" compiled by ");
11.54 - msg.append(StaticMethodTest.dumpJS(codeSeq));
11.55 + msg.append(code.toString());
11.56 fail(msg.toString());
11.57 }
11.58 }