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.htmlpage.api.OnReceive;
22 import org.apidesign.bck2brwsr.htmlpage.api.Page;
23 import org.apidesign.bck2brwsr.htmlpage.api.Property;
24 import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
25 import org.apidesign.bck2brwsr.vmtest.Http;
26 import org.apidesign.bck2brwsr.vmtest.VMTest;
27 import org.json.JSONException;
28 import org.json.JSONObject;
29 import org.json.JSONTokener;
30 import org.testng.annotations.Test;
31 import static org.testng.Assert.*;
32 import org.testng.annotations.Factory;
34 /** Need to verify that models produce reasonable JSON objects.
36 * @author Jaroslav Tulach <jtulach@netbeans.org>
38 @Page(xhtml = "Empty.html", className = "JSONik", properties = {
39 @Property(name = "fetched", type = PersonImpl.class),
40 @Property(name = "fetchedCount", type = int.class),
41 @Property(name = "fetchedSex", type = Sex.class, array = true)
43 public class JSONTest {
46 @Test public void personToString() throws JSONException {
47 Person p = new Person();
49 p.setFirstName("Jarda");
50 p.setLastName("Tulach");
52 JSONTokener t = new JSONTokener(p.toString());
55 o = new JSONObject(t);
56 } catch (JSONException ex) {
57 throw new AssertionError("Can't parse " + p.toString(), ex);
60 Iterator it = o.sortedKeys();
61 assertEquals(it.next(), "firstName");
62 assertEquals(it.next(), "lastName");
63 assertEquals(it.next(), "sex");
65 assertEquals(o.getString("firstName"), "Jarda");
66 assertEquals(o.getString("lastName"), "Tulach");
67 assertEquals(o.getString("sex"), "MALE");
70 @Test public void personWithWildCharactersAndNulls() throws JSONException {
71 Person p = new Person();
72 p.setFirstName("'\"\n");
73 p.setLastName("\t\r\u0002");
75 JSONTokener t = new JSONTokener(p.toString());
78 o = new JSONObject(t);
79 } catch (JSONException ex) {
80 throw new AssertionError("Can't parse " + p.toString(), ex);
83 Iterator it = o.sortedKeys();
84 assertEquals(it.next(), "firstName");
85 assertEquals(it.next(), "lastName");
86 assertEquals(it.next(), "sex");
88 assertEquals(o.getString("firstName"), p.getFirstName());
89 assertEquals(o.getString("lastName"), p.getLastName());
90 assertEquals(o.get("sex"), JSONObject.NULL);
93 @Test public void personsInArray() throws JSONException {
94 Person p1 = new Person();
95 p1.setFirstName("One");
97 Person p2 = new Person();
98 p2.setFirstName("Two");
100 People arr = new People();
101 arr.getInfo().add(p1);
102 arr.getInfo().add(p2);
103 arr.getNicknames().add("Prvn\u00ed k\u016f\u0148");
104 final String n2 = "Druh\u00fd hlem\u00fd\u017e\u010f, star\u0161\u00ed";
105 arr.getNicknames().add(n2);
106 arr.getAge().add(33);
107 arr.getAge().add(73);
110 final String json = arr.toString();
112 JSONTokener t = new JSONTokener(json);
115 o = new JSONObject(t);
116 } catch (JSONException ex) {
117 throw new AssertionError("Can't parse " + json, ex);
120 assertEquals(o.getJSONArray("info").getJSONObject(0).getString("firstName"), "One");
121 assertEquals(o.getJSONArray("nicknames").getString(1), n2);
122 assertEquals(o.getJSONArray("age").getInt(1), 73);
126 @OnReceive(url="/{url}")
127 static void fetch(Person p, JSONik model) {
131 @OnReceive(url="/{url}")
132 static void fetchArray(Person[] p, JSONik model) {
133 model.setFetchedCount(p.length);
134 model.setFetched(p[0]);
137 @OnReceive(url="/{url}")
138 static void fetchPeople(People p, JSONik model) {
139 model.setFetchedCount(p.getInfo().size());
140 model.setFetched(p.getInfo().get(0));
143 @OnReceive(url="/{url}")
144 static void fetchPeopleAge(People p, JSONik model) {
146 for (int a : p.getAge()) {
149 model.setFetchedCount(sum);
152 @Http(@Http.Resource(
153 content = "{'firstName': 'Sitar', 'sex': 'MALE'}",
155 mimeType = "application/json"
157 @BrwsrTest public void loadAndParseJSON() throws InterruptedException {
162 js.fetch("person.json");
165 Person p = js.getFetched();
167 throw new InterruptedException();
170 assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
171 assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
174 @OnReceive(url="/{url}?callme={me}", jsonp = "me")
175 static void fetchViaJSONP(Person p, JSONik model) {
179 @Http(@Http.Resource(
180 content = "$0({'firstName': 'Mitar', 'sex': 'MALE'})",
182 mimeType = "application/javascript",
183 parameters = { "callme" }
185 @BrwsrTest public void loadAndParseJSONP() throws InterruptedException {
190 js.fetchViaJSONP("person.json");
193 Person p = js.getFetched();
195 throw new InterruptedException();
198 assert "Mitar".equals(p.getFirstName()) : "Unexpected: " + p.getFirstName();
199 assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
202 @Http(@Http.Resource(
203 content = "{'firstName': 'Sitar', 'sex': 'MALE'}",
205 mimeType = "application/json"
207 @BrwsrTest public void loadAndParseJSONSentToArray() throws InterruptedException {
212 js.fetchArray("person.json");
215 Person p = js.getFetched();
217 throw new InterruptedException();
220 assert p != null : "We should get our person back: " + p;
221 assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
222 assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
225 @Http(@Http.Resource(
226 content = "[{'firstName': 'Gitar', 'sex': 'FEMALE'}]",
228 mimeType = "application/json"
230 @BrwsrTest public void loadAndParseJSONArraySingle() throws InterruptedException {
235 js.fetch("person.json");
238 Person p = js.getFetched();
240 throw new InterruptedException();
243 assert p != null : "We should get our person back: " + p;
244 assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
245 assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
248 @Http(@Http.Resource(
249 content = "{'info':[{'firstName': 'Gitar', 'sex': 'FEMALE'}]}",
251 mimeType = "application/json"
253 @BrwsrTest public void loadAndParseArrayInPeople() throws InterruptedException {
258 js.fetchPeople("people.json");
261 if (0 == js.getFetchedCount()) {
262 throw new InterruptedException();
265 assert js.getFetchedCount() == 1 : "One person loaded: " + js.getFetchedCount();
267 Person p = js.getFetched();
269 assert p != null : "We should get our person back: " + p;
270 assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
271 assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
274 @Http(@Http.Resource(
275 content = "{'age':[1, 2, 3]}",
277 mimeType = "application/json"
279 @BrwsrTest public void loadAndParseArrayOfIntegers() throws InterruptedException {
284 js.fetchPeopleAge("people.json");
287 if (0 == js.getFetchedCount()) {
288 throw new InterruptedException();
291 assert js.getFetchedCount() == 6 : "1 + 2 + 3 is " + js.getFetchedCount();
294 @OnReceive(url="/{url}")
295 static void fetchPeopleSex(People p, JSONik model) {
296 model.setFetchedCount(1);
297 model.getFetchedSex().addAll(p.getSex());
301 @Http(@Http.Resource(
302 content = "{'sex':['FEMALE', 'MALE', 'MALE']}",
304 mimeType = "application/json"
306 @BrwsrTest public void loadAndParseArrayOfEnums() throws InterruptedException {
311 js.fetchPeopleSex("people.json");
314 if (0 == js.getFetchedCount()) {
315 throw new InterruptedException();
318 assert js.getFetchedCount() == 1 : "Loaded";
320 assert js.getFetchedSex().size() == 3 : "Three values " + js.getFetchedSex();
321 assert js.getFetchedSex().get(0) == Sex.FEMALE : "Female first " + js.getFetchedSex();
322 assert js.getFetchedSex().get(1) == Sex.MALE : "male 2nd " + js.getFetchedSex();
323 assert js.getFetchedSex().get(2) == Sex.MALE : "male 3rd " + js.getFetchedSex();
326 @Http(@Http.Resource(
327 content = "[{'firstName': 'Gitar', 'sex': 'FEMALE'},"
328 + "{'firstName': 'Peter', 'sex': 'MALE'}"
331 mimeType = "application/json"
333 @BrwsrTest public void loadAndParseJSONArray() throws InterruptedException {
337 js.fetchArray("person.json");
341 Person p = js.getFetched();
343 throw new InterruptedException();
346 assert js.getFetchedCount() == 2 : "We got two values: " + js.getFetchedCount();
347 assert p != null : "We should get our person back: " + p;
348 assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
349 assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
352 @Factory public static Object[] create() {
353 return VMTest.create(JSONTest.class);