jaroslav@35
|
1 |
/**
|
jaroslav@358
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
jaroslav@35
|
3 |
*
|
jaroslav@551
|
4 |
* Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
|
jaroslav@35
|
5 |
*
|
jaroslav@358
|
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
|
jaroslav@358
|
7 |
* Other names may be trademarks of their respective owners.
|
jaroslav@35
|
8 |
*
|
jaroslav@358
|
9 |
* The contents of this file are subject to the terms of either the GNU
|
jaroslav@358
|
10 |
* General Public License Version 2 only ("GPL") or the Common
|
jaroslav@358
|
11 |
* Development and Distribution License("CDDL") (collectively, the
|
jaroslav@358
|
12 |
* "License"). You may not use this file except in compliance with the
|
jaroslav@358
|
13 |
* License. You can obtain a copy of the License at
|
jaroslav@358
|
14 |
* http://www.netbeans.org/cddl-gplv2.html
|
jaroslav@358
|
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
jaroslav@358
|
16 |
* specific language governing permissions and limitations under the
|
jaroslav@358
|
17 |
* License. When distributing the software, include this License Header
|
jaroslav@358
|
18 |
* Notice in each file and include the License file at
|
jaroslav@358
|
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
|
jaroslav@358
|
20 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@358
|
21 |
* by Oracle in the GPL Version 2 section of the License file that
|
jaroslav@358
|
22 |
* accompanied this code. If applicable, add the following below the
|
jaroslav@358
|
23 |
* License Header, with the fields enclosed by brackets [] replaced by
|
jaroslav@358
|
24 |
* your own identifying information:
|
jaroslav@358
|
25 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
jaroslav@358
|
26 |
*
|
jaroslav@358
|
27 |
* Contributor(s):
|
jaroslav@358
|
28 |
*
|
jaroslav@358
|
29 |
* The Original Software is NetBeans. The Initial Developer of the Original
|
jaroslav@551
|
30 |
* Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
|
jaroslav@358
|
31 |
*
|
jaroslav@358
|
32 |
* If you wish your version of this file to be governed by only the CDDL
|
jaroslav@358
|
33 |
* or only the GPL Version 2, indicate your decision by adding
|
jaroslav@358
|
34 |
* "[Contributor] elects to include this software in this distribution
|
jaroslav@358
|
35 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a
|
jaroslav@358
|
36 |
* single choice of license, a recipient has the option to distribute
|
jaroslav@358
|
37 |
* your version of this file under either the CDDL, the GPL Version 2 or
|
jaroslav@358
|
38 |
* to extend the choice of license to its licensees as provided above.
|
jaroslav@358
|
39 |
* However, if you add GPL Version 2 code and therefore, elected the GPL
|
jaroslav@358
|
40 |
* Version 2 license, then the option applies only if the new code is
|
jaroslav@358
|
41 |
* made subject to such option by the copyright holder.
|
jaroslav@35
|
42 |
*/
|
jaroslav@35
|
43 |
package net.java.html.json.tests;
|
jaroslav@35
|
44 |
|
jaroslav@138
|
45 |
import java.net.URI;
|
jaroslav@393
|
46 |
import java.util.Collections;
|
jaroslav@35
|
47 |
import java.util.Map;
|
jaroslav@35
|
48 |
import java.util.ServiceLoader;
|
jaroslav@110
|
49 |
import net.java.html.BrwsrCtx;
|
jaroslav@35
|
50 |
import org.apidesign.html.json.tck.KnockoutTCK;
|
jaroslav@35
|
51 |
|
jaroslav@35
|
52 |
/**
|
jaroslav@35
|
53 |
*
|
jaroslav@35
|
54 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@35
|
55 |
*/
|
jaroslav@393
|
56 |
public final class Utils {
|
jaroslav@393
|
57 |
private static KnockoutTCK instantiatedTCK;
|
jaroslav@393
|
58 |
|
jaroslav@35
|
59 |
private Utils() {
|
jaroslav@35
|
60 |
}
|
jaroslav@393
|
61 |
|
jaroslav@393
|
62 |
public static void registerTCK(KnockoutTCK tck) {
|
jaroslav@393
|
63 |
instantiatedTCK = tck;
|
jaroslav@393
|
64 |
}
|
jaroslav@35
|
65 |
|
jaroslav@121
|
66 |
static BrwsrCtx newContext(Class<?> clazz) {
|
jaroslav@393
|
67 |
for (KnockoutTCK tck : tcks(clazz)) {
|
jaroslav@110
|
68 |
BrwsrCtx c = tck.createContext();
|
jaroslav@35
|
69 |
if (c != null) {
|
jaroslav@35
|
70 |
return c;
|
jaroslav@35
|
71 |
}
|
jaroslav@35
|
72 |
}
|
jaroslav@35
|
73 |
throw new AssertionError("Can't find appropriate Context in ServiceLoader!");
|
jaroslav@35
|
74 |
}
|
jaroslav@121
|
75 |
static Object createObject(Map<String,Object> values, Class<?> clazz) {
|
jaroslav@393
|
76 |
for (KnockoutTCK tck : tcks(clazz)) {
|
jaroslav@35
|
77 |
Object o = tck.createJSON(values);
|
jaroslav@35
|
78 |
if (o != null) {
|
jaroslav@35
|
79 |
return o;
|
jaroslav@35
|
80 |
}
|
jaroslav@35
|
81 |
}
|
jaroslav@35
|
82 |
throw new AssertionError("Can't find appropriate Context in ServiceLoader!");
|
jaroslav@35
|
83 |
}
|
jaroslav@121
|
84 |
static Object executeScript(Class<?> clazz,
|
jaroslav@121
|
85 |
String script, Object... arguments
|
jaroslav@121
|
86 |
) throws Exception {
|
jaroslav@393
|
87 |
for (KnockoutTCK tck : tcks(clazz)) {
|
jaroslav@36
|
88 |
return tck.executeScript(script, arguments);
|
jaroslav@36
|
89 |
}
|
jaroslav@36
|
90 |
throw new AssertionError("Can't find appropriate Context in ServiceLoader!");
|
jaroslav@36
|
91 |
}
|
jaroslav@393
|
92 |
|
jaroslav@393
|
93 |
private static Iterable<KnockoutTCK> tcks(Class<?> clazz) {
|
jaroslav@393
|
94 |
if (instantiatedTCK != null) {
|
jaroslav@393
|
95 |
return Collections.singleton(instantiatedTCK);
|
jaroslav@393
|
96 |
}
|
jaroslav@393
|
97 |
return ServiceLoader.load(KnockoutTCK.class, cl(clazz));
|
jaroslav@393
|
98 |
}
|
jaroslav@35
|
99 |
|
jaroslav@137
|
100 |
static Object exposeHTML(Class<?> clazz, String html) throws Exception {
|
jaroslav@137
|
101 |
String s =
|
jaroslav@137
|
102 |
"var n = window.document.getElementById('ko.test.div'); \n "
|
jaroslav@137
|
103 |
+ "if (!n) { \n"
|
jaroslav@137
|
104 |
+ " n = window.document.createElement('div'); \n "
|
jaroslav@137
|
105 |
+ " n.id = 'ko.test.div'; \n "
|
jaroslav@137
|
106 |
+ " var body = window.document.getElementsByTagName('body')[0];\n"
|
jaroslav@142
|
107 |
+ " body.appendChild(n);\n"
|
jaroslav@137
|
108 |
+ "}\n"
|
jaroslav@137
|
109 |
+ "n.innerHTML = arguments[0]; \n ";
|
jaroslav@137
|
110 |
return executeScript(clazz, s, html);
|
jaroslav@137
|
111 |
}
|
jaroslav@138
|
112 |
|
jaroslav@138
|
113 |
static String prepareURL(
|
jaroslav@138
|
114 |
Class<?> clazz, String content, String mimeType, String... parameters) {
|
jaroslav@393
|
115 |
for (KnockoutTCK tck : tcks(clazz)) {
|
jaroslav@138
|
116 |
URI o = tck.prepareURL(content, mimeType, parameters);
|
jaroslav@138
|
117 |
if (o != null) {
|
jaroslav@138
|
118 |
return o.toString();
|
jaroslav@138
|
119 |
}
|
jaroslav@138
|
120 |
}
|
jaroslav@138
|
121 |
throw new IllegalStateException();
|
jaroslav@138
|
122 |
}
|
jaroslav@260
|
123 |
|
jaroslav@260
|
124 |
static boolean canFailWebSockets(
|
jaroslav@260
|
125 |
Class<?> clazz) {
|
jaroslav@393
|
126 |
for (KnockoutTCK tck : tcks(clazz)) {
|
jaroslav@260
|
127 |
if (tck.canFailWebSocketTest()) {
|
jaroslav@260
|
128 |
return true;
|
jaroslav@260
|
129 |
}
|
jaroslav@260
|
130 |
}
|
jaroslav@260
|
131 |
return false;
|
jaroslav@260
|
132 |
}
|
jaroslav@137
|
133 |
|
jaroslav@121
|
134 |
private static ClassLoader cl(Class<?> c) {
|
jaroslav@121
|
135 |
try {
|
jaroslav@121
|
136 |
return c.getClassLoader();
|
jaroslav@121
|
137 |
} catch (SecurityException ex) {
|
jaroslav@121
|
138 |
return null;
|
jaroslav@121
|
139 |
}
|
jaroslav@121
|
140 |
}
|
jaroslav@35
|
141 |
}
|