1.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/VMLazyTest.java Thu Feb 14 12:06:16 2013 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,98 +0,0 @@
1.4 -/**
1.5 - * Back 2 Browser Bytecode Translator
1.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 - *
1.8 - * This program is free software: you can redistribute it and/or modify
1.9 - * it under the terms of the GNU General Public License as published by
1.10 - * the Free Software Foundation, version 2 of the License.
1.11 - *
1.12 - * This program is distributed in the hope that it will be useful,
1.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 - * GNU General Public License for more details.
1.16 - *
1.17 - * You should have received a copy of the GNU General Public License
1.18 - * along with this program. Look for COPYING file in the top folder.
1.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 - */
1.21 -package org.apidesign.vm4brwsr;
1.22 -
1.23 -import javax.script.Invocable;
1.24 -import javax.script.ScriptContext;
1.25 -import javax.script.ScriptEngine;
1.26 -import javax.script.ScriptException;
1.27 -import org.testng.annotations.BeforeClass;
1.28 -import static org.testng.Assert.*;
1.29 -import org.testng.annotations.Test;
1.30 -
1.31 -/** Implements loading class by class.
1.32 - *
1.33 - * @author Jaroslav Tulach <jtulach@netbeans.org>
1.34 - */
1.35 -public class VMLazyTest {
1.36 -
1.37 -
1.38 - private static CharSequence codeSeq;
1.39 - private static Invocable code;
1.40 -
1.41 - @BeforeClass
1.42 - public void compileTheCode() throws Exception {
1.43 - StringBuilder sb = new StringBuilder();
1.44 - sb.append("\nvar data = {};");
1.45 - sb.append("\nfunction test(clazz, method) {");
1.46 - sb.append("\n if (!data.bck2brwsr) data.bck2brwsr = bck2brwsr(function(name) { return loader.get(name); });");
1.47 - sb.append("\n var c = data.bck2brwsr.loadClass(clazz);");
1.48 - sb.append("\n return c[method]();");
1.49 - sb.append("\n}");
1.50 -
1.51 - sb.append("\nfunction checkKO() {");
1.52 - sb.append("\n return ko !== null;");
1.53 - sb.append("\n}");
1.54 -
1.55 - ScriptEngine[] arr = { null };
1.56 - code = StaticMethodTest.compileClass(sb, arr,
1.57 - new String[]{"org/apidesign/vm4brwsr/VM", "org/apidesign/vm4brwsr/StaticMethod"}
1.58 - );
1.59 - arr[0].getContext().setAttribute("loader", new BytesLoader(), ScriptContext.ENGINE_SCOPE);
1.60 - codeSeq = sb;
1.61 - }
1.62 -
1.63 - @Test public void invokeStaticMethod() throws Exception {
1.64 - assertExec("Trying to get -1", "test", Double.valueOf(-1),
1.65 - StaticMethod.class.getName(), "minusOne__I"
1.66 - );
1.67 - }
1.68 -
1.69 - @Test public void loadDependantClass() throws Exception {
1.70 - assertExec("Expecting zero", "test", Double.valueOf(0),
1.71 - InstanceSub.class.getName(), "recallDbl__D"
1.72 - );
1.73 - }
1.74 -
1.75 - @Test public void loadClassWithAssociatedScript() throws Exception {
1.76 - assertExec("ko is defined", "test", true,
1.77 - Script.class.getName(), "checkNotNull__Z"
1.78 - );
1.79 -
1.80 - Object res = code.invokeFunction("checkKO");
1.81 - assertEquals(res, true, "KO is defined on a global level");
1.82 - }
1.83 -
1.84 - private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
1.85 - Object ret = null;
1.86 - try {
1.87 - ret = code.invokeFunction(methodName, args);
1.88 - } catch (ScriptException ex) {
1.89 - fail("Execution failed in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
1.90 - } catch (NoSuchMethodException ex) {
1.91 - fail("Cannot find method in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
1.92 - }
1.93 - if (ret == null && expRes == null) {
1.94 - return;
1.95 - }
1.96 - if (expRes.equals(ret)) {
1.97 - return;
1.98 - }
1.99 - assertEquals(ret, expRes, msg + "was: " + ret + "\n" + StaticMethodTest.dumpJS(codeSeq));
1.100 - }
1.101 -}