launcher/fx/src/test/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoaderTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 12 Jun 2013 18:19:29 +0200
branchclassloader
changeset 1174 f78cdceed17b
parent 1172 c04c43d5fdc6
child 1175 15c6903c8612
permissions -rw-r--r--
Trying to use JsClassLoader in FX launcher
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.bck2brwsr.launcher.fximpl;
    19 
    20 import java.lang.reflect.InvocationTargetException;
    21 import java.lang.reflect.Method;
    22 import java.net.URL;
    23 import java.net.URLClassLoader;
    24 import javax.script.Invocable;
    25 import javax.script.ScriptEngine;
    26 import javax.script.ScriptEngineManager;
    27 import javax.script.ScriptException;
    28 import org.objectweb.asm.signature.SignatureReader;
    29 import static org.testng.Assert.*;
    30 import org.testng.annotations.BeforeClass;
    31 import org.testng.annotations.Test;
    32 
    33 /**
    34  *
    35  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    36  */
    37 public class JsClassLoaderTest {
    38     private static ClassLoader loader;
    39     private static Class<?> methodClass;
    40     
    41     public JsClassLoaderTest() {
    42     }
    43 
    44     @BeforeClass
    45     public static void setUpClass() throws Exception {
    46         ScriptEngineManager sem = new ScriptEngineManager();
    47         final ScriptEngine eng = sem.getEngineByMimeType("text/javascript");
    48         
    49         final URL my = JsClassLoaderTest.class.getProtectionDomain().getCodeSource().getLocation();
    50         ClassLoader parent = JsClassLoaderTest.class.getClassLoader().getParent();
    51         final URLClassLoader ul = new URLClassLoader(new URL[] { my }, parent);
    52         loader = new JsClassLoader(parent) {
    53             @Override
    54             protected URL findResource(String name) {
    55                 return ul.getResource(name);
    56             }
    57             @Override
    58             protected Fn defineFn(String code, String... names) {
    59                 StringBuilder sb = new StringBuilder();
    60                 sb.append("(function() {");
    61                 sb.append("var r = {};");
    62                 sb.append("r.fn = function(");
    63                 String sep = "";
    64                 for (String n : names) {
    65                     sb.append(sep);
    66                     sb.append(n);
    67                     sep = ", ";
    68                 }
    69                 sb.append(") {");
    70                 sb.append(code);
    71                 sb.append("};");
    72                 sb.append("return r;");
    73                 sb.append("})()");
    74                 try {
    75                     final Object val = eng.eval(sb.toString());
    76                     return new Fn() {
    77                         @Override
    78                         public Object invoke(Object... args) throws Exception {
    79                             Invocable inv = (Invocable)eng;
    80                             return inv.invokeMethod(val, "fn", args);
    81                         }
    82                     };
    83                 } catch (ScriptException ex) {
    84                     throw new LinkageError("Can't parse: " + sb, ex);
    85                 }
    86             }
    87         };
    88         
    89         methodClass = loader.loadClass(JsMethods.class.getName());
    90     }
    91     
    92     @Test public void noParamMethod() throws Throwable {
    93         Method plus = methodClass.getMethod("fortyTwo");
    94         try {
    95             final Object val = plus.invoke(null);
    96             assertTrue(val instanceof Number, "A number returned " + val);
    97             assertEquals(((Number)val).intValue(), 42);
    98         } catch (InvocationTargetException ex) {
    99             throw ex.getTargetException();
   100         }
   101     }
   102     
   103     @Test public void testExecuteScript() throws Throwable {
   104         Method plus = methodClass.getMethod("plus", int.class, int.class);
   105         try {
   106             assertEquals(plus.invoke(null, 10, 20), 30);
   107         } catch (InvocationTargetException ex) {
   108             throw ex.getTargetException();
   109         }
   110     }
   111 }