boot/src/test/java/org/apidesign/html/boot/impl/FnTest.java
branchclassloader
changeset 128 3cc64ce45056
parent 127 28ea7ef534e9
child 161 ea5ca9cc685d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/boot/src/test/java/org/apidesign/html/boot/impl/FnTest.java	Wed Jun 19 22:29:41 2013 +0200
     1.3 @@ -0,0 +1,109 @@
     1.4 +/**
     1.5 + * HTML via Java(tm) Language Bindings
     1.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details. apidesign.org
    1.16 + * designates this particular file as subject to the
    1.17 + * "Classpath" exception as provided by apidesign.org
    1.18 + * in the License file that accompanied this code.
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License
    1.21 + * along with this program. Look for COPYING file in the top folder.
    1.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    1.23 + */
    1.24 +
    1.25 +package org.apidesign.html.boot.impl;
    1.26 +
    1.27 +import java.net.URL;
    1.28 +import java.net.URLClassLoader;
    1.29 +import java.util.ArrayList;
    1.30 +import java.util.Arrays;
    1.31 +import java.util.Collection;
    1.32 +import java.util.List;
    1.33 +import javax.script.Invocable;
    1.34 +import javax.script.ScriptEngine;
    1.35 +import javax.script.ScriptEngineManager;
    1.36 +import javax.script.ScriptException;
    1.37 +import org.apidesign.html.boot.spi.Fn;
    1.38 +import org.testng.annotations.BeforeClass;
    1.39 +
    1.40 +/**
    1.41 + *
    1.42 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.43 + */
    1.44 +public class FnTest extends JsClassLoaderBase {
    1.45 +    
    1.46 +    public FnTest() {
    1.47 +    }
    1.48 +
    1.49 +    @BeforeClass
    1.50 +    public static void createClassLoader() throws Exception {
    1.51 +        ScriptEngineManager sem = new ScriptEngineManager();
    1.52 +        final ScriptEngine eng = sem.getEngineByMimeType("text/javascript");
    1.53 +        
    1.54 +        final URL my = FnTest.class.getProtectionDomain().getCodeSource().getLocation();
    1.55 +        ClassLoader parent = JsClassLoaderTest.class.getClassLoader().getParent();
    1.56 +        final URLClassLoader ul = new URLClassLoader(new URL[] { my }, parent);
    1.57 +        
    1.58 +        class Impl implements FindResources, Fn.Presenter {
    1.59 +            @Override
    1.60 +            public void findResources(String path, Collection<? super URL> results, boolean oneIsEnough) {
    1.61 +                URL u = ul.findResource(path);
    1.62 +                if (u != null) {
    1.63 +                    results.add(u);
    1.64 +                }
    1.65 +            }
    1.66 +
    1.67 +            @Override
    1.68 +            public Fn defineFn(String code, String... names) {
    1.69 +                StringBuilder sb = new StringBuilder();
    1.70 +                sb.append("(function() {");
    1.71 +                sb.append("return function(");
    1.72 +                String sep = "";
    1.73 +                for (String n : names) {
    1.74 +                    sb.append(sep);
    1.75 +                    sb.append(n);
    1.76 +                    sep = ", ";
    1.77 +                }
    1.78 +                sb.append(") {");
    1.79 +                sb.append(code);
    1.80 +                sb.append("};");
    1.81 +                sb.append("})()");
    1.82 +                try {
    1.83 +                    final Object val = eng.eval(sb.toString());
    1.84 +                    return new Fn() {
    1.85 +                        @Override
    1.86 +                        public Object invoke(Object thiz, Object... args) throws Exception {
    1.87 +                            List<Object> all = new ArrayList<Object>(args.length + 1);
    1.88 +                            all.add(thiz == null ? val : thiz);
    1.89 +                            all.addAll(Arrays.asList(args));
    1.90 +                            Invocable inv = (Invocable)eng;
    1.91 +                            Object ret = inv.invokeMethod(val, "call", all.toArray());
    1.92 +                            return ret == val ? null : ret;
    1.93 +                        }
    1.94 +                    };
    1.95 +                } catch (ScriptException ex) {
    1.96 +                    throw new LinkageError("Can't parse: " + sb, ex);
    1.97 +                }
    1.98 +            }
    1.99 +
   1.100 +            @Override
   1.101 +            public void displayPage(URL resource, Runnable r) {
   1.102 +                throw new UnsupportedOperationException();
   1.103 +            }
   1.104 +        }
   1.105 +        Impl impl = new Impl();
   1.106 +        ClassLoader loader = FnUtils.newLoader(impl, impl, parent);
   1.107 +       
   1.108 +        
   1.109 +        methodClass = loader.loadClass(JsMethods.class.getName());
   1.110 +    }
   1.111 +    
   1.112 +}