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 java.util.List;
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)
44 public class JSONTest {
47 @Test public void personToString() throws JSONException {
48 Person p = new Person();
50 p.setFirstName("Jarda");
51 p.setLastName("Tulach");
53 JSONTokener t = new JSONTokener(p.toString());
56 o = new JSONObject(t);
57 } catch (JSONException ex) {
58 throw new AssertionError("Can't parse " + p.toString(), ex);
61 Iterator it = o.sortedKeys();
62 assertEquals(it.next(), "firstName");
63 assertEquals(it.next(), "lastName");
64 assertEquals(it.next(), "sex");
66 assertEquals(o.getString("firstName"), "Jarda");
67 assertEquals(o.getString("lastName"), "Tulach");
68 assertEquals(o.getString("sex"), "MALE");
71 @Test public void personWithWildCharactersAndNulls() throws JSONException {
72 Person p = new Person();
73 p.setFirstName("'\"\n");
74 p.setLastName("\t\r\u0002");
76 JSONTokener t = new JSONTokener(p.toString());
79 o = new JSONObject(t);
80 } catch (JSONException ex) {
81 throw new AssertionError("Can't parse " + p.toString(), ex);
84 Iterator it = o.sortedKeys();
85 assertEquals(it.next(), "firstName");
86 assertEquals(it.next(), "lastName");
87 assertEquals(it.next(), "sex");
89 assertEquals(o.getString("firstName"), p.getFirstName());
90 assertEquals(o.getString("lastName"), p.getLastName());
91 assertEquals(o.get("sex"), JSONObject.NULL);
94 @Test public void personsInArray() throws JSONException {
95 Person p1 = new Person();
96 p1.setFirstName("One");
98 Person p2 = new Person();
99 p2.setFirstName("Two");
101 People arr = new People();
102 arr.getInfo().add(p1);
103 arr.getInfo().add(p2);
104 arr.getNicknames().add("Prvn\u00ed k\u016f\u0148");
105 final String n2 = "Druh\u00fd hlem\u00fd\u017e\u010f, star\u0161\u00ed";
106 arr.getNicknames().add(n2);
107 arr.getAge().add(33);
108 arr.getAge().add(73);
111 final String json = arr.toString();
113 JSONTokener t = new JSONTokener(json);
116 o = new JSONObject(t);
117 } catch (JSONException ex) {
118 throw new AssertionError("Can't parse " + json, ex);
121 assertEquals(o.getJSONArray("info").getJSONObject(0).getString("firstName"), "One");
122 assertEquals(o.getJSONArray("nicknames").getString(1), n2);
123 assertEquals(o.getJSONArray("age").getInt(1), 73);
127 @OnReceive(url="/{url}")
128 static void fetch(Person p, JSONik model) {
132 @OnReceive(url="/{url}")
133 static void fetchArray(Person[] p, JSONik model) {
134 model.setFetchedCount(p.length);
135 model.setFetched(p[0]);
138 @OnReceive(url="/{url}")
139 static void fetchPeople(People p, JSONik model) {
140 model.setFetchedCount(p.getInfo().size());
141 model.setFetched(p.getInfo().get(0));
144 @OnReceive(url="/{url}")
145 static void fetchPeopleAge(People p, JSONik model) {
147 for (int a : p.getAge()) {
150 model.setFetchedCount(sum);
153 @Http(@Http.Resource(
154 content = "{'firstName': 'Sitar', 'sex': 'MALE'}",
156 mimeType = "application/json"
158 @BrwsrTest public void loadAndParseJSON() throws InterruptedException {
163 js.fetch("person.json");
166 Person p = js.getFetched();
168 throw new InterruptedException();
171 assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
172 // assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
175 @OnReceive(url="/{url}?callme={me}", jsonp = "me")
176 static void fetchViaJSONP(Person p, JSONik model) {
180 @Http(@Http.Resource(
181 content = "$0({'firstName': 'Mitar', 'sex': 'MALE'})",
183 mimeType = "application/javascript",
184 parameters = { "callme" }
186 @BrwsrTest public void loadAndParseJSONP() throws InterruptedException {
191 js.fetchViaJSONP("person.json");
194 Person p = js.getFetched();
196 throw new InterruptedException();
199 assert "Mitar".equals(p.getFirstName()) : "Unexpected: " + p.getFirstName();
200 // assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
203 @Http(@Http.Resource(
204 content = "{'firstName': 'Sitar', 'sex': 'MALE'}",
206 mimeType = "application/json"
208 @BrwsrTest public void loadAndParseJSONSentToArray() throws InterruptedException {
213 js.fetchArray("person.json");
216 Person p = js.getFetched();
218 throw new InterruptedException();
221 assert p != null : "We should get our person back: " + p;
222 assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
223 // assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
226 @Http(@Http.Resource(
227 content = "[{'firstName': 'Gitar', 'sex': 'FEMALE'}]",
229 mimeType = "application/json"
231 @BrwsrTest public void loadAndParseJSONArraySingle() throws InterruptedException {
236 js.fetch("person.json");
239 Person p = js.getFetched();
241 throw new InterruptedException();
244 assert p != null : "We should get our person back: " + p;
245 assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
246 // assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
249 @Http(@Http.Resource(
250 content = "{'info':[{'firstName': 'Gitar', 'sex': 'FEMALE'}]}",
252 mimeType = "application/json"
254 @BrwsrTest public void loadAndParseArrayInPeople() throws InterruptedException {
259 js.fetchPeople("people.json");
262 if (0 == js.getFetchedCount()) {
263 throw new InterruptedException();
266 assert js.getFetchedCount() == 1 : "One person loaded: " + js.getFetchedCount();
268 Person p = js.getFetched();
270 assert p != null : "We should get our person back: " + p;
271 assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
272 // assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
275 @Http(@Http.Resource(
276 content = "{'age':[1, 2, 3]}",
278 mimeType = "application/json"
280 @BrwsrTest public void loadAndParseArrayOfIntegers() throws InterruptedException {
285 js.fetchPeopleAge("people.json");
288 if (0 == js.getFetchedCount()) {
289 throw new InterruptedException();
292 assert js.getFetchedCount() == 6 : "1 + 2 + 3 is " + js.getFetchedCount();
295 @Http(@Http.Resource(
296 content = "[{'firstName': 'Gitar', 'sex': 'FEMALE'},"
297 + "{'firstName': 'Peter', 'sex': 'MALE'}"
300 mimeType = "application/json"
302 @BrwsrTest public void loadAndParseJSONArray() throws InterruptedException {
306 js.fetchArray("person.json");
310 Person p = js.getFetched();
312 throw new InterruptedException();
315 assert js.getFetchedCount() == 2 : "We got two values: " + js.getFetchedCount();
316 assert p != null : "We should get our person back: " + p;
317 assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
318 // assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
321 @Factory public static Object[] create() {
322 return VMTest.create(JSONTest.class);