rt/vm/src/test/java/org/apidesign/vm4brwsr/VMLazyTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 708 vm/src/test/java/org/apidesign/vm4brwsr/VMLazyTest.java@59d5596a9c6c
child 789 bb7506513353
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
     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.ScriptContext;
    21 import javax.script.ScriptEngine;
    22 import javax.script.ScriptException;
    23 import org.testng.annotations.BeforeClass;
    24 import static org.testng.Assert.*;
    25 import org.testng.annotations.Test;
    26 
    27 /** Implements loading class by class.
    28  *
    29  * @author Jaroslav Tulach <jtulach@netbeans.org>
    30  */
    31 public class VMLazyTest {
    32     private static TestVM code;
    33 
    34     @BeforeClass
    35     public void compileTheCode() throws Exception {
    36         StringBuilder sb = new StringBuilder();
    37         sb.append("\nvar data = {};");
    38         sb.append("\nfunction test(clazz, method) {");
    39         sb.append("\n  if (!data.bck2brwsr) data.bck2brwsr = bck2brwsr(function(name) { return loader.get(name); });");
    40         sb.append("\n  var c = data.bck2brwsr.loadClass(clazz);");
    41         sb.append("\n  return c[method]();");
    42         sb.append("\n}");
    43         
    44         sb.append("\nfunction checkKO() {");
    45         sb.append("\n  return ko !== null;");
    46         sb.append("\n}");
    47        
    48         ScriptEngine[] arr = { null };
    49         code = TestVM.compileClass(sb, arr,
    50             new String[]{"org/apidesign/vm4brwsr/VM", "org/apidesign/vm4brwsr/StaticMethod"}
    51         );
    52         arr[0].getContext().setAttribute("loader", new BytesLoader(), ScriptContext.ENGINE_SCOPE);
    53     }
    54     
    55     @Test public void invokeStaticMethod() throws Exception {
    56         assertExec("Trying to get -1", "test", Double.valueOf(-1),
    57             StaticMethod.class.getName(), "minusOne__I"
    58         );
    59     }
    60 
    61     @Test public void loadDependantClass() throws Exception {
    62         assertExec("Expecting zero", "test", Double.valueOf(0),
    63             InstanceSub.class.getName(), "recallDbl__D"
    64         );
    65     }
    66 
    67     @Test public void loadClassWithAssociatedScript() throws Exception {
    68         assertExec("ko is defined", "test", true,
    69             Script.class.getName(), "checkNotNull__Z"
    70         );
    71         
    72         Object res = code.invokeFunction("checkKO");
    73         assertEquals(res, true, "KO is defined on a global level");
    74     }
    75 
    76     private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
    77         Object ret = null;
    78         try {
    79             ret = code.invokeFunction(methodName, args);
    80         } catch (ScriptException ex) {
    81             fail("Execution failed in\n" + code.toString(), ex);
    82         } catch (NoSuchMethodException ex) {
    83             fail("Cannot find method in\n" + code.toString(), ex);
    84         }
    85         if (ret == null && expRes == null) {
    86             return;
    87         }
    88         if (expRes.equals(ret)) {
    89             return;
    90         }
    91         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + code.toString());
    92     }
    93 }