launcher/fx/src/test/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoaderTest.java
The static calculator demo needs to reference just a single application .js file from its main HTML page. The rest is loaded based on classpath attribute.
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.netbeans.html.boot.impl.FindResources;
35 import org.netbeans.html.boot.impl.FnUtils;
36 import static org.testng.Assert.*;
37 import org.testng.annotations.BeforeClass;
38 import org.testng.annotations.BeforeMethod;
39 import org.testng.annotations.Test;
43 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
45 public class JsClassLoaderTest {
46 private static ClassLoader loader;
47 private static Class<?> methodClass;
48 private static Fn.Presenter presenter;
50 public JsClassLoaderTest() {
54 public static void setUpClass() throws Exception {
55 ScriptEngineManager sem = new ScriptEngineManager();
56 final ScriptEngine eng = sem.getEngineByMimeType("text/javascript");
58 final URL my = JsClassLoaderTest.class.getProtectionDomain().getCodeSource().getLocation();
59 ClassLoader parent = JsClassLoaderTest.class.getClassLoader().getParent();
60 final URLClassLoader ul = new URLClassLoader(new URL[] { my }, parent);
61 class Fr implements FindResources, Fn.Presenter {
63 public void findResources(String path, Collection<? super URL> results, boolean oneIsEnough) {
64 URL u = ul.getResource(path);
71 public Fn defineFn(String code, String... names) {
72 StringBuilder sb = new StringBuilder();
73 sb.append("(function() {");
74 sb.append("return function(");
76 for (String n : names) {
86 final Object val = eng.eval(sb.toString());
89 public Object invoke(Object thiz, Object... args) throws Exception {
90 List<Object> all = new ArrayList<Object>(args.length + 1);
91 all.add(thiz == null ? val : thiz);
92 all.addAll(Arrays.asList(args));
93 Invocable inv = (Invocable)eng;
94 Object ret = inv.invokeMethod(val, "call", all.toArray());
95 return val.equals(ret) ? null : ret;
98 } catch (ScriptException ex) {
99 throw new LinkageError("Can't parse: " + sb, ex);
104 public void displayPage(URL page, Runnable onPageLoad) {
105 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
109 public void loadScript(Reader code) throws Exception {
110 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
116 loader = FnUtils.newLoader(fr, fr, parent);
117 methodClass = loader.loadClass(JsMethods.class.getName());
120 @BeforeMethod public void registerPresenter() {
121 Fn.activate(presenter);
124 @Test public void noParamMethod() throws Throwable {
125 Method plus = methodClass.getMethod("fortyTwo");
127 final Object val = plus.invoke(null);
128 assertTrue(val instanceof Number, "A number returned " + val);
129 assertEquals(((Number)val).intValue(), 42);
130 } catch (InvocationTargetException ex) {
131 throw ex.getTargetException();
135 @Test public void testExecuteScript() throws Throwable {
136 Method plus = methodClass.getMethod("plus", int.class, int.class);
138 assertEquals(plus.invoke(null, 10, 20), 30);
139 } catch (InvocationTargetException ex) {
140 throw ex.getTargetException();
144 @Test public void overloadedMethod() throws Throwable {
145 Method plus = methodClass.getMethod("plus", int.class);
147 assertEquals(plus.invoke(null, 10), 10);
148 } catch (InvocationTargetException ex) {
149 throw ex.getTargetException();
153 @Test public void instanceMethod() throws Throwable {
154 Method plus = methodClass.getMethod("plusInst", int.class);
155 Object inst = methodClass.newInstance();
157 assertEquals(plus.invoke(inst, 10), 10);
158 } catch (InvocationTargetException ex) {
159 throw ex.getTargetException();
163 @Test public void staticThis() throws Throwable {
164 Method st = methodClass.getMethod("staticThis");
166 assertNull(st.invoke(null));
167 } catch (InvocationTargetException ex) {
168 throw ex.getTargetException();
172 @Test public void getThis() throws Throwable {
173 Object th = methodClass.newInstance();
174 Method st = methodClass.getMethod("getThis");
176 assertEquals(st.invoke(th), th);
177 } catch (InvocationTargetException ex) {
178 throw ex.getTargetException();