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.boot.impl.FnContext;
40 import org.apidesign.html.boot.impl.FnUtils;
41 import org.apidesign.html.boot.spi.Fn;
42 import org.apidesign.html.context.spi.Contexts;
43 import org.apidesign.html.json.spi.Technology;
44 import org.apidesign.html.json.spi.Transfer;
45 import org.apidesign.html.json.spi.WSTransfer;
46 import org.apidesign.html.json.tck.KOTest;
47 import org.apidesign.html.json.tck.KnockoutTCK;
48 import org.apidesign.html.kofx.FXContext;
49 import org.json.JSONException;
50 import org.json.JSONObject;
51 import org.openide.util.lookup.ServiceProvider;
52 import org.testng.annotations.Factory;
53 import static org.testng.Assert.*;
57 * @author Jaroslav Tulach <jtulach@netbeans.org>
59 @ServiceProvider(service = KnockoutTCK.class)
60 public final class TyrusKnockoutTest extends KnockoutTCK {
61 private static Class<?> browserClass;
62 private static Fn.Presenter browserContext;
64 public TyrusKnockoutTest() {
67 @Factory public static Object[] compatibilityTests() throws Exception {
68 Class[] arr = testClasses();
69 for (int i = 0; i < arr.length; i++) {
71 arr[i].getClassLoader(),
72 TyrusKnockoutTest.class.getClassLoader(),
73 "All classes loaded by the same classloader"
77 URI uri = TyrusDynamicHTTP.initServer();
79 final BrowserBuilder bb = BrowserBuilder.newBrowser().loadClass(TyrusKnockoutTest.class).
80 loadPage(uri.toString()).
81 invoke("initialized");
83 Executors.newSingleThreadExecutor().submit(new Runnable() {
90 ClassLoader l = getClassLoader();
91 List<Object> res = new ArrayList<Object>();
92 for (int i = 0; i < arr.length; i++) {
93 Class<?> c = Class.forName(arr[i].getName(), true, l);
94 Class<? extends Annotation> koTest =
95 c.getClassLoader().loadClass(KOTest.class.getName()).
96 asSubclass(Annotation.class);
97 for (Method m : c.getMethods()) {
98 if (m.getAnnotation(koTest) != null) {
99 res.add(new TyrusFX(browserContext, m));
103 return res.toArray();
106 static synchronized ClassLoader getClassLoader() throws InterruptedException {
107 while (browserClass == null) {
108 TyrusKnockoutTest.class.wait();
110 return browserClass.getClassLoader();
113 public static synchronized void initialized(Class<?> browserCls) throws Exception {
114 browserClass = browserCls;
115 browserContext = Fn.activePresenter();
116 TyrusKnockoutTest.class.notifyAll();
119 public static void initialized() throws Exception {
120 Class<?> classpathClass = ClassLoader.getSystemClassLoader().loadClass(TyrusKnockoutTest.class.getName());
121 Method m = classpathClass.getMethod("initialized", Class.class);
122 m.invoke(null, TyrusKnockoutTest.class);
123 browserContext = Fn.activePresenter();
127 public BrwsrCtx createContext() {
128 FXContext fx = new FXContext(browserContext);
129 TyrusContext tc = new TyrusContext();
130 Contexts.Builder cb = Contexts.newBuilder().
131 register(Technology.class, fx, 10).
132 register(Transfer.class, fx, 10).
133 register(WSTransfer.class, tc, 10);
138 public Object createJSON(Map<String, Object> values) {
139 JSONObject json = new JSONObject();
140 for (Map.Entry<String, Object> entry : values.entrySet()) {
142 json.put(entry.getKey(), entry.getValue());
143 } catch (JSONException ex) {
144 throw new IllegalStateException(ex);
151 @JavaScriptBody(args = { "s", "args" }, body = ""
152 + "var f = new Function(s); "
153 + "return f.apply(null, args);"
155 public native Object executeScript(String script, Object[] arguments);
157 @JavaScriptBody(args = { }, body =
159 + "if (!!window && !!window.location && !!window.location.href)\n"
160 + " h = window.location.href;\n"
165 private static native String findBaseURL();
168 public URI prepareURL(String content, String mimeType, String[] parameters) {
170 final URL baseURL = new URL(findBaseURL());
171 StringBuilder sb = new StringBuilder();
172 sb.append("/dynamic?mimeType=").append(mimeType);
173 for (int i = 0; i < parameters.length; i++) {
174 sb.append("¶m" + i).append("=").append(parameters[i]);
176 String mangle = content.replace("\n", "%0a")
177 .replace("\"", "\\\"").replace(" ", "%20");
178 sb.append("&content=").append(mangle);
180 URL query = new URL(baseURL, sb.toString());
181 URLConnection c = query.openConnection();
182 BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
183 URI connectTo = new URI(br.readLine());
185 } catch (IOException ex) {
186 throw new IllegalStateException(ex);
187 } catch (URISyntaxException ex) {
188 throw new IllegalStateException(ex);