1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/VMLazyTest.java Tue Feb 26 16:54:16 2013 +0100
1.3 @@ -0,0 +1,93 @@
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.ScriptContext;
1.24 +import javax.script.ScriptEngine;
1.25 +import javax.script.ScriptException;
1.26 +import org.testng.annotations.BeforeClass;
1.27 +import static org.testng.Assert.*;
1.28 +import org.testng.annotations.Test;
1.29 +
1.30 +/** Implements loading class by class.
1.31 + *
1.32 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.33 + */
1.34 +public class VMLazyTest {
1.35 + private static TestVM code;
1.36 +
1.37 + @BeforeClass
1.38 + public void compileTheCode() throws Exception {
1.39 + StringBuilder sb = new StringBuilder();
1.40 + sb.append("\nvar data = {};");
1.41 + sb.append("\nfunction test(clazz, method) {");
1.42 + sb.append("\n if (!data.bck2brwsr) data.bck2brwsr = bck2brwsr(function(name) { return loader.get(name); });");
1.43 + sb.append("\n var c = data.bck2brwsr.loadClass(clazz);");
1.44 + sb.append("\n return c[method]();");
1.45 + sb.append("\n}");
1.46 +
1.47 + sb.append("\nfunction checkKO() {");
1.48 + sb.append("\n return ko !== null;");
1.49 + sb.append("\n}");
1.50 +
1.51 + ScriptEngine[] arr = { null };
1.52 + code = TestVM.compileClass(sb, arr,
1.53 + new String[]{"org/apidesign/vm4brwsr/VM", "org/apidesign/vm4brwsr/StaticMethod"}
1.54 + );
1.55 + arr[0].getContext().setAttribute("loader", new BytesLoader(), ScriptContext.ENGINE_SCOPE);
1.56 + }
1.57 +
1.58 + @Test public void invokeStaticMethod() throws Exception {
1.59 + assertExec("Trying to get -1", "test", Double.valueOf(-1),
1.60 + StaticMethod.class.getName(), "minusOne__I"
1.61 + );
1.62 + }
1.63 +
1.64 + @Test public void loadDependantClass() throws Exception {
1.65 + assertExec("Expecting zero", "test", Double.valueOf(0),
1.66 + InstanceSub.class.getName(), "recallDbl__D"
1.67 + );
1.68 + }
1.69 +
1.70 + @Test public void loadClassWithAssociatedScript() throws Exception {
1.71 + assertExec("ko is defined", "test", true,
1.72 + Script.class.getName(), "checkNotNull__Z"
1.73 + );
1.74 +
1.75 + Object res = code.invokeFunction("checkKO");
1.76 + assertEquals(res, true, "KO is defined on a global level");
1.77 + }
1.78 +
1.79 + private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
1.80 + Object ret = null;
1.81 + try {
1.82 + ret = code.invokeFunction(methodName, args);
1.83 + } catch (ScriptException ex) {
1.84 + fail("Execution failed in\n" + code.toString(), ex);
1.85 + } catch (NoSuchMethodException ex) {
1.86 + fail("Cannot find method in\n" + code.toString(), ex);
1.87 + }
1.88 + if (ret == null && expRes == null) {
1.89 + return;
1.90 + }
1.91 + if (expRes.equals(ret)) {
1.92 + return;
1.93 + }
1.94 + assertEquals(ret, expRes, msg + "was: " + ret + "\n" + code.toString());
1.95 + }
1.96 +}