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