boot/src/test/java/org/apidesign/html/boot/impl/JsClassLoaderTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 12 Sep 2013 11:31:04 +0200
changeset 289 5fe8b52d74f9
parent 288 8c5b40231d26
child 309 7025177bd67e
permissions -rw-r--r--
More compatible change with one deprecation
     1 /**
     2  * HTML via Java(tm) Language Bindings
     3  * Copyright (C) 2013 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. apidesign.org
    13  * designates this particular file as subject to the
    14  * "Classpath" exception as provided by apidesign.org
    15  * in the License file that accompanied this code.
    16  *
    17  * You should have received a copy of the GNU General Public License
    18  * along with this program. Look for COPYING file in the top folder.
    19  * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    20  */
    21 package org.apidesign.html.boot.impl;
    22 
    23 import java.io.Reader;
    24 import org.apidesign.html.boot.spi.Fn;
    25 import java.net.URL;
    26 import java.net.URLClassLoader;
    27 import java.util.ArrayList;
    28 import java.util.Arrays;
    29 import java.util.Enumeration;
    30 import java.util.List;
    31 import javax.script.Invocable;
    32 import javax.script.ScriptEngine;
    33 import javax.script.ScriptEngineManager;
    34 import javax.script.ScriptException;
    35 import org.testng.annotations.AfterClass;
    36 import org.testng.annotations.BeforeClass;
    37 import org.testng.annotations.BeforeMethod;
    38 
    39 /**
    40  *
    41  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    42  */
    43 public class JsClassLoaderTest extends JsClassLoaderBase{
    44     private static Fn.Presenter loader;
    45 
    46     @BeforeClass
    47     public static void setUpClass() throws Exception {
    48         ScriptEngineManager sem = new ScriptEngineManager();
    49         final ScriptEngine eng = sem.getEngineByMimeType("text/javascript");
    50         
    51         final URL my = JsClassLoaderTest.class.getProtectionDomain().getCodeSource().getLocation();
    52         ClassLoader parent = JsClassLoaderTest.class.getClassLoader().getParent();
    53         final URLClassLoader ul = new URLClassLoader(new URL[] { my }, parent);
    54         class MyCL extends JsClassLoader implements Fn.Presenter {
    55 
    56             public MyCL(ClassLoader parent) {
    57                 super(parent);
    58             }
    59             
    60             @Override
    61             protected URL findResource(String name) {
    62                 return ul.getResource(name);
    63             }
    64             @Override
    65             public Fn defineFn(String code, String... names) {
    66                 StringBuilder sb = new StringBuilder();
    67                 sb.append("(function() {");
    68                 sb.append("return function(");
    69                 String sep = "";
    70                 for (String n : names) {
    71                     sb.append(sep);
    72                     sb.append(n);
    73                     sep = ", ";
    74                 }
    75                 sb.append(") {");
    76                 sb.append(code);
    77                 sb.append("};");
    78                 sb.append("})()");
    79                 try {
    80                     final Object val = eng.eval(sb.toString());
    81                     return new Fn(this) {
    82                         @Override
    83                         public Object invoke(Object thiz, Object... args) throws Exception {
    84                             List<Object> all = new ArrayList<Object>(args.length + 1);
    85                             all.add(thiz == null ? val : thiz);
    86                             all.addAll(Arrays.asList(args));
    87                             Invocable inv = (Invocable)eng;
    88                             try {
    89                                 Object ret = inv.invokeMethod(val, "call", all.toArray());
    90                                 return val.equals(ret) ? null : ret;
    91                             } catch (Exception ex) {
    92                                 throw ex;
    93                             }
    94                         }
    95                     };
    96                 } catch (ScriptException ex) {
    97                     throw new LinkageError("Can't parse: " + sb, ex);
    98                 }
    99             }
   100 
   101             @Override
   102             protected Enumeration<URL> findResources(String name) {
   103                 throw new UnsupportedOperationException();
   104             }
   105 
   106             @Override
   107             public void loadScript(Reader code) throws ScriptException {
   108                 eng.eval(code);
   109             }
   110 
   111             @Override
   112             public void displayPage(URL page, Runnable onPageLoad) {
   113                 throw new UnsupportedOperationException();
   114             }
   115         };
   116         
   117         MyCL l = new MyCL(parent);
   118         methodClass = l.loadClass(JsMethods.class.getName());
   119         loader = l;
   120     }
   121     
   122     @BeforeMethod public void initPresenter() {
   123         FnUtils.currentPresenter(loader);
   124     }
   125 
   126     @AfterClass
   127     public static void cleanUp() {
   128         methodClass = null;
   129     }
   130 }