Encapsulation. Moving shared code into TestVM instance.
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.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;
27 /** Implements loading class by class.
29 * @author Jaroslav Tulach <jtulach@netbeans.org>
31 public class VMLazyTest {
32 private static TestVM code;
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]();");
44 sb.append("\nfunction checkKO() {");
45 sb.append("\n return ko !== null;");
48 ScriptEngine[] arr = { null };
49 code = TestVM.compileClass(sb, arr,
50 new String[]{"org/apidesign/vm4brwsr/VM", "org/apidesign/vm4brwsr/StaticMethod"}
52 arr[0].getContext().setAttribute("loader", new BytesLoader(), ScriptContext.ENGINE_SCOPE);
55 @Test public void invokeStaticMethod() throws Exception {
56 assertExec("Trying to get -1", "test", Double.valueOf(-1),
57 StaticMethod.class.getName(), "minusOne__I"
61 @Test public void loadDependantClass() throws Exception {
62 assertExec("Expecting zero", "test", Double.valueOf(0),
63 InstanceSub.class.getName(), "recallDbl__D"
67 @Test public void loadClassWithAssociatedScript() throws Exception {
68 assertExec("ko is defined", "test", true,
69 Script.class.getName(), "checkNotNull__Z"
72 Object res = code.invokeFunction("checkKO");
73 assertEquals(res, true, "KO is defined on a global level");
76 private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
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);
85 if (ret == null && expRes == null) {
88 if (expRes.equals(ret)) {
91 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + code.toString());