rt/vm/src/test/java/org/apidesign/vm4brwsr/VMLazyTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 30 Oct 2013 17:15:23 +0100
changeset 1392 da9e5973e699
parent 789 bb7506513353
child 1787 ea12a3bb4b33
permissions -rw-r--r--
Adopting to JDK8's Nashorn differences. Most tests should now pass with Nashorn now.
     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.AfterClass;
    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     private static TestVM code;
    34 
    35     @BeforeClass
    36     public static void compileTheCode() throws Exception {
    37         StringBuilder sb = new StringBuilder();
    38         sb.append("\nvar data = {};");
    39         sb.append("\nfunction test(clazz, method) {");
    40         sb.append("\n  if (!data.bck2brwsr) data.bck2brwsr = bck2brwsr(function(name) { return loader.get(name); });");
    41         sb.append("\n  var c = data.bck2brwsr.loadClass(clazz);");
    42         sb.append("\n  return c[method]();");
    43         sb.append("\n}");
    44         
    45         sb.append("\nfunction checkKO() {");
    46         sb.append("\n  return ko !== null;");
    47         sb.append("\n}");
    48        
    49         ScriptEngine[] arr = { null };
    50         code = TestVM.compileClass(sb, arr,
    51             new String[]{"org/apidesign/vm4brwsr/VM", "org/apidesign/vm4brwsr/StaticMethod"}
    52         );
    53         arr[0].getContext().setAttribute("loader", new BytesLoader(), ScriptContext.ENGINE_SCOPE);
    54     }
    55     @AfterClass
    56     public static void releaseTheCode() {
    57         code = null;
    58     }
    59     
    60     @Test public void invokeStaticMethod() throws Exception {
    61         assertExec("Trying to get -1", "test", Double.valueOf(-1),
    62             StaticMethod.class.getName(), "minusOne__I"
    63         );
    64     }
    65 
    66     @Test public void loadDependantClass() throws Exception {
    67         assertExec("Expecting zero", "test", Double.valueOf(0),
    68             InstanceSub.class.getName(), "recallDbl__D"
    69         );
    70     }
    71 
    72     @Test public void loadClassWithAssociatedScript() throws Exception {
    73         assertExec("ko is defined", "test", true,
    74             Script.class.getName(), "checkNotNull__Z"
    75         );
    76         
    77         Object res = code.invokeFunction("checkKO");
    78         assertEquals(res, true, "KO is defined on a global level");
    79     }
    80 
    81     private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
    82         Object ret = null;
    83         try {
    84             ret = code.invokeFunction(methodName, args);
    85         } catch (ScriptException ex) {
    86             fail("Execution failed in\n" + code.toString(), ex);
    87         } catch (NoSuchMethodException ex) {
    88             fail("Cannot find method in\n" + code.toString(), ex);
    89         }
    90         if (ret == null && expRes == null) {
    91             return;
    92         }
    93         if (expRes instanceof Double && ret instanceof Number) {
    94             ret = ((Number)ret).doubleValue();
    95         }
    96         if (expRes.equals(ret)) {
    97             return;
    98         }
    99         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + code.toString());
   100     }
   101 }