2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.vm4brwsr;
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;
28 /** Implements loading class by class.
30 * @author Jaroslav Tulach <jtulach@netbeans.org>
32 public class VMLazyTest {
35 private static CharSequence codeSeq;
36 private static Invocable code;
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]();");
48 sb.append("\nfunction checkKO() {");
49 sb.append("\n return ko !== null;");
52 ScriptEngine[] arr = { null };
53 code = StaticMethodTest.compileClass(sb, arr,
54 "org/apidesign/vm4brwsr/VM"
56 arr[0].getContext().setAttribute("loader", new BytesLoader(), ScriptContext.ENGINE_SCOPE);
60 @Test public void invokeStaticMethod() throws Exception {
61 assertExec("Trying to get -1", "test", Double.valueOf(-1),
62 StaticMethod.class.getName(), "minusOne__I"
66 @Test public void loadDependantClass() throws Exception {
67 assertExec("Expecting zero", "test", Double.valueOf(0),
68 InstanceSub.class.getName(), "recallDbl__D"
72 @Test public void loadClassWithAssociatedScript() throws Exception {
73 assertExec("ko is defined", "test", true,
74 Script.class.getName(), "checkNotNull__Z"
77 Object res = code.invokeFunction("checkKO");
78 assertEquals(res, true, "KO is defined on a global level");
81 private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
84 ret = code.invokeFunction(methodName, args);
85 } catch (ScriptException ex) {
86 fail("Execution failed in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
87 } catch (NoSuchMethodException ex) {
88 fail("Cannot find method in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
90 if (ret == null && expRes == null) {
93 if (expRes.equals(ret)) {
96 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + StaticMethodTest.dumpJS(codeSeq));