launcher/fx/src/test/java/org/apidesign/bck2brwsr/launcher/fximpl/JsClassLoaderTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 20 Jun 2013 10:22:50 +0200
branchclassloader
changeset 1184 ccf2447021f6
parent 1182 743f2fe4f0bc
child 1227 5a907f38608d
permissions -rw-r--r--
Using the Browser Bootstrap classloader
     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.ArrayList;
    25 import java.util.Arrays;
    26 import java.util.Collection;
    27 import java.util.List;
    28 import javax.script.Invocable;
    29 import javax.script.ScriptEngine;
    30 import javax.script.ScriptEngineManager;
    31 import javax.script.ScriptException;
    32 import org.apidesign.html.boot.spi.Fn;
    33 import org.apidesign.html.boot.impl.FindResources;
    34 import org.apidesign.html.boot.impl.FnUtils;
    35 import static org.testng.Assert.*;
    36 import org.testng.annotations.BeforeClass;
    37 import org.testng.annotations.Test;
    38 
    39 /**
    40  *
    41  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    42  */
    43 public class JsClassLoaderTest {
    44     private static ClassLoader loader;
    45     private static Class<?> methodClass;
    46     
    47     public JsClassLoaderTest() {
    48     }
    49 
    50     @BeforeClass
    51     public static void setUpClass() throws Exception {
    52         ScriptEngineManager sem = new ScriptEngineManager();
    53         final ScriptEngine eng = sem.getEngineByMimeType("text/javascript");
    54         
    55         final URL my = JsClassLoaderTest.class.getProtectionDomain().getCodeSource().getLocation();
    56         ClassLoader parent = JsClassLoaderTest.class.getClassLoader().getParent();
    57         final URLClassLoader ul = new URLClassLoader(new URL[] { my }, parent);
    58         class Fr implements FindResources, Fn.Presenter {
    59             @Override
    60             public void findResources(String path, Collection<? super URL> results, boolean oneIsEnough) {
    61                 URL u = ul.getResource(path);
    62                 if (u != null) {
    63                     results.add(u);
    64                 }
    65             }
    66 
    67             @Override
    68             public Fn defineFn(String code, String... names) {
    69                 StringBuilder sb = new StringBuilder();
    70                 sb.append("(function() {");
    71                 sb.append("return function(");
    72                 String sep = "";
    73                 for (String n : names) {
    74                     sb.append(sep);
    75                     sb.append(n);
    76                     sep = ", ";
    77                 }
    78                 sb.append(") {");
    79                 sb.append(code);
    80                 sb.append("};");
    81                 sb.append("})()");
    82                 try {
    83                     final Object val = eng.eval(sb.toString());
    84                     return new Fn() {
    85                         @Override
    86                         public Object invoke(Object thiz, Object... args) throws Exception {
    87                             List<Object> all = new ArrayList<Object>(args.length + 1);
    88                             all.add(thiz == null ? val : thiz);
    89                             all.addAll(Arrays.asList(args));
    90                             Invocable inv = (Invocable)eng;
    91                             Object ret = inv.invokeMethod(val, "call", all.toArray());
    92                             return ret == val ? null : ret;
    93                         }
    94                     };
    95                 } catch (ScriptException ex) {
    96                     throw new LinkageError("Can't parse: " + sb, ex);
    97                 }
    98             }
    99 
   100             @Override
   101             public void displayPage(URL page, Runnable onPageLoad) {
   102                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
   103             }
   104         }
   105         
   106         loader = FnUtils.newLoader(new Fr(), new Fr(), parent);
   107         methodClass = loader.loadClass(JsMethods.class.getName());
   108     }
   109     
   110     @Test public void noParamMethod() throws Throwable {
   111         Method plus = methodClass.getMethod("fortyTwo");
   112         try {
   113             final Object val = plus.invoke(null);
   114             assertTrue(val instanceof Number, "A number returned " + val);
   115             assertEquals(((Number)val).intValue(), 42);
   116         } catch (InvocationTargetException ex) {
   117             throw ex.getTargetException();
   118         }
   119     }
   120     
   121     @Test public void testExecuteScript() throws Throwable {
   122         Method plus = methodClass.getMethod("plus", int.class, int.class);
   123         try {
   124             assertEquals(plus.invoke(null, 10, 20), 30);
   125         } catch (InvocationTargetException ex) {
   126             throw ex.getTargetException();
   127         }
   128     }
   129 
   130     @Test public void overloadedMethod() throws Throwable {
   131         Method plus = methodClass.getMethod("plus", int.class);
   132         try {
   133             assertEquals(plus.invoke(null, 10), 10);
   134         } catch (InvocationTargetException ex) {
   135             throw ex.getTargetException();
   136         }
   137     }
   138     
   139     @Test public void instanceMethod() throws Throwable {
   140         Method plus = methodClass.getMethod("plusInst", int.class);
   141         Object inst = methodClass.newInstance();
   142         try {
   143             assertEquals(plus.invoke(inst, 10), 10);
   144         } catch (InvocationTargetException ex) {
   145             throw ex.getTargetException();
   146         }
   147     }
   148     
   149     @Test public void staticThis() throws Throwable {
   150         Method st = methodClass.getMethod("staticThis");
   151         try {
   152             assertNull(st.invoke(null));
   153         } catch (InvocationTargetException ex) {
   154             throw ex.getTargetException();
   155         }
   156     }
   157 
   158     @Test public void getThis() throws Throwable {
   159         Object th = methodClass.newInstance();
   160         Method st = methodClass.getMethod("getThis");
   161         try {
   162             assertEquals(st.invoke(th), th);
   163         } catch (InvocationTargetException ex) {
   164             throw ex.getTargetException();
   165         }
   166     }
   167     
   168 }