jaroslav@34
|
1 |
/**
|
jaroslav@358
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
jaroslav@34
|
3 |
*
|
jaroslav@551
|
4 |
* Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
|
jaroslav@34
|
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@34
|
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@34
|
42 |
*/
|
jaroslav@34
|
43 |
package net.java.html.json.tests;
|
jaroslav@34
|
44 |
|
jaroslav@77
|
45 |
import java.io.ByteArrayInputStream;
|
jaroslav@507
|
46 |
import java.io.ByteArrayOutputStream;
|
jaroslav@507
|
47 |
import java.io.PrintStream;
|
jaroslav@110
|
48 |
import net.java.html.BrwsrCtx;
|
jaroslav@34
|
49 |
import net.java.html.json.Model;
|
jaroslav@240
|
50 |
import net.java.html.json.ModelOperation;
|
jaroslav@77
|
51 |
import net.java.html.json.Models;
|
jaroslav@34
|
52 |
import net.java.html.json.OnReceive;
|
jaroslav@34
|
53 |
import net.java.html.json.Property;
|
jtulach@838
|
54 |
import org.netbeans.html.json.tck.KOTest;
|
jaroslav@34
|
55 |
|
jaroslav@34
|
56 |
/** Need to verify that models produce reasonable JSON objects.
|
jaroslav@34
|
57 |
*
|
jtulach@790
|
58 |
* @author Jaroslav Tulach
|
jaroslav@34
|
59 |
*/
|
jaroslav@34
|
60 |
@Model(className = "JSONik", properties = {
|
jaroslav@34
|
61 |
@Property(name = "fetched", type = Person.class),
|
jaroslav@34
|
62 |
@Property(name = "fetchedCount", type = int.class),
|
jaroslav@73
|
63 |
@Property(name = "fetchedResponse", type = String.class),
|
jaroslav@34
|
64 |
@Property(name = "fetchedSex", type = Sex.class, array = true)
|
jaroslav@34
|
65 |
})
|
jaroslav@34
|
66 |
public final class JSONTest {
|
jaroslav@34
|
67 |
private JSONik js;
|
jaroslav@34
|
68 |
private Integer orig;
|
jaroslav@138
|
69 |
private String url;
|
jaroslav@34
|
70 |
|
jaroslav@240
|
71 |
@ModelOperation static void assignFetched(JSONik m, Person p) {
|
jaroslav@240
|
72 |
m.setFetched(p);
|
jaroslav@240
|
73 |
}
|
jtulach@569
|
74 |
private BrwsrCtx ctx;
|
jaroslav@240
|
75 |
|
jaroslav@137
|
76 |
@KOTest public void toJSONInABrowser() throws Throwable {
|
jaroslav@121
|
77 |
Person p = Models.bind(new Person(), newContext());
|
jaroslav@34
|
78 |
p.setSex(Sex.MALE);
|
jaroslav@34
|
79 |
p.setFirstName("Jarda");
|
jaroslav@34
|
80 |
p.setLastName("Tulach");
|
jaroslav@34
|
81 |
|
jaroslav@34
|
82 |
Object json;
|
jaroslav@34
|
83 |
try {
|
jaroslav@34
|
84 |
json = parseJSON(p.toString());
|
jaroslav@34
|
85 |
} catch (Throwable ex) {
|
jaroslav@34
|
86 |
throw new IllegalStateException("Can't parse " + p).initCause(ex);
|
jaroslav@34
|
87 |
}
|
jaroslav@34
|
88 |
|
jaroslav@388
|
89 |
Person p2 = Models.fromRaw(newContext(), Person.class, json);
|
jaroslav@34
|
90 |
|
jaroslav@34
|
91 |
assert p2.getFirstName().equals(p.getFirstName()) :
|
jaroslav@34
|
92 |
"Should be the same: " + p.getFirstName() + " != " + p2.getFirstName();
|
jaroslav@34
|
93 |
}
|
jaroslav@34
|
94 |
|
jaroslav@329
|
95 |
@KOTest public void toJSONWithEscapeCharactersInABrowser() throws Throwable {
|
jaroslav@329
|
96 |
Person p = Models.bind(new Person(), newContext());
|
jaroslav@329
|
97 |
p.setSex(Sex.MALE);
|
jaroslav@329
|
98 |
p.setFirstName("/*\n * Copyright (c) 2013");
|
jaroslav@329
|
99 |
|
jaroslav@329
|
100 |
|
jaroslav@329
|
101 |
final String txt = p.toString();
|
jaroslav@329
|
102 |
Object json;
|
jaroslav@329
|
103 |
try {
|
jaroslav@329
|
104 |
json = parseJSON(txt);
|
jaroslav@329
|
105 |
} catch (Throwable ex) {
|
jaroslav@329
|
106 |
throw new IllegalStateException("Can't parse " + txt).initCause(ex);
|
jaroslav@329
|
107 |
}
|
jaroslav@329
|
108 |
|
jaroslav@388
|
109 |
Person p2 = Models.fromRaw(newContext(), Person.class, json);
|
jaroslav@329
|
110 |
|
jaroslav@329
|
111 |
assert p2.getFirstName().equals(p.getFirstName()) :
|
jaroslav@329
|
112 |
"Should be the same: " + p.getFirstName() + " != " + p2.getFirstName();
|
jaroslav@329
|
113 |
}
|
jaroslav@329
|
114 |
|
jaroslav@329
|
115 |
@KOTest public void toJSONWithDoubleSlashInABrowser() throws Throwable {
|
jaroslav@329
|
116 |
Person p = Models.bind(new Person(), newContext());
|
jaroslav@329
|
117 |
p.setSex(Sex.MALE);
|
jaroslav@329
|
118 |
p.setFirstName("/*\\n * Copyright (c) 2013");
|
jaroslav@329
|
119 |
|
jaroslav@329
|
120 |
|
jaroslav@329
|
121 |
final String txt = p.toString();
|
jaroslav@329
|
122 |
Object json;
|
jaroslav@329
|
123 |
try {
|
jaroslav@329
|
124 |
json = parseJSON(txt);
|
jaroslav@329
|
125 |
} catch (Throwable ex) {
|
jaroslav@329
|
126 |
throw new IllegalStateException("Can't parse " + txt).initCause(ex);
|
jaroslav@329
|
127 |
}
|
jaroslav@329
|
128 |
|
jaroslav@388
|
129 |
Person p2 = Models.fromRaw(newContext(), Person.class, json);
|
jaroslav@329
|
130 |
|
jaroslav@329
|
131 |
assert p2.getFirstName().equals(p.getFirstName()) :
|
jaroslav@329
|
132 |
"Should be the same: " + p.getFirstName() + " != " + p2.getFirstName();
|
jaroslav@329
|
133 |
}
|
jaroslav@329
|
134 |
|
jaroslav@338
|
135 |
@KOTest public void toJSONWithApostrophInABrowser() throws Throwable {
|
jaroslav@338
|
136 |
Person p = Models.bind(new Person(), newContext());
|
jaroslav@338
|
137 |
p.setSex(Sex.MALE);
|
jaroslav@338
|
138 |
p.setFirstName("Jimmy 'Jim' Rambo");
|
jaroslav@338
|
139 |
|
jaroslav@338
|
140 |
|
jaroslav@338
|
141 |
final String txt = p.toString();
|
jaroslav@338
|
142 |
Object json;
|
jaroslav@338
|
143 |
try {
|
jaroslav@338
|
144 |
json = parseJSON(txt);
|
jaroslav@338
|
145 |
} catch (Throwable ex) {
|
jaroslav@338
|
146 |
throw new IllegalStateException("Can't parse " + txt).initCause(ex);
|
jaroslav@338
|
147 |
}
|
jaroslav@338
|
148 |
|
jaroslav@388
|
149 |
Person p2 = Models.fromRaw(newContext(), Person.class, json);
|
jaroslav@338
|
150 |
|
jaroslav@338
|
151 |
assert p2.getFirstName().equals(p.getFirstName()) :
|
jaroslav@338
|
152 |
"Should be the same: " + p.getFirstName() + " != " + p2.getFirstName();
|
jaroslav@338
|
153 |
}
|
jtulach@569
|
154 |
|
jtulach@569
|
155 |
private static BrwsrCtx onCallback;
|
jaroslav@338
|
156 |
|
jaroslav@138
|
157 |
@OnReceive(url="{url}")
|
jtulach@650
|
158 |
static void fetch(JSONik model, Person p) {
|
jaroslav@34
|
159 |
model.setFetched(p);
|
jtulach@569
|
160 |
onCallback = BrwsrCtx.findDefault(model.getClass());
|
jaroslav@34
|
161 |
}
|
jaroslav@34
|
162 |
|
jaroslav@245
|
163 |
@OnReceive(url="{url}", onError = "setMessage")
|
jtulach@650
|
164 |
static void fetchArray(JSONik model, Person[] p) {
|
jaroslav@34
|
165 |
model.setFetchedCount(p.length);
|
jaroslav@34
|
166 |
model.setFetched(p[0]);
|
jtulach@569
|
167 |
onCallback = BrwsrCtx.findDefault(model.getClass());
|
jaroslav@34
|
168 |
}
|
jaroslav@34
|
169 |
|
jaroslav@245
|
170 |
static void setMessage(JSONik m, Exception t) {
|
jaroslav@245
|
171 |
assert t != null;
|
jaroslav@245
|
172 |
m.setFetchedResponse("Exception");
|
jaroslav@245
|
173 |
}
|
jaroslav@245
|
174 |
|
jaroslav@138
|
175 |
@OnReceive(url="{url}")
|
jtulach@650
|
176 |
static void fetchPeople(JSONik model, People p) {
|
jaroslav@528
|
177 |
final int size = p.getInfo().size();
|
jaroslav@528
|
178 |
if (size > 0) {
|
jaroslav@528
|
179 |
model.setFetched(p.getInfo().get(0));
|
jaroslav@528
|
180 |
}
|
jaroslav@528
|
181 |
model.setFetchedCount(size);
|
jaroslav@34
|
182 |
}
|
jaroslav@34
|
183 |
|
jaroslav@138
|
184 |
@OnReceive(url="{url}")
|
jtulach@650
|
185 |
static void fetchPeopleAge(JSONik model, People p) {
|
jaroslav@34
|
186 |
int sum = 0;
|
jaroslav@34
|
187 |
for (int a : p.getAge()) {
|
jaroslav@34
|
188 |
sum += a;
|
jaroslav@34
|
189 |
}
|
jaroslav@34
|
190 |
model.setFetchedCount(sum);
|
jaroslav@34
|
191 |
}
|
jaroslav@34
|
192 |
|
jaroslav@137
|
193 |
@KOTest public void loadAndParseJSON() throws InterruptedException {
|
jaroslav@34
|
194 |
if (js == null) {
|
jaroslav@138
|
195 |
url = Utils.prepareURL(
|
jaroslav@138
|
196 |
JSONTest.class, "{'firstName': 'Sitar', 'sex': 'MALE'}",
|
jaroslav@138
|
197 |
"application/json"
|
jaroslav@138
|
198 |
);
|
jtulach@569
|
199 |
js = Models.bind(new JSONik(), ctx = newContext());
|
jaroslav@34
|
200 |
js.applyBindings();
|
jaroslav@34
|
201 |
|
jaroslav@234
|
202 |
js.setFetched(null);
|
jaroslav@138
|
203 |
js.fetch(url);
|
jaroslav@34
|
204 |
}
|
jaroslav@34
|
205 |
|
jaroslav@34
|
206 |
Person p = js.getFetched();
|
jaroslav@34
|
207 |
if (p == null) {
|
jaroslav@34
|
208 |
throw new InterruptedException();
|
jaroslav@34
|
209 |
}
|
jaroslav@34
|
210 |
|
jaroslav@34
|
211 |
assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
|
jaroslav@34
|
212 |
assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
|
jtulach@569
|
213 |
|
jtulach@569
|
214 |
assert ctx == onCallback;
|
jaroslav@34
|
215 |
}
|
jaroslav@34
|
216 |
|
jaroslav@139
|
217 |
@OnReceive(url="{url}?callme={me}", jsonp = "me")
|
jtulach@650
|
218 |
static void fetchViaJSONP(JSONik model, Person p) {
|
jaroslav@34
|
219 |
model.setFetched(p);
|
jaroslav@34
|
220 |
}
|
jaroslav@34
|
221 |
|
jaroslav@137
|
222 |
@KOTest public void loadAndParseJSONP() throws InterruptedException, Exception {
|
jaroslav@34
|
223 |
if (js == null) {
|
jaroslav@138
|
224 |
url = Utils.prepareURL(
|
jaroslav@138
|
225 |
JSONTest.class, "$0({'firstName': 'Mitar', 'sex': 'MALE'})",
|
jaroslav@138
|
226 |
"application/javascript",
|
jaroslav@138
|
227 |
"callme"
|
jaroslav@138
|
228 |
);
|
jaroslav@34
|
229 |
orig = scriptElements();
|
jaroslav@34
|
230 |
assert orig > 0 : "There should be some scripts on the page";
|
jaroslav@34
|
231 |
|
jaroslav@121
|
232 |
js = Models.bind(new JSONik(), newContext());
|
jaroslav@34
|
233 |
js.applyBindings();
|
jaroslav@34
|
234 |
|
jaroslav@234
|
235 |
js.setFetched(null);
|
jaroslav@138
|
236 |
js.fetchViaJSONP(url);
|
jaroslav@34
|
237 |
}
|
jaroslav@34
|
238 |
|
jaroslav@34
|
239 |
Person p = js.getFetched();
|
jaroslav@34
|
240 |
if (p == null) {
|
jaroslav@34
|
241 |
throw new InterruptedException();
|
jaroslav@34
|
242 |
}
|
jaroslav@34
|
243 |
|
jaroslav@34
|
244 |
assert "Mitar".equals(p.getFirstName()) : "Unexpected: " + p.getFirstName();
|
jaroslav@34
|
245 |
assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
|
jaroslav@34
|
246 |
|
jaroslav@34
|
247 |
int now = scriptElements();
|
jaroslav@34
|
248 |
|
jaroslav@34
|
249 |
assert orig == now : "The set of elements is unchanged. Delta: " + (now - orig);
|
jaroslav@34
|
250 |
}
|
jaroslav@34
|
251 |
|
jaroslav@72
|
252 |
|
jaroslav@72
|
253 |
|
jaroslav@72
|
254 |
@OnReceive(url="{url}", method = "PUT", data = Person.class)
|
jaroslav@72
|
255 |
static void putPerson(JSONik model, String reply) {
|
jaroslav@72
|
256 |
model.setFetchedCount(1);
|
jaroslav@73
|
257 |
model.setFetchedResponse(reply);
|
jaroslav@72
|
258 |
}
|
jaroslav@138
|
259 |
|
jaroslav@137
|
260 |
@KOTest public void putPeopleUsesRightMethod() throws InterruptedException, Exception {
|
jaroslav@72
|
261 |
if (js == null) {
|
jaroslav@138
|
262 |
url = Utils.prepareURL(
|
jaroslav@138
|
263 |
JSONTest.class, "$0\n$1",
|
jaroslav@138
|
264 |
"text/plain",
|
jaroslav@138
|
265 |
"http.method", "http.requestBody"
|
jaroslav@138
|
266 |
);
|
jaroslav@72
|
267 |
orig = scriptElements();
|
jaroslav@72
|
268 |
assert orig > 0 : "There should be some scripts on the page";
|
jaroslav@72
|
269 |
|
jaroslav@121
|
270 |
js = Models.bind(new JSONik(), newContext());
|
jaroslav@72
|
271 |
js.applyBindings();
|
jaroslav@72
|
272 |
|
jaroslav@115
|
273 |
Person p = Models.bind(new Person(), BrwsrCtx.EMPTY);
|
jaroslav@72
|
274 |
p.setFirstName("Jarda");
|
jaroslav@138
|
275 |
js.putPerson(url, p);
|
jaroslav@72
|
276 |
}
|
jaroslav@72
|
277 |
|
jaroslav@72
|
278 |
int cnt = js.getFetchedCount();
|
jaroslav@72
|
279 |
if (cnt == 0) {
|
jaroslav@72
|
280 |
throw new InterruptedException();
|
jaroslav@72
|
281 |
}
|
jaroslav@73
|
282 |
String res = js.getFetchedResponse();
|
jaroslav@73
|
283 |
int line = res.indexOf('\n');
|
jaroslav@73
|
284 |
String msg;
|
jaroslav@73
|
285 |
if (line >= 0) {
|
jaroslav@75
|
286 |
msg = res.substring(line + 1);
|
jaroslav@73
|
287 |
res = res.substring(0, line);
|
jaroslav@73
|
288 |
} else {
|
jaroslav@73
|
289 |
msg = res;
|
jaroslav@73
|
290 |
}
|
jaroslav@73
|
291 |
|
jaroslav@73
|
292 |
assert "PUT".equals(res) : "Server was queried with PUT method: " + js.getFetchedResponse();
|
jaroslav@73
|
293 |
|
jaroslav@73
|
294 |
assert msg.contains("Jarda") : "Data transferred to the server: " + msg;
|
jaroslav@72
|
295 |
}
|
jaroslav@72
|
296 |
|
jaroslav@36
|
297 |
private static int scriptElements() throws Exception {
|
jaroslav@121
|
298 |
return ((Number)Utils.executeScript(
|
jaroslav@121
|
299 |
JSONTest.class,
|
jaroslav@121
|
300 |
"return window.document.getElementsByTagName('script').length;")).intValue();
|
jaroslav@36
|
301 |
}
|
jaroslav@34
|
302 |
|
jaroslav@36
|
303 |
private static Object parseJSON(String s) throws Exception {
|
jaroslav@121
|
304 |
return Utils.executeScript(
|
jaroslav@121
|
305 |
JSONTest.class,
|
jaroslav@121
|
306 |
"return window.JSON.parse(arguments[0]);", s);
|
jaroslav@36
|
307 |
}
|
jaroslav@34
|
308 |
|
jaroslav@137
|
309 |
@KOTest public void loadAndParseJSONSentToArray() throws InterruptedException {
|
jaroslav@34
|
310 |
if (js == null) {
|
jaroslav@138
|
311 |
url = Utils.prepareURL(
|
jaroslav@138
|
312 |
JSONTest.class, "{'firstName': 'Sitar', 'sex': 'MALE'}",
|
jaroslav@138
|
313 |
"application/json"
|
jaroslav@138
|
314 |
);
|
jaroslav@138
|
315 |
|
jaroslav@121
|
316 |
js = Models.bind(new JSONik(), newContext());
|
jaroslav@34
|
317 |
js.applyBindings();
|
jaroslav@34
|
318 |
|
jaroslav@234
|
319 |
js.setFetched(null);
|
jaroslav@138
|
320 |
js.fetchArray(url);
|
jaroslav@34
|
321 |
}
|
jaroslav@34
|
322 |
|
jaroslav@34
|
323 |
Person p = js.getFetched();
|
jaroslav@34
|
324 |
if (p == null) {
|
jaroslav@34
|
325 |
throw new InterruptedException();
|
jaroslav@34
|
326 |
}
|
jaroslav@34
|
327 |
|
jaroslav@34
|
328 |
assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
|
jaroslav@34
|
329 |
assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
|
jaroslav@34
|
330 |
}
|
jaroslav@34
|
331 |
|
jaroslav@137
|
332 |
@KOTest public void loadAndParseJSONArraySingle() throws InterruptedException {
|
jaroslav@34
|
333 |
if (js == null) {
|
jaroslav@138
|
334 |
url = Utils.prepareURL(
|
jaroslav@138
|
335 |
JSONTest.class, "[{'firstName': 'Gitar', 'sex': 'FEMALE'}]",
|
jaroslav@138
|
336 |
"application/json"
|
jaroslav@138
|
337 |
);
|
jaroslav@121
|
338 |
js = Models.bind(new JSONik(), newContext());
|
jaroslav@34
|
339 |
js.applyBindings();
|
jaroslav@34
|
340 |
|
jaroslav@234
|
341 |
js.setFetched(null);
|
jaroslav@138
|
342 |
js.fetch(url);
|
jaroslav@34
|
343 |
}
|
jaroslav@34
|
344 |
|
jaroslav@34
|
345 |
Person p = js.getFetched();
|
jaroslav@34
|
346 |
if (p == null) {
|
jaroslav@34
|
347 |
throw new InterruptedException();
|
jaroslav@34
|
348 |
}
|
jaroslav@34
|
349 |
|
jaroslav@34
|
350 |
assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
|
jaroslav@34
|
351 |
assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
|
jaroslav@34
|
352 |
}
|
jaroslav@34
|
353 |
|
jaroslav@137
|
354 |
@KOTest public void loadAndParseArrayInPeople() throws InterruptedException {
|
jaroslav@34
|
355 |
if (js == null) {
|
jaroslav@138
|
356 |
url = Utils.prepareURL(
|
jaroslav@138
|
357 |
JSONTest.class, "{'info':[{'firstName': 'Gitar', 'sex': 'FEMALE'}]}",
|
jaroslav@138
|
358 |
"application/json"
|
jaroslav@138
|
359 |
);
|
jaroslav@121
|
360 |
js = Models.bind(new JSONik(), newContext());
|
jaroslav@34
|
361 |
js.applyBindings();
|
jaroslav@34
|
362 |
|
jaroslav@138
|
363 |
js.fetchPeople(url);
|
jaroslav@34
|
364 |
}
|
jaroslav@34
|
365 |
|
jaroslav@34
|
366 |
if (0 == js.getFetchedCount()) {
|
jaroslav@34
|
367 |
throw new InterruptedException();
|
jaroslav@34
|
368 |
}
|
jaroslav@34
|
369 |
|
jaroslav@34
|
370 |
assert js.getFetchedCount() == 1 : "One person loaded: " + js.getFetchedCount();
|
jaroslav@34
|
371 |
|
jaroslav@34
|
372 |
Person p = js.getFetched();
|
jaroslav@34
|
373 |
|
jaroslav@34
|
374 |
assert p != null : "We should get our person back: " + p;
|
jaroslav@34
|
375 |
assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
|
jaroslav@34
|
376 |
assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
|
jaroslav@34
|
377 |
}
|
jaroslav@34
|
378 |
|
jaroslav@137
|
379 |
@KOTest public void loadAndParseArrayOfIntegers() throws InterruptedException {
|
jaroslav@34
|
380 |
if (js == null) {
|
jaroslav@138
|
381 |
url = Utils.prepareURL(
|
jaroslav@138
|
382 |
JSONTest.class, "{'age':[1, 2, 3]}",
|
jaroslav@138
|
383 |
"application/json"
|
jaroslav@138
|
384 |
);
|
jaroslav@121
|
385 |
js = Models.bind(new JSONik(), newContext());
|
jaroslav@34
|
386 |
js.applyBindings();
|
jaroslav@34
|
387 |
|
jaroslav@138
|
388 |
js.fetchPeopleAge(url);
|
jaroslav@34
|
389 |
}
|
jaroslav@34
|
390 |
|
jaroslav@34
|
391 |
if (0 == js.getFetchedCount()) {
|
jaroslav@34
|
392 |
throw new InterruptedException();
|
jaroslav@34
|
393 |
}
|
jaroslav@34
|
394 |
|
jaroslav@34
|
395 |
assert js.getFetchedCount() == 6 : "1 + 2 + 3 is " + js.getFetchedCount();
|
jaroslav@34
|
396 |
}
|
jaroslav@34
|
397 |
|
jaroslav@139
|
398 |
@OnReceive(url="{url}")
|
jtulach@650
|
399 |
static void fetchPeopleSex(JSONik model, People p) {
|
jaroslav@34
|
400 |
model.setFetchedCount(1);
|
jaroslav@34
|
401 |
model.getFetchedSex().addAll(p.getSex());
|
jaroslav@34
|
402 |
}
|
jaroslav@34
|
403 |
|
jaroslav@137
|
404 |
@KOTest public void loadAndParseArrayOfEnums() throws InterruptedException {
|
jaroslav@34
|
405 |
if (js == null) {
|
jaroslav@138
|
406 |
url = Utils.prepareURL(
|
jaroslav@138
|
407 |
JSONTest.class, "{'sex':['FEMALE', 'MALE', 'MALE']}",
|
jaroslav@138
|
408 |
"application/json"
|
jaroslav@138
|
409 |
);
|
jaroslav@121
|
410 |
js = Models.bind(new JSONik(), newContext());
|
jaroslav@34
|
411 |
js.applyBindings();
|
jaroslav@34
|
412 |
|
jaroslav@138
|
413 |
js.fetchPeopleSex(url);
|
jaroslav@34
|
414 |
}
|
jaroslav@34
|
415 |
|
jaroslav@34
|
416 |
if (0 == js.getFetchedCount()) {
|
jaroslav@34
|
417 |
throw new InterruptedException();
|
jaroslav@34
|
418 |
}
|
jaroslav@34
|
419 |
|
jaroslav@34
|
420 |
assert js.getFetchedCount() == 1 : "Loaded";
|
jaroslav@34
|
421 |
|
jaroslav@34
|
422 |
assert js.getFetchedSex().size() == 3 : "Three values " + js.getFetchedSex();
|
jaroslav@34
|
423 |
assert js.getFetchedSex().get(0) == Sex.FEMALE : "Female first " + js.getFetchedSex();
|
jaroslav@34
|
424 |
assert js.getFetchedSex().get(1) == Sex.MALE : "male 2nd " + js.getFetchedSex();
|
jaroslav@34
|
425 |
assert js.getFetchedSex().get(2) == Sex.MALE : "male 3rd " + js.getFetchedSex();
|
jaroslav@34
|
426 |
}
|
jaroslav@34
|
427 |
|
jaroslav@137
|
428 |
@KOTest public void loadAndParseJSONArray() throws InterruptedException {
|
jaroslav@34
|
429 |
if (js == null) {
|
jaroslav@138
|
430 |
url = Utils.prepareURL(
|
jaroslav@138
|
431 |
JSONTest.class, "[{'firstName': 'Gitar', 'sex': 'FEMALE'},"
|
jaroslav@138
|
432 |
+ "{'firstName': 'Peter', 'sex': 'MALE'}"
|
jaroslav@138
|
433 |
+ "]",
|
jaroslav@138
|
434 |
"application/json"
|
jaroslav@138
|
435 |
);
|
jaroslav@121
|
436 |
js = Models.bind(new JSONik(), newContext());
|
jaroslav@34
|
437 |
js.applyBindings();
|
jaroslav@234
|
438 |
js.setFetched(null);
|
jaroslav@234
|
439 |
|
jaroslav@138
|
440 |
js.fetchArray(url);
|
jaroslav@34
|
441 |
}
|
jaroslav@34
|
442 |
|
jaroslav@34
|
443 |
|
jaroslav@34
|
444 |
Person p = js.getFetched();
|
jaroslav@34
|
445 |
if (p == null) {
|
jaroslav@34
|
446 |
throw new InterruptedException();
|
jaroslav@34
|
447 |
}
|
jaroslav@34
|
448 |
|
jaroslav@34
|
449 |
assert js.getFetchedCount() == 2 : "We got two values: " + js.getFetchedCount();
|
jaroslav@34
|
450 |
assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
|
jaroslav@34
|
451 |
assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
|
jaroslav@34
|
452 |
}
|
jaroslav@77
|
453 |
|
jaroslav@245
|
454 |
@KOTest public void loadError() throws InterruptedException {
|
jaroslav@245
|
455 |
if (js == null) {
|
jaroslav@245
|
456 |
js = Models.bind(new JSONik(), newContext());
|
jaroslav@245
|
457 |
js.applyBindings();
|
jaroslav@245
|
458 |
js.setFetched(null);
|
jaroslav@245
|
459 |
|
jaroslav@268
|
460 |
js.fetchArray("http://127.0.0.1:54253/does/not/exist.txt");
|
jaroslav@245
|
461 |
}
|
jaroslav@245
|
462 |
|
jaroslav@245
|
463 |
|
jaroslav@245
|
464 |
if (js.getFetchedResponse() == null) {
|
jaroslav@245
|
465 |
throw new InterruptedException();
|
jaroslav@245
|
466 |
}
|
jaroslav@245
|
467 |
|
jaroslav@245
|
468 |
assert "Exception".equals(js.getFetchedResponse()) : js.getFetchedResponse();
|
jaroslav@245
|
469 |
}
|
jaroslav@245
|
470 |
|
jaroslav@77
|
471 |
@Model(className = "NameAndValue", properties = {
|
jaroslav@77
|
472 |
@Property(name = "name", type = String.class),
|
jaroslav@77
|
473 |
@Property(name = "value", type = long.class),
|
jaroslav@77
|
474 |
@Property(name = "small", type = byte.class)
|
jaroslav@77
|
475 |
})
|
jaroslav@77
|
476 |
static class NandV {
|
jaroslav@77
|
477 |
}
|
jaroslav@77
|
478 |
|
jaroslav@137
|
479 |
@KOTest public void parseNullNumber() throws Exception {
|
jaroslav@77
|
480 |
String txt = "{ \"name\":\"M\" }";
|
jaroslav@77
|
481 |
ByteArrayInputStream is = new ByteArrayInputStream(txt.getBytes("UTF-8"));
|
jaroslav@121
|
482 |
NameAndValue v = Models.parse(newContext(), NameAndValue.class, is);
|
jaroslav@77
|
483 |
assert "M".equals(v.getName()) : "Name is 'M': " + v.getName();
|
jaroslav@77
|
484 |
assert 0 == v.getValue() : "Value is empty: " + v.getValue();
|
jaroslav@77
|
485 |
assert 0 == v.getSmall() : "Small value is empty: " + v.getSmall();
|
jaroslav@77
|
486 |
}
|
jaroslav@340
|
487 |
|
jaroslav@340
|
488 |
@KOTest public void deserializeWrongEnum() throws Exception {
|
jaroslav@507
|
489 |
PrintStream prev = null;
|
jaroslav@507
|
490 |
ByteArrayOutputStream err = new ByteArrayOutputStream();
|
jaroslav@507
|
491 |
try {
|
jaroslav@507
|
492 |
prev = System.err;
|
jaroslav@507
|
493 |
System.setErr(new PrintStream(err));
|
jaroslav@518
|
494 |
} catch (SecurityException e) {
|
jaroslav@518
|
495 |
err = null;
|
jaroslav@518
|
496 |
prev = null;
|
jaroslav@507
|
497 |
} catch (LinkageError e) {
|
jaroslav@507
|
498 |
err = null;
|
jaroslav@507
|
499 |
prev = null;
|
jaroslav@507
|
500 |
}
|
jaroslav@507
|
501 |
|
jaroslav@340
|
502 |
String str = "{ \"sex\" : \"unknown\" }";
|
jaroslav@340
|
503 |
ByteArrayInputStream is = new ByteArrayInputStream(str.getBytes("UTF-8"));
|
jaroslav@340
|
504 |
Person p = Models.parse(newContext(), Person.class, is);
|
jaroslav@340
|
505 |
assert p.getSex() == null : "Wrong sex means null, but was: " + p.getSex();
|
jaroslav@507
|
506 |
|
jaroslav@507
|
507 |
if (err != null) {
|
jaroslav@526
|
508 |
assert err.toString().contains("unknown") && err.toString().contains("Sex"): "Expecting error: " + err.toString();
|
jaroslav@507
|
509 |
}
|
jaroslav@507
|
510 |
if (prev != null) {
|
jaroslav@507
|
511 |
try {
|
jaroslav@507
|
512 |
System.setErr(prev);
|
jaroslav@507
|
513 |
} catch (LinkageError e) {
|
jaroslav@507
|
514 |
// ignore
|
jaroslav@507
|
515 |
}
|
jaroslav@507
|
516 |
}
|
jaroslav@340
|
517 |
}
|
jaroslav@340
|
518 |
|
jaroslav@240
|
519 |
|
jaroslav@121
|
520 |
private static BrwsrCtx newContext() {
|
jaroslav@121
|
521 |
return Utils.newContext(JSONTest.class);
|
jaroslav@121
|
522 |
}
|
jaroslav@77
|
523 |
|
jaroslav@34
|
524 |
}
|