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