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;
|
jtulach@838
|
50 |
import org.netbeans.html.json.tck.KnockoutTCK;
|
jaroslav@35
|
51 |
|
jaroslav@35
|
52 |
/**
|
jaroslav@35
|
53 |
*
|
jtulach@790
|
54 |
* @author Jaroslav Tulach
|
jaroslav@35
|
55 |
*/
|
jaroslav@393
|
56 |
public final class Utils {
|
jaroslav@393
|
57 |
private static KnockoutTCK instantiatedTCK;
|
jtulach@681
|
58 |
|
jaroslav@717
|
59 |
static boolean skipIfNoFullJDK() {
|
jaroslav@717
|
60 |
try {
|
jaroslav@717
|
61 |
Class<?> thread = Class.forName("java.lang.Thread");
|
jaroslav@717
|
62 |
Thread t = new Thread("Empty");
|
jaroslav@717
|
63 |
t.setName("Different");
|
jaroslav@717
|
64 |
t.setDaemon(false);
|
jaroslav@717
|
65 |
t.interrupt();
|
jaroslav@717
|
66 |
t.start();
|
jaroslav@717
|
67 |
} catch (ClassNotFoundException ex) {
|
jaroslav@717
|
68 |
return true;
|
jaroslav@717
|
69 |
} catch (SecurityException ex) {
|
jaroslav@717
|
70 |
return true;
|
jaroslav@717
|
71 |
}
|
jaroslav@717
|
72 |
return false;
|
jaroslav@717
|
73 |
}
|
jaroslav@717
|
74 |
|
jaroslav@35
|
75 |
private Utils() {
|
jaroslav@35
|
76 |
}
|
jaroslav@393
|
77 |
|
jaroslav@393
|
78 |
public static void registerTCK(KnockoutTCK tck) {
|
jaroslav@393
|
79 |
instantiatedTCK = tck;
|
jaroslav@393
|
80 |
}
|
jaroslav@35
|
81 |
|
jaroslav@121
|
82 |
static BrwsrCtx newContext(Class<?> clazz) {
|
jaroslav@393
|
83 |
for (KnockoutTCK tck : tcks(clazz)) {
|
jaroslav@110
|
84 |
BrwsrCtx c = tck.createContext();
|
jaroslav@35
|
85 |
if (c != null) {
|
jaroslav@35
|
86 |
return c;
|
jaroslav@35
|
87 |
}
|
jaroslav@35
|
88 |
}
|
jaroslav@35
|
89 |
throw new AssertionError("Can't find appropriate Context in ServiceLoader!");
|
jaroslav@35
|
90 |
}
|
jaroslav@121
|
91 |
static Object createObject(Map<String,Object> values, Class<?> clazz) {
|
jaroslav@393
|
92 |
for (KnockoutTCK tck : tcks(clazz)) {
|
jaroslav@35
|
93 |
Object o = tck.createJSON(values);
|
jaroslav@35
|
94 |
if (o != null) {
|
jaroslav@35
|
95 |
return o;
|
jaroslav@35
|
96 |
}
|
jaroslav@35
|
97 |
}
|
jaroslav@35
|
98 |
throw new AssertionError("Can't find appropriate Context in ServiceLoader!");
|
jaroslav@35
|
99 |
}
|
jaroslav@121
|
100 |
static Object executeScript(Class<?> clazz,
|
jaroslav@121
|
101 |
String script, Object... arguments
|
jaroslav@121
|
102 |
) throws Exception {
|
jaroslav@393
|
103 |
for (KnockoutTCK tck : tcks(clazz)) {
|
jaroslav@36
|
104 |
return tck.executeScript(script, arguments);
|
jaroslav@36
|
105 |
}
|
jaroslav@36
|
106 |
throw new AssertionError("Can't find appropriate Context in ServiceLoader!");
|
jaroslav@36
|
107 |
}
|
jaroslav@393
|
108 |
|
jaroslav@393
|
109 |
private static Iterable<KnockoutTCK> tcks(Class<?> clazz) {
|
jaroslav@393
|
110 |
if (instantiatedTCK != null) {
|
jaroslav@393
|
111 |
return Collections.singleton(instantiatedTCK);
|
jaroslav@393
|
112 |
}
|
jaroslav@393
|
113 |
return ServiceLoader.load(KnockoutTCK.class, cl(clazz));
|
jaroslav@393
|
114 |
}
|
jaroslav@35
|
115 |
|
jaroslav@137
|
116 |
static Object exposeHTML(Class<?> clazz, String html) throws Exception {
|
jaroslav@137
|
117 |
String s =
|
jaroslav@137
|
118 |
"var n = window.document.getElementById('ko.test.div'); \n "
|
jaroslav@137
|
119 |
+ "if (!n) { \n"
|
jaroslav@137
|
120 |
+ " n = window.document.createElement('div'); \n "
|
jaroslav@137
|
121 |
+ " n.id = 'ko.test.div'; \n "
|
jaroslav@137
|
122 |
+ " var body = window.document.getElementsByTagName('body')[0];\n"
|
jaroslav@142
|
123 |
+ " body.appendChild(n);\n"
|
jaroslav@137
|
124 |
+ "}\n"
|
jaroslav@137
|
125 |
+ "n.innerHTML = arguments[0]; \n ";
|
jaroslav@137
|
126 |
return executeScript(clazz, s, html);
|
jaroslav@137
|
127 |
}
|
jaroslav@138
|
128 |
|
jtulach@681
|
129 |
static int countChildren(Class<?> caller, String id) throws Exception {
|
jtulach@681
|
130 |
return ((Number) executeScript(caller,
|
jtulach@681
|
131 |
"var e = window.document.getElementById(arguments[0]);\n" +
|
jtulach@681
|
132 |
"if (typeof e === 'undefined') return -2;\n " +
|
jtulach@681
|
133 |
"var list = e.childNodes;\n" +
|
jtulach@681
|
134 |
"var cnt = 0;\n" +
|
jtulach@681
|
135 |
"for (var i = 0; i < list.length; i++) {\n" +
|
jtulach@681
|
136 |
" if (list[i].nodeType == 1) cnt++;\n" +
|
jtulach@681
|
137 |
"}\n" +
|
jtulach@681
|
138 |
"return cnt;\n"
|
jtulach@681
|
139 |
, id
|
jtulach@681
|
140 |
)).intValue();
|
jtulach@681
|
141 |
}
|
jtulach@824
|
142 |
|
jtulach@825
|
143 |
static Object addChildren(Class<?> caller, String id, String field, Object value) throws Exception {
|
jtulach@824
|
144 |
return executeScript(caller,
|
jtulach@824
|
145 |
"var e = window.document.getElementById(arguments[0]);\n" +
|
jtulach@825
|
146 |
"var f = arguments[1];\n" +
|
jtulach@825
|
147 |
"var v = arguments[2];\n" +
|
jtulach@824
|
148 |
"if (typeof e === 'undefined') return -2;\n " +
|
jtulach@824
|
149 |
"var c = ko.contextFor(e);\n" +
|
jtulach@825
|
150 |
"var fn = c.$rawData[f];\n" +
|
jtulach@825
|
151 |
"var arr = c.$rawData[f]();\n" +
|
jtulach@824
|
152 |
"arr.push(v);\n" +
|
jtulach@824
|
153 |
"fn(arr);\n" +
|
jtulach@824
|
154 |
"return arr;\n"
|
jtulach@825
|
155 |
, id, field, value
|
jtulach@824
|
156 |
);
|
jtulach@824
|
157 |
}
|
jtulach@681
|
158 |
|
jaroslav@138
|
159 |
static String prepareURL(
|
jaroslav@138
|
160 |
Class<?> clazz, String content, String mimeType, String... parameters) {
|
jaroslav@393
|
161 |
for (KnockoutTCK tck : tcks(clazz)) {
|
jaroslav@138
|
162 |
URI o = tck.prepareURL(content, mimeType, parameters);
|
jaroslav@138
|
163 |
if (o != null) {
|
jaroslav@138
|
164 |
return o.toString();
|
jaroslav@138
|
165 |
}
|
jaroslav@138
|
166 |
}
|
jaroslav@138
|
167 |
throw new IllegalStateException();
|
jaroslav@138
|
168 |
}
|
jaroslav@260
|
169 |
|
jaroslav@260
|
170 |
static boolean canFailWebSockets(
|
jaroslav@260
|
171 |
Class<?> clazz) {
|
jaroslav@393
|
172 |
for (KnockoutTCK tck : tcks(clazz)) {
|
jaroslav@260
|
173 |
if (tck.canFailWebSocketTest()) {
|
jaroslav@260
|
174 |
return true;
|
jaroslav@260
|
175 |
}
|
jaroslav@260
|
176 |
}
|
jaroslav@260
|
177 |
return false;
|
jaroslav@260
|
178 |
}
|
jaroslav@137
|
179 |
|
jaroslav@121
|
180 |
private static ClassLoader cl(Class<?> c) {
|
jaroslav@121
|
181 |
try {
|
jaroslav@121
|
182 |
return c.getClassLoader();
|
jaroslav@121
|
183 |
} catch (SecurityException ex) {
|
jaroslav@121
|
184 |
return null;
|
jaroslav@121
|
185 |
}
|
jaroslav@121
|
186 |
}
|
jtulach@934
|
187 |
|
jtulach@934
|
188 |
static void fail(String msg) {
|
jtulach@934
|
189 |
throw new AssertionError(msg);
|
jtulach@934
|
190 |
}
|
jtulach@934
|
191 |
|
jtulach@934
|
192 |
static void assertTrue(boolean c, String msg) {
|
jtulach@934
|
193 |
if (!c) {
|
jtulach@934
|
194 |
throw new AssertionError(msg);
|
jtulach@934
|
195 |
}
|
jtulach@934
|
196 |
}
|
jtulach@934
|
197 |
|
jtulach@934
|
198 |
static void assertFalse(boolean c, String msg) {
|
jtulach@934
|
199 |
if (c) {
|
jtulach@934
|
200 |
throw new AssertionError(msg);
|
jtulach@934
|
201 |
}
|
jtulach@934
|
202 |
}
|
jtulach@934
|
203 |
|
jtulach@934
|
204 |
static void assertNull(Object o, String msg) {
|
jtulach@934
|
205 |
if (o != null) {
|
jtulach@1038
|
206 |
throw new AssertionError(msg + " but was: " + o);
|
jtulach@934
|
207 |
}
|
jtulach@934
|
208 |
}
|
jtulach@934
|
209 |
|
jtulach@934
|
210 |
static void assertNotNull(Object o, String msg) {
|
jtulach@934
|
211 |
if (o == null) {
|
jtulach@934
|
212 |
throw new AssertionError(msg);
|
jtulach@934
|
213 |
}
|
jtulach@934
|
214 |
}
|
jtulach@934
|
215 |
|
jtulach@934
|
216 |
static void assertEquals(Object a, Object b, String msg) {
|
jtulach@934
|
217 |
if (a == b) {
|
jtulach@934
|
218 |
return;
|
jtulach@934
|
219 |
}
|
jtulach@934
|
220 |
if (a != null && a.equals(b)) {
|
jtulach@934
|
221 |
return;
|
jtulach@934
|
222 |
}
|
jtulach@1038
|
223 |
throw new AssertionError(msg + " expecting: " + b + " actual: " + a);
|
jtulach@934
|
224 |
}
|
jaroslav@35
|
225 |
}
|