launcher/fx/src/test/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoaderTest.java
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.bck2brwsr.launcher.fximpl;
20 import java.lang.reflect.InvocationTargetException;
21 import java.lang.reflect.Method;
23 import java.net.URLClassLoader;
24 import java.util.Enumeration;
25 import javax.script.Invocable;
26 import javax.script.ScriptEngine;
27 import javax.script.ScriptEngineManager;
28 import javax.script.ScriptException;
29 import org.objectweb.asm.signature.SignatureReader;
30 import static org.testng.Assert.*;
31 import org.testng.annotations.BeforeClass;
32 import org.testng.annotations.Test;
36 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
38 public class JsClassLoaderTest {
39 private static ClassLoader loader;
40 private static Class<?> methodClass;
42 public JsClassLoaderTest() {
46 public static void setUpClass() throws Exception {
47 ScriptEngineManager sem = new ScriptEngineManager();
48 final ScriptEngine eng = sem.getEngineByMimeType("text/javascript");
50 final URL my = JsClassLoaderTest.class.getProtectionDomain().getCodeSource().getLocation();
51 ClassLoader parent = JsClassLoaderTest.class.getClassLoader().getParent();
52 final URLClassLoader ul = new URLClassLoader(new URL[] { my }, parent);
53 loader = new JsClassLoader(parent) {
55 protected URL findResource(String name) {
56 return ul.getResource(name);
59 protected Fn defineFn(String code, String... names) {
60 StringBuilder sb = new StringBuilder();
61 sb.append("(function() {");
62 sb.append("var r = {};");
63 sb.append("r.fn = function(");
65 for (String n : names) {
73 sb.append("return r;");
76 final Object val = eng.eval(sb.toString());
79 public Object invoke(Object thiz, Object... args) throws Exception {
80 Invocable inv = (Invocable)eng;
81 return inv.invokeMethod(val, "fn", args);
84 } catch (ScriptException ex) {
85 throw new LinkageError("Can't parse: " + sb, ex);
90 protected Enumeration<URL> findResources(String name) {
91 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
95 methodClass = loader.loadClass(JsMethods.class.getName());
98 @Test public void noParamMethod() throws Throwable {
99 Method plus = methodClass.getMethod("fortyTwo");
101 final Object val = plus.invoke(null);
102 assertTrue(val instanceof Number, "A number returned " + val);
103 assertEquals(((Number)val).intValue(), 42);
104 } catch (InvocationTargetException ex) {
105 throw ex.getTargetException();
109 @Test public void testExecuteScript() throws Throwable {
110 Method plus = methodClass.getMethod("plus", int.class, int.class);
112 assertEquals(plus.invoke(null, 10, 20), 30);
113 } catch (InvocationTargetException ex) {
114 throw ex.getTargetException();
118 @Test public void overloadedMethod() throws Throwable {
119 Method plus = methodClass.getMethod("plus", int.class);
121 assertEquals(plus.invoke(null, 10), 10);
122 } catch (InvocationTargetException ex) {
123 throw ex.getTargetException();
127 @Test public void instanceMethod() throws Throwable {
128 Method plus = methodClass.getMethod("plusInst", int.class);
129 Object inst = methodClass.newInstance();
131 assertEquals(plus.invoke(inst, 10), 10);
132 } catch (InvocationTargetException ex) {
133 throw ex.getTargetException();
137 @Test public void staticThis() throws Throwable {
138 Method st = methodClass.getMethod("staticThis");
140 assertNull(st.invoke(null));
141 } catch (InvocationTargetException ex) {
142 throw ex.getTargetException();
146 @Test public void getThis() throws Throwable {
147 Object th = methodClass.newInstance();
148 Method st = methodClass.getMethod("getThis");
150 assertEquals(st.invoke(th), th);
151 } catch (InvocationTargetException ex) {
152 throw ex.getTargetException();