2 * HTML via Java(tm) Language Bindings
3 * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. apidesign.org
13 * designates this particular file as subject to the
14 * "Classpath" exception as provided by apidesign.org
15 * in the License file that accompanied this code.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. Look for COPYING file in the top folder.
19 * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
21 package org.apidesign.html.wstyrus;
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStreamReader;
26 import java.lang.annotation.Annotation;
27 import java.lang.reflect.Method;
29 import java.net.URISyntaxException;
31 import java.net.URLConnection;
32 import java.util.ArrayList;
33 import java.util.List;
35 import java.util.concurrent.Executors;
36 import net.java.html.BrwsrCtx;
37 import net.java.html.boot.BrowserBuilder;
38 import net.java.html.js.JavaScriptBody;
39 import org.apidesign.html.context.spi.Contexts;
40 import org.apidesign.html.json.spi.Technology;
41 import org.apidesign.html.json.spi.Transfer;
42 import org.apidesign.html.json.spi.WSTransfer;
43 import org.apidesign.html.json.tck.KOTest;
44 import org.apidesign.html.json.tck.KnockoutTCK;
45 import org.apidesign.html.kofx.FXContext;
46 import org.json.JSONException;
47 import org.json.JSONObject;
48 import org.openide.util.lookup.ServiceProvider;
49 import org.testng.annotations.Factory;
50 import static org.testng.Assert.*;
54 * @author Jaroslav Tulach <jtulach@netbeans.org>
56 @ServiceProvider(service = KnockoutTCK.class)
57 public final class TyrusKnockoutTest extends KnockoutTCK {
58 private static Class<?> browserClass;
60 public TyrusKnockoutTest() {
63 @Factory public static Object[] compatibilityTests() throws Exception {
64 Class[] arr = testClasses();
65 for (int i = 0; i < arr.length; i++) {
67 arr[i].getClassLoader(),
68 TyrusKnockoutTest.class.getClassLoader(),
69 "All classes loaded by the same classloader"
73 URI uri = TyrusDynamicHTTP.initServer();
75 final BrowserBuilder bb = BrowserBuilder.newBrowser().loadClass(TyrusKnockoutTest.class).
76 loadPage(uri.toString()).
77 invoke("initialized");
79 Executors.newSingleThreadExecutor().submit(new Runnable() {
86 ClassLoader l = getClassLoader();
87 List<Object> res = new ArrayList<Object>();
88 for (int i = 0; i < arr.length; i++) {
89 Class<?> c = Class.forName(arr[i].getName(), true, l);
90 Class<? extends Annotation> koTest =
91 c.getClassLoader().loadClass(KOTest.class.getName()).
92 asSubclass(Annotation.class);
93 for (Method m : c.getMethods()) {
94 if (m.getAnnotation(koTest) != null) {
95 res.add(new TyrusFX(m));
102 static synchronized ClassLoader getClassLoader() throws InterruptedException {
103 while (browserClass == null) {
104 TyrusKnockoutTest.class.wait();
106 return browserClass.getClassLoader();
109 public static synchronized void initialized(Class<?> browserCls) throws Exception {
110 browserClass = browserCls;
111 TyrusKnockoutTest.class.notifyAll();
114 public static void initialized() throws Exception {
115 Class<?> classpathClass = ClassLoader.getSystemClassLoader().loadClass(TyrusKnockoutTest.class.getName());
116 Method m = classpathClass.getMethod("initialized", Class.class);
117 m.invoke(null, TyrusKnockoutTest.class);
121 public BrwsrCtx createContext() {
122 FXContext fx = new FXContext();
123 TyrusContext tc = new TyrusContext();
124 Contexts.Builder cb = Contexts.newBuilder().
125 register(Technology.class, fx, 10).
126 register(Transfer.class, fx, 10).
127 register(WSTransfer.class, tc, 10);
132 public Object createJSON(Map<String, Object> values) {
133 JSONObject json = new JSONObject();
134 for (Map.Entry<String, Object> entry : values.entrySet()) {
136 json.put(entry.getKey(), entry.getValue());
137 } catch (JSONException ex) {
138 throw new IllegalStateException(ex);
145 @JavaScriptBody(args = { "s", "args" }, body = ""
146 + "var f = new Function(s); "
147 + "return f.apply(null, args);"
149 public native Object executeScript(String script, Object[] arguments);
151 @JavaScriptBody(args = { }, body =
153 + "if (!!window && !!window.location && !!window.location.href)\n"
154 + " h = window.location.href;\n"
159 private static native String findBaseURL();
162 public URI prepareURL(String content, String mimeType, String[] parameters) {
164 final URL baseURL = new URL(findBaseURL());
165 StringBuilder sb = new StringBuilder();
166 sb.append("/dynamic?mimeType=").append(mimeType);
167 for (int i = 0; i < parameters.length; i++) {
168 sb.append("¶m" + i).append("=").append(parameters[i]);
170 String mangle = content.replace("\n", "%0a")
171 .replace("\"", "\\\"").replace(" ", "%20");
172 sb.append("&content=").append(mangle);
174 URL query = new URL(baseURL, sb.toString());
175 URLConnection c = query.openConnection();
176 BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
177 URI connectTo = new URI(br.readLine());
179 } catch (IOException ex) {
180 throw new IllegalStateException(ex);
181 } catch (URISyntaxException ex) {
182 throw new IllegalStateException(ex);