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 {
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) {
129 throw new IllegalStateException("Got him: " + p);
132 @OnReceive(url="/{url}")
133 static void fetchArray(Person[] p, JSONik model) {
134 model.setFetchedCount(p.length);
135 model.setFetched(p[0]);
136 throw new IllegalStateException("Got in array him: " + Arrays.asList(p));
139 @Http(@Http.Resource(
140 content = "{'firstName': 'Sitar', 'sex': 'MALE'}",
142 mimeType = "application/json"
144 @BrwsrTest public void loadAndParseJSON() {
145 JSONik js = new JSONik();
148 js.fetch("person.json");
151 for (int i = 0; i < 10000000; i++) {
152 if (js.getFetched() != null) {
156 assert p != null : "We should get our person back: " + p;
157 assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
158 assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
161 @Http(@Http.Resource(
162 content = "{'firstName': 'Sitar', 'sex': 'MALE'}",
164 mimeType = "application/json"
166 @BrwsrTest public void loadAndParseJSONSentToArray() {
167 JSONik js = new JSONik();
170 js.fetchArray("person.json");
173 for (int i = 0; i < 10000000; i++) {
174 if (js.getFetched() != null) {
178 assert p != null : "We should get our person back: " + p;
179 assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
180 assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
183 @Http(@Http.Resource(
184 content = "[{'firstName': 'Gitar', 'sex': 'FEMALE'}]",
186 mimeType = "application/json"
188 @BrwsrTest public void loadAndParseJSONArraySingle() {
189 JSONik js = new JSONik();
192 js.fetch("person.json");
195 for (int i = 0; i < 10000000; i++) {
196 if (js.getFetched() != null) {
200 assert p != null : "We should get our person back: " + p;
201 assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
202 assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
206 @Http(@Http.Resource(
207 content = "[{'firstName': 'Gitar', 'sex': 'FEMALE'}"
208 + "{'firstName': 'Peter', 'sex': 'MALE'}"
211 mimeType = "application/json"
213 @BrwsrTest public void loadAndParseJSONArray() {
214 JSONik js = new JSONik();
217 js.fetchArray("person.json");
220 for (int i = 0; i < 10000000; i++) {
221 if (js.getFetched() != null) {
226 assert js.getFetchedCount() == 2 : "We got two values: " + js.getFetchedCount();
227 assert p != null : "We should get our person back: " + p;
228 assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
229 assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
232 @Factory public static Object[] create() {
233 return VMTest.create(JSONTest.class);