1.1 --- a/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/VMTest.java Tue Dec 18 13:25:32 2012 +0100
1.2 +++ b/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/VMTest.java Thu Dec 20 11:03:34 2012 +0100
1.3 @@ -29,13 +29,9 @@
1.4 import java.util.logging.Level;
1.5 import java.util.logging.Logger;
1.6 import javax.script.Invocable;
1.7 -import javax.script.ScriptEngine;
1.8 -import javax.script.ScriptEngineManager;
1.9 import org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher;
1.10 -import org.apidesign.vm4brwsr.Bck2Brwsr;
1.11 import org.testng.Assert;
1.12 import org.testng.ITest;
1.13 -import org.testng.annotations.BeforeTest;
1.14 import org.testng.annotations.Factory;
1.15 import org.testng.annotations.Test;
1.16
1.17 @@ -49,6 +45,9 @@
1.18 * @author Jaroslav Tulach <jtulach@netbeans.org>
1.19 */
1.20 public final class VMTest implements ITest {
1.21 + private static final Launcher JS = new Launcher("js");
1.22 + private static final Launcher BROWSER = new Launcher();
1.23 +
1.24 private final Run first, second;
1.25 private final Method m;
1.26
1.27 @@ -96,8 +95,10 @@
1.28 @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
1.29 Object v1 = first.value;
1.30 Object v2 = second.value;
1.31 - if (v1 instanceof Number) {
1.32 - v1 = ((Number)v1).doubleValue();
1.33 + if (v1 != null) {
1.34 + v1 = v1.toString();
1.35 + } else {
1.36 + v1 = "null";
1.37 }
1.38 Assert.assertEquals(v2, v1, "Comparing results");
1.39 }
1.40 @@ -160,51 +161,23 @@
1.41 }
1.42 }
1.43
1.44 - private void compileTheCode(Class<?> clazz) throws Exception {
1.45 - final Object[] data = compiled.get(clazz);
1.46 - if (data != null) {
1.47 - code = (Invocable) data[0];
1.48 - codeSeq = (CharSequence) data[1];
1.49 - return;
1.50 - }
1.51 - StringBuilder sb = new StringBuilder();
1.52 - Bck2Brwsr.generate(sb, VMTest.class.getClassLoader());
1.53 -
1.54 - ScriptEngineManager sem = new ScriptEngineManager();
1.55 - ScriptEngine mach = sem.getEngineByExtension("js");
1.56 -
1.57 - sb.append(
1.58 - "\nvar vm = bck2brwsr(org.apidesign.bck2brwsr.vmtest.VMTest.read);"
1.59 - + "\nfunction initVM() { return vm; };"
1.60 - + "\n");
1.61 -
1.62 - Object res = mach.eval(sb.toString());
1.63 - Assert.assertTrue(mach instanceof Invocable, "It is invocable object: " + res);
1.64 - code = (Invocable) mach;
1.65 - codeSeq = sb;
1.66 - compiled.put(clazz, new Object[] { code, codeSeq });
1.67 - }
1.68 -
1.69 private void initialize() throws Throwable {
1.70 if (type == 1) {
1.71 - compileTheCode(m.getDeclaringClass());
1.72 - Object vm = code.invokeFunction("initVM");
1.73 - inst = code.invokeMethod(vm, "loadClass", m.getDeclaringClass().getName());
1.74 - } else if (type == 2) {
1.75 - inst = addBrowserMethod(m.getDeclaringClass(), m.getName());
1.76 + inst = JS.addMethod(m.getDeclaringClass(), m.getName());
1.77 + }
1.78 + if (type == 2) {
1.79 + inst = BROWSER.addMethod(m.getDeclaringClass(), m.getName());
1.80 }
1.81 }
1.82
1.83 @Test(groups = "run") public void executeCode() throws Throwable {
1.84 if (type == 1) {
1.85 - try {
1.86 - value = code.invokeMethod(inst, m.getName() + "__" + computeSignature(m));
1.87 - } catch (Exception ex) {
1.88 - throw new AssertionError(dumpJS(codeSeq)).initCause(ex);
1.89 - }
1.90 + Bck2BrwsrLauncher.MethodInvocation c = (Bck2BrwsrLauncher.MethodInvocation) inst;
1.91 + JS.exec();
1.92 + value = c.toString();
1.93 } else if (type == 2) {
1.94 Bck2BrwsrLauncher.MethodInvocation c = (Bck2BrwsrLauncher.MethodInvocation) inst;
1.95 - execBrowser();
1.96 + BROWSER.exec();
1.97 value = c.toString();
1.98 } else {
1.99 value = m.invoke(m.getDeclaringClass().newInstance());
1.100 @@ -283,28 +256,4 @@
1.101 }
1.102 return new StringBuilder(f.getPath());
1.103 }
1.104 -
1.105 - private static Bck2BrwsrLauncher launcher;
1.106 -
1.107 - private static synchronized Bck2BrwsrLauncher.MethodInvocation addBrowserMethod(
1.108 - Class<?> clazz, String name
1.109 - ) {
1.110 - if (launcher == null) {
1.111 - launcher = new Bck2BrwsrLauncher();
1.112 - launcher.setTimeout(5000);
1.113 - }
1.114 - return launcher.addMethod(clazz, name);
1.115 - }
1.116 -
1.117 - private static void execBrowser() throws Exception {
1.118 - Bck2BrwsrLauncher l = clearBrowser();
1.119 - if (l != null) {
1.120 - l.execute();
1.121 - }
1.122 - }
1.123 - private static synchronized Bck2BrwsrLauncher clearBrowser() {
1.124 - Bck2BrwsrLauncher l = launcher;
1.125 - launcher = null;
1.126 - return l;
1.127 - }
1.128 }