vm/src/test/java/org/apidesign/vm4brwsr/VMLazyTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 11 Dec 2012 09:36:44 +0100
branchlazyvm
changeset 298 885acca2fa0b
parent 277 e4b9eee9be83
child 503 80a388c8c27b
permissions -rw-r--r--
Creating Bck2Brwsr entrypoint to for those who wish to generate their JavaScript based Java VM
     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 javax.script.Invocable;
    21 import javax.script.ScriptContext;
    22 import javax.script.ScriptEngine;
    23 import javax.script.ScriptException;
    24 import org.testng.annotations.BeforeClass;
    25 import static org.testng.Assert.*;
    26 import org.testng.annotations.Test;
    27 
    28 /** Implements loading class by class.
    29  *
    30  * @author Jaroslav Tulach <jtulach@netbeans.org>
    31  */
    32 public class VMLazyTest {
    33 
    34     
    35     private static CharSequence codeSeq;
    36     private static Invocable code;
    37 
    38     @BeforeClass
    39     public void compileTheCode() throws Exception {
    40         StringBuilder sb = new StringBuilder();
    41         sb.append("\nvar data = {};");
    42         sb.append("\nfunction test(clazz, method) {");
    43         sb.append("\n  if (!data.bck2brwsr) data.bck2brwsr = bck2brwsr(function(name) { return loader.get(name); });");
    44         sb.append("\n  var c = data.bck2brwsr.loadClass(clazz);");
    45         sb.append("\n  return c[method]();");
    46         sb.append("\n}");
    47         
    48         
    49        
    50         ScriptEngine[] arr = { null };
    51         code = StaticMethodTest.compileClass(sb, arr,
    52             "org/apidesign/vm4brwsr/VM"
    53         );
    54         arr[0].getContext().setAttribute("loader", new BytesLoader(), ScriptContext.ENGINE_SCOPE);
    55         codeSeq = sb;
    56     }
    57     
    58     @Test public void invokeStaticMethod() throws Exception {
    59         assertExec("Trying to get -1", "test", Double.valueOf(-1),
    60             StaticMethod.class.getName(), "minusOne__I"
    61         );
    62     }
    63 
    64     @Test public void loadDependantClass() throws Exception {
    65         assertExec("Expecting zero", "test", Double.valueOf(0),
    66             InstanceSub.class.getName(), "recallDbl__D"
    67         );
    68     }
    69 
    70     private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
    71         Object ret = null;
    72         try {
    73             ret = code.invokeFunction(methodName, args);
    74         } catch (ScriptException ex) {
    75             fail("Execution failed in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
    76         } catch (NoSuchMethodException ex) {
    77             fail("Cannot find method in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
    78         }
    79         if (ret == null && expRes == null) {
    80             return;
    81         }
    82         if (expRes.equals(ret)) {
    83             return;
    84         }
    85         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + StaticMethodTest.dumpJS(codeSeq));
    86     }
    87 }