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