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