boot/src/test/java/org/apidesign/html/boot/impl/FnTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 10 Oct 2013 14:02:18 +0200
changeset 309 7025177bd67e
parent 289 5fe8b52d74f9
child 323 86aabecda7a3
permissions -rw-r--r--
FnUtils are bloated and contain references to Asm classes. Separate the code needed by generated JsCallbacks into own class
     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 
    22 package org.apidesign.html.boot.impl;
    23 
    24 import java.io.Reader;
    25 import java.net.URL;
    26 import java.net.URLClassLoader;
    27 import java.util.ArrayList;
    28 import java.util.Arrays;
    29 import java.util.Collection;
    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.apidesign.html.boot.spi.Fn;
    36 import org.testng.annotations.BeforeClass;
    37 import org.testng.annotations.BeforeMethod;
    38 
    39 /**
    40  *
    41  * @author Jaroslav Tulach <jtulach@netbeans.org>
    42  */
    43 public class FnTest extends JsClassLoaderBase {
    44     private static Fn.Presenter presenter;
    45     
    46     public FnTest() {
    47     }
    48 
    49     @BeforeClass
    50     public static void createClassLoader() throws Exception {
    51         ScriptEngineManager sem = new ScriptEngineManager();
    52         final ScriptEngine eng = sem.getEngineByMimeType("text/javascript");
    53         
    54         final URL my = FnTest.class.getProtectionDomain().getCodeSource().getLocation();
    55         ClassLoader parent = JsClassLoaderTest.class.getClassLoader().getParent();
    56         final URLClassLoader ul = new URLClassLoader(new URL[] { my }, parent);
    57         
    58         class Impl implements FindResources, Fn.Presenter {
    59             @Override
    60             public void findResources(String path, Collection<? super URL> results, boolean oneIsEnough) {
    61                 URL u = ul.findResource(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(this) {
    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                             try {
    92                                 Object ret = inv.invokeMethod(val, "call", all.toArray());
    93                                 return val.equals(ret) ? null : ret;
    94                             } catch (ScriptException ex) {
    95                                 throw ex;
    96                             }
    97                         }
    98                     };
    99                 } catch (ScriptException ex) {
   100                     throw new LinkageError("Can't parse: " + sb, ex);
   101                 }
   102             }
   103 
   104             @Override
   105             public void displayPage(URL resource, Runnable r) {
   106                 throw new UnsupportedOperationException();
   107             }
   108 
   109             @Override
   110             public void loadScript(Reader code) throws Exception {
   111                 eng.eval(code);
   112             }
   113         }
   114         Impl impl = new Impl();
   115         ClassLoader loader = FnUtils.newLoader(impl, impl, parent);
   116         presenter = impl;
   117         
   118         methodClass = loader.loadClass(JsMethods.class.getName());
   119     }
   120 
   121     @BeforeMethod public void initPresenter() {
   122         FnContext.currentPresenter(presenter);
   123     }
   124 }