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