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.Arrays;
21 import java.util.Iterator;
22 import org.apidesign.bck2brwsr.core.JavaScriptBody;
23 import org.apidesign.bck2brwsr.htmlpage.api.OnReceive;
24 import org.apidesign.bck2brwsr.htmlpage.api.Page;
25 import org.apidesign.bck2brwsr.htmlpage.api.Property;
26 import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
27 import org.apidesign.bck2brwsr.vmtest.Http;
28 import org.apidesign.bck2brwsr.vmtest.VMTest;
29 import org.json.JSONException;
30 import org.json.JSONObject;
31 import org.json.JSONTokener;
32 import org.testng.annotations.Test;
33 import static org.testng.Assert.*;
34 import org.testng.annotations.Factory;
36 /** Need to verify that models produce reasonable JSON objects.
38 * @author Jaroslav Tulach <jtulach@netbeans.org>
40 @Page(xhtml = "Empty.html", className = "JSONik", properties = {
41 @Property(name = "fetched", type = PersonImpl.class),
42 @Property(name = "fetchedCount", type = int.class),
43 @Property(name = "fetchedSex", type = Sex.class, array = true)
45 public class JSONTest {
49 @Test public void personToString() throws JSONException {
50 Person p = new Person();
52 p.setFirstName("Jarda");
53 p.setLastName("Tulach");
55 JSONTokener t = new JSONTokener(p.toString());
58 o = new JSONObject(t);
59 } catch (JSONException ex) {
60 throw new AssertionError("Can't parse " + p.toString(), ex);
63 Iterator it = o.sortedKeys();
64 assertEquals(it.next(), "firstName");
65 assertEquals(it.next(), "lastName");
66 assertEquals(it.next(), "sex");
68 assertEquals(o.getString("firstName"), "Jarda");
69 assertEquals(o.getString("lastName"), "Tulach");
70 assertEquals(o.getString("sex"), "MALE");
73 @Test public void personWithWildCharactersAndNulls() throws JSONException {
74 Person p = new Person();
75 p.setFirstName("'\"\n");
76 p.setLastName("\t\r\u0002");
78 JSONTokener t = new JSONTokener(p.toString());
81 o = new JSONObject(t);
82 } catch (JSONException ex) {
83 throw new AssertionError("Can't parse " + p.toString(), ex);
86 Iterator it = o.sortedKeys();
87 assertEquals(it.next(), "firstName");
88 assertEquals(it.next(), "lastName");
89 assertEquals(it.next(), "sex");
91 assertEquals(o.getString("firstName"), p.getFirstName());
92 assertEquals(o.getString("lastName"), p.getLastName());
93 assertEquals(o.get("sex"), JSONObject.NULL);
96 @Test public void personsInArray() throws JSONException {
97 Person p1 = new Person();
98 p1.setFirstName("One");
100 Person p2 = new Person();
101 p2.setFirstName("Two");
103 People arr = new People();
104 arr.getInfo().add(p1);
105 arr.getInfo().add(p2);
106 arr.getNicknames().add("Prvn\u00ed k\u016f\u0148");
107 final String n2 = "Druh\u00fd hlem\u00fd\u017e\u010f, star\u0161\u00ed";
108 arr.getNicknames().add(n2);
109 arr.getAge().add(33);
110 arr.getAge().add(73);
113 final String json = arr.toString();
115 JSONTokener t = new JSONTokener(json);
118 o = new JSONObject(t);
119 } catch (JSONException ex) {
120 throw new AssertionError("Can't parse " + json, ex);
123 assertEquals(o.getJSONArray("info").getJSONObject(0).getString("firstName"), "One");
124 assertEquals(o.getJSONArray("nicknames").getString(1), n2);
125 assertEquals(o.getJSONArray("age").getInt(1), 73);
129 @OnReceive(url="/{url}")
130 static void fetch(Person p, JSONik model) {
134 @OnReceive(url="/{url}")
135 static void fetchArray(Person[] p, JSONik model) {
136 model.setFetchedCount(p.length);
137 model.setFetched(p[0]);
140 @OnReceive(url="/{url}")
141 static void fetchPeople(People p, JSONik model) {
142 model.setFetchedCount(p.getInfo().size());
143 model.setFetched(p.getInfo().get(0));
146 @OnReceive(url="/{url}")
147 static void fetchPeopleAge(People p, JSONik model) {
149 for (int a : p.getAge()) {
152 model.setFetchedCount(sum);
155 @Http(@Http.Resource(
156 content = "{'firstName': 'Sitar', 'sex': 'MALE'}",
158 mimeType = "application/json"
160 @BrwsrTest public void loadAndParseJSON() throws InterruptedException {
165 js.fetch("person.json");
168 Person p = js.getFetched();
170 throw new InterruptedException();
173 assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
174 assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
177 @OnReceive(url="/{url}?callme={me}", jsonp = "me")
178 static void fetchViaJSONP(Person p, JSONik model) {
182 @Http(@Http.Resource(
183 content = "$0({'firstName': 'Mitar', 'sex': 'MALE'})",
185 mimeType = "application/javascript",
186 parameters = { "callme" }
188 @BrwsrTest public void loadAndParseJSONP() throws InterruptedException {
191 orig = scriptElements();
192 assert orig > 0 : "There should be some scripts on the page";
197 js.fetchViaJSONP("person.json");
200 Person p = js.getFetched();
202 throw new InterruptedException();
205 assert "Mitar".equals(p.getFirstName()) : "Unexpected: " + p.getFirstName();
206 assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
208 int now = scriptElements();
210 assert orig == now : "The set of elements is unchanged. Delta: " + (now - orig);
213 @JavaScriptBody(args = { }, body = "return window.document.getElementsByTagName('script').length;")
214 private static native int scriptElements();
216 @Http(@Http.Resource(
217 content = "{'firstName': 'Sitar', 'sex': 'MALE'}",
219 mimeType = "application/json"
221 @BrwsrTest public void loadAndParseJSONSentToArray() throws InterruptedException {
226 js.fetchArray("person.json");
229 Person p = js.getFetched();
231 throw new InterruptedException();
234 assert p != null : "We should get our person back: " + p;
235 assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
236 assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
239 @Http(@Http.Resource(
240 content = "[{'firstName': 'Gitar', 'sex': 'FEMALE'}]",
242 mimeType = "application/json"
244 @BrwsrTest public void loadAndParseJSONArraySingle() throws InterruptedException {
249 js.fetch("person.json");
252 Person p = js.getFetched();
254 throw new InterruptedException();
257 assert p != null : "We should get our person back: " + p;
258 assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
259 assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
262 @Http(@Http.Resource(
263 content = "{'info':[{'firstName': 'Gitar', 'sex': 'FEMALE'}]}",
265 mimeType = "application/json"
267 @BrwsrTest public void loadAndParseArrayInPeople() throws InterruptedException {
272 js.fetchPeople("people.json");
275 if (0 == js.getFetchedCount()) {
276 throw new InterruptedException();
279 assert js.getFetchedCount() == 1 : "One person loaded: " + js.getFetchedCount();
281 Person p = js.getFetched();
283 assert p != null : "We should get our person back: " + p;
284 assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
285 assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
288 @Http(@Http.Resource(
289 content = "{'age':[1, 2, 3]}",
291 mimeType = "application/json"
293 @BrwsrTest public void loadAndParseArrayOfIntegers() throws InterruptedException {
298 js.fetchPeopleAge("people.json");
301 if (0 == js.getFetchedCount()) {
302 throw new InterruptedException();
305 assert js.getFetchedCount() == 6 : "1 + 2 + 3 is " + js.getFetchedCount();
308 @OnReceive(url="/{url}")
309 static void fetchPeopleSex(People p, JSONik model) {
310 model.setFetchedCount(1);
311 model.getFetchedSex().addAll(p.getSex());
315 @Http(@Http.Resource(
316 content = "{'sex':['FEMALE', 'MALE', 'MALE']}",
318 mimeType = "application/json"
320 @BrwsrTest public void loadAndParseArrayOfEnums() throws InterruptedException {
325 js.fetchPeopleSex("people.json");
328 if (0 == js.getFetchedCount()) {
329 throw new InterruptedException();
332 assert js.getFetchedCount() == 1 : "Loaded";
334 assert js.getFetchedSex().size() == 3 : "Three values " + js.getFetchedSex();
335 assert js.getFetchedSex().get(0) == Sex.FEMALE : "Female first " + js.getFetchedSex();
336 assert js.getFetchedSex().get(1) == Sex.MALE : "male 2nd " + js.getFetchedSex();
337 assert js.getFetchedSex().get(2) == Sex.MALE : "male 3rd " + js.getFetchedSex();
340 @Http(@Http.Resource(
341 content = "[{'firstName': 'Gitar', 'sex': 'FEMALE'},"
342 + "{'firstName': 'Peter', 'sex': 'MALE'}"
345 mimeType = "application/json"
347 @BrwsrTest public void loadAndParseJSONArray() throws InterruptedException {
351 js.fetchArray("person.json");
355 Person p = js.getFetched();
357 throw new InterruptedException();
360 assert js.getFetchedCount() == 2 : "We got two values: " + js.getFetchedCount();
361 assert p != null : "We should get our person back: " + p;
362 assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
363 assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
366 @Factory public static Object[] create() {
367 return VMTest.create(JSONTest.class);