ko-felix-test/src/main/java/org/netbeans/html/ko/felix/test/KnockoutFelixTCKImpl.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/ko-felix-test/src/main/java/org/netbeans/html/ko/felix/test/KnockoutFelixTCKImpl.java Thu Jul 31 14:36:00 2014 +0200
1.3 @@ -0,0 +1,279 @@
1.4 +/**
1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 + *
1.7 + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
1.8 + *
1.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
1.10 + * Other names may be trademarks of their respective owners.
1.11 + *
1.12 + * The contents of this file are subject to the terms of either the GNU
1.13 + * General Public License Version 2 only ("GPL") or the Common
1.14 + * Development and Distribution License("CDDL") (collectively, the
1.15 + * "License"). You may not use this file except in compliance with the
1.16 + * License. You can obtain a copy of the License at
1.17 + * http://www.netbeans.org/cddl-gplv2.html
1.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.19 + * specific language governing permissions and limitations under the
1.20 + * License. When distributing the software, include this License Header
1.21 + * Notice in each file and include the License file at
1.22 + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
1.23 + * particular file as subject to the "Classpath" exception as provided
1.24 + * by Oracle in the GPL Version 2 section of the License file that
1.25 + * accompanied this code. If applicable, add the following below the
1.26 + * License Header, with the fields enclosed by brackets [] replaced by
1.27 + * your own identifying information:
1.28 + * "Portions Copyrighted [year] [name of copyright owner]"
1.29 + *
1.30 + * Contributor(s):
1.31 + *
1.32 + * The Original Software is NetBeans. The Initial Developer of the Original
1.33 + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
1.34 + *
1.35 + * If you wish your version of this file to be governed by only the CDDL
1.36 + * or only the GPL Version 2, indicate your decision by adding
1.37 + * "[Contributor] elects to include this software in this distribution
1.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
1.39 + * single choice of license, a recipient has the option to distribute
1.40 + * your version of this file under either the CDDL, the GPL Version 2 or
1.41 + * to extend the choice of license to its licensees as provided above.
1.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
1.43 + * Version 2 license, then the option applies only if the new code is
1.44 + * made subject to such option by the copyright holder.
1.45 + */
1.46 +package org.netbeans.html.ko.felix.test;
1.47 +
1.48 +import java.io.BufferedReader;
1.49 +import java.io.IOException;
1.50 +import java.io.InputStreamReader;
1.51 +import java.lang.reflect.Constructor;
1.52 +import java.lang.reflect.Method;
1.53 +import java.net.URI;
1.54 +import java.net.URISyntaxException;
1.55 +import java.net.URL;
1.56 +import java.net.URLConnection;
1.57 +import java.util.ArrayList;
1.58 +import java.util.Collections;
1.59 +import java.util.Enumeration;
1.60 +import java.util.List;
1.61 +import java.util.Map;
1.62 +import java.util.concurrent.Callable;
1.63 +import java.util.concurrent.Executor;
1.64 +import java.util.concurrent.Executors;
1.65 +import net.java.html.BrwsrCtx;
1.66 +import net.java.html.boot.BrowserBuilder;
1.67 +import net.java.html.js.JavaScriptBody;
1.68 +import org.apidesign.html.boot.spi.Fn;
1.69 +import org.apidesign.html.context.spi.Contexts;
1.70 +import org.apidesign.html.json.spi.Technology;
1.71 +import org.apidesign.html.json.spi.Transfer;
1.72 +import org.apidesign.html.json.tck.KnockoutTCK;
1.73 +import org.json.JSONException;
1.74 +import org.json.JSONObject;
1.75 +import org.openide.util.lookup.ServiceProvider;
1.76 +import org.osgi.framework.Bundle;
1.77 +import org.osgi.framework.BundleContext;
1.78 +import org.osgi.framework.FrameworkUtil;
1.79 +
1.80 +/**
1.81 + *
1.82 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.83 + */
1.84 +@ServiceProvider(service = KnockoutTCK.class)
1.85 +public class KnockoutFelixTCKImpl extends KnockoutTCK implements Callable<Class[]> {
1.86 +
1.87 + private static Fn.Presenter browserContext;
1.88 +
1.89 + public static Class loadOSGiClass(String name, BundleContext ctx) throws Exception {
1.90 + for (Bundle b : ctx.getBundles()) {
1.91 + try {
1.92 + Class<?> osgiClass = b.loadClass(name);
1.93 + if (osgiClass != null && osgiClass.getClassLoader() != ClassLoader.getSystemClassLoader()) {
1.94 + return osgiClass;
1.95 + }
1.96 + } catch (ClassNotFoundException cnfe) {
1.97 + // go on
1.98 + }
1.99 + }
1.100 + throw new IllegalStateException("Cannot load " + name + " from the OSGi container!");
1.101 + }
1.102 +
1.103 + @Override
1.104 + public Class[] call() throws Exception {
1.105 + return testClasses();
1.106 + }
1.107 +
1.108 + public static void start(URI server) throws Exception {
1.109 + final BrowserBuilder bb = BrowserBuilder.newBrowser().loadClass(KnockoutFelixTCKImpl.class).
1.110 + loadPage(server.toString()).
1.111 + invoke("initialized");
1.112 +
1.113 + Executors.newSingleThreadExecutor().submit(new Runnable() {
1.114 + @Override
1.115 + public void run() {
1.116 + try {
1.117 + Bundle[] arr = FrameworkUtil.getBundle(BrowserBuilder.class).getBundleContext().getBundles();
1.118 + final ClassLoader osgiClassLoader = new AllBundlesLoader(arr);
1.119 + bb.classloader(osgiClassLoader);
1.120 + bb.showAndWait();
1.121 + } catch (Throwable t) {
1.122 + t.printStackTrace();
1.123 + }
1.124 + }
1.125 + });
1.126 + }
1.127 +
1.128 + public static void initialized() throws Exception {
1.129 + Bundle bundle = FrameworkUtil.getBundle(KnockoutFelixTCKImpl.class);
1.130 + if (bundle == null) {
1.131 + throw new IllegalStateException(
1.132 + "Should be loaded from a bundle. But was: " + KnockoutFelixTCKImpl.class.getClassLoader()
1.133 + );
1.134 + }
1.135 + Class<?> classpathClass = ClassLoader.getSystemClassLoader().loadClass(
1.136 + "org.netbeans.html.ko.felix.test.KnockoutFelixIT"
1.137 + );
1.138 + Method m = classpathClass.getMethod("initialized", Class.class, Object.class);
1.139 + browserContext = Fn.activePresenter();
1.140 + m.invoke(null, KnockoutFelixTCKImpl.class, browserContext);
1.141 + }
1.142 +
1.143 + @Override
1.144 + public BrwsrCtx createContext() {
1.145 + try {
1.146 + Class<?> fxCls = loadOSGiClass(
1.147 + "org.netbeans.html.ko4j.FXContext",
1.148 + FrameworkUtil.getBundle(KnockoutFelixTCKImpl.class).getBundleContext()
1.149 + );
1.150 + final Constructor<?> cnstr = fxCls.getConstructor(Fn.Presenter.class);
1.151 + cnstr.setAccessible(true);
1.152 + Object fx = cnstr.newInstance(browserContext);
1.153 + Contexts.Builder cb = Contexts.newBuilder().
1.154 + register(Technology.class, (Technology)fx, 10).
1.155 + register(Transfer.class, (Transfer)fx, 10).
1.156 + register(Executor.class, (Executor)browserContext, 10);
1.157 +// if (fx.areWebSocketsSupported()) {
1.158 +// cb.register(WSTransfer.class, fx, 10);
1.159 +// }
1.160 + return cb.build();
1.161 + } catch (Exception ex) {
1.162 + throw new IllegalStateException(ex);
1.163 + }
1.164 + }
1.165 +
1.166 + @Override
1.167 + public Object createJSON(Map<String, Object> values) {
1.168 + JSONObject json = new JSONObject();
1.169 + for (Map.Entry<String, Object> entry : values.entrySet()) {
1.170 + try {
1.171 + json.put(entry.getKey(), entry.getValue());
1.172 + } catch (JSONException ex) {
1.173 + throw new IllegalStateException(ex);
1.174 + }
1.175 + }
1.176 + return json;
1.177 + }
1.178 +
1.179 + @Override
1.180 + @JavaScriptBody(args = { "s", "args" }, body = ""
1.181 + + "var f = new Function(s); "
1.182 + + "return f.apply(null, args);"
1.183 + )
1.184 + public native Object executeScript(String script, Object[] arguments);
1.185 +
1.186 + @JavaScriptBody(args = { }, body =
1.187 + "var h;"
1.188 + + "if (!!window && !!window.location && !!window.location.href)\n"
1.189 + + " h = window.location.href;\n"
1.190 + + "else "
1.191 + + " h = null;"
1.192 + + "return h;\n"
1.193 + )
1.194 + private static native String findBaseURL();
1.195 +
1.196 + @Override
1.197 + public URI prepareURL(String content, String mimeType, String[] parameters) {
1.198 + try {
1.199 + final URL baseURL = new URL(findBaseURL());
1.200 + StringBuilder sb = new StringBuilder();
1.201 + sb.append("/dynamic?mimeType=").append(mimeType);
1.202 + for (int i = 0; i < parameters.length; i++) {
1.203 + sb.append("¶m" + i).append("=").append(parameters[i]);
1.204 + }
1.205 + String mangle = content.replace("\n", "%0a")
1.206 + .replace("\"", "\\\"").replace(" ", "%20");
1.207 + sb.append("&content=").append(mangle);
1.208 +
1.209 + URL query = new URL(baseURL, sb.toString());
1.210 + URLConnection c = query.openConnection();
1.211 + BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
1.212 + URI connectTo = new URI(br.readLine());
1.213 + return connectTo;
1.214 + } catch (IOException ex) {
1.215 + throw new IllegalStateException(ex);
1.216 + } catch (URISyntaxException ex) {
1.217 + throw new IllegalStateException(ex);
1.218 + }
1.219 + }
1.220 +
1.221 + @Override
1.222 + public boolean canFailWebSocketTest() {
1.223 + return true;
1.224 + }
1.225 +
1.226 + private static final class AllBundlesLoader extends ClassLoader {
1.227 + private final Bundle[] arr;
1.228 +
1.229 + public AllBundlesLoader(Bundle[] arr) {
1.230 + super(ClassLoader.getSystemClassLoader().getParent());
1.231 + this.arr = arr;
1.232 + }
1.233 +
1.234 + @Override
1.235 + public Class<?> loadClass(String name) throws ClassNotFoundException {
1.236 + return loadClass(name, false);
1.237 + }
1.238 +
1.239 + @Override
1.240 + protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
1.241 + ClassNotFoundException err = null;
1.242 + for (Bundle b : arr) {
1.243 + try {
1.244 + Class<?> cls = b.loadClass(name);
1.245 + if (FrameworkUtil.getBundle(cls) == b) {
1.246 + return cls;
1.247 + }
1.248 + } catch (ClassNotFoundException ex) {
1.249 + err = ex;
1.250 + }
1.251 + }
1.252 + throw err;
1.253 + }
1.254 +
1.255 + @Override
1.256 + protected URL findResource(String name) {
1.257 + for (Bundle b : arr) {
1.258 + URL r = b.getResource(name);
1.259 + if (r != null) {
1.260 + return r;
1.261 + }
1.262 + }
1.263 + return null;
1.264 + }
1.265 +
1.266 + @Override
1.267 + protected Enumeration<URL> findResources(String name) throws IOException {
1.268 + List<URL> ret = new ArrayList<URL>();
1.269 + for (Bundle b : arr) {
1.270 + Enumeration<URL> en = b.getResources(name);
1.271 + if (en != null) while (en.hasMoreElements()) {
1.272 + URL u = en.nextElement();
1.273 + ret.add(u);
1.274 + }
1.275 + }
1.276 + return Collections.enumeration(ret);
1.277 + }
1.278 +
1.279 +
1.280 +
1.281 + }
1.282 +}