Support for null enum values. Transplanted from 7a7686e6f875
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 @BrwsrTest public void toJSONInABrowser() throws Throwable {
74 Person p = new Person();
76 p.setFirstName("Jarda");
77 p.setLastName("Tulach");
81 json = parseJSON(p.toString());
82 } catch (Throwable ex) {
83 throw new IllegalStateException("Can't parse " + p).initCause(ex);
86 Person p2 = new Person(json);
88 assert p2.getFirstName().equals(p.getFirstName()) :
89 "Should be the same: " + p.getFirstName() + " != " + p2.getFirstName();
92 @Test public void personWithWildCharactersAndNulls() throws JSONException {
93 Person p = new Person();
94 p.setFirstName("'\"\n");
95 p.setLastName("\t\r\u0002");
97 JSONTokener t = new JSONTokener(p.toString());
100 o = new JSONObject(t);
101 } catch (JSONException ex) {
102 throw new AssertionError("Can't parse " + p.toString(), ex);
105 Iterator it = o.sortedKeys();
106 assertEquals(it.next(), "firstName");
107 assertEquals(it.next(), "lastName");
108 assertEquals(it.next(), "sex");
110 assertEquals(o.getString("firstName"), p.getFirstName());
111 assertEquals(o.getString("lastName"), p.getLastName());
112 assertEquals(o.get("sex"), JSONObject.NULL);
115 @Test public void personsInArray() throws JSONException {
116 Person p1 = new Person();
117 p1.setFirstName("One");
119 Person p2 = new Person();
120 p2.setFirstName("Two");
122 People arr = new People();
123 arr.getInfo().add(p1);
124 arr.getInfo().add(p2);
125 arr.getNicknames().add("Prvn\u00ed k\u016f\u0148");
126 final String n2 = "Druh\u00fd hlem\u00fd\u017e\u010f, star\u0161\u00ed";
127 arr.getNicknames().add(n2);
128 arr.getAge().add(33);
129 arr.getAge().add(73);
132 final String json = arr.toString();
134 JSONTokener t = new JSONTokener(json);
137 o = new JSONObject(t);
138 } catch (JSONException ex) {
139 throw new AssertionError("Can't parse " + json, ex);
142 assertEquals(o.getJSONArray("info").getJSONObject(0).getString("firstName"), "One");
143 assertEquals(o.getJSONArray("nicknames").getString(1), n2);
144 assertEquals(o.getJSONArray("age").getInt(1), 73);
148 @OnReceive(url="/{url}")
149 static void fetch(Person p, JSONik model) {
153 @OnReceive(url="/{url}")
154 static void fetchArray(Person[] p, JSONik model) {
155 model.setFetchedCount(p.length);
156 model.setFetched(p[0]);
159 @OnReceive(url="/{url}")
160 static void fetchPeople(People p, JSONik model) {
161 model.setFetchedCount(p.getInfo().size());
162 model.setFetched(p.getInfo().get(0));
165 @OnReceive(url="/{url}")
166 static void fetchPeopleAge(People p, JSONik model) {
168 for (int a : p.getAge()) {
171 model.setFetchedCount(sum);
174 @Http(@Http.Resource(
175 content = "{'firstName': 'Sitar', 'sex': 'MALE'}",
177 mimeType = "application/json"
179 @BrwsrTest public void loadAndParseJSON() throws InterruptedException {
184 js.fetch("person.json");
187 Person p = js.getFetched();
189 throw new InterruptedException();
192 assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
193 assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
196 @OnReceive(url="/{url}?callme={me}", jsonp = "me")
197 static void fetchViaJSONP(Person p, JSONik model) {
201 @Http(@Http.Resource(
202 content = "$0({'firstName': 'Mitar', 'sex': 'MALE'})",
204 mimeType = "application/javascript",
205 parameters = { "callme" }
207 @BrwsrTest public void loadAndParseJSONP() throws InterruptedException {
210 orig = scriptElements();
211 assert orig > 0 : "There should be some scripts on the page";
216 js.fetchViaJSONP("person.json");
219 Person p = js.getFetched();
221 throw new InterruptedException();
224 assert "Mitar".equals(p.getFirstName()) : "Unexpected: " + p.getFirstName();
225 assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
227 int now = scriptElements();
229 assert orig == now : "The set of elements is unchanged. Delta: " + (now - orig);
232 @JavaScriptBody(args = { }, body = "return window.document.getElementsByTagName('script').length;")
233 private static native int scriptElements();
235 @JavaScriptBody(args = { "s" }, body = "return window.JSON.parse(s);")
236 private static native Object parseJSON(String s);
238 @Http(@Http.Resource(
239 content = "{'firstName': 'Sitar', 'sex': 'MALE'}",
241 mimeType = "application/json"
243 @BrwsrTest public void loadAndParseJSONSentToArray() throws InterruptedException {
248 js.fetchArray("person.json");
251 Person p = js.getFetched();
253 throw new InterruptedException();
256 assert p != null : "We should get our person back: " + p;
257 assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
258 assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
261 @Http(@Http.Resource(
262 content = "[{'firstName': 'Gitar', 'sex': 'FEMALE'}]",
264 mimeType = "application/json"
266 @BrwsrTest public void loadAndParseJSONArraySingle() throws InterruptedException {
271 js.fetch("person.json");
274 Person p = js.getFetched();
276 throw new InterruptedException();
279 assert p != null : "We should get our person back: " + p;
280 assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
281 assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
284 @Http(@Http.Resource(
285 content = "{'info':[{'firstName': 'Gitar', 'sex': 'FEMALE'}]}",
287 mimeType = "application/json"
289 @BrwsrTest public void loadAndParseArrayInPeople() throws InterruptedException {
294 js.fetchPeople("people.json");
297 if (0 == js.getFetchedCount()) {
298 throw new InterruptedException();
301 assert js.getFetchedCount() == 1 : "One person loaded: " + js.getFetchedCount();
303 Person p = js.getFetched();
305 assert p != null : "We should get our person back: " + p;
306 assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
307 assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
310 @Http(@Http.Resource(
311 content = "{'age':[1, 2, 3]}",
313 mimeType = "application/json"
315 @BrwsrTest public void loadAndParseArrayOfIntegers() throws InterruptedException {
320 js.fetchPeopleAge("people.json");
323 if (0 == js.getFetchedCount()) {
324 throw new InterruptedException();
327 assert js.getFetchedCount() == 6 : "1 + 2 + 3 is " + js.getFetchedCount();
330 @OnReceive(url="/{url}")
331 static void fetchPeopleSex(People p, JSONik model) {
332 model.setFetchedCount(1);
333 model.getFetchedSex().addAll(p.getSex());
337 @Http(@Http.Resource(
338 content = "{'sex':['FEMALE', 'MALE', 'MALE']}",
340 mimeType = "application/json"
342 @BrwsrTest public void loadAndParseArrayOfEnums() throws InterruptedException {
347 js.fetchPeopleSex("people.json");
350 if (0 == js.getFetchedCount()) {
351 throw new InterruptedException();
354 assert js.getFetchedCount() == 1 : "Loaded";
356 assert js.getFetchedSex().size() == 3 : "Three values " + js.getFetchedSex();
357 assert js.getFetchedSex().get(0) == Sex.FEMALE : "Female first " + js.getFetchedSex();
358 assert js.getFetchedSex().get(1) == Sex.MALE : "male 2nd " + js.getFetchedSex();
359 assert js.getFetchedSex().get(2) == Sex.MALE : "male 3rd " + js.getFetchedSex();
362 @Http(@Http.Resource(
363 content = "[{'firstName': 'Gitar', 'sex': 'FEMALE'},"
364 + "{'firstName': 'Peter', 'sex': 'MALE'}"
367 mimeType = "application/json"
369 @BrwsrTest public void loadAndParseJSONArray() throws InterruptedException {
373 js.fetchArray("person.json");
377 Person p = js.getFetched();
379 throw new InterruptedException();
382 assert js.getFetchedCount() == 2 : "We got two values: " + js.getFetchedCount();
383 assert p != null : "We should get our person back: " + p;
384 assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
385 assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
388 @Factory public static Object[] create() {
389 return VMTest.create(JSONTest.class);