launcher/fx/src/test/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoaderTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 17 Jun 2013 19:36:24 +0200
branchclassloader
changeset 1181 b703d9d71f25
parent 1178 4ae766848ce0
child 1182 743f2fe4f0bc
permissions -rw-r--r--
Initial attempt to pass this into non-static methods
     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 java.util.Enumeration;
    25 import javax.script.Invocable;
    26 import javax.script.ScriptEngine;
    27 import javax.script.ScriptEngineManager;
    28 import javax.script.ScriptException;
    29 import org.objectweb.asm.signature.SignatureReader;
    30 import static org.testng.Assert.*;
    31 import org.testng.annotations.BeforeClass;
    32 import org.testng.annotations.Test;
    33 
    34 /**
    35  *
    36  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    37  */
    38 public class JsClassLoaderTest {
    39     private static ClassLoader loader;
    40     private static Class<?> methodClass;
    41     
    42     public JsClassLoaderTest() {
    43     }
    44 
    45     @BeforeClass
    46     public static void setUpClass() throws Exception {
    47         ScriptEngineManager sem = new ScriptEngineManager();
    48         final ScriptEngine eng = sem.getEngineByMimeType("text/javascript");
    49         
    50         final URL my = JsClassLoaderTest.class.getProtectionDomain().getCodeSource().getLocation();
    51         ClassLoader parent = JsClassLoaderTest.class.getClassLoader().getParent();
    52         final URLClassLoader ul = new URLClassLoader(new URL[] { my }, parent);
    53         loader = new JsClassLoader(parent) {
    54             @Override
    55             protected URL findResource(String name) {
    56                 return ul.getResource(name);
    57             }
    58             @Override
    59             protected Fn defineFn(String code, String... names) {
    60                 StringBuilder sb = new StringBuilder();
    61                 sb.append("(function() {");
    62                 sb.append("var r = {};");
    63                 sb.append("r.fn = function(");
    64                 String sep = "";
    65                 for (String n : names) {
    66                     sb.append(sep);
    67                     sb.append(n);
    68                     sep = ", ";
    69                 }
    70                 sb.append(") {");
    71                 sb.append(code);
    72                 sb.append("};");
    73                 sb.append("return r;");
    74                 sb.append("})()");
    75                 try {
    76                     final Object val = eng.eval(sb.toString());
    77                     return new Fn() {
    78                         @Override
    79                         public Object invoke(Object thiz, Object... args) throws Exception {
    80                             Invocable inv = (Invocable)eng;
    81                             return inv.invokeMethod(val, "fn", args);
    82                         }
    83                     };
    84                 } catch (ScriptException ex) {
    85                     throw new LinkageError("Can't parse: " + sb, ex);
    86                 }
    87             }
    88 
    89             @Override
    90             protected Enumeration<URL> findResources(String name) {
    91                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    92             }
    93         };
    94         
    95         methodClass = loader.loadClass(JsMethods.class.getName());
    96     }
    97     
    98     @Test public void noParamMethod() throws Throwable {
    99         Method plus = methodClass.getMethod("fortyTwo");
   100         try {
   101             final Object val = plus.invoke(null);
   102             assertTrue(val instanceof Number, "A number returned " + val);
   103             assertEquals(((Number)val).intValue(), 42);
   104         } catch (InvocationTargetException ex) {
   105             throw ex.getTargetException();
   106         }
   107     }
   108     
   109     @Test public void testExecuteScript() throws Throwable {
   110         Method plus = methodClass.getMethod("plus", int.class, int.class);
   111         try {
   112             assertEquals(plus.invoke(null, 10, 20), 30);
   113         } catch (InvocationTargetException ex) {
   114             throw ex.getTargetException();
   115         }
   116     }
   117 
   118     @Test public void overloadedMethod() throws Throwable {
   119         Method plus = methodClass.getMethod("plus", int.class);
   120         try {
   121             assertEquals(plus.invoke(null, 10), 10);
   122         } catch (InvocationTargetException ex) {
   123             throw ex.getTargetException();
   124         }
   125     }
   126     
   127     @Test public void instanceMethod() throws Throwable {
   128         Method plus = methodClass.getMethod("plusInst", int.class);
   129         Object inst = methodClass.newInstance();
   130         try {
   131             assertEquals(plus.invoke(inst, 10), 10);
   132         } catch (InvocationTargetException ex) {
   133             throw ex.getTargetException();
   134         }
   135     }
   136     
   137     @Test public void staticThis() throws Throwable {
   138         Method st = methodClass.getMethod("staticThis");
   139         try {
   140             assertNull(st.invoke(null));
   141         } catch (InvocationTargetException ex) {
   142             throw ex.getTargetException();
   143         }
   144     }
   145 
   146     @Test public void getThis() throws Throwable {
   147         Object th = methodClass.newInstance();
   148         Method st = methodClass.getMethod("getThis");
   149         try {
   150             assertEquals(st.invoke(th), th);
   151         } catch (InvocationTargetException ex) {
   152             throw ex.getTargetException();
   153         }
   154     }
   155     
   156 }