Proper presenter that is using javax.script API JavaScript engine
authorJaroslav Tulach <jtulach@netbeans.org>
Tue, 03 Jun 2014 20:56:36 +0200
changeset 674a61fd3f48997
parent 673 8ba1935e4b08
child 675 fad65c387592
Proper presenter that is using javax.script API JavaScript engine
boot-script/pom.xml
boot-script/src/main/java/net/java/html/boot/script/ScriptPresenter.java
boot-script/src/test/java/net/java/html/boot/script/Jsr223JavaScriptTest.java
boot-script/src/test/java/net/java/html/boot/script/Jsr223JavaScriptTst.java
boot-script/src/test/java/net/java/html/boot/script/SingleCase.java
pom.xml
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/boot-script/pom.xml	Tue Jun 03 20:56:36 2014 +0200
     1.3 @@ -0,0 +1,41 @@
     1.4 +<?xml version="1.0" encoding="UTF-8"?>
     1.5 +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     1.6 +    <modelVersion>4.0.0</modelVersion>
     1.7 +    <parent>
     1.8 +        <groupId>org.netbeans.html</groupId>
     1.9 +        <artifactId>pom</artifactId>
    1.10 +        <version>1.0-SNAPSHOT</version>
    1.11 +    </parent>
    1.12 +    <name>Presenter via javax.script</name>
    1.13 +    <artifactId>net.java.html.boot.script</artifactId>
    1.14 +    <version>1.0-SNAPSHOT</version>
    1.15 +    <packaging>jar</packaging>
    1.16 +    <properties>
    1.17 +        <netbeans.compile.on.save>NONE</netbeans.compile.on.save>
    1.18 +    </properties>
    1.19 +    <dependencies>
    1.20 +        <dependency>
    1.21 +            <groupId>org.netbeans.html</groupId>
    1.22 +            <artifactId>net.java.html.boot</artifactId>
    1.23 +            <version>${project.version}</version>
    1.24 +            <type>jar</type>
    1.25 +        </dependency>
    1.26 +        <dependency>
    1.27 +            <groupId>org.testng</groupId>
    1.28 +            <artifactId>testng</artifactId>
    1.29 +            <scope>test</scope>
    1.30 +        </dependency>
    1.31 +        <dependency>
    1.32 +            <groupId>org.webjars</groupId>
    1.33 +            <artifactId>envjs</artifactId>
    1.34 +            <version>1.2</version>
    1.35 +            <scope>test</scope>
    1.36 +        </dependency>
    1.37 +        <dependency>
    1.38 +            <groupId>${project.groupId}</groupId>
    1.39 +            <artifactId>net.java.html.json.tck</artifactId>
    1.40 +            <version>${project.version}</version>
    1.41 +            <scope>test</scope>
    1.42 +        </dependency>
    1.43 +    </dependencies>
    1.44 +</project>
    1.45 \ No newline at end of file
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/boot-script/src/main/java/net/java/html/boot/script/ScriptPresenter.java	Tue Jun 03 20:56:36 2014 +0200
     2.3 @@ -0,0 +1,244 @@
     2.4 +/**
     2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 + *
     2.7 + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     2.8 + *
     2.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    2.10 + * Other names may be trademarks of their respective owners.
    2.11 + *
    2.12 + * The contents of this file are subject to the terms of either the GNU
    2.13 + * General Public License Version 2 only ("GPL") or the Common
    2.14 + * Development and Distribution License("CDDL") (collectively, the
    2.15 + * "License"). You may not use this file except in compliance with the
    2.16 + * License. You can obtain a copy of the License at
    2.17 + * http://www.netbeans.org/cddl-gplv2.html
    2.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    2.19 + * specific language governing permissions and limitations under the
    2.20 + * License.  When distributing the software, include this License Header
    2.21 + * Notice in each file and include the License file at
    2.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    2.23 + * particular file as subject to the "Classpath" exception as provided
    2.24 + * by Oracle in the GPL Version 2 section of the License file that
    2.25 + * accompanied this code. If applicable, add the following below the
    2.26 + * License Header, with the fields enclosed by brackets [] replaced by
    2.27 + * your own identifying information:
    2.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    2.29 + *
    2.30 + * Contributor(s):
    2.31 + *
    2.32 + * The Original Software is NetBeans. The Initial Developer of the Original
    2.33 + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    2.34 + *
    2.35 + * If you wish your version of this file to be governed by only the CDDL
    2.36 + * or only the GPL Version 2, indicate your decision by adding
    2.37 + * "[Contributor] elects to include this software in this distribution
    2.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    2.39 + * single choice of license, a recipient has the option to distribute
    2.40 + * your version of this file under either the CDDL, the GPL Version 2 or
    2.41 + * to extend the choice of license to its licensees as provided above.
    2.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    2.43 + * Version 2 license, then the option applies only if the new code is
    2.44 + * made subject to such option by the copyright holder.
    2.45 + */
    2.46 +package net.java.html.boot.script;
    2.47 +
    2.48 +import java.io.Reader;
    2.49 +import java.net.URL;
    2.50 +import java.util.ArrayList;
    2.51 +import java.util.Arrays;
    2.52 +import java.util.List;
    2.53 +import java.util.logging.Level;
    2.54 +import java.util.logging.Logger;
    2.55 +import javax.script.Invocable;
    2.56 +import javax.script.ScriptEngine;
    2.57 +import javax.script.ScriptEngineManager;
    2.58 +import javax.script.ScriptException;
    2.59 +import org.apidesign.html.boot.spi.Fn;
    2.60 +import org.apidesign.html.boot.spi.Fn.Presenter;
    2.61 +
    2.62 +/** Implementation of {@link Presenter} that delegates
    2.63 + * to Java {@link ScriptEngine scripting} API. The presenter runs headless
    2.64 + * without appropriate simulation of browser APIs. Its primary usefulness
    2.65 + * is inside testing environments. 
    2.66 + * <p>
    2.67 + * One can load in browser simulation for example from 
    2.68 + * <a href="http://www.envjs.com/">env.js</a>. The best way to achieve so,
    2.69 + * is to add dependency on XXX
    2.70 + * 
    2.71 + *
    2.72 + * @author Jaroslav Tulach
    2.73 + */
    2.74 +public final class ScriptPresenter implements Presenter, Fn.FromJavaScript, Fn.ToJavaScript {
    2.75 +    private final ScriptEngine eng;
    2.76 +
    2.77 +    public ScriptPresenter() {
    2.78 +        try {
    2.79 +            eng = new ScriptEngineManager().getEngineByName("javascript");
    2.80 +            eng.eval("function alert(msg) { Packages.java.lang.System.out.println(msg); };");
    2.81 +        } catch (ScriptException ex) {
    2.82 +            throw new IllegalStateException(ex);
    2.83 +        }
    2.84 +    }
    2.85 +
    2.86 +    @Override
    2.87 +    public Fn defineFn(String code, String... names) {
    2.88 +        return defineImpl(code, names);
    2.89 +    }
    2.90 +    private FnImpl defineImpl(String code, String... names) {
    2.91 +        StringBuilder sb = new StringBuilder();
    2.92 +        sb.append("(function() {");
    2.93 +        sb.append("  return function(");
    2.94 +        String sep = "";
    2.95 +        if (names != null) for (String n : names) {
    2.96 +            sb.append(sep).append(n);
    2.97 +            sep = ",";
    2.98 +        }
    2.99 +        sb.append(") {\n");
   2.100 +        sb.append(code);
   2.101 +        sb.append("};");
   2.102 +        sb.append("})()");
   2.103 +
   2.104 +        final Object fn;
   2.105 +        try {
   2.106 +            fn = eng.eval(sb.toString());
   2.107 +        } catch (ScriptException ex) {
   2.108 +            throw new IllegalStateException(ex);
   2.109 +        }
   2.110 +        return new FnImpl(this, fn);
   2.111 +    }
   2.112 +
   2.113 +    @Override
   2.114 +    public void displayPage(URL page, Runnable onPageLoad) {
   2.115 +        // not really displaying anything
   2.116 +        if (onPageLoad != null) {
   2.117 +            onPageLoad.run();
   2.118 +        }
   2.119 +    }
   2.120 +
   2.121 +    @Override
   2.122 +    public void loadScript(Reader code) throws Exception {
   2.123 +        eng.eval(code);
   2.124 +    }
   2.125 +    
   2.126 +    //
   2.127 +    // array conversions
   2.128 +    //
   2.129 +    
   2.130 +    final Object convertArrays(Object[] arr) throws Exception {
   2.131 +        for (int i = 0; i < arr.length; i++) {
   2.132 +            if (arr[i] instanceof Object[]) {
   2.133 +                arr[i] = convertArrays((Object[]) arr[i]);
   2.134 +            }
   2.135 +        }
   2.136 +        final Object wrapArr = wrapArrFn().invokeImpl(null, false, arr); // NOI18N
   2.137 +        return wrapArr;
   2.138 +    }
   2.139 +
   2.140 +    private FnImpl wrapArrImpl;
   2.141 +    private FnImpl wrapArrFn() {
   2.142 +        if (wrapArrImpl == null) {
   2.143 +            try {
   2.144 +                wrapArrImpl = defineImpl("return Array.prototype.slice.call(arguments);");
   2.145 +            } catch (Exception ex) {
   2.146 +                throw new IllegalStateException(ex);
   2.147 +            }
   2.148 +        }
   2.149 +        return wrapArrImpl;
   2.150 +    }
   2.151 +
   2.152 +    final Object checkArray(Object val) throws Exception {
   2.153 +        final FnImpl fn = arraySizeFn();
   2.154 +        final Object fnRes = fn.invokeImpl(null, false, val, null);
   2.155 +        int length = ((Number) fnRes).intValue();
   2.156 +        if (length == -1) {
   2.157 +            return val;
   2.158 +        }
   2.159 +        Object[] arr = new Object[length];
   2.160 +        fn.invokeImpl(null, false, val, arr);
   2.161 +        return arr;
   2.162 +    }
   2.163 +    private FnImpl arraySize;
   2.164 +    private FnImpl arraySizeFn() {
   2.165 +        if (arraySize == null) {
   2.166 +            try {
   2.167 +                arraySize = defineImpl("\n"
   2.168 +                    + "if (to === null) {\n"
   2.169 +                    + "  if (Object.prototype.toString.call(arr) === '[object Array]') return arr.length;\n"
   2.170 +                    + "  else return -1;\n"
   2.171 +                    + "} else {\n"
   2.172 +                    + "  var l = arr.length;\n"
   2.173 +                    + "  for (var i = 0; i < l; i++) to[i] = arr[i];\n"
   2.174 +                    + "  return l;\n"
   2.175 +                    + "}", "arr", "to"
   2.176 +                );
   2.177 +            } catch (Exception ex) {
   2.178 +                throw new IllegalStateException(ex);
   2.179 +            }
   2.180 +        }
   2.181 +        return arraySize;
   2.182 +    }
   2.183 +
   2.184 +    @Override
   2.185 +    public Object toJava(Object jsArray) {
   2.186 +        try {
   2.187 +            return checkArray(jsArray);
   2.188 +        } catch (Exception ex) {
   2.189 +            throw new IllegalStateException(ex);
   2.190 +        }
   2.191 +    }
   2.192 +    
   2.193 +    @Override
   2.194 +    public Object toJavaScript(Object toReturn) {
   2.195 +        if (toReturn instanceof Object[]) {
   2.196 +            try {
   2.197 +                return convertArrays((Object[])toReturn);
   2.198 +            } catch (Exception ex) {
   2.199 +                throw new IllegalStateException(ex);
   2.200 +            }
   2.201 +        } else {
   2.202 +            return toReturn;
   2.203 +        }
   2.204 +    }
   2.205 +
   2.206 +    private class FnImpl extends Fn {
   2.207 +
   2.208 +        private final Object fn;
   2.209 +
   2.210 +        public FnImpl(Presenter presenter, Object fn) {
   2.211 +            super(presenter);
   2.212 +            this.fn = fn;
   2.213 +        }
   2.214 +
   2.215 +        @Override
   2.216 +        public Object invoke(Object thiz, Object... args) throws Exception {
   2.217 +            return invokeImpl(thiz, true, args);
   2.218 +        }
   2.219 +
   2.220 +            final Object invokeImpl(Object thiz, boolean arrayChecks, Object... args) throws Exception {
   2.221 +                List<Object> all = new ArrayList<Object>(args.length + 1);
   2.222 +                all.add(thiz == null ? fn : thiz);
   2.223 +                for (int i = 0; i < args.length; i++) {
   2.224 +                    if (arrayChecks) {
   2.225 +                        if (args[i] instanceof Object[]) {
   2.226 +                            Object[] arr = (Object[]) args[i];
   2.227 +                            Object conv = ((ScriptPresenter)presenter()).convertArrays(arr);
   2.228 +                            args[i] = conv;
   2.229 +                        }
   2.230 +                        if (args[i] instanceof Character) {
   2.231 +                            args[i] = (int)((Character)args[i]);
   2.232 +                        }
   2.233 +                    }
   2.234 +                    all.add(args[i]);
   2.235 +                }
   2.236 +                Object ret = ((Invocable)eng).invokeMethod(fn, "call", all.toArray()); // NOI18N
   2.237 +                if (ret == fn) {
   2.238 +                    return null;
   2.239 +                }
   2.240 +                if (!arrayChecks) {
   2.241 +                    return ret;
   2.242 +                }
   2.243 +                return ((ScriptPresenter)presenter()).checkArray(ret);
   2.244 +            }
   2.245 +    }
   2.246 +    
   2.247 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/boot-script/src/test/java/net/java/html/boot/script/Jsr223JavaScriptTest.java	Tue Jun 03 20:56:36 2014 +0200
     3.3 @@ -0,0 +1,117 @@
     3.4 +/**
     3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3.6 + *
     3.7 + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     3.8 + *
     3.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    3.10 + * Other names may be trademarks of their respective owners.
    3.11 + *
    3.12 + * The contents of this file are subject to the terms of either the GNU
    3.13 + * General Public License Version 2 only ("GPL") or the Common
    3.14 + * Development and Distribution License("CDDL") (collectively, the
    3.15 + * "License"). You may not use this file except in compliance with the
    3.16 + * License. You can obtain a copy of the License at
    3.17 + * http://www.netbeans.org/cddl-gplv2.html
    3.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    3.19 + * specific language governing permissions and limitations under the
    3.20 + * License.  When distributing the software, include this License Header
    3.21 + * Notice in each file and include the License file at
    3.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    3.23 + * particular file as subject to the "Classpath" exception as provided
    3.24 + * by Oracle in the GPL Version 2 section of the License file that
    3.25 + * accompanied this code. If applicable, add the following below the
    3.26 + * License Header, with the fields enclosed by brackets [] replaced by
    3.27 + * your own identifying information:
    3.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    3.29 + *
    3.30 + * Contributor(s):
    3.31 + *
    3.32 + * The Original Software is NetBeans. The Initial Developer of the Original
    3.33 + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    3.34 + *
    3.35 + * If you wish your version of this file to be governed by only the CDDL
    3.36 + * or only the GPL Version 2, indicate your decision by adding
    3.37 + * "[Contributor] elects to include this software in this distribution
    3.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    3.39 + * single choice of license, a recipient has the option to distribute
    3.40 + * your version of this file under either the CDDL, the GPL Version 2 or
    3.41 + * to extend the choice of license to its licensees as provided above.
    3.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    3.43 + * Version 2 license, then the option applies only if the new code is
    3.44 + * made subject to such option by the copyright holder.
    3.45 + */
    3.46 +package net.java.html.boot.script;
    3.47 +
    3.48 +import java.lang.annotation.Annotation;
    3.49 +import java.lang.reflect.Method;
    3.50 +import java.util.ArrayList;
    3.51 +import java.util.List;
    3.52 +import java.util.concurrent.Executors;
    3.53 +import net.java.html.boot.BrowserBuilder;
    3.54 +import org.apidesign.html.boot.spi.Fn;
    3.55 +import org.apidesign.html.json.tck.KOTest;
    3.56 +import org.testng.Assert;
    3.57 +import org.testng.annotations.Factory;
    3.58 +
    3.59 +/**
    3.60 + *
    3.61 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.62 + */
    3.63 +public class Jsr223JavaScriptTest {
    3.64 +    private static Class<?> browserClass;
    3.65 +    private static Fn.Presenter browserPresenter;
    3.66 +    
    3.67 +    public Jsr223JavaScriptTest() {
    3.68 +    }
    3.69 +
    3.70 +    @Factory public static Object[] compatibilityTests() throws Exception {
    3.71 +        final BrowserBuilder bb = BrowserBuilder.newBrowser(new ScriptPresenter()).
    3.72 +            loadClass(Jsr223JavaScriptTest.class).
    3.73 +            loadPage("empty.html").
    3.74 +            invoke("initialized");
    3.75 +
    3.76 +        Executors.newSingleThreadExecutor().submit(new Runnable() {
    3.77 +            @Override
    3.78 +            public void run() {
    3.79 +                bb.showAndWait();
    3.80 +            }
    3.81 +        });
    3.82 +
    3.83 +        List<Object> res = new ArrayList<Object>();
    3.84 +        Class<? extends Annotation> test = 
    3.85 +            loadClass().getClassLoader().loadClass(KOTest.class.getName()).
    3.86 +            asSubclass(Annotation.class);
    3.87 +
    3.88 +        Class[] arr = (Class[]) loadClass().getDeclaredMethod("tests").invoke(null);
    3.89 +        for (Class c : arr) {
    3.90 +            for (Method m : c.getMethods()) {
    3.91 +                if (m.getAnnotation(test) != null) {
    3.92 +                    res.add(new SingleCase(browserPresenter, m));
    3.93 +                }
    3.94 +            }
    3.95 +        }
    3.96 +        return res.toArray();
    3.97 +    }
    3.98 +
    3.99 +    static synchronized Class<?> loadClass() throws InterruptedException {
   3.100 +        while (browserClass == null) {
   3.101 +            Jsr223JavaScriptTest.class.wait();
   3.102 +        }
   3.103 +        return browserClass;
   3.104 +    }
   3.105 +    
   3.106 +    public static synchronized void ready(Class<?> browserCls) throws Exception {
   3.107 +        browserClass = browserCls;
   3.108 +        browserPresenter = Fn.activePresenter();
   3.109 +        Jsr223JavaScriptTest.class.notifyAll();
   3.110 +    }
   3.111 +    
   3.112 +    public static void initialized() throws Exception {
   3.113 +        Assert.assertSame(
   3.114 +            Jsr223JavaScriptTest.class.getClassLoader(),
   3.115 +            ClassLoader.getSystemClassLoader(),
   3.116 +            "No special classloaders"
   3.117 +        );
   3.118 +        Jsr223JavaScriptTest.ready(Jsr223JavaScriptTst.class);
   3.119 +    }
   3.120 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/boot-script/src/test/java/net/java/html/boot/script/Jsr223JavaScriptTst.java	Tue Jun 03 20:56:36 2014 +0200
     4.3 @@ -0,0 +1,55 @@
     4.4 +/**
     4.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     4.6 + *
     4.7 + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     4.8 + *
     4.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    4.10 + * Other names may be trademarks of their respective owners.
    4.11 + *
    4.12 + * The contents of this file are subject to the terms of either the GNU
    4.13 + * General Public License Version 2 only ("GPL") or the Common
    4.14 + * Development and Distribution License("CDDL") (collectively, the
    4.15 + * "License"). You may not use this file except in compliance with the
    4.16 + * License. You can obtain a copy of the License at
    4.17 + * http://www.netbeans.org/cddl-gplv2.html
    4.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    4.19 + * specific language governing permissions and limitations under the
    4.20 + * License.  When distributing the software, include this License Header
    4.21 + * Notice in each file and include the License file at
    4.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    4.23 + * particular file as subject to the "Classpath" exception as provided
    4.24 + * by Oracle in the GPL Version 2 section of the License file that
    4.25 + * accompanied this code. If applicable, add the following below the
    4.26 + * License Header, with the fields enclosed by brackets [] replaced by
    4.27 + * your own identifying information:
    4.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    4.29 + *
    4.30 + * Contributor(s):
    4.31 + *
    4.32 + * The Original Software is NetBeans. The Initial Developer of the Original
    4.33 + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    4.34 + *
    4.35 + * If you wish your version of this file to be governed by only the CDDL
    4.36 + * or only the GPL Version 2, indicate your decision by adding
    4.37 + * "[Contributor] elects to include this software in this distribution
    4.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    4.39 + * single choice of license, a recipient has the option to distribute
    4.40 + * your version of this file under either the CDDL, the GPL Version 2 or
    4.41 + * to extend the choice of license to its licensees as provided above.
    4.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    4.43 + * Version 2 license, then the option applies only if the new code is
    4.44 + * made subject to such option by the copyright holder.
    4.45 + */
    4.46 +package net.java.html.boot.script;
    4.47 +
    4.48 +import org.apidesign.html.json.tck.JavaScriptTCK;
    4.49 +
    4.50 +/**
    4.51 + *
    4.52 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.53 + */
    4.54 +public final class Jsr223JavaScriptTst extends JavaScriptTCK {
    4.55 +    public static Class[] tests() {
    4.56 +        return testClasses();
    4.57 +    }
    4.58 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/boot-script/src/test/java/net/java/html/boot/script/SingleCase.java	Tue Jun 03 20:56:36 2014 +0200
     5.3 @@ -0,0 +1,127 @@
     5.4 +/**
     5.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     5.6 + *
     5.7 + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     5.8 + *
     5.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    5.10 + * Other names may be trademarks of their respective owners.
    5.11 + *
    5.12 + * The contents of this file are subject to the terms of either the GNU
    5.13 + * General Public License Version 2 only ("GPL") or the Common
    5.14 + * Development and Distribution License("CDDL") (collectively, the
    5.15 + * "License"). You may not use this file except in compliance with the
    5.16 + * License. You can obtain a copy of the License at
    5.17 + * http://www.netbeans.org/cddl-gplv2.html
    5.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    5.19 + * specific language governing permissions and limitations under the
    5.20 + * License.  When distributing the software, include this License Header
    5.21 + * Notice in each file and include the License file at
    5.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    5.23 + * particular file as subject to the "Classpath" exception as provided
    5.24 + * by Oracle in the GPL Version 2 section of the License file that
    5.25 + * accompanied this code. If applicable, add the following below the
    5.26 + * License Header, with the fields enclosed by brackets [] replaced by
    5.27 + * your own identifying information:
    5.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    5.29 + *
    5.30 + * Contributor(s):
    5.31 + *
    5.32 + * The Original Software is NetBeans. The Initial Developer of the Original
    5.33 + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    5.34 + *
    5.35 + * If you wish your version of this file to be governed by only the CDDL
    5.36 + * or only the GPL Version 2, indicate your decision by adding
    5.37 + * "[Contributor] elects to include this software in this distribution
    5.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    5.39 + * single choice of license, a recipient has the option to distribute
    5.40 + * your version of this file under either the CDDL, the GPL Version 2 or
    5.41 + * to extend the choice of license to its licensees as provided above.
    5.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    5.43 + * Version 2 license, then the option applies only if the new code is
    5.44 + * made subject to such option by the copyright holder.
    5.45 + */
    5.46 +package net.java.html.boot.script;
    5.47 +
    5.48 +import java.lang.reflect.InvocationTargetException;
    5.49 +import java.lang.reflect.Method;
    5.50 +import java.util.concurrent.Executor;
    5.51 +import java.util.concurrent.Executors;
    5.52 +import org.apidesign.html.boot.spi.Fn;
    5.53 +import org.netbeans.html.boot.impl.FnContext;
    5.54 +import org.testng.IHookCallBack;
    5.55 +import org.testng.IHookable;
    5.56 +import org.testng.ITest;
    5.57 +import org.testng.ITestResult;
    5.58 +import org.testng.annotations.Test;
    5.59 +
    5.60 +/**
    5.61 + *
    5.62 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    5.63 + */
    5.64 +public final class SingleCase implements ITest, IHookable, Runnable {
    5.65 +    private static final Executor JS = Executors.newSingleThreadExecutor();
    5.66 +    private final Fn.Presenter p;
    5.67 +    private final Method m;
    5.68 +    private Object result;
    5.69 +    private Object inst;
    5.70 +
    5.71 +    SingleCase(Fn.Presenter p, Method m) {
    5.72 +        this.p = p;
    5.73 +        this.m = m;
    5.74 +    }
    5.75 +
    5.76 +    @Override
    5.77 +    public String getTestName() {
    5.78 +        return m.getName();
    5.79 +    }
    5.80 +
    5.81 +    @Test
    5.82 +    public synchronized void executeTest() throws Exception {
    5.83 +        if (result == null) {
    5.84 +            JS.execute(this);
    5.85 +            wait();
    5.86 +        }
    5.87 +        if (result instanceof Exception) {
    5.88 +            throw (Exception)result;
    5.89 +        }
    5.90 +        if (result instanceof Error) {
    5.91 +            throw (Error)result;
    5.92 +        }
    5.93 +    }
    5.94 +
    5.95 +    @Override
    5.96 +    public synchronized void run() {
    5.97 +        boolean notify = true;
    5.98 +        try {
    5.99 +            FnContext.currentPresenter(p);
   5.100 +            if (inst == null) {
   5.101 +                inst = m.getDeclaringClass().newInstance();
   5.102 +            }
   5.103 +            result = m.invoke(inst);
   5.104 +            if (result == null) {
   5.105 +                result = this;
   5.106 +            }
   5.107 +        } catch (InvocationTargetException ex) {
   5.108 +            Throwable r = ex.getTargetException();
   5.109 +            if (r instanceof InterruptedException) {
   5.110 +                notify = false;
   5.111 +                JS.execute(this);
   5.112 +                return;
   5.113 +            }
   5.114 +            result = r;
   5.115 +        } catch (Exception ex) {
   5.116 +            result = ex;
   5.117 +        } finally {
   5.118 +            if (notify) {
   5.119 +                notifyAll();
   5.120 +            }
   5.121 +            FnContext.currentPresenter(null);
   5.122 +        }
   5.123 +    }
   5.124 +
   5.125 +    @Override
   5.126 +    public void run(IHookCallBack ihcb, ITestResult itr) {
   5.127 +        ihcb.runTestMethod(itr);
   5.128 +    }
   5.129 +    
   5.130 +}
     6.1 --- a/pom.xml	Wed May 28 14:50:18 2014 +0200
     6.2 +++ b/pom.xml	Tue Jun 03 20:56:36 2014 +0200
     6.3 @@ -32,6 +32,7 @@
     6.4      <module>html4j-maven-plugin</module>
     6.5      <module>ko-osgi-test</module>
     6.6      <module>equinox-agentclass-hook</module>
     6.7 +    <module>boot-script</module>
     6.8    </modules>
     6.9    <licenses>
    6.10        <license>