Using the Console for invoking VMTests in Rhino. All tests are passing now. launcher
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 20 Dec 2012 11:03:34 +0100
branchlauncher
changeset 356e078953818d2
parent 355 eea0065bcc1a
child 357 dc375a56fd15
Using the Console for invoking VMTests in Rhino. All tests are passing now.
launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java
launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java
vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/Launcher.java
vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/VMTest.java
     1.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java	Thu Dec 20 08:59:47 2012 +0100
     1.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java	Thu Dec 20 11:03:34 2012 +0100
     1.3 @@ -20,6 +20,7 @@
     1.4  import java.awt.Desktop;
     1.5  import java.io.IOException;
     1.6  import java.io.InputStream;
     1.7 +import java.io.InterruptedIOException;
     1.8  import java.io.OutputStream;
     1.9  import java.io.Writer;
    1.10  import java.net.URI;
    1.11 @@ -32,6 +33,12 @@
    1.12  import java.util.Set;
    1.13  import java.util.concurrent.CountDownLatch;
    1.14  import java.util.concurrent.TimeUnit;
    1.15 +import java.util.logging.Level;
    1.16 +import java.util.logging.Logger;
    1.17 +import javax.script.Invocable;
    1.18 +import javax.script.ScriptEngine;
    1.19 +import javax.script.ScriptEngineManager;
    1.20 +import javax.script.ScriptException;
    1.21  import static org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher.copyStream;
    1.22  import org.apidesign.vm4brwsr.Bck2Brwsr;
    1.23  import org.glassfish.grizzly.PortRange;
    1.24 @@ -49,6 +56,7 @@
    1.25      private Set<ClassLoader> loaders = new LinkedHashSet<>();
    1.26      private List<MethodInvocation> methods = new ArrayList<>();
    1.27      private long timeOut;
    1.28 +    private String sen;
    1.29      
    1.30      
    1.31      public MethodInvocation addMethod(Class<?> clazz, String method) {
    1.32 @@ -62,6 +70,10 @@
    1.33          timeOut = ms;
    1.34      }
    1.35      
    1.36 +    public void setScriptEngineName(String sen) {
    1.37 +        this.sen = sen;
    1.38 +    }
    1.39 +    
    1.40      public static void main( String[] args ) throws Exception {
    1.41          Bck2BrwsrLauncher l = new Bck2BrwsrLauncher();
    1.42          
    1.43 @@ -78,7 +90,59 @@
    1.44      }
    1.45  
    1.46  
    1.47 -    public void execute() throws URISyntaxException, IOException, InterruptedException {
    1.48 +    public void execute() throws IOException {
    1.49 +        try {
    1.50 +            if (sen != null) {
    1.51 +                executeRhino();
    1.52 +            } else {
    1.53 +                executeInBrowser();
    1.54 +            }
    1.55 +        } catch (InterruptedException ex) {
    1.56 +            final InterruptedIOException iio = new InterruptedIOException(ex.getMessage());
    1.57 +            iio.initCause(ex);
    1.58 +            throw iio;
    1.59 +        } catch (Exception ex) {
    1.60 +            if (ex instanceof IOException) {
    1.61 +                throw (IOException)ex;
    1.62 +            }
    1.63 +            if (ex instanceof RuntimeException) {
    1.64 +                throw (RuntimeException)ex;
    1.65 +            }
    1.66 +            throw new IOException(ex);
    1.67 +        }
    1.68 +    }
    1.69 +    
    1.70 +    private void executeRhino() throws IOException, ScriptException, NoSuchMethodException {
    1.71 +        StringBuilder sb = new StringBuilder();
    1.72 +        Bck2Brwsr.generate(sb, new Res());
    1.73 +
    1.74 +        ScriptEngineManager sem = new ScriptEngineManager();
    1.75 +        ScriptEngine mach = sem.getEngineByExtension(sen);
    1.76 +
    1.77 +        sb.append(
    1.78 +              "\nvar vm = bck2brwsr(org.apidesign.bck2brwsr.vmtest.VMTest.read);"
    1.79 +            + "\nfunction initVM() { return vm; };"
    1.80 +            + "\n");
    1.81 +
    1.82 +        Object res = mach.eval(sb.toString());
    1.83 +        if (!(mach instanceof Invocable)) {
    1.84 +            throw new IOException("It is invocable object: " + res);
    1.85 +        }
    1.86 +        Invocable code = (Invocable) mach;
    1.87 +        
    1.88 +        Object vm = code.invokeFunction("initVM");
    1.89 +        Object console = code.invokeMethod(vm, "loadClass", Console.class.getName());
    1.90 +
    1.91 +        final MethodInvocation[] cases = this.methods.toArray(new MethodInvocation[0]);
    1.92 +        for (MethodInvocation mi : cases) {
    1.93 +            mi.result = code.invokeMethod(console, 
    1.94 +                "invoke__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2", 
    1.95 +                mi.className, mi.methodName
    1.96 +            ).toString();
    1.97 +        }
    1.98 +    }
    1.99 +    
   1.100 +    private void executeInBrowser() throws InterruptedException, URISyntaxException, IOException {
   1.101          final CountDownLatch wait = new CountDownLatch(1);
   1.102          final MethodInvocation[] cases = this.methods.toArray(new MethodInvocation[0]);
   1.103          
   1.104 @@ -158,7 +222,7 @@
   1.105              }
   1.106          }
   1.107      }
   1.108 -    
   1.109 +
   1.110      private class Res implements Bck2Brwsr.Resources {
   1.111          @Override
   1.112          public InputStream get(String resource) throws IOException {
     2.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java	Thu Dec 20 08:59:47 2012 +0100
     2.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Console.java	Thu Dec 20 11:03:34 2012 +0100
     2.3 @@ -83,6 +83,11 @@
     2.4              log(ex.getMessage());
     2.5          }
     2.6      }
     2.7 +    
     2.8 +    static String invoke(String clazz, String method) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException {
     2.9 +        final Object r = invokeMethod(clazz, method);
    2.10 +        return r == null ? "null" : r.toString().toString();
    2.11 +    }
    2.12  
    2.13      private static Object invokeMethod(String clazz, String method) 
    2.14      throws ClassNotFoundException, InvocationTargetException, 
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/Launcher.java	Thu Dec 20 11:03:34 2012 +0100
     3.3 @@ -0,0 +1,61 @@
     3.4 +/**
     3.5 + * Back 2 Browser Bytecode Translator
     3.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     3.7 + *
     3.8 + * This program is free software: you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License as published by
    3.10 + * the Free Software Foundation, version 2 of the License.
    3.11 + *
    3.12 + * This program is distributed in the hope that it will be useful,
    3.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.15 + * GNU General Public License for more details.
    3.16 + *
    3.17 + * You should have received a copy of the GNU General Public License
    3.18 + * along with this program. Look for COPYING file in the top folder.
    3.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    3.20 + */
    3.21 +package org.apidesign.bck2brwsr.vmtest;
    3.22 +
    3.23 +import org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher;
    3.24 +
    3.25 +/**
    3.26 + *
    3.27 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.28 + */
    3.29 +final class Launcher {
    3.30 +    private final String sen;
    3.31 +    private Bck2BrwsrLauncher launcher;
    3.32 +    
    3.33 +    Launcher() {
    3.34 +        this(null);
    3.35 +    }
    3.36 +    Launcher(String sen) {
    3.37 +        this.sen = sen;
    3.38 +    }
    3.39 +
    3.40 +    synchronized Bck2BrwsrLauncher clear() {
    3.41 +        Bck2BrwsrLauncher l = launcher;
    3.42 +        launcher = null;
    3.43 +        return l;
    3.44 +    }
    3.45 +
    3.46 +    synchronized Bck2BrwsrLauncher.MethodInvocation addMethod(Class<?> clazz, String name) {
    3.47 +        if (launcher == null) {
    3.48 +            launcher = new Bck2BrwsrLauncher();
    3.49 +            launcher.setTimeout(5000);
    3.50 +            if (sen != null) {
    3.51 +                launcher.setScriptEngineName(sen);
    3.52 +            }
    3.53 +        }
    3.54 +        return launcher.addMethod(clazz, name);
    3.55 +    }
    3.56 +
    3.57 +    void exec() throws Exception {
    3.58 +        Bck2BrwsrLauncher l = clear();
    3.59 +        if (l != null) {
    3.60 +            l.execute();
    3.61 +        }
    3.62 +    }
    3.63 +    
    3.64 +}
     4.1 --- a/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/VMTest.java	Thu Dec 20 08:59:47 2012 +0100
     4.2 +++ b/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/VMTest.java	Thu Dec 20 11:03:34 2012 +0100
     4.3 @@ -29,13 +29,9 @@
     4.4  import java.util.logging.Level;
     4.5  import java.util.logging.Logger;
     4.6  import javax.script.Invocable;
     4.7 -import javax.script.ScriptEngine;
     4.8 -import javax.script.ScriptEngineManager;
     4.9  import org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher;
    4.10 -import org.apidesign.vm4brwsr.Bck2Brwsr;
    4.11  import org.testng.Assert;
    4.12  import org.testng.ITest;
    4.13 -import org.testng.annotations.BeforeTest;
    4.14  import org.testng.annotations.Factory;
    4.15  import org.testng.annotations.Test;
    4.16  
    4.17 @@ -49,6 +45,9 @@
    4.18   * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.19   */
    4.20  public final class VMTest implements ITest {
    4.21 +    private static final Launcher JS = new Launcher("js");
    4.22 +    private static final Launcher BROWSER = new Launcher();
    4.23 +    
    4.24      private final Run first, second;
    4.25      private final Method m;
    4.26      
    4.27 @@ -96,8 +95,10 @@
    4.28      @Test(dependsOnGroups = "run") public void compareResults() throws Throwable {
    4.29          Object v1 = first.value;
    4.30          Object v2 = second.value;
    4.31 -        if (v1 instanceof Number) {
    4.32 -            v1 = ((Number)v1).doubleValue();
    4.33 +        if (v1 != null) {
    4.34 +            v1 = v1.toString();
    4.35 +        } else {
    4.36 +            v1 = "null";
    4.37          }
    4.38          Assert.assertEquals(v2, v1, "Comparing results");
    4.39      }
    4.40 @@ -160,51 +161,23 @@
    4.41              }
    4.42          }
    4.43  
    4.44 -        private void compileTheCode(Class<?> clazz) throws Exception {
    4.45 -            final Object[] data = compiled.get(clazz);
    4.46 -            if (data != null) {
    4.47 -                code = (Invocable) data[0];
    4.48 -                codeSeq = (CharSequence) data[1];
    4.49 -                return;
    4.50 -            }
    4.51 -            StringBuilder sb = new StringBuilder();
    4.52 -            Bck2Brwsr.generate(sb, VMTest.class.getClassLoader());
    4.53 -
    4.54 -            ScriptEngineManager sem = new ScriptEngineManager();
    4.55 -            ScriptEngine mach = sem.getEngineByExtension("js");
    4.56 -            
    4.57 -            sb.append(
    4.58 -                  "\nvar vm = bck2brwsr(org.apidesign.bck2brwsr.vmtest.VMTest.read);"
    4.59 -                + "\nfunction initVM() { return vm; };"
    4.60 -                + "\n");
    4.61 -
    4.62 -            Object res = mach.eval(sb.toString());
    4.63 -            Assert.assertTrue(mach instanceof Invocable, "It is invocable object: " + res);
    4.64 -            code = (Invocable) mach;
    4.65 -            codeSeq = sb;
    4.66 -            compiled.put(clazz, new Object[] { code, codeSeq });
    4.67 -        }
    4.68 -        
    4.69          private void initialize() throws Throwable {
    4.70              if (type == 1) {
    4.71 -                compileTheCode(m.getDeclaringClass());
    4.72 -                Object vm = code.invokeFunction("initVM");
    4.73 -                inst = code.invokeMethod(vm, "loadClass", m.getDeclaringClass().getName());
    4.74 -            } else if (type == 2) {
    4.75 -                inst = addBrowserMethod(m.getDeclaringClass(), m.getName());
    4.76 +                inst = JS.addMethod(m.getDeclaringClass(), m.getName());
    4.77 +            }
    4.78 +            if (type == 2) {
    4.79 +                inst = BROWSER.addMethod(m.getDeclaringClass(), m.getName());
    4.80              }
    4.81          }
    4.82  
    4.83          @Test(groups = "run") public void executeCode() throws Throwable {
    4.84              if (type == 1) {
    4.85 -                try {
    4.86 -                    value = code.invokeMethod(inst, m.getName() + "__" + computeSignature(m));
    4.87 -                } catch (Exception ex) {
    4.88 -                    throw new AssertionError(dumpJS(codeSeq)).initCause(ex);
    4.89 -                }
    4.90 +                Bck2BrwsrLauncher.MethodInvocation c = (Bck2BrwsrLauncher.MethodInvocation) inst;
    4.91 +                JS.exec();
    4.92 +                value = c.toString();
    4.93              } else if (type == 2) {
    4.94                  Bck2BrwsrLauncher.MethodInvocation c = (Bck2BrwsrLauncher.MethodInvocation) inst;
    4.95 -                execBrowser();
    4.96 +                BROWSER.exec();
    4.97                  value = c.toString();
    4.98              } else {
    4.99                  value = m.invoke(m.getDeclaringClass().newInstance());
   4.100 @@ -283,28 +256,4 @@
   4.101          }
   4.102          return new StringBuilder(f.getPath());
   4.103      }
   4.104 -    
   4.105 -    private static Bck2BrwsrLauncher launcher;
   4.106 -
   4.107 -    private static synchronized Bck2BrwsrLauncher.MethodInvocation addBrowserMethod(
   4.108 -        Class<?> clazz, String name
   4.109 -    ) {
   4.110 -        if (launcher == null) {
   4.111 -            launcher = new Bck2BrwsrLauncher();
   4.112 -            launcher.setTimeout(5000);
   4.113 -        }
   4.114 -        return launcher.addMethod(clazz, name);
   4.115 -    }
   4.116 -    
   4.117 -    private static void execBrowser() throws Exception {
   4.118 -        Bck2BrwsrLauncher l = clearBrowser();
   4.119 -        if (l != null) {
   4.120 -            l.execute();
   4.121 -        }
   4.122 -    }
   4.123 -    private static synchronized Bck2BrwsrLauncher clearBrowser() {
   4.124 -        Bck2BrwsrLauncher l = launcher;
   4.125 -        launcher = null;
   4.126 -        return l;
   4.127 -    }
   4.128  }