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 java.io.IOException;
21 import java.io.InputStream;
23 import java.util.TreeSet;
24 import javax.script.Invocable;
25 import javax.script.ScriptContext;
26 import javax.script.ScriptEngine;
27 import javax.script.ScriptException;
28 import org.testng.annotations.BeforeClass;
29 import static org.testng.Assert.*;
30 import org.testng.annotations.Test;
32 /** Implements loading class by class.
34 * @author Jaroslav Tulach <jtulach@netbeans.org>
36 public class VMLazyTest {
39 private static CharSequence codeSeq;
40 private static Invocable code;
43 public void compileTheCode() throws Exception {
44 StringBuilder sb = new StringBuilder();
45 sb.append("\nvar data = {};");
46 sb.append("\nfunction test(clazz, method) {");
47 sb.append("\n if (!data.bck2brwsr) data.bck2brwsr = bck2brwsr(function(name) { return loader.get(name); });");
48 sb.append("\n var c = data.bck2brwsr.loadClass(clazz);");
49 sb.append("\n return c[method]();");
54 ScriptEngine[] arr = { null };
55 code = StaticMethodTest.compileClass(sb, arr,
56 "org/apidesign/vm4brwsr/GenJS"
58 arr[0].getContext().setAttribute("loader", new FindBytes(), ScriptContext.ENGINE_SCOPE);
62 @Test public void invokeStaticMethod() throws Exception {
63 assertExec("Trying to get -1", "test", Double.valueOf(-1),
64 StaticMethod.class.getName(), "minusOne__I"
68 @Test public void loadDependantClass() throws Exception {
69 assertExec("Expecting zero", "test", Double.valueOf(0),
70 InstanceSub.class.getName(), "recallDbl__D"
74 private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
77 ret = code.invokeFunction(methodName, args);
78 } catch (ScriptException ex) {
79 fail("Execution failed in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
80 } catch (NoSuchMethodException ex) {
81 fail("Cannot find method in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
83 if (ret == null && expRes == null) {
86 if (expRes.equals(ret)) {
89 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + StaticMethodTest.dumpJS(codeSeq));
92 public static final class FindBytes {
93 private static Set<String> requested = new TreeSet<String>();
95 public byte[] get(String name) throws IOException {
96 if (!requested.add(name)) {
97 throw new IllegalStateException("Requested for second time: " + name);
100 InputStream is = VMLazyTest.class.getClassLoader().getResourceAsStream(name);
102 throw new IOException("Can't find " + name);
104 byte[] arr = new byte[is.available()];
105 int len = is.read(arr);
106 if (len != arr.length) {
107 throw new IOException("Read only " + len + " wanting " + arr.length);
110 System.err.print("loader['" + name + "'] = [");
111 for (int i = 0; i < arr.length; i++) {
113 System.err.print(", ");
115 System.err.print(arr[i]);
117 System.err.println("]");