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)
41 public class JSONTest {
43 @Test public void personToString() throws JSONException {
44 Person p = new Person();
46 p.setFirstName("Jarda");
47 p.setLastName("Tulach");
49 JSONTokener t = new JSONTokener(p.toString());
52 o = new JSONObject(t);
53 } catch (JSONException ex) {
54 throw new AssertionError("Can't parse " + p.toString(), ex);
57 Iterator it = o.sortedKeys();
58 assertEquals(it.next(), "firstName");
59 assertEquals(it.next(), "lastName");
60 assertEquals(it.next(), "sex");
62 assertEquals(o.getString("firstName"), "Jarda");
63 assertEquals(o.getString("lastName"), "Tulach");
64 assertEquals(o.getString("sex"), "MALE");
67 @Test public void personWithWildCharactersAndNulls() throws JSONException {
68 Person p = new Person();
69 p.setFirstName("'\"\n");
70 p.setLastName("\t\r\u0002");
72 JSONTokener t = new JSONTokener(p.toString());
75 o = new JSONObject(t);
76 } catch (JSONException ex) {
77 throw new AssertionError("Can't parse " + p.toString(), ex);
80 Iterator it = o.sortedKeys();
81 assertEquals(it.next(), "firstName");
82 assertEquals(it.next(), "lastName");
83 assertEquals(it.next(), "sex");
85 assertEquals(o.getString("firstName"), p.getFirstName());
86 assertEquals(o.getString("lastName"), p.getLastName());
87 assertEquals(o.get("sex"), JSONObject.NULL);
90 @Test public void personsInArray() throws JSONException {
91 Person p1 = new Person();
92 p1.setFirstName("One");
94 Person p2 = new Person();
95 p2.setFirstName("Two");
97 People arr = new People();
98 arr.getInfo().add(p1);
99 arr.getInfo().add(p2);
100 arr.getNicknames().add("Prvn\u00ed k\u016f\u0148");
101 final String n2 = "Druh\u00fd hlem\u00fd\u017e\u010f, star\u0161\u00ed";
102 arr.getNicknames().add(n2);
103 arr.getAge().add(33);
104 arr.getAge().add(73);
107 final String json = arr.toString();
109 JSONTokener t = new JSONTokener(json);
112 o = new JSONObject(t);
113 } catch (JSONException ex) {
114 throw new AssertionError("Can't parse " + json, ex);
117 assertEquals(o.getJSONArray("info").getJSONObject(0).getString("firstName"), "One");
118 assertEquals(o.getJSONArray("nicknames").getString(1), n2);
119 assertEquals(o.getJSONArray("age").getInt(1), 73);
123 @OnReceive(url="/{url}")
124 static void fetch(Person p, JSONik model) {
126 throw new IllegalStateException("Got him: " + p);
129 @Http(@Http.Resource(
130 content = "{'firstName': 'Sitar', 'sex': 'MALE'}",
132 mimeType = "application/json"
134 @BrwsrTest public void loadAndParseJSON() {
135 JSONik js = new JSONik();
138 js.fetch("person.json");
141 for (int i = 0; i < 10000000; i++) {
142 if (js.getFetched() != null) {
146 assert p != null : "We should get our person back: " + p;
147 assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
148 assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
151 @Http(@Http.Resource(
152 content = "[{'firstName': 'Gitar', 'sex': 'FEMALE'}]",
154 mimeType = "application/json"
156 @BrwsrTest public void loadAndParseJSONArray() {
157 JSONik js = new JSONik();
160 js.fetch("person.json");
163 for (int i = 0; i < 10000000; i++) {
164 if (js.getFetched() != null) {
168 assert p != null : "We should get our person back: " + p;
169 assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
170 assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
173 @Factory public static Object[] create() {
174 return VMTest.create(JSONTest.class);