1.1 --- a/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/VMTest.java Tue Dec 18 13:10:56 2012 +0100
1.2 +++ b/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/VMTest.java Tue Dec 18 13:25:32 2012 +0100
1.3 @@ -26,6 +26,8 @@
1.4 import java.util.Enumeration;
1.5 import java.util.Map;
1.6 import java.util.WeakHashMap;
1.7 +import java.util.logging.Level;
1.8 +import java.util.logging.Logger;
1.9 import javax.script.Invocable;
1.10 import javax.script.ScriptEngine;
1.11 import javax.script.ScriptEngineManager;
1.12 @@ -33,6 +35,7 @@
1.13 import org.apidesign.vm4brwsr.Bck2Brwsr;
1.14 import org.testng.Assert;
1.15 import org.testng.ITest;
1.16 +import org.testng.annotations.BeforeTest;
1.17 import org.testng.annotations.Factory;
1.18 import org.testng.annotations.Test;
1.19
1.20 @@ -145,10 +148,16 @@
1.21 private Invocable code;
1.22 private CharSequence codeSeq;
1.23 private static final Map<Class,Object[]> compiled = new WeakHashMap<>();
1.24 + private Object inst;
1.25
1.26 private Run(Method m, int type) {
1.27 this.m = m;
1.28 this.type = type;
1.29 + try {
1.30 + initialize();
1.31 + } catch (Throwable ex) {
1.32 + Logger.getLogger(VMTest.class.getName()).log(Level.SEVERE, null, ex);
1.33 + }
1.34 }
1.35
1.36 private void compileTheCode(Class<?> clazz) throws Exception {
1.37 @@ -175,22 +184,27 @@
1.38 codeSeq = sb;
1.39 compiled.put(clazz, new Object[] { code, codeSeq });
1.40 }
1.41 +
1.42 + private void initialize() throws Throwable {
1.43 + if (type == 1) {
1.44 + compileTheCode(m.getDeclaringClass());
1.45 + Object vm = code.invokeFunction("initVM");
1.46 + inst = code.invokeMethod(vm, "loadClass", m.getDeclaringClass().getName());
1.47 + } else if (type == 2) {
1.48 + inst = addBrowserMethod(m.getDeclaringClass(), m.getName());
1.49 + }
1.50 + }
1.51
1.52 @Test(groups = "run") public void executeCode() throws Throwable {
1.53 if (type == 1) {
1.54 try {
1.55 - compileTheCode(m.getDeclaringClass());
1.56 - Object vm = code.invokeFunction("initVM");
1.57 - Object inst = code.invokeMethod(vm, "loadClass", m.getDeclaringClass().getName());
1.58 value = code.invokeMethod(inst, m.getName() + "__" + computeSignature(m));
1.59 } catch (Exception ex) {
1.60 throw new AssertionError(dumpJS(codeSeq)).initCause(ex);
1.61 }
1.62 } else if (type == 2) {
1.63 - Bck2BrwsrLauncher l = new Bck2BrwsrLauncher();
1.64 - l.setTimeout(5000);
1.65 - Bck2BrwsrLauncher.MethodInvocation c = l.addMethod(m.getDeclaringClass(), m.getName());
1.66 - l.execute();
1.67 + Bck2BrwsrLauncher.MethodInvocation c = (Bck2BrwsrLauncher.MethodInvocation) inst;
1.68 + execBrowser();
1.69 value = c.toString();
1.70 } else {
1.71 value = m.invoke(m.getDeclaringClass().newInstance());
1.72 @@ -269,4 +283,28 @@
1.73 }
1.74 return new StringBuilder(f.getPath());
1.75 }
1.76 +
1.77 + private static Bck2BrwsrLauncher launcher;
1.78 +
1.79 + private static synchronized Bck2BrwsrLauncher.MethodInvocation addBrowserMethod(
1.80 + Class<?> clazz, String name
1.81 + ) {
1.82 + if (launcher == null) {
1.83 + launcher = new Bck2BrwsrLauncher();
1.84 + launcher.setTimeout(5000);
1.85 + }
1.86 + return launcher.addMethod(clazz, name);
1.87 + }
1.88 +
1.89 + private static void execBrowser() throws Exception {
1.90 + Bck2BrwsrLauncher l = clearBrowser();
1.91 + if (l != null) {
1.92 + l.execute();
1.93 + }
1.94 + }
1.95 + private static synchronized Bck2BrwsrLauncher clearBrowser() {
1.96 + Bck2BrwsrLauncher l = launcher;
1.97 + launcher = null;
1.98 + return l;
1.99 + }
1.100 }