launcher/fx/src/test/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoaderTest.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/launcher/fx/src/test/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoaderTest.java Mon Jun 10 18:12:38 2013 +0200
1.3 @@ -0,0 +1,106 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.bck2brwsr.launcher.fximpl;
1.22 +
1.23 +import java.lang.reflect.InvocationTargetException;
1.24 +import java.lang.reflect.Method;
1.25 +import java.net.URL;
1.26 +import javax.script.Invocable;
1.27 +import javax.script.ScriptEngine;
1.28 +import javax.script.ScriptEngineManager;
1.29 +import javax.script.ScriptException;
1.30 +import static org.testng.Assert.*;
1.31 +import org.testng.annotations.BeforeClass;
1.32 +import org.testng.annotations.Test;
1.33 +
1.34 +/**
1.35 + *
1.36 + * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.37 + */
1.38 +public class JsClassLoaderTest {
1.39 + private static ClassLoader loader;
1.40 + private static Class<?> methodClass;
1.41 +
1.42 + public JsClassLoaderTest() {
1.43 + }
1.44 +
1.45 + @BeforeClass
1.46 + public static void setUpClass() throws Exception {
1.47 + ScriptEngineManager sem = new ScriptEngineManager();
1.48 + final ScriptEngine eng = sem.getEngineByMimeType("text/javascript");
1.49 +
1.50 + URL my = JsClassLoaderTest.class.getProtectionDomain().getCodeSource().getLocation();
1.51 + ClassLoader parent = JsClassLoaderTest.class.getClassLoader().getParent();
1.52 + loader = new JsClassLoader(new URL[] { my }, parent) {
1.53 + @Override
1.54 + protected JsClassLoader.Fn defineFn(String code, String... names) {
1.55 + StringBuilder sb = new StringBuilder();
1.56 + sb.append("(function() {");
1.57 + sb.append("var r = {};");
1.58 + sb.append("r.fn = function(");
1.59 + String sep = "";
1.60 + for (String n : names) {
1.61 + sb.append(sep);
1.62 + sb.append(n);
1.63 + sep = ", ";
1.64 + }
1.65 + sb.append(") {");
1.66 + sb.append(code);
1.67 + sb.append("};");
1.68 + sb.append("return r;");
1.69 + sb.append("})()");
1.70 + try {
1.71 + final Object val = eng.eval(sb.toString());
1.72 + return new JsClassLoader.Fn() {
1.73 + @Override
1.74 + public Object invoke(Object... args) throws Exception {
1.75 + Invocable inv = (Invocable)eng;
1.76 + return inv.invokeMethod(val, "fn", args);
1.77 + }
1.78 + };
1.79 + } catch (ScriptException ex) {
1.80 + throw new LinkageError("Can't parse: " + sb, ex);
1.81 + }
1.82 + }
1.83 + };
1.84 +
1.85 + methodClass = loader.loadClass(JsMethods.class.getName());
1.86 + }
1.87 +
1.88 + @Test public void noParamMethod() throws Throwable {
1.89 + Method plus = methodClass.getMethod("fortyTwo");
1.90 + try {
1.91 + final Object val = plus.invoke(null);
1.92 + assertTrue(val instanceof Number, "A number returned " + val);
1.93 + assertEquals(((Number)val).intValue(), 42);
1.94 + } catch (InvocationTargetException ex) {
1.95 + throw ex.getTargetException();
1.96 + }
1.97 + }
1.98 + /*
1.99 + @Test public void testExecuteScript() throws Throwable {
1.100 + Method plus = methodClass.getMethod("plus", int.class, int.class);
1.101 + try {
1.102 + assertEquals(plus.invoke(null, 10, 20), 30);
1.103 + } catch (InvocationTargetException ex) {
1.104 + throw ex.getTargetException();
1.105 + }
1.106 + }
1.107 + */
1.108 +
1.109 +}
1.110 \ No newline at end of file