Internet Explorer somehow caches values of person.json and people.json from previous runs. Let's use unique names to prevent that.
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.htmlpage;
20 import java.util.Iterator;
21 import org.apidesign.bck2brwsr.core.JavaScriptBody;
22 import org.apidesign.bck2brwsr.htmlpage.api.OnReceive;
23 import org.apidesign.bck2brwsr.htmlpage.api.Page;
24 import org.apidesign.bck2brwsr.htmlpage.api.Property;
25 import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
26 import org.apidesign.bck2brwsr.vmtest.Http;
27 import org.apidesign.bck2brwsr.vmtest.VMTest;
28 import org.json.JSONException;
29 import org.json.JSONObject;
30 import org.json.JSONTokener;
31 import org.testng.annotations.Test;
32 import static org.testng.Assert.*;
33 import org.testng.annotations.Factory;
35 /** Need to verify that models produce reasonable JSON objects.
37 * @author Jaroslav Tulach <jtulach@netbeans.org>
39 @Page(xhtml = "Empty.html", className = "JSONik", properties = {
40 @Property(name = "fetched", type = PersonImpl.class),
41 @Property(name = "fetchedCount", type = int.class),
42 @Property(name = "fetchedSex", type = Sex.class, array = true)
44 public class JSONTest {
48 @Test public void personToString() throws JSONException {
49 Person p = new Person();
51 p.setFirstName("Jarda");
52 p.setLastName("Tulach");
54 JSONTokener t = new JSONTokener(p.toString());
57 o = new JSONObject(t);
58 } catch (JSONException ex) {
59 throw new AssertionError("Can't parse " + p.toString(), ex);
62 Iterator it = o.sortedKeys();
63 assertEquals(it.next(), "firstName");
64 assertEquals(it.next(), "lastName");
65 assertEquals(it.next(), "sex");
67 assertEquals(o.getString("firstName"), "Jarda");
68 assertEquals(o.getString("lastName"), "Tulach");
69 assertEquals(o.getString("sex"), "MALE");
72 @BrwsrTest public void toJSONInABrowser() throws Throwable {
73 Person p = new Person();
75 p.setFirstName("Jarda");
76 p.setLastName("Tulach");
80 json = parseJSON(p.toString());
81 } catch (Throwable ex) {
82 throw new IllegalStateException("Can't parse " + p).initCause(ex);
85 Person p2 = new Person(json);
87 assert p2.getFirstName().equals(p.getFirstName()) :
88 "Should be the same: " + p.getFirstName() + " != " + p2.getFirstName();
91 @Test public void personWithWildCharactersAndNulls() throws JSONException {
92 Person p = new Person();
93 p.setFirstName("'\"\n");
94 p.setLastName("\t\r\u0002");
96 JSONTokener t = new JSONTokener(p.toString());
99 o = new JSONObject(t);
100 } catch (JSONException ex) {
101 throw new AssertionError("Can't parse " + p.toString(), ex);
104 Iterator it = o.sortedKeys();
105 assertEquals(it.next(), "firstName");
106 assertEquals(it.next(), "lastName");
107 assertEquals(it.next(), "sex");
109 assertEquals(o.getString("firstName"), p.getFirstName());
110 assertEquals(o.getString("lastName"), p.getLastName());
111 assertEquals(o.get("sex"), JSONObject.NULL);
114 @Test public void personsInArray() throws JSONException {
115 Person p1 = new Person();
116 p1.setFirstName("One");
118 Person p2 = new Person();
119 p2.setFirstName("Two");
121 People arr = new People();
122 arr.getInfo().add(p1);
123 arr.getInfo().add(p2);
124 arr.getNicknames().add("Prvn\u00ed k\u016f\u0148");
125 final String n2 = "Druh\u00fd hlem\u00fd\u017e\u010f, star\u0161\u00ed";
126 arr.getNicknames().add(n2);
127 arr.getAge().add(33);
128 arr.getAge().add(73);
131 final String json = arr.toString();
133 JSONTokener t = new JSONTokener(json);
136 o = new JSONObject(t);
137 } catch (JSONException ex) {
138 throw new AssertionError("Can't parse " + json, ex);
141 assertEquals(o.getJSONArray("info").getJSONObject(0).getString("firstName"), "One");
142 assertEquals(o.getJSONArray("nicknames").getString(1), n2);
143 assertEquals(o.getJSONArray("age").getInt(1), 73);
147 @OnReceive(url="/{url}")
148 static void fetch(Person p, JSONik model) {
152 @OnReceive(url="/{url}")
153 static void fetchArray(Person[] p, JSONik model) {
154 model.setFetchedCount(p.length);
155 model.setFetched(p[0]);
158 @OnReceive(url="/{url}")
159 static void fetchPeople(People p, JSONik model) {
160 model.setFetchedCount(p.getInfo().size());
161 model.setFetched(p.getInfo().get(0));
164 @OnReceive(url="/{url}")
165 static void fetchPeopleAge(People p, JSONik model) {
167 for (int a : p.getAge()) {
170 model.setFetchedCount(sum);
173 @Http(@Http.Resource(
174 content = "{'firstName': 'Sitar', 'sex': 'MALE'}",
175 path="/person33.json",
176 mimeType = "application/json"
178 @BrwsrTest public void loadAndParseJSON() throws InterruptedException {
179 try { throw new Exception(); } catch (Exception ex) {
185 js.fetch("person33.json");
188 Person p = js.getFetched();
190 throw new InterruptedException();
193 assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
194 assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
197 @OnReceive(url="/{url}?callme={me}", jsonp = "me")
198 static void fetchViaJSONP(Person p, JSONik model) {
202 @Http(@Http.Resource(
203 content = "$0({'firstName': 'Mitar', 'sex': 'MALE'})",
204 path="/person22.json",
205 mimeType = "application/javascript",
206 parameters = { "callme" }
208 @BrwsrTest public void loadAndParseJSONP() throws InterruptedException {
211 orig = scriptElements();
212 assert orig > 0 : "There should be some scripts on the page";
217 js.fetchViaJSONP("person22.json");
220 Person p = js.getFetched();
222 throw new InterruptedException();
225 assert "Mitar".equals(p.getFirstName()) : "Unexpected: " + p.getFirstName();
226 assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
228 int now = scriptElements();
230 assert orig == now : "The set of elements is unchanged. Delta: " + (now - orig);
233 @JavaScriptBody(args = { }, body = "return window.document.getElementsByTagName('script').length;")
234 private static native int scriptElements();
236 @JavaScriptBody(args = { "s" }, body = "return window.JSON.parse(s);")
237 private static native Object parseJSON(String s);
239 @Http(@Http.Resource(
240 content = "{'firstName': 'Sitar', 'sex': 'MALE'}",
241 path="/person15.json",
242 mimeType = "application/json"
244 @BrwsrTest public void loadAndParseJSONSentToArray() throws InterruptedException {
249 js.fetchArray("person15.json");
252 Person p = js.getFetched();
254 throw new InterruptedException();
257 assert p != null : "We should get our person back: " + p;
258 assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
259 assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
262 @Http(@Http.Resource(
263 content = "[{'firstName': 'Gitar', 'sex': 'FEMALE'}]",
264 path="/person5.json",
265 mimeType = "application/json"
267 @BrwsrTest public void loadAndParseJSONArraySingle() throws InterruptedException {
272 js.fetch("person5.json");
275 Person p = js.getFetched();
277 throw new InterruptedException();
280 assert p != null : "We should get our person back: " + p;
281 assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
282 assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
285 // @Http(@Http.Resource(
286 // content = "{'info':[{'firstName': 'Gitar', 'sex': 'FEMALE'}]}",
287 // path="/people.json",
288 // mimeType = "application/json"
290 // @BrwsrTest public void loadAndParseArrayInPeople() throws InterruptedException {
292 // js = new JSONik();
293 // js.applyBindings();
295 // js.fetchPeople("people.json");
298 // if (0 == js.getFetchedCount()) {
299 // throw new InterruptedException();
302 // assert js.getFetchedCount() == 1 : "One person loaded: " + js.getFetchedCount();
304 // Person p = js.getFetched();
306 // assert p != null : "We should get our person back: " + p;
307 // assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
308 // assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
311 @Http(@Http.Resource(
312 content = "{'age':[1, 2, 3]}",
313 path="/people8.json",
314 mimeType = "application/json"
316 @BrwsrTest public void loadAndParseArrayOfIntegers() throws InterruptedException {
321 js.fetchPeopleAge("people8.json");
324 if (0 == js.getFetchedCount()) {
325 throw new InterruptedException();
328 assert js.getFetchedCount() == 6 : "1 + 2 + 3 is " + js.getFetchedCount();
331 @OnReceive(url="/{url}")
332 static void fetchPeopleSex(People p, JSONik model) {
333 model.setFetchedCount(1);
334 model.getFetchedSex().addAll(p.getSex());
338 @Http(@Http.Resource(
339 content = "{'sex':['FEMALE', 'MALE', 'MALE']}",
340 path="/people11.json",
341 mimeType = "application/json"
343 @BrwsrTest public void loadAndParseArrayOfEnums() throws InterruptedException {
348 js.fetchPeopleSex("people11.json");
351 if (0 == js.getFetchedCount()) {
352 throw new InterruptedException();
355 assert js.getFetchedCount() == 1 : "Loaded";
357 assert js.getFetchedSex().size() == 3 : "Three values " + js.getFetchedSex();
358 assert js.getFetchedSex().get(0) == Sex.FEMALE : "Female first " + js.getFetchedSex();
359 assert js.getFetchedSex().get(1) == Sex.MALE : "male 2nd " + js.getFetchedSex();
360 assert js.getFetchedSex().get(2) == Sex.MALE : "male 3rd " + js.getFetchedSex();
363 @Http(@Http.Resource(
364 content = "[{'firstName': 'Gitar', 'sex': 'FEMALE'},"
365 + "{'firstName': 'Peter', 'sex': 'MALE'}"
367 path="/person13.json",
368 mimeType = "application/json"
370 @BrwsrTest public void loadAndParseJSONArray() throws InterruptedException {
374 js.fetchArray("person13.json");
378 Person p = js.getFetched();
380 throw new InterruptedException();
383 assert js.getFetchedCount() == 2 : "We got two values: " + js.getFetchedCount();
384 assert p != null : "We should get our person back: " + p;
385 assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
386 assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
389 @Factory public static Object[] create() {
390 return VMTest.create(JSONTest.class);