ko-bck2brwsr/src/test/java/org/apidesign/html/ko2brwsr/Bck2BrwsrKnockoutTest.java
Using HTTP protocol rather than reflection tricks as HTTP will work on all known implementations
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.ko2brwsr;
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStreamReader;
27 import java.net.URISyntaxException;
29 import java.net.URLConnection;
31 import net.java.html.BrwsrCtx;
32 import org.apidesign.bck2brwsr.core.JavaScriptBody;
33 import org.apidesign.bck2brwsr.vmtest.VMTest;
34 import org.apidesign.html.context.spi.Contexts;
35 import org.apidesign.html.json.spi.Technology;
36 import org.apidesign.html.json.spi.Transfer;
37 import org.apidesign.html.json.tck.KOTest;
38 import org.apidesign.html.json.tck.KnockoutTCK;
39 import org.openide.util.lookup.ServiceProvider;
40 import org.testng.annotations.Factory;
44 * @author Jaroslav Tulach <jtulach@netbeans.org>
46 @ServiceProvider(service = KnockoutTCK.class)
47 public final class Bck2BrwsrKnockoutTest extends KnockoutTCK {
48 @Factory public static Object[] create() {
49 return VMTest.newTests().
50 withClasses(testClasses()).
51 withLaunchers("bck2brwsr").
52 withTestAnnotation(KOTest.class).
57 public BrwsrCtx createContext() {
58 return Contexts.newBuilder().
59 register(Transfer.class, BrwsrCtxImpl.DEFAULT, 9).
60 register(Technology.class, BrwsrCtxImpl.DEFAULT, 9).build();
66 public Object createJSON(Map<String, Object> values) {
67 Object json = createJSON();
69 for (Map.Entry<String, Object> entry : values.entrySet()) {
70 putValue(json, entry.getKey(), entry.getValue());
75 @JavaScriptBody(args = {}, body = "return new Object();")
76 private static native Object createJSON();
78 @JavaScriptBody(args = { "json", "key", "value" }, body = "json[key] = value;")
79 private static native void putValue(Object json, String key, Object value);
82 public Object executeScript(String script, Object[] arguments) {
83 return execScript(script, arguments);
86 @JavaScriptBody(args = { "s", "args" }, body =
87 "var f = new Function(s); return f.apply(null, args);"
89 private static native Object execScript(String s, Object[] arguments);
91 @JavaScriptBody(args = { }, body =
93 + "if (!!window && !!window.location && !!window.location.href)\n"
94 + " h = window.location.href;\n"
99 private static native String findBaseURL();
102 public URI prepareURL(String content, String mimeType, String[] parameters) {
104 final URL baseURL = new URL(findBaseURL());
105 StringBuilder sb = new StringBuilder();
106 sb.append("/dynamic?mimeType=").append(mimeType);
107 for (int i = 0; i < parameters.length; i++) {
108 sb.append("¶m" + i).append("=").append(parameters[i]);
110 String mangle = content.replace("\n", "%0a")
111 .replace("\"", "\\\"").replace(" ", "%20");
112 sb.append("&content=").append(mangle);
114 URL query = new URL(baseURL, sb.toString());
115 String uri = (String) query.getContent(new Class[] { String.class });
116 URI connectTo = new URI(uri.trim());
118 } catch (IOException ex) {
119 throw new IllegalStateException(ex);
120 } catch (URISyntaxException ex) {
121 throw new IllegalStateException(ex);