vm/src/test/java/org/apidesign/vm4brwsr/TestVM.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 11 Feb 2013 12:46:43 +0100
branchemul
changeset 708 59d5596a9c6c
child 747 ae352b763959
permissions -rw-r--r--
Encapsulation. Moving shared code into TestVM instance.
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 import java.io.File;
    21 import java.io.FileWriter;
    22 import java.io.IOException;
    23 import java.io.InputStream;
    24 import java.net.URL;
    25 import java.util.Enumeration;
    26 import javax.script.Invocable;
    27 import javax.script.ScriptEngine;
    28 import javax.script.ScriptEngineManager;
    29 import javax.script.ScriptException;
    30 import static org.testng.Assert.*;
    31 
    32 final class TestVM {
    33     private final Invocable code;
    34     private final CharSequence codeSeq;
    35     private final Object bck2brwsr;
    36     
    37     
    38     private TestVM(Invocable code, CharSequence codeSeq) throws ScriptException, NoSuchMethodException {
    39         this.code = code;
    40         this.codeSeq = codeSeq;
    41         this.bck2brwsr = code.invokeFunction("bck2brwsr");
    42     }
    43     
    44 
    45     public Object execCode(
    46         String msg, Class<?> clazz, String method, 
    47         Object expRes, Object... args
    48     ) throws Exception {
    49         Object ret = null;
    50         try {
    51             ret = code.invokeMethod(bck2brwsr, "loadClass", clazz.getName());
    52             ret = code.invokeMethod(ret, method, args);
    53         } catch (ScriptException ex) {
    54             fail("Execution failed in " + dumpJS(codeSeq), ex);
    55         } catch (NoSuchMethodException ex) {
    56             fail("Cannot find method in " + dumpJS(codeSeq), ex);
    57         }
    58         if (ret == null && expRes == null) {
    59             return null;
    60         }
    61         if (expRes.equals(ret)) {
    62             return null;
    63         }
    64         if (expRes instanceof Number) {
    65             // in case of Long it is necessary convert it to number
    66             // since the Long is represented by two numbers in JavaScript
    67             try {
    68                 ret = code.invokeMethod(ret, "toFP");
    69                 ret = code.invokeFunction("Number", ret);
    70             } catch (ScriptException ex) {
    71                 fail("Conversion to number failed in " + dumpJS(codeSeq), ex);
    72             } catch (NoSuchMethodException ex) {
    73                 fail("Cannot find global Number(x) function in " + dumpJS(codeSeq), ex);
    74             }
    75         }
    76         return ret;
    77     }
    78     
    79     void assertExec(
    80         String msg, Class clazz, String method, Object expRes, Object... args
    81     ) throws Exception {
    82         Object ret = execCode(msg, clazz, method, expRes, args);
    83         if (ret == null) {
    84             return;
    85         }
    86         if (expRes != null && expRes.equals(ret)) {
    87             return;
    88         }
    89         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + dumpJS(codeSeq));
    90     }    
    91 
    92     static TestVM compileClass(String... names) throws ScriptException, IOException {
    93         return compileClass(null, names);
    94     }
    95     
    96     static TestVM compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
    97         return compileClass(sb, null, names);
    98     }
    99 
   100     static TestVM compileClass(StringBuilder sb, ScriptEngine[] eng, String... names) throws ScriptException, IOException {
   101         if (sb == null) {
   102             sb = new StringBuilder();
   103         }
   104         Bck2Brwsr.generate(sb, new EmulationResources(), names);
   105         ScriptEngineManager sem = new ScriptEngineManager();
   106         ScriptEngine js = sem.getEngineByExtension("js");
   107         if (eng != null) {
   108             eng[0] = js;
   109         }
   110         try {
   111             Object res = js.eval(sb.toString());
   112             assertTrue(js instanceof Invocable, "It is invocable object: " + res);
   113             return new TestVM((Invocable) js, sb);
   114         } catch (Exception ex) {
   115             if (sb.length() > 2000) {
   116                 sb = dumpJS(sb);
   117             }
   118             fail("Could not evaluate:\n" + sb, ex);
   119             return null;
   120         }
   121     }
   122 
   123     Object loadClass(String loadClass, String name) throws ScriptException, NoSuchMethodException {
   124         return code.invokeMethod(bck2brwsr, "loadClass", Exceptions.class.getName());
   125     }
   126     
   127     Object invokeMethod(Object obj, String method, Object... params) throws ScriptException, NoSuchMethodException {
   128         return code.invokeMethod(obj, method, params);
   129     }
   130 
   131     Object invokeFunction(String methodName, Object... args) throws ScriptException, NoSuchMethodException {
   132         return code.invokeFunction(methodName, args);
   133     }
   134 
   135     static StringBuilder dumpJS(CharSequence sb) throws IOException {
   136         File f = File.createTempFile("execution", ".js");
   137         FileWriter w = new FileWriter(f);
   138         w.append(sb);
   139         w.close();
   140         return new StringBuilder(f.getPath());
   141     }
   142 
   143     @Override
   144     public String toString() {
   145         try {
   146             return dumpJS(codeSeq).toString();
   147         } catch (IOException ex) {
   148             return ex.toString();
   149         }
   150     }
   151     
   152     
   153     private static class EmulationResources implements Bck2Brwsr.Resources {
   154         @Override
   155         public InputStream get(String name) throws IOException {
   156             Enumeration<URL> en = StaticMethodTest.class.getClassLoader().getResources(name);
   157             URL u = null;
   158             while (en.hasMoreElements()) {
   159                 u = en.nextElement();
   160             }
   161             if (u == null) {
   162                 throw new IOException("Can't find " + name);
   163             }
   164             if (u.toExternalForm().contains("rt.jar!")) {
   165                 throw new IOException("No emulation for " + u);
   166             }
   167             return u.openStream();
   168         }
   169     }
   170 }