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.ArrayList;
25 import java.util.Arrays;
26 import java.util.Enumeration;
27 import java.util.List;
28 import javax.script.Invocable;
29 import javax.script.ScriptEngine;
30 import javax.script.ScriptEngineManager;
31 import javax.script.ScriptException;
32 import static org.testng.Assert.*;
33 import org.testng.annotations.BeforeClass;
34 import org.testng.annotations.Test;
38 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
40 public class JsClassLoaderTest {
41 private static ClassLoader loader;
42 private static Class<?> methodClass;
44 public JsClassLoaderTest() {
48 public static void setUpClass() throws Exception {
49 ScriptEngineManager sem = new ScriptEngineManager();
50 final ScriptEngine eng = sem.getEngineByMimeType("text/javascript");
52 final URL my = JsClassLoaderTest.class.getProtectionDomain().getCodeSource().getLocation();
53 ClassLoader parent = JsClassLoaderTest.class.getClassLoader().getParent();
54 final URLClassLoader ul = new URLClassLoader(new URL[] { my }, parent);
55 loader = new JsClassLoader(parent) {
57 protected URL findResource(String name) {
58 return ul.getResource(name);
61 protected Fn defineFn(String code, String... names) {
62 StringBuilder sb = new StringBuilder();
63 sb.append("(function() {");
64 sb.append("return function(");
66 for (String n : names) {
76 final Object val = eng.eval(sb.toString());
79 public Object invoke(Object thiz, Object... args) throws Exception {
80 List<Object> all = new ArrayList<Object>(args.length + 1);
81 all.add(thiz == null ? val : thiz);
82 all.addAll(Arrays.asList(args));
83 Invocable inv = (Invocable)eng;
84 Object ret = inv.invokeMethod(val, "call", all.toArray());
85 return ret == val ? null : ret;
88 } catch (ScriptException ex) {
89 throw new LinkageError("Can't parse: " + sb, ex);
94 protected Enumeration<URL> findResources(String name) {
95 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
99 methodClass = loader.loadClass(JsMethods.class.getName());
102 @Test public void noParamMethod() throws Throwable {
103 Method plus = methodClass.getMethod("fortyTwo");
105 final Object val = plus.invoke(null);
106 assertTrue(val instanceof Number, "A number returned " + val);
107 assertEquals(((Number)val).intValue(), 42);
108 } catch (InvocationTargetException ex) {
109 throw ex.getTargetException();
113 @Test public void testExecuteScript() throws Throwable {
114 Method plus = methodClass.getMethod("plus", int.class, int.class);
116 assertEquals(plus.invoke(null, 10, 20), 30);
117 } catch (InvocationTargetException ex) {
118 throw ex.getTargetException();
122 @Test public void overloadedMethod() throws Throwable {
123 Method plus = methodClass.getMethod("plus", int.class);
125 assertEquals(plus.invoke(null, 10), 10);
126 } catch (InvocationTargetException ex) {
127 throw ex.getTargetException();
131 @Test public void instanceMethod() throws Throwable {
132 Method plus = methodClass.getMethod("plusInst", int.class);
133 Object inst = methodClass.newInstance();
135 assertEquals(plus.invoke(inst, 10), 10);
136 } catch (InvocationTargetException ex) {
137 throw ex.getTargetException();
141 @Test public void staticThis() throws Throwable {
142 Method st = methodClass.getMethod("staticThis");
144 assertNull(st.invoke(null));
145 } catch (InvocationTargetException ex) {
146 throw ex.getTargetException();
150 @Test public void getThis() throws Throwable {
151 Object th = methodClass.newInstance();
152 Method st = methodClass.getMethod("getThis");
154 assertEquals(st.invoke(th), th);
155 } catch (InvocationTargetException ex) {
156 throw ex.getTargetException();