ko-osgi-test/src/main/java/org/netbeans/html/ko/osgi/test/KnockoutEquinoxTCKImpl.java
During the API review process (bug 246133) the reviewers decided that in order to include html4j to NetBeans Platform, we need to stop using org.apidesign namespace and switch to NetBeans one. Repackaging all SPI packages into org.netbeans.html.smthng.spi.
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
9 * The contents of this file are subject to the terms of either the GNU
10 * General Public License Version 2 only ("GPL") or the Common
11 * Development and Distribution License("CDDL") (collectively, the
12 * "License"). You may not use this file except in compliance with the
13 * License. You can obtain a copy of the License at
14 * http://www.netbeans.org/cddl-gplv2.html
15 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16 * specific language governing permissions and limitations under the
17 * License. When distributing the software, include this License Header
18 * Notice in each file and include the License file at
19 * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20 * particular file as subject to the "Classpath" exception as provided
21 * by Oracle in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the
23 * License Header, with the fields enclosed by brackets [] replaced by
24 * your own identifying information:
25 * "Portions Copyrighted [year] [name of copyright owner]"
29 * The Original Software is NetBeans. The Initial Developer of the Original
30 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
32 * If you wish your version of this file to be governed by only the CDDL
33 * or only the GPL Version 2, indicate your decision by adding
34 * "[Contributor] elects to include this software in this distribution
35 * under the [CDDL or GPL Version 2] license." If you do not indicate a
36 * single choice of license, a recipient has the option to distribute
37 * your version of this file under either the CDDL, the GPL Version 2 or
38 * to extend the choice of license to its licensees as provided above.
39 * However, if you add GPL Version 2 code and therefore, elected the GPL
40 * Version 2 license, then the option applies only if the new code is
41 * made subject to such option by the copyright holder.
43 package org.netbeans.html.ko.osgi.test;
45 import java.io.BufferedReader;
46 import java.io.IOException;
47 import java.io.InputStreamReader;
48 import java.lang.reflect.Constructor;
49 import java.lang.reflect.Method;
51 import java.net.URISyntaxException;
53 import java.net.URLConnection;
55 import java.util.concurrent.Callable;
56 import java.util.concurrent.Executor;
57 import java.util.concurrent.Executors;
58 import net.java.html.BrwsrCtx;
59 import net.java.html.boot.BrowserBuilder;
60 import net.java.html.js.JavaScriptBody;
61 import org.netbeans.html.boot.spi.Fn;
62 import org.netbeans.html.context.spi.Contexts;
63 import org.netbeans.html.json.spi.Technology;
64 import org.netbeans.html.json.spi.Transfer;
65 import org.netbeans.html.json.tck.KnockoutTCK;
66 import org.json.JSONException;
67 import org.json.JSONObject;
68 import org.openide.util.lookup.ServiceProvider;
69 import org.osgi.framework.Bundle;
70 import org.osgi.framework.BundleContext;
71 import org.osgi.framework.FrameworkUtil;
75 * @author Jaroslav Tulach
77 @ServiceProvider(service = KnockoutTCK.class)
78 public class KnockoutEquinoxTCKImpl extends KnockoutTCK implements Callable<Class[]> {
80 private static Fn.Presenter browserContext;
82 public static Class loadOSGiClass(String name, BundleContext ctx) throws Exception {
83 for (Bundle b : ctx.getBundles()) {
85 Class<?> osgiClass = b.loadClass(name);
86 if (osgiClass != null && osgiClass.getClassLoader() != ClassLoader.getSystemClassLoader()) {
89 } catch (ClassNotFoundException cnfe) {
93 throw new IllegalStateException("Cannot load " + name + " from the OSGi container!");
97 public Class[] call() throws Exception {
101 public static void start(URI server) throws Exception {
102 final BrowserBuilder bb = BrowserBuilder.newBrowser().loadClass(KnockoutEquinoxTCKImpl.class).
103 loadPage(server.toString()).
104 invoke("initialized");
106 Executors.newSingleThreadExecutor().submit(new Runnable() {
110 final ClassLoader osgiClassLoader = BrowserBuilder.class.getClassLoader();
111 bb.classloader(osgiClassLoader);
113 } catch (Throwable t) {
120 public static void initialized() throws Exception {
121 Bundle bundle = FrameworkUtil.getBundle(KnockoutEquinoxTCKImpl.class);
122 if (bundle == null) {
123 throw new IllegalStateException(
124 "Should be loaded from a bundle. But was: " + KnockoutEquinoxTCKImpl.class.getClassLoader()
127 Class<?> classpathClass = ClassLoader.getSystemClassLoader().loadClass(
128 "org.netbeans.html.ko.osgi.test.KnockoutEquinoxIT"
130 Method m = classpathClass.getMethod("initialized", Class.class, Object.class);
131 browserContext = Fn.activePresenter();
132 m.invoke(null, KnockoutEquinoxTCKImpl.class, browserContext);
136 public BrwsrCtx createContext() {
138 Class<?> fxCls = loadOSGiClass(
139 "org.netbeans.html.ko4j.FXContext",
140 FrameworkUtil.getBundle(KnockoutEquinoxTCKImpl.class).getBundleContext()
142 final Constructor<?> cnstr = fxCls.getConstructor(Fn.Presenter.class);
143 cnstr.setAccessible(true);
144 Object fx = cnstr.newInstance(browserContext);
145 Contexts.Builder cb = Contexts.newBuilder().
146 register(Technology.class, (Technology)fx, 10).
147 register(Transfer.class, (Transfer)fx, 10).
148 register(Executor.class, (Executor)browserContext, 10);
149 // if (fx.areWebSocketsSupported()) {
150 // cb.register(WSTransfer.class, fx, 10);
153 } catch (Exception ex) {
154 throw new IllegalStateException(ex);
159 public Object createJSON(Map<String, Object> values) {
160 JSONObject json = new JSONObject();
161 for (Map.Entry<String, Object> entry : values.entrySet()) {
163 json.put(entry.getKey(), entry.getValue());
164 } catch (JSONException ex) {
165 throw new IllegalStateException(ex);
172 @JavaScriptBody(args = { "s", "args" }, body = ""
173 + "var f = new Function(s); "
174 + "return f.apply(null, args);"
176 public native Object executeScript(String script, Object[] arguments);
178 @JavaScriptBody(args = { }, body =
180 + "if (!!window && !!window.location && !!window.location.href)\n"
181 + " h = window.location.href;\n"
186 private static native String findBaseURL();
189 public URI prepareURL(String content, String mimeType, String[] parameters) {
191 final URL baseURL = new URL(findBaseURL());
192 StringBuilder sb = new StringBuilder();
193 sb.append("/dynamic?mimeType=").append(mimeType);
194 for (int i = 0; i < parameters.length; i++) {
195 sb.append("¶m" + i).append("=").append(parameters[i]);
197 String mangle = content.replace("\n", "%0a")
198 .replace("\"", "\\\"").replace(" ", "%20");
199 sb.append("&content=").append(mangle);
201 URL query = new URL(baseURL, sb.toString());
202 URLConnection c = query.openConnection();
203 BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
204 URI connectTo = new URI(br.readLine());
206 } catch (IOException ex) {
207 throw new IllegalStateException(ex);
208 } catch (URISyntaxException ex) {
209 throw new IllegalStateException(ex);
214 public boolean canFailWebSocketTest() {