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.io.Reader;
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
24 import java.net.URLClassLoader;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collection;
28 import java.util.List;
29 import javax.script.Invocable;
30 import javax.script.ScriptEngine;
31 import javax.script.ScriptEngineManager;
32 import javax.script.ScriptException;
33 import org.apidesign.html.boot.spi.Fn;
34 import org.apidesign.html.boot.impl.FindResources;
35 import org.apidesign.html.boot.impl.FnUtils;
36 import static org.testng.Assert.*;
37 import org.testng.annotations.BeforeClass;
38 import org.testng.annotations.Test;
42 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
44 public class JsClassLoaderTest {
45 private static ClassLoader loader;
46 private static Class<?> methodClass;
48 public JsClassLoaderTest() {
52 public static void setUpClass() throws Exception {
53 ScriptEngineManager sem = new ScriptEngineManager();
54 final ScriptEngine eng = sem.getEngineByMimeType("text/javascript");
56 final URL my = JsClassLoaderTest.class.getProtectionDomain().getCodeSource().getLocation();
57 ClassLoader parent = JsClassLoaderTest.class.getClassLoader().getParent();
58 final URLClassLoader ul = new URLClassLoader(new URL[] { my }, parent);
59 class Fr implements FindResources, Fn.Presenter {
61 public void findResources(String path, Collection<? super URL> results, boolean oneIsEnough) {
62 URL u = ul.getResource(path);
69 public Fn defineFn(String code, String... names) {
70 StringBuilder sb = new StringBuilder();
71 sb.append("(function() {");
72 sb.append("return function(");
74 for (String n : names) {
84 final Object val = eng.eval(sb.toString());
87 public Object invoke(Object thiz, Object... args) throws Exception {
88 List<Object> all = new ArrayList<Object>(args.length + 1);
89 all.add(thiz == null ? val : thiz);
90 all.addAll(Arrays.asList(args));
91 Invocable inv = (Invocable)eng;
92 Object ret = inv.invokeMethod(val, "call", all.toArray());
93 return ret == val ? null : ret;
96 } catch (ScriptException ex) {
97 throw new LinkageError("Can't parse: " + sb, ex);
102 public void displayPage(URL page, Runnable onPageLoad) {
103 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
107 public void loadScript(Reader code) throws Exception {
108 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
112 loader = FnUtils.newLoader(new Fr(), new Fr(), parent);
113 methodClass = loader.loadClass(JsMethods.class.getName());
116 @Test public void noParamMethod() throws Throwable {
117 Method plus = methodClass.getMethod("fortyTwo");
119 final Object val = plus.invoke(null);
120 assertTrue(val instanceof Number, "A number returned " + val);
121 assertEquals(((Number)val).intValue(), 42);
122 } catch (InvocationTargetException ex) {
123 throw ex.getTargetException();
127 @Test public void testExecuteScript() throws Throwable {
128 Method plus = methodClass.getMethod("plus", int.class, int.class);
130 assertEquals(plus.invoke(null, 10, 20), 30);
131 } catch (InvocationTargetException ex) {
132 throw ex.getTargetException();
136 @Test public void overloadedMethod() throws Throwable {
137 Method plus = methodClass.getMethod("plus", int.class);
139 assertEquals(plus.invoke(null, 10), 10);
140 } catch (InvocationTargetException ex) {
141 throw ex.getTargetException();
145 @Test public void instanceMethod() throws Throwable {
146 Method plus = methodClass.getMethod("plusInst", int.class);
147 Object inst = methodClass.newInstance();
149 assertEquals(plus.invoke(inst, 10), 10);
150 } catch (InvocationTargetException ex) {
151 throw ex.getTargetException();
155 @Test public void staticThis() throws Throwable {
156 Method st = methodClass.getMethod("staticThis");
158 assertNull(st.invoke(null));
159 } catch (InvocationTargetException ex) {
160 throw ex.getTargetException();
164 @Test public void getThis() throws Throwable {
165 Object th = methodClass.newInstance();
166 Method st = methodClass.getMethod("getThis");
168 assertEquals(st.invoke(th), th);
169 } catch (InvocationTargetException ex) {
170 throw ex.getTargetException();