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