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.Collection;
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 org.apidesign.html.boot.spi.Fn;
33 import org.apidesign.html.boot.impl.FindResources;
34 import org.apidesign.html.boot.impl.FnUtils;
35 import static org.testng.Assert.*;
36 import org.testng.annotations.BeforeClass;
37 import org.testng.annotations.Test;
41 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
43 public class JsClassLoaderTest {
44 private static ClassLoader loader;
45 private static Class<?> methodClass;
47 public JsClassLoaderTest() {
51 public static void setUpClass() throws Exception {
52 ScriptEngineManager sem = new ScriptEngineManager();
53 final ScriptEngine eng = sem.getEngineByMimeType("text/javascript");
55 final URL my = JsClassLoaderTest.class.getProtectionDomain().getCodeSource().getLocation();
56 ClassLoader parent = JsClassLoaderTest.class.getClassLoader().getParent();
57 final URLClassLoader ul = new URLClassLoader(new URL[] { my }, parent);
58 class Fr implements FindResources, Fn.Presenter {
60 public void findResources(String path, Collection<? super URL> results, boolean oneIsEnough) {
61 URL u = ul.getResource(path);
68 public Fn defineFn(String code, String... names) {
69 StringBuilder sb = new StringBuilder();
70 sb.append("(function() {");
71 sb.append("return function(");
73 for (String n : names) {
83 final Object val = eng.eval(sb.toString());
86 public Object invoke(Object thiz, Object... args) throws Exception {
87 List<Object> all = new ArrayList<Object>(args.length + 1);
88 all.add(thiz == null ? val : thiz);
89 all.addAll(Arrays.asList(args));
90 Invocable inv = (Invocable)eng;
91 Object ret = inv.invokeMethod(val, "call", all.toArray());
92 return ret == val ? null : ret;
95 } catch (ScriptException ex) {
96 throw new LinkageError("Can't parse: " + sb, ex);
101 public void displayPage(URL page, Runnable onPageLoad) {
102 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
106 loader = FnUtils.newLoader(new Fr(), new Fr(), parent);
107 methodClass = loader.loadClass(JsMethods.class.getName());
110 @Test public void noParamMethod() throws Throwable {
111 Method plus = methodClass.getMethod("fortyTwo");
113 final Object val = plus.invoke(null);
114 assertTrue(val instanceof Number, "A number returned " + val);
115 assertEquals(((Number)val).intValue(), 42);
116 } catch (InvocationTargetException ex) {
117 throw ex.getTargetException();
121 @Test public void testExecuteScript() throws Throwable {
122 Method plus = methodClass.getMethod("plus", int.class, int.class);
124 assertEquals(plus.invoke(null, 10, 20), 30);
125 } catch (InvocationTargetException ex) {
126 throw ex.getTargetException();
130 @Test public void overloadedMethod() throws Throwable {
131 Method plus = methodClass.getMethod("plus", int.class);
133 assertEquals(plus.invoke(null, 10), 10);
134 } catch (InvocationTargetException ex) {
135 throw ex.getTargetException();
139 @Test public void instanceMethod() throws Throwable {
140 Method plus = methodClass.getMethod("plusInst", int.class);
141 Object inst = methodClass.newInstance();
143 assertEquals(plus.invoke(inst, 10), 10);
144 } catch (InvocationTargetException ex) {
145 throw ex.getTargetException();
149 @Test public void staticThis() throws Throwable {
150 Method st = methodClass.getMethod("staticThis");
152 assertNull(st.invoke(null));
153 } catch (InvocationTargetException ex) {
154 throw ex.getTargetException();
158 @Test public void getThis() throws Throwable {
159 Object th = methodClass.newInstance();
160 Method st = methodClass.getMethod("getThis");
162 assertEquals(st.invoke(th), th);
163 } catch (InvocationTargetException ex) {
164 throw ex.getTargetException();