1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/ko-ws-tyrus/src/test/java/org/apidesign/html/wstyrus/TyrusKnockoutTest.java Sun Aug 25 14:40:16 2013 +0200
1.3 @@ -0,0 +1,185 @@
1.4 +/**
1.5 + * HTML via Java(tm) Language Bindings
1.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details. apidesign.org
1.16 + * designates this particular file as subject to the
1.17 + * "Classpath" exception as provided by apidesign.org
1.18 + * in the License file that accompanied this code.
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License
1.21 + * along with this program. Look for COPYING file in the top folder.
1.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
1.23 + */
1.24 +package org.apidesign.html.wstyrus;
1.25 +
1.26 +import java.io.BufferedReader;
1.27 +import java.io.IOException;
1.28 +import java.io.InputStreamReader;
1.29 +import java.lang.annotation.Annotation;
1.30 +import java.lang.reflect.Method;
1.31 +import java.net.URI;
1.32 +import java.net.URISyntaxException;
1.33 +import java.net.URL;
1.34 +import java.net.URLConnection;
1.35 +import java.util.ArrayList;
1.36 +import java.util.List;
1.37 +import java.util.Map;
1.38 +import java.util.concurrent.Executors;
1.39 +import net.java.html.BrwsrCtx;
1.40 +import net.java.html.boot.BrowserBuilder;
1.41 +import net.java.html.js.JavaScriptBody;
1.42 +import org.apidesign.html.context.spi.Contexts;
1.43 +import org.apidesign.html.json.spi.Technology;
1.44 +import org.apidesign.html.json.spi.Transfer;
1.45 +import org.apidesign.html.json.spi.WSTransfer;
1.46 +import org.apidesign.html.json.tck.KOTest;
1.47 +import org.apidesign.html.json.tck.KnockoutTCK;
1.48 +import org.apidesign.html.kofx.FXContext;
1.49 +import org.json.JSONException;
1.50 +import org.json.JSONObject;
1.51 +import org.openide.util.lookup.ServiceProvider;
1.52 +import org.testng.annotations.Factory;
1.53 +import static org.testng.Assert.*;
1.54 +
1.55 +/**
1.56 + *
1.57 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.58 + */
1.59 +@ServiceProvider(service = KnockoutTCK.class)
1.60 +public final class TyrusKnockoutTest extends KnockoutTCK {
1.61 + private static Class<?> browserClass;
1.62 +
1.63 + public TyrusKnockoutTest() {
1.64 + }
1.65 +
1.66 + @Factory public static Object[] compatibilityTests() throws Exception {
1.67 + Class[] arr = testClasses();
1.68 + for (int i = 0; i < arr.length; i++) {
1.69 + assertEquals(
1.70 + arr[i].getClassLoader(),
1.71 + TyrusKnockoutTest.class.getClassLoader(),
1.72 + "All classes loaded by the same classloader"
1.73 + );
1.74 + }
1.75 +
1.76 + URI uri = TyrusDynamicHTTP.initServer();
1.77 +
1.78 + final BrowserBuilder bb = BrowserBuilder.newBrowser().loadClass(TyrusKnockoutTest.class).
1.79 + loadPage(uri.toString()).
1.80 + invoke("initialized");
1.81 +
1.82 + Executors.newSingleThreadExecutor().submit(new Runnable() {
1.83 + @Override
1.84 + public void run() {
1.85 + bb.showAndWait();
1.86 + }
1.87 + });
1.88 +
1.89 + ClassLoader l = getClassLoader();
1.90 + List<Object> res = new ArrayList<Object>();
1.91 + for (int i = 0; i < arr.length; i++) {
1.92 + Class<?> c = Class.forName(arr[i].getName(), true, l);
1.93 + Class<? extends Annotation> koTest =
1.94 + c.getClassLoader().loadClass(KOTest.class.getName()).
1.95 + asSubclass(Annotation.class);
1.96 + for (Method m : c.getMethods()) {
1.97 + if (m.getAnnotation(koTest) != null) {
1.98 + res.add(new TyrusFX(m));
1.99 + }
1.100 + }
1.101 + }
1.102 + return res.toArray();
1.103 + }
1.104 +
1.105 + static synchronized ClassLoader getClassLoader() throws InterruptedException {
1.106 + while (browserClass == null) {
1.107 + TyrusKnockoutTest.class.wait();
1.108 + }
1.109 + return browserClass.getClassLoader();
1.110 + }
1.111 +
1.112 + public static synchronized void initialized(Class<?> browserCls) throws Exception {
1.113 + browserClass = browserCls;
1.114 + TyrusKnockoutTest.class.notifyAll();
1.115 + }
1.116 +
1.117 + public static void initialized() throws Exception {
1.118 + Class<?> classpathClass = ClassLoader.getSystemClassLoader().loadClass(TyrusKnockoutTest.class.getName());
1.119 + Method m = classpathClass.getMethod("initialized", Class.class);
1.120 + m.invoke(null, TyrusKnockoutTest.class);
1.121 + }
1.122 +
1.123 + @Override
1.124 + public BrwsrCtx createContext() {
1.125 + FXContext fx = new FXContext();
1.126 + TyrusContext tc = new TyrusContext();
1.127 + Contexts.Builder cb = Contexts.newBuilder().
1.128 + register(Technology.class, fx, 10).
1.129 + register(Transfer.class, fx, 10).
1.130 + register(WSTransfer.class, tc, 10);
1.131 + return cb.build();
1.132 + }
1.133 +
1.134 + @Override
1.135 + public Object createJSON(Map<String, Object> values) {
1.136 + JSONObject json = new JSONObject();
1.137 + for (Map.Entry<String, Object> entry : values.entrySet()) {
1.138 + try {
1.139 + json.put(entry.getKey(), entry.getValue());
1.140 + } catch (JSONException ex) {
1.141 + throw new IllegalStateException(ex);
1.142 + }
1.143 + }
1.144 + return json;
1.145 + }
1.146 +
1.147 + @Override
1.148 + @JavaScriptBody(args = { "s", "args" }, body = ""
1.149 + + "var f = new Function(s); "
1.150 + + "return f.apply(null, args);"
1.151 + )
1.152 + public native Object executeScript(String script, Object[] arguments);
1.153 +
1.154 + @JavaScriptBody(args = { }, body =
1.155 + "var h;"
1.156 + + "if (!!window && !!window.location && !!window.location.href)\n"
1.157 + + " h = window.location.href;\n"
1.158 + + "else "
1.159 + + " h = null;"
1.160 + + "return h;\n"
1.161 + )
1.162 + private static native String findBaseURL();
1.163 +
1.164 + @Override
1.165 + public URI prepareURL(String content, String mimeType, String[] parameters) {
1.166 + try {
1.167 + final URL baseURL = new URL(findBaseURL());
1.168 + StringBuilder sb = new StringBuilder();
1.169 + sb.append("/dynamic?mimeType=").append(mimeType);
1.170 + for (int i = 0; i < parameters.length; i++) {
1.171 + sb.append("¶m" + i).append("=").append(parameters[i]);
1.172 + }
1.173 + String mangle = content.replace("\n", "%0a")
1.174 + .replace("\"", "\\\"").replace(" ", "%20");
1.175 + sb.append("&content=").append(mangle);
1.176 +
1.177 + URL query = new URL(baseURL, sb.toString());
1.178 + URLConnection c = query.openConnection();
1.179 + BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
1.180 + URI connectTo = new URI(br.readLine());
1.181 + return connectTo;
1.182 + } catch (IOException ex) {
1.183 + throw new IllegalStateException(ex);
1.184 + } catch (URISyntaxException ex) {
1.185 + throw new IllegalStateException(ex);
1.186 + }
1.187 + }
1.188 +}