More infrastructure for OSGi test. Now fails on TCK importing impl package - as generated by annotation processor osgi
authorJaroslav Tulach <jaroslav.tulach@netbeans.org>
Sun, 22 Dec 2013 07:14:05 +0100
branchosgi
changeset 37187bbddaa2db1
parent 370 b4293550a485
child 372 f16037a6937c
More infrastructure for OSGi test. Now fails on TCK importing impl package - as generated by annotation processor
ko-osgi-test/pom.xml
ko-osgi-test/src/test/java/org/netbeans/html/ko/osgi/test/DynamicHTTP.java
ko-osgi-test/src/test/java/org/netbeans/html/ko/osgi/test/KOFx.java
ko-osgi-test/src/test/java/org/netbeans/html/ko/osgi/test/KnockoutEquinoxTest.java
ko-osgi-test/src/test/resources/org/netbeans/html/ko/osgi/test/test.html
     1.1 --- a/ko-osgi-test/pom.xml	Sun Dec 22 05:45:13 2013 +0100
     1.2 +++ b/ko-osgi-test/pom.xml	Sun Dec 22 07:14:05 2013 +0100
     1.3 @@ -9,6 +9,19 @@
     1.4      <artifactId>ko-osgi-test</artifactId>
     1.5      <packaging>jar</packaging>
     1.6      <description>Runs the TCK for Knockout in an OSGi Container</description>
     1.7 +    <build>
     1.8 +        <plugins>
     1.9 +            <plugin>
    1.10 +                <groupId>org.apache.maven.plugins</groupId>
    1.11 +                <artifactId>maven-compiler-plugin</artifactId>
    1.12 +                <version>2.3.2</version>
    1.13 +                <configuration>
    1.14 +                    <source>1.7</source>
    1.15 +                    <target>1.7</target>
    1.16 +                </configuration>
    1.17 +            </plugin>
    1.18 +        </plugins>
    1.19 +    </build>
    1.20      <dependencies>
    1.21          <dependency>
    1.22              <groupId>com.oracle</groupId>
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/ko-osgi-test/src/test/java/org/netbeans/html/ko/osgi/test/DynamicHTTP.java	Sun Dec 22 07:14:05 2013 +0100
     2.3 @@ -0,0 +1,259 @@
     2.4 +/**
     2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 + *
     2.7 + * Copyright 2013-2013 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-2013 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 org.netbeans.html.ko.osgi.test;
    2.47 +
    2.48 +import java.io.ByteArrayInputStream;
    2.49 +import java.io.ByteArrayOutputStream;
    2.50 +import java.io.IOException;
    2.51 +import java.io.InputStream;
    2.52 +import java.io.OutputStream;
    2.53 +import java.io.Reader;
    2.54 +import java.net.URI;
    2.55 +import java.net.URISyntaxException;
    2.56 +import java.util.ArrayList;
    2.57 +import java.util.List;
    2.58 +import java.util.logging.Level;
    2.59 +import java.util.logging.Logger;
    2.60 +import org.glassfish.grizzly.PortRange;
    2.61 +import org.glassfish.grizzly.http.server.HttpHandler;
    2.62 +import org.glassfish.grizzly.http.server.HttpServer;
    2.63 +import org.glassfish.grizzly.http.server.NetworkListener;
    2.64 +import org.glassfish.grizzly.http.server.Request;
    2.65 +import org.glassfish.grizzly.http.server.Response;
    2.66 +import org.glassfish.grizzly.http.server.ServerConfiguration;
    2.67 +import org.glassfish.grizzly.websockets.WebSocket;
    2.68 +import org.glassfish.grizzly.websockets.WebSocketAddOn;
    2.69 +import org.glassfish.grizzly.websockets.WebSocketApplication;
    2.70 +import org.glassfish.grizzly.websockets.WebSocketEngine;
    2.71 +
    2.72 +/**
    2.73 + *
    2.74 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    2.75 + */
    2.76 +final class DynamicHTTP extends HttpHandler {
    2.77 +    private static final Logger LOG = Logger.getLogger(DynamicHTTP.class.getName());
    2.78 +    private static int resourcesCount;
    2.79 +    private static List<Resource> resources;
    2.80 +    private static ServerConfiguration conf;
    2.81 +    private static HttpServer server;
    2.82 +    
    2.83 +    private DynamicHTTP() {
    2.84 +    }
    2.85 +    
    2.86 +    static URI initServer() throws Exception {
    2.87 +        server = HttpServer.createSimpleServer(null, new PortRange(8080, 65535));
    2.88 +        final WebSocketAddOn addon = new WebSocketAddOn();
    2.89 +        for (NetworkListener listener : server.getListeners()) {
    2.90 +            listener.registerAddOn(addon);
    2.91 +        }        
    2.92 +        resources = new ArrayList<Resource>();
    2.93 +
    2.94 +        conf = server.getServerConfiguration();
    2.95 +        final DynamicHTTP dh = new DynamicHTTP();
    2.96 +
    2.97 +        conf.addHttpHandler(dh, "/");
    2.98 +        
    2.99 +        server.start();
   2.100 +
   2.101 +        return pageURL("http", server, "/test.html");
   2.102 +    }
   2.103 +    
   2.104 +    @Override
   2.105 +    public void service(Request request, Response response) throws Exception {
   2.106 +        if ("/test.html".equals(request.getRequestURI())) {
   2.107 +            response.setContentType("text/html");
   2.108 +            final InputStream is = DynamicHTTP.class.getResourceAsStream("test.html");
   2.109 +            copyStream(is, response.getOutputStream(), null);
   2.110 +            return;
   2.111 +        }
   2.112 +        if ("/dynamic".equals(request.getRequestURI())) {
   2.113 +            String mimeType = request.getParameter("mimeType");
   2.114 +            List<String> params = new ArrayList<String>();
   2.115 +            boolean webSocket = false;
   2.116 +            for (int i = 0;; i++) {
   2.117 +                String p = request.getParameter("param" + i);
   2.118 +                if (p == null) {
   2.119 +                    break;
   2.120 +                }
   2.121 +                if ("protocol:ws".equals(p)) {
   2.122 +                    webSocket = true;
   2.123 +                    continue;
   2.124 +                }
   2.125 +                params.add(p);
   2.126 +            }
   2.127 +            final String cnt = request.getParameter("content");
   2.128 +            String mangle = cnt.replace("%20", " ").replace("%0A", "\n");
   2.129 +            ByteArrayInputStream is = new ByteArrayInputStream(mangle.getBytes("UTF-8"));
   2.130 +            URI url;
   2.131 +            final Resource res = new Resource(is, mimeType, "/dynamic/res" + ++resourcesCount, params.toArray(new String[params.size()]));
   2.132 +            if (webSocket) {
   2.133 +                url = registerWebSocket(res);
   2.134 +            } else {
   2.135 +                url = registerResource(res);
   2.136 +            }
   2.137 +            response.getWriter().write(url.toString());
   2.138 +            response.getWriter().write("\n");
   2.139 +            return;
   2.140 +        }
   2.141 +
   2.142 +        for (Resource r : resources) {
   2.143 +            if (r.httpPath.equals(request.getRequestURI())) {
   2.144 +                response.setContentType(r.httpType);
   2.145 +                r.httpContent.reset();
   2.146 +                String[] params = null;
   2.147 +                if (r.parameters.length != 0) {
   2.148 +                    params = new String[r.parameters.length];
   2.149 +                    for (int i = 0; i < r.parameters.length; i++) {
   2.150 +                        params[i] = request.getParameter(r.parameters[i]);
   2.151 +                        if (params[i] == null) {
   2.152 +                            if ("http.method".equals(r.parameters[i])) {
   2.153 +                                params[i] = request.getMethod().toString();
   2.154 +                            } else if ("http.requestBody".equals(r.parameters[i])) {
   2.155 +                                Reader rdr = request.getReader();
   2.156 +                                StringBuilder sb = new StringBuilder();
   2.157 +                                for (;;) {
   2.158 +                                    int ch = rdr.read();
   2.159 +                                    if (ch == -1) {
   2.160 +                                        break;
   2.161 +                                    }
   2.162 +                                    sb.append((char) ch);
   2.163 +                                }
   2.164 +                                params[i] = sb.toString();
   2.165 +                            }
   2.166 +                        }
   2.167 +                        if (params[i] == null) {
   2.168 +                            params[i] = "null";
   2.169 +                        }
   2.170 +                    }
   2.171 +                }
   2.172 +
   2.173 +                copyStream(r.httpContent, response.getOutputStream(), null, params);
   2.174 +            }
   2.175 +        }
   2.176 +    }
   2.177 +    
   2.178 +    private URI registerWebSocket(Resource r) {
   2.179 +        WebSocketEngine.getEngine().register("", r.httpPath, new WS(r));
   2.180 +        return pageURL("ws", server, r.httpPath);
   2.181 +    }
   2.182 +
   2.183 +    private URI registerResource(Resource r) {
   2.184 +        if (!resources.contains(r)) {
   2.185 +            resources.add(r);
   2.186 +            conf.addHttpHandler(this, r.httpPath);
   2.187 +        }
   2.188 +        return pageURL("http", server, r.httpPath);
   2.189 +    }
   2.190 +    
   2.191 +    private static URI pageURL(String proto, HttpServer server, final String page) {
   2.192 +        NetworkListener listener = server.getListeners().iterator().next();
   2.193 +        int port = listener.getPort();
   2.194 +        try {
   2.195 +            return new URI(proto + "://localhost:" + port + page);
   2.196 +        } catch (URISyntaxException ex) {
   2.197 +            throw new IllegalStateException(ex);
   2.198 +        }
   2.199 +    }
   2.200 +    
   2.201 +    static final class Resource {
   2.202 +
   2.203 +        final InputStream httpContent;
   2.204 +        final String httpType;
   2.205 +        final String httpPath;
   2.206 +        final String[] parameters;
   2.207 +
   2.208 +        Resource(InputStream httpContent, String httpType, String httpPath,
   2.209 +            String[] parameters) {
   2.210 +            httpContent.mark(Integer.MAX_VALUE);
   2.211 +            this.httpContent = httpContent;
   2.212 +            this.httpType = httpType;
   2.213 +            this.httpPath = httpPath;
   2.214 +            this.parameters = parameters;
   2.215 +        }
   2.216 +    }
   2.217 +
   2.218 +    static void copyStream(InputStream is, OutputStream os, String baseURL, String... params) throws IOException {
   2.219 +        for (;;) {
   2.220 +            int ch = is.read();
   2.221 +            if (ch == -1) {
   2.222 +                break;
   2.223 +            }
   2.224 +            if (ch == '$' && params.length > 0) {
   2.225 +                int cnt = is.read() - '0';
   2.226 +                if (baseURL != null && cnt == 'U' - '0') {
   2.227 +                    os.write(baseURL.getBytes("UTF-8"));
   2.228 +                } else {
   2.229 +                    if (cnt >= 0 && cnt < params.length) {
   2.230 +                        os.write(params[cnt].getBytes("UTF-8"));
   2.231 +                    } else {
   2.232 +                        os.write('$');
   2.233 +                        os.write(cnt + '0');
   2.234 +                    }
   2.235 +                }
   2.236 +            } else {
   2.237 +                os.write(ch);
   2.238 +            }
   2.239 +        }
   2.240 +    }
   2.241 +    
   2.242 +    private static class WS extends WebSocketApplication {
   2.243 +        private final Resource r;
   2.244 +
   2.245 +        private WS(Resource r) {
   2.246 +            this.r = r;
   2.247 +        }
   2.248 +
   2.249 +        @Override
   2.250 +        public void onMessage(WebSocket socket, String text) {
   2.251 +            try {
   2.252 +                r.httpContent.reset();
   2.253 +                ByteArrayOutputStream out = new ByteArrayOutputStream();
   2.254 +                copyStream(r.httpContent, out, null, text);
   2.255 +                String s = new String(out.toByteArray(), "UTF-8");
   2.256 +                socket.send(s);
   2.257 +            } catch (IOException ex) {
   2.258 +                LOG.log(Level.WARNING, "Error processing message " + text, ex);
   2.259 +            }
   2.260 +        }
   2.261 +    }
   2.262 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/ko-osgi-test/src/test/java/org/netbeans/html/ko/osgi/test/KOFx.java	Sun Dec 22 07:14:05 2013 +0100
     3.3 @@ -0,0 +1,118 @@
     3.4 +/**
     3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3.6 + *
     3.7 + * Copyright 2013-2013 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-2013 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 org.netbeans.html.ko.osgi.test;
    3.47 +
    3.48 +import java.io.Closeable;
    3.49 +import java.lang.reflect.InvocationTargetException;
    3.50 +import java.lang.reflect.Method;
    3.51 +import javafx.application.Platform;
    3.52 +import org.apidesign.html.boot.spi.Fn;
    3.53 +import org.testng.ITest;
    3.54 +import org.testng.annotations.Test;
    3.55 +
    3.56 +/**
    3.57 + *
    3.58 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.59 + */
    3.60 +public final class KOFx implements ITest, Runnable {
    3.61 +    private final Fn.Presenter p;
    3.62 +    private final Method m;
    3.63 +    private Object result;
    3.64 +    private Object inst;
    3.65 +    private int count;
    3.66 +
    3.67 +    KOFx(Fn.Presenter p, Method m) {
    3.68 +        this.p = p;
    3.69 +        this.m = m;
    3.70 +    }
    3.71 +
    3.72 +    @Override
    3.73 +    public String getTestName() {
    3.74 +        return m.getName();
    3.75 +    }
    3.76 +
    3.77 +    @Test
    3.78 +    public synchronized void executeTest() throws Exception {
    3.79 +        if (result == null) {
    3.80 +            Platform.runLater(this);
    3.81 +            wait();
    3.82 +        }
    3.83 +        if (result instanceof Exception) {
    3.84 +            throw (Exception)result;
    3.85 +        }
    3.86 +        if (result instanceof Error) {
    3.87 +            throw (Error)result;
    3.88 +        }
    3.89 +    }
    3.90 +
    3.91 +    @Override
    3.92 +    public synchronized void run() {
    3.93 +        boolean notify = true;
    3.94 +        try (Closeable a = Fn.activate(p)) {
    3.95 +            if (inst == null) {
    3.96 +                inst = m.getDeclaringClass().newInstance();
    3.97 +            }
    3.98 +            result = m.invoke(inst);
    3.99 +            if (result == null) {
   3.100 +                result = this;
   3.101 +            }
   3.102 +        } catch (InvocationTargetException ex) {
   3.103 +            Throwable r = ex.getTargetException();
   3.104 +            if (r instanceof InterruptedException) {
   3.105 +                if (count++ < 10000) {
   3.106 +                    notify = false;
   3.107 +                    Platform.runLater(this);
   3.108 +                    return;
   3.109 +                }
   3.110 +            }
   3.111 +            result = r;
   3.112 +        } catch (Exception ex) {
   3.113 +            result = ex;
   3.114 +        } finally {
   3.115 +            if (notify) {
   3.116 +                notifyAll();
   3.117 +            }
   3.118 +        }
   3.119 +    }
   3.120 +    
   3.121 +}
     4.1 --- a/ko-osgi-test/src/test/java/org/netbeans/html/ko/osgi/test/KnockoutEquinoxTest.java	Sun Dec 22 05:45:13 2013 +0100
     4.2 +++ b/ko-osgi-test/src/test/java/org/netbeans/html/ko/osgi/test/KnockoutEquinoxTest.java	Sun Dec 22 07:14:05 2013 +0100
     4.3 @@ -39,42 +39,137 @@
     4.4   * However, if you add GPL Version 2 code and therefore, elected the GPL
     4.5   * Version 2 license, then the option applies only if the new code is
     4.6   * made subject to such option by the copyright holder.
     4.7 + *//**
     4.8 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     4.9 + *
    4.10 + * Copyright 2013-2013 Oracle and/or its affiliates. All rights reserved.
    4.11 + *
    4.12 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    4.13 + * Other names may be trademarks of their respective owners.
    4.14 + *
    4.15 + * The contents of this file are subject to the terms of either the GNU
    4.16 + * General Public License Version 2 only ("GPL") or the Common
    4.17 + * Development and Distribution License("CDDL") (collectively, the
    4.18 + * "License"). You may not use this file except in compliance with the
    4.19 + * License. You can obtain a copy of the License at
    4.20 + * http://www.netbeans.org/cddl-gplv2.html
    4.21 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    4.22 + * specific language governing permissions and limitations under the
    4.23 + * License.  When distributing the software, include this License Header
    4.24 + * Notice in each file and include the License file at
    4.25 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    4.26 + * particular file as subject to the "Classpath" exception as provided
    4.27 + * by Oracle in the GPL Version 2 section of the License file that
    4.28 + * accompanied this code. If applicable, add the following below the
    4.29 + * License Header, with the fields enclosed by brackets [] replaced by
    4.30 + * your own identifying information:
    4.31 + * "Portions Copyrighted [year] [name of copyright owner]"
    4.32 + *
    4.33 + * Contributor(s):
    4.34 + *
    4.35 + * The Original Software is NetBeans. The Initial Developer of the Original
    4.36 + * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
    4.37 + *
    4.38 + * If you wish your version of this file to be governed by only the CDDL
    4.39 + * or only the GPL Version 2, indicate your decision by adding
    4.40 + * "[Contributor] elects to include this software in this distribution
    4.41 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    4.42 + * single choice of license, a recipient has the option to distribute
    4.43 + * your version of this file under either the CDDL, the GPL Version 2 or
    4.44 + * to extend the choice of license to its licensees as provided above.
    4.45 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    4.46 + * Version 2 license, then the option applies only if the new code is
    4.47 + * made subject to such option by the copyright holder.
    4.48   */
    4.49  package org.netbeans.html.ko.osgi.test;
    4.50  
    4.51 +import java.io.BufferedReader;
    4.52  import java.io.File;
    4.53  import java.io.IOException;
    4.54 +import java.io.InputStreamReader;
    4.55 +import java.lang.annotation.Annotation;
    4.56 +import java.lang.reflect.Method;
    4.57 +import java.net.URI;
    4.58 +import java.net.URISyntaxException;
    4.59 +import java.net.URL;
    4.60 +import java.net.URLConnection;
    4.61 +import java.util.ArrayList;
    4.62  import java.util.HashMap;
    4.63 +import java.util.List;
    4.64  import java.util.Map;
    4.65  import java.util.ServiceLoader;
    4.66 +import java.util.concurrent.Callable;
    4.67 +import java.util.concurrent.Executors;
    4.68  import java.util.jar.JarFile;
    4.69  import net.java.html.BrwsrCtx;
    4.70 +import net.java.html.boot.BrowserBuilder;
    4.71 +import net.java.html.js.JavaScriptBody;
    4.72 +import org.apidesign.html.boot.spi.Fn;
    4.73 +import org.apidesign.html.context.spi.Contexts;
    4.74 +import org.apidesign.html.json.spi.Technology;
    4.75 +import org.apidesign.html.json.spi.Transfer;
    4.76 +import org.apidesign.html.json.tck.KOTest;
    4.77 +import org.apidesign.html.json.tck.KnockoutTCK;
    4.78 +import org.json.JSONException;
    4.79 +import org.json.JSONObject;
    4.80 +import org.netbeans.html.boot.impl.FnContext;
    4.81 +import org.netbeans.html.kofx.FXContext;
    4.82 +import org.openide.util.lookup.ServiceProvider;
    4.83  import org.osgi.framework.Bundle;
    4.84  import org.osgi.framework.BundleException;
    4.85 +import org.osgi.framework.Constants;
    4.86  import org.osgi.framework.launch.Framework;
    4.87  import org.osgi.framework.launch.FrameworkFactory;
    4.88 -import static org.testng.Assert.assertNotNull;
    4.89  import static org.testng.Assert.fail;
    4.90 -import org.testng.annotations.BeforeClass;
    4.91 -import org.testng.annotations.Test;
    4.92 +import org.testng.annotations.AfterClass;
    4.93 +import org.testng.annotations.Factory;
    4.94  
    4.95  /**
    4.96   *
    4.97   * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.98   */
    4.99 -public class KnockoutEquinoxTest {
   4.100 +@ServiceProvider(service = KnockoutTCK.class)
   4.101 +public class KnockoutEquinoxTest extends KnockoutTCK {
   4.102      private static Framework framework;
   4.103 -    @BeforeClass public static void initOSGi() throws Exception {
   4.104 +    private static File dir;
   4.105 +    static Framework framework() throws Exception {
   4.106 +        if (framework != null) {
   4.107 +            return framework;
   4.108 +        }
   4.109          for (FrameworkFactory ff : ServiceLoader.load(FrameworkFactory.class)) {
   4.110 -            Map<String,String> config = new HashMap<String, String>();
   4.111 +            Map<String,String> config = new HashMap<>();
   4.112 +            dir = File.createTempFile("osgi", "tmp");
   4.113 +            dir.delete();
   4.114 +            dir.mkdirs();
   4.115 +            config.put(Constants.FRAMEWORK_STORAGE, dir.getPath());
   4.116 +            config.put(Constants.FRAMEWORK_STORAGE_CLEAN, "true");
   4.117              framework = ff.newFramework(config);
   4.118              framework.init();
   4.119              loadClassPathBundles(framework);
   4.120              framework.start();
   4.121 -            return;
   4.122 +            for (Bundle b : framework.getBundleContext().getBundles()) {
   4.123 +                b.start();
   4.124 +            }
   4.125 +            return framework;
   4.126          }
   4.127          fail("No OSGi framework in the path");
   4.128 +        return null;
   4.129      }
   4.130 +    
   4.131 +    @AfterClass public static void cleanUp() throws Exception {
   4.132 +        if (framework != null) framework.stop();
   4.133 +        clearUpDir(dir);
   4.134 +    }
   4.135 +    private static void clearUpDir(File dir) {
   4.136 +        if (dir.isDirectory()) {
   4.137 +            for (File f : dir.listFiles()) {
   4.138 +                clearUpDir(f);
   4.139 +            }
   4.140 +        }
   4.141 +        dir.delete();
   4.142 +    }
   4.143 +    
   4.144 +    
   4.145  
   4.146      private static void loadClassPathBundles(Framework f) throws IOException, BundleException {
   4.147          for (String jar : System.getProperty("java.class.path").split(File.pathSeparator)) {
   4.148 @@ -89,22 +184,179 @@
   4.149                  if (name.contains("org.eclipse.osgi")) {
   4.150                      continue;
   4.151                  }
   4.152 -                Bundle b = f.getBundleContext().installBundle("reference:" + file.toURI().toString());
   4.153 -                b.start();
   4.154 +                if (name.contains("testng")) {
   4.155 +                    continue;
   4.156 +                }
   4.157 +                final String path = "reference:" + file.toURI().toString();
   4.158 +                Bundle b = f.getBundleContext().installBundle(path);
   4.159              }
   4.160          }
   4.161      }
   4.162      
   4.163 -    @Test public void loadAClass() throws Exception {
   4.164 -        Bundle bundle = null;
   4.165 -        for (Bundle b : framework.getBundleContext().getBundles()) {
   4.166 -            if (b.getSymbolicName().equals("net.java.html")) {
   4.167 -                bundle = b;
   4.168 -                break;
   4.169 +    private static Class<?> loadAClass(Class<?> c) throws Exception {
   4.170 +        for (Bundle b : framework().getBundleContext().getBundles()) {
   4.171 +            try {
   4.172 +                Class<?> osgiClass = b.loadClass(c.getName());
   4.173 +                if (
   4.174 +                    osgiClass != null && 
   4.175 +                    osgiClass.getClassLoader() != ClassLoader.getSystemClassLoader()
   4.176 +                ) {
   4.177 +                    return osgiClass;
   4.178 +                }
   4.179 +            } catch (ClassNotFoundException cnfe) {
   4.180 +                // go on
   4.181              }
   4.182          }
   4.183 -        assertNotNull(bundle, "My bundle was found");
   4.184 -        Class<?> clazz = bundle.loadClass(BrwsrCtx.class.getName());
   4.185 -        assertNotNull(clazz, "Class loaded OK");
   4.186 +        fail("Cannot load " + c + " from the OSGi container!");
   4.187 +        return null;
   4.188      }
   4.189 +    
   4.190 +    private static Class<?> browserClass;
   4.191 +    private static Fn.Presenter browserContext;
   4.192 +    
   4.193 +    @Factory public static Object[] compatibilityTests() throws Exception {
   4.194 +        Class<?> tck = loadAClass(KnockoutTCK.class);
   4.195 +        Class<?> peer = loadAClass(KnockoutEquinoxTest.class);
   4.196 +        // initialize the TCK
   4.197 +        Callable<Class[]> inst = (Callable<Class[]>) peer.newInstance();
   4.198 +        
   4.199 +        Class[] arr = inst.call();
   4.200 +        for (int i = 0; i < arr.length; i++) {
   4.201 +            if (arr[i].getClassLoader() == ClassLoader.getSystemClassLoader()) {
   4.202 +                fail("Should be an OSGi class: " + arr[i]);
   4.203 +            }
   4.204 +        }
   4.205 +        
   4.206 +        URI uri = DynamicHTTP.initServer();
   4.207 +
   4.208 +        
   4.209 +        final BrowserBuilder bb = BrowserBuilder.newBrowser().loadClass(peer).
   4.210 +            loadPage(uri.toString()).
   4.211 +            invoke("initialized");
   4.212 +        
   4.213 +        Executors.newSingleThreadExecutor().submit(new Runnable() {
   4.214 +            @Override
   4.215 +            public void run() {
   4.216 +                bb.showAndWait();
   4.217 +            }
   4.218 +        });
   4.219 +        
   4.220 +        ClassLoader l = getClassLoader();
   4.221 +        List<Object> res = new ArrayList<Object>();
   4.222 +        for (int i = 0; i < arr.length; i++) {
   4.223 +            Class<?> c = Class.forName(arr[i].getName(), true, l);
   4.224 +            seekKOTests(c, res);
   4.225 +        }
   4.226 +        return res.toArray();
   4.227 +    }
   4.228 +
   4.229 +    private static void seekKOTests(Class<?> c, List<Object> res) throws SecurityException, ClassNotFoundException {
   4.230 +        Class<? extends Annotation> koTest =
   4.231 +            c.getClassLoader().loadClass(KOTest.class.getName()).
   4.232 +            asSubclass(Annotation.class);
   4.233 +        for (Method m : c.getMethods()) {
   4.234 +            if (m.getAnnotation(koTest) != null) {
   4.235 +                res.add(new KOFx(browserContext, m));
   4.236 +            }
   4.237 +        }
   4.238 +    }
   4.239 +
   4.240 +    static synchronized ClassLoader getClassLoader() throws InterruptedException {
   4.241 +        while (browserClass == null) {
   4.242 +            KnockoutEquinoxTest.class.wait();
   4.243 +        }
   4.244 +        return browserClass.getClassLoader();
   4.245 +    }
   4.246 +    
   4.247 +    public static synchronized void initialized(Class<?> browserCls) throws Exception {
   4.248 +        browserClass = browserCls;
   4.249 +        browserContext = FnContext.currentPresenter();
   4.250 +        KnockoutEquinoxTest.class.notifyAll();
   4.251 +    }
   4.252 +    
   4.253 +    public static void initialized() throws Exception {
   4.254 +        Class<?> classpathClass = ClassLoader.getSystemClassLoader().loadClass(KnockoutEquinoxTest.class.getName());
   4.255 +        Method m = classpathClass.getMethod("initialized", Class.class);
   4.256 +        m.invoke(null, KnockoutEquinoxTest.class);
   4.257 +        browserContext = FnContext.currentPresenter();
   4.258 +    }
   4.259 +
   4.260 +    @Override
   4.261 +    public BrwsrCtx createContext() {
   4.262 +        FXContext fx = new FXContext(browserContext);
   4.263 +        Contexts.Builder cb = Contexts.newBuilder().
   4.264 +            register(Technology.class, fx, 10).
   4.265 +            register(Transfer.class, fx, 10);
   4.266 +//        if (fx.areWebSocketsSupported()) {
   4.267 +//            cb.register(WSTransfer.class, fx, 10);
   4.268 +//        }
   4.269 +        return cb.build();
   4.270 +    }
   4.271 +
   4.272 +    @Override
   4.273 +    public Object createJSON(Map<String, Object> values) {
   4.274 +        JSONObject json = new JSONObject();
   4.275 +        for (Map.Entry<String, Object> entry : values.entrySet()) {
   4.276 +            try {
   4.277 +                json.put(entry.getKey(), entry.getValue());
   4.278 +            } catch (JSONException ex) {
   4.279 +                throw new IllegalStateException(ex);
   4.280 +            }
   4.281 +        }
   4.282 +        return json;
   4.283 +    }
   4.284 +
   4.285 +    @Override
   4.286 +    @JavaScriptBody(args = { "s", "args" }, body = ""
   4.287 +        + "var f = new Function(s); "
   4.288 +        + "return f.apply(null, args);"
   4.289 +    )
   4.290 +    public native Object executeScript(String script, Object[] arguments);
   4.291 +
   4.292 +    @JavaScriptBody(args = {  }, body = 
   4.293 +          "var h;"
   4.294 +        + "if (!!window && !!window.location && !!window.location.href)\n"
   4.295 +        + "  h = window.location.href;\n"
   4.296 +        + "else "
   4.297 +        + "  h = null;"
   4.298 +        + "return h;\n"
   4.299 +    )
   4.300 +    private static native String findBaseURL();
   4.301 +    
   4.302 +    @Override
   4.303 +    public URI prepareURL(String content, String mimeType, String[] parameters) {
   4.304 +        try {
   4.305 +            final URL baseURL = new URL(findBaseURL());
   4.306 +            StringBuilder sb = new StringBuilder();
   4.307 +            sb.append("/dynamic?mimeType=").append(mimeType);
   4.308 +            for (int i = 0; i < parameters.length; i++) {
   4.309 +                sb.append("&param" + i).append("=").append(parameters[i]);
   4.310 +            }
   4.311 +            String mangle = content.replace("\n", "%0a")
   4.312 +                .replace("\"", "\\\"").replace(" ", "%20");
   4.313 +            sb.append("&content=").append(mangle);
   4.314 +
   4.315 +            URL query = new URL(baseURL, sb.toString());
   4.316 +            URLConnection c = query.openConnection();
   4.317 +            BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
   4.318 +            URI connectTo = new URI(br.readLine());
   4.319 +            return connectTo;
   4.320 +        } catch (IOException ex) {
   4.321 +            throw new IllegalStateException(ex);
   4.322 +        } catch (URISyntaxException ex) {
   4.323 +            throw new IllegalStateException(ex);
   4.324 +        }
   4.325 +    }
   4.326 +
   4.327 +    @Override
   4.328 +    public boolean canFailWebSocketTest() {
   4.329 +        try {
   4.330 +            Class.forName("java.util.function.Function");
   4.331 +            return false;
   4.332 +        } catch (ClassNotFoundException ex) {
   4.333 +            // running on JDK7, FX WebView WebSocket impl does not work
   4.334 +            return true;
   4.335 +        }
   4.336 +    }
   4.337 +    
   4.338  }
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/ko-osgi-test/src/test/resources/org/netbeans/html/ko/osgi/test/test.html	Sun Dec 22 07:14:05 2013 +0100
     5.3 @@ -0,0 +1,56 @@
     5.4 +<!--
     5.5 +
     5.6 +    DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     5.7 +
     5.8 +    Copyright 2013-2013 Oracle and/or its affiliates. All rights reserved.
     5.9 +
    5.10 +    Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    5.11 +    Other names may be trademarks of their respective owners.
    5.12 +
    5.13 +    The contents of this file are subject to the terms of either the GNU
    5.14 +    General Public License Version 2 only ("GPL") or the Common
    5.15 +    Development and Distribution License("CDDL") (collectively, the
    5.16 +    "License"). You may not use this file except in compliance with the
    5.17 +    License. You can obtain a copy of the License at
    5.18 +    http://www.netbeans.org/cddl-gplv2.html
    5.19 +    or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    5.20 +    specific language governing permissions and limitations under the
    5.21 +    License.  When distributing the software, include this License Header
    5.22 +    Notice in each file and include the License file at
    5.23 +    nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    5.24 +    particular file as subject to the "Classpath" exception as provided
    5.25 +    by Oracle in the GPL Version 2 section of the License file that
    5.26 +    accompanied this code. If applicable, add the following below the
    5.27 +    License Header, with the fields enclosed by brackets [] replaced by
    5.28 +    your own identifying information:
    5.29 +    "Portions Copyrighted [year] [name of copyright owner]"
    5.30 +
    5.31 +    Contributor(s):
    5.32 +
    5.33 +    The Original Software is NetBeans. The Initial Developer of the Original
    5.34 +    Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
    5.35 +
    5.36 +    If you wish your version of this file to be governed by only the CDDL
    5.37 +    or only the GPL Version 2, indicate your decision by adding
    5.38 +    "[Contributor] elects to include this software in this distribution
    5.39 +    under the [CDDL or GPL Version 2] license." If you do not indicate a
    5.40 +    single choice of license, a recipient has the option to distribute
    5.41 +    your version of this file under either the CDDL, the GPL Version 2 or
    5.42 +    to extend the choice of license to its licensees as provided above.
    5.43 +    However, if you add GPL Version 2 code and therefore, elected the GPL
    5.44 +    Version 2 license, then the option applies only if the new code is
    5.45 +    made subject to such option by the copyright holder.
    5.46 +
    5.47 +-->
    5.48 +<!DOCTYPE html>
    5.49 +<html>
    5.50 +    <head>
    5.51 +        <title>Knockout.fx Execution Harness</title>
    5.52 +        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    5.53 +        <meta name="viewport" content="width=device-width">
    5.54 +    </head>
    5.55 +    <body>
    5.56 +        <h1>Knockout.fx Execution Harness</h1>
    5.57 +    </body>
    5.58 +    <script></script>
    5.59 +</html>