boot/src/test/java/org/apidesign/html/boot/impl/FnTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 06 Nov 2013 15:15:54 +0100
changeset 323 86aabecda7a3
parent 309 7025177bd67e
child 358 80702021b851
permissions -rw-r--r--
Introducing Agent-Class to allow java.lang.instrument-like transforms of the @JavaScriptBody annotation
     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.Closeable;
    25 import java.io.Reader;
    26 import java.net.URL;
    27 import java.net.URLClassLoader;
    28 import java.util.ArrayList;
    29 import java.util.Arrays;
    30 import java.util.Collection;
    31 import java.util.List;
    32 import javax.script.Invocable;
    33 import javax.script.ScriptEngine;
    34 import javax.script.ScriptEngineManager;
    35 import javax.script.ScriptException;
    36 import org.apidesign.html.boot.spi.Fn;
    37 import org.testng.annotations.BeforeClass;
    38 import org.testng.annotations.BeforeMethod;
    39 
    40 /**
    41  *
    42  * @author Jaroslav Tulach <jtulach@netbeans.org>
    43  */
    44 public class FnTest extends JsClassLoaderBase {
    45     private static Fn.Presenter presenter;
    46     
    47     public FnTest() {
    48     }
    49 
    50     @BeforeClass
    51     public static void createClassLoader() throws Exception {
    52         ScriptEngineManager sem = new ScriptEngineManager();
    53         final ScriptEngine eng = sem.getEngineByMimeType("text/javascript");
    54         
    55         final URL my = FnTest.class.getProtectionDomain().getCodeSource().getLocation();
    56         ClassLoader parent = JsClassLoaderTest.class.getClassLoader().getParent();
    57         final URLClassLoader ul = new URLClassLoader(new URL[] { my }, parent);
    58         
    59         class Impl implements FindResources, Fn.Presenter {
    60             @Override
    61             public void findResources(String path, Collection<? super URL> results, boolean oneIsEnough) {
    62                 URL u = ul.findResource(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(this) {
    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                             try {
    93                                 Object ret = inv.invokeMethod(val, "call", all.toArray());
    94                                 return val.equals(ret) ? null : ret;
    95                             } catch (ScriptException ex) {
    96                                 throw ex;
    97                             }
    98                         }
    99                     };
   100                 } catch (ScriptException ex) {
   101                     throw new LinkageError("Can't parse: " + sb, ex);
   102                 }
   103             }
   104 
   105             @Override
   106             public void displayPage(URL resource, Runnable r) {
   107                 throw new UnsupportedOperationException();
   108             }
   109 
   110             @Override
   111             public void loadScript(Reader code) throws Exception {
   112                 eng.eval(code);
   113             }
   114         }
   115         Impl impl = new Impl();
   116         ClassLoader loader = FnUtils.newLoader(impl, impl, parent);
   117         presenter = impl;
   118         
   119         Closeable close = FnContext.activate(impl);
   120         methodClass = loader.loadClass(JsMethods.class.getName());
   121         close.close();
   122     }
   123 
   124     @BeforeMethod public void initPresenter() {
   125         FnContext.currentPresenter(presenter);
   126     }
   127 }