1.1 --- a/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/JSONTest.java Sat Jan 10 19:38:00 2015 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,393 +0,0 @@
1.4 -/**
1.5 - * Back 2 Browser Bytecode Translator
1.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 - *
1.8 - * This program is free software: you can redistribute it and/or modify
1.9 - * it under the terms of the GNU General Public License as published by
1.10 - * the Free Software Foundation, version 2 of the License.
1.11 - *
1.12 - * This program is distributed in the hope that it will be useful,
1.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 - * GNU General Public License for more details.
1.16 - *
1.17 - * You should have received a copy of the GNU General Public License
1.18 - * along with this program. Look for COPYING file in the top folder.
1.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 - */
1.21 -package org.apidesign.bck2brwsr.htmlpage;
1.22 -
1.23 -import java.util.Iterator;
1.24 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.25 -import org.apidesign.bck2brwsr.htmlpage.api.OnReceive;
1.26 -import org.apidesign.bck2brwsr.htmlpage.api.Page;
1.27 -import org.apidesign.bck2brwsr.htmlpage.api.Property;
1.28 -import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
1.29 -import org.apidesign.bck2brwsr.vmtest.Http;
1.30 -import org.apidesign.bck2brwsr.vmtest.VMTest;
1.31 -import org.json.JSONException;
1.32 -import org.json.JSONObject;
1.33 -import org.json.JSONTokener;
1.34 -import org.testng.annotations.Test;
1.35 -import static org.testng.Assert.*;
1.36 -import org.testng.annotations.Factory;
1.37 -
1.38 -/** Need to verify that models produce reasonable JSON objects.
1.39 - *
1.40 - * @author Jaroslav Tulach <jtulach@netbeans.org>
1.41 - */
1.42 -@Page(xhtml = "Empty.html", className = "JSONik", properties = {
1.43 - @Property(name = "fetched", type = PersonImpl.class),
1.44 - @Property(name = "fetchedCount", type = int.class),
1.45 - @Property(name = "fetchedSex", type = Sex.class, array = true)
1.46 -})
1.47 -public class JSONTest {
1.48 - private JSONik js;
1.49 - private Integer orig;
1.50 -
1.51 - @Test public void personToString() throws JSONException {
1.52 - Person p = new Person();
1.53 - p.setSex(Sex.MALE);
1.54 - p.setFirstName("Jarda");
1.55 - p.setLastName("Tulach");
1.56 -
1.57 - JSONTokener t = new JSONTokener(p.toString());
1.58 - JSONObject o;
1.59 - try {
1.60 - o = new JSONObject(t);
1.61 - } catch (JSONException ex) {
1.62 - throw new AssertionError("Can't parse " + p.toString(), ex);
1.63 - }
1.64 -
1.65 - Iterator it = o.sortedKeys();
1.66 - assertEquals(it.next(), "firstName");
1.67 - assertEquals(it.next(), "lastName");
1.68 - assertEquals(it.next(), "sex");
1.69 -
1.70 - assertEquals(o.getString("firstName"), "Jarda");
1.71 - assertEquals(o.getString("lastName"), "Tulach");
1.72 - assertEquals(o.getString("sex"), "MALE");
1.73 - }
1.74 -
1.75 - @BrwsrTest public void toJSONInABrowser() throws Throwable {
1.76 - Person p = new Person();
1.77 - p.setSex(Sex.MALE);
1.78 - p.setFirstName("Jarda");
1.79 - p.setLastName("Tulach");
1.80 -
1.81 - Object json;
1.82 - try {
1.83 - json = parseJSON(p.toString());
1.84 - } catch (Throwable ex) {
1.85 - throw new IllegalStateException("Can't parse " + p).initCause(ex);
1.86 - }
1.87 -
1.88 - Person p2 = new Person(json);
1.89 -
1.90 - assert p2.getFirstName().equals(p.getFirstName()) :
1.91 - "Should be the same: " + p.getFirstName() + " != " + p2.getFirstName();
1.92 - }
1.93 -
1.94 - @Test public void personWithWildCharactersAndNulls() throws JSONException {
1.95 - Person p = new Person();
1.96 - p.setFirstName("'\"\n");
1.97 - p.setLastName("\t\r\u0002");
1.98 -
1.99 - JSONTokener t = new JSONTokener(p.toString());
1.100 - JSONObject o;
1.101 - try {
1.102 - o = new JSONObject(t);
1.103 - } catch (JSONException ex) {
1.104 - throw new AssertionError("Can't parse " + p.toString(), ex);
1.105 - }
1.106 -
1.107 - Iterator it = o.sortedKeys();
1.108 - assertEquals(it.next(), "firstName");
1.109 - assertEquals(it.next(), "lastName");
1.110 - assertEquals(it.next(), "sex");
1.111 -
1.112 - assertEquals(o.getString("firstName"), p.getFirstName());
1.113 - assertEquals(o.getString("lastName"), p.getLastName());
1.114 - assertEquals(o.get("sex"), JSONObject.NULL);
1.115 - }
1.116 -
1.117 - @Test public void personsInArray() throws JSONException {
1.118 - Person p1 = new Person();
1.119 - p1.setFirstName("One");
1.120 -
1.121 - Person p2 = new Person();
1.122 - p2.setFirstName("Two");
1.123 -
1.124 - People arr = new People();
1.125 - arr.getInfo().add(p1);
1.126 - arr.getInfo().add(p2);
1.127 - arr.getNicknames().add("Prvn\u00ed k\u016f\u0148");
1.128 - final String n2 = "Druh\u00fd hlem\u00fd\u017e\u010f, star\u0161\u00ed";
1.129 - arr.getNicknames().add(n2);
1.130 - arr.getAge().add(33);
1.131 - arr.getAge().add(73);
1.132 -
1.133 -
1.134 - final String json = arr.toString();
1.135 -
1.136 - JSONTokener t = new JSONTokener(json);
1.137 - JSONObject o;
1.138 - try {
1.139 - o = new JSONObject(t);
1.140 - } catch (JSONException ex) {
1.141 - throw new AssertionError("Can't parse " + json, ex);
1.142 - }
1.143 -
1.144 - assertEquals(o.getJSONArray("info").getJSONObject(0).getString("firstName"), "One");
1.145 - assertEquals(o.getJSONArray("nicknames").getString(1), n2);
1.146 - assertEquals(o.getJSONArray("age").getInt(1), 73);
1.147 - }
1.148 -
1.149 -
1.150 - @OnReceive(url="/{url}")
1.151 - static void fetch(Person p, JSONik model) {
1.152 - model.setFetched(p);
1.153 - }
1.154 -
1.155 - @OnReceive(url="/{url}")
1.156 - static void fetchArray(Person[] p, JSONik model) {
1.157 - model.setFetchedCount(p.length);
1.158 - model.setFetched(p[0]);
1.159 - }
1.160 -
1.161 - @OnReceive(url="/{url}")
1.162 - static void fetchPeople(People p, JSONik model) {
1.163 - model.setFetchedCount(p.getInfo().size());
1.164 - model.setFetched(p.getInfo().get(0));
1.165 - }
1.166 -
1.167 - @OnReceive(url="/{url}")
1.168 - static void fetchPeopleAge(People p, JSONik model) {
1.169 - int sum = 0;
1.170 - for (int a : p.getAge()) {
1.171 - sum += a;
1.172 - }
1.173 - model.setFetchedCount(sum);
1.174 - }
1.175 -
1.176 - @Http(@Http.Resource(
1.177 - content = "{'firstName': 'Sitar', 'sex': 'MALE'}",
1.178 - path="/person33.json",
1.179 - mimeType = "application/json"
1.180 - ))
1.181 - @BrwsrTest public void loadAndParseJSON() throws InterruptedException {
1.182 - try { throw new Exception(); } catch (Exception ex) {
1.183 - }
1.184 - if (js == null) {
1.185 - js = new JSONik();
1.186 - js.applyBindings();
1.187 -
1.188 - js.fetch("person33.json");
1.189 - }
1.190 -
1.191 - Person p = js.getFetched();
1.192 - if (p == null) {
1.193 - throw new InterruptedException();
1.194 - }
1.195 -
1.196 - assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
1.197 - assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
1.198 - }
1.199 -
1.200 - @OnReceive(url="/{url}?callme={me}", jsonp = "me")
1.201 - static void fetchViaJSONP(Person p, JSONik model) {
1.202 - model.setFetched(p);
1.203 - }
1.204 -
1.205 - @Http(@Http.Resource(
1.206 - content = "$0({'firstName': 'Mitar', 'sex': 'MALE'})",
1.207 - path="/person22.json",
1.208 - mimeType = "application/javascript",
1.209 - parameters = { "callme" }
1.210 - ))
1.211 - @BrwsrTest public void loadAndParseJSONP() throws InterruptedException {
1.212 -
1.213 - if (js == null) {
1.214 - orig = scriptElements();
1.215 - assert orig > 0 : "There should be some scripts on the page";
1.216 -
1.217 - js = new JSONik();
1.218 - js.applyBindings();
1.219 -
1.220 - js.fetchViaJSONP("person22.json");
1.221 - }
1.222 -
1.223 - Person p = js.getFetched();
1.224 - if (p == null) {
1.225 - throw new InterruptedException();
1.226 - }
1.227 -
1.228 - assert "Mitar".equals(p.getFirstName()) : "Unexpected: " + p.getFirstName();
1.229 - assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
1.230 -
1.231 - int now = scriptElements();
1.232 -
1.233 - assert orig == now : "The set of elements is unchanged. Delta: " + (now - orig);
1.234 - }
1.235 -
1.236 - @JavaScriptBody(args = { }, body = "return window.document.getElementsByTagName('script').length;")
1.237 - private static native int scriptElements();
1.238 -
1.239 - @JavaScriptBody(args = { "s" }, body = "return window.JSON.parse(s);")
1.240 - private static native Object parseJSON(String s);
1.241 -
1.242 - @Http(@Http.Resource(
1.243 - content = "{'firstName': 'Sitar', 'sex': 'MALE'}",
1.244 - path="/person15.json",
1.245 - mimeType = "application/json"
1.246 - ))
1.247 - @BrwsrTest public void loadAndParseJSONSentToArray() throws InterruptedException {
1.248 - if (js == null) {
1.249 - js = new JSONik();
1.250 - js.applyBindings();
1.251 -
1.252 - js.fetchArray("person15.json");
1.253 - }
1.254 -
1.255 - Person p = js.getFetched();
1.256 - if (p == null) {
1.257 - throw new InterruptedException();
1.258 - }
1.259 -
1.260 - assert p != null : "We should get our person back: " + p;
1.261 - assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
1.262 - assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
1.263 - }
1.264 -
1.265 - @Http(@Http.Resource(
1.266 - content = "[{'firstName': 'Gitar', 'sex': 'FEMALE'}]",
1.267 - path="/person5.json",
1.268 - mimeType = "application/json"
1.269 - ))
1.270 - @BrwsrTest public void loadAndParseJSONArraySingle() throws InterruptedException {
1.271 - if (js == null) {
1.272 - js = new JSONik();
1.273 - js.applyBindings();
1.274 -
1.275 - js.fetch("person5.json");
1.276 - }
1.277 -
1.278 - Person p = js.getFetched();
1.279 - if (p == null) {
1.280 - throw new InterruptedException();
1.281 - }
1.282 -
1.283 - assert p != null : "We should get our person back: " + p;
1.284 - assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
1.285 - assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
1.286 - }
1.287 -
1.288 -// @Http(@Http.Resource(
1.289 -// content = "{'info':[{'firstName': 'Gitar', 'sex': 'FEMALE'}]}",
1.290 -// path="/people.json",
1.291 -// mimeType = "application/json"
1.292 -// ))
1.293 -// @BrwsrTest public void loadAndParseArrayInPeople() throws InterruptedException {
1.294 -// if (js == null) {
1.295 -// js = new JSONik();
1.296 -// js.applyBindings();
1.297 -//
1.298 -// js.fetchPeople("people.json");
1.299 -// }
1.300 -//
1.301 -// if (0 == js.getFetchedCount()) {
1.302 -// throw new InterruptedException();
1.303 -// }
1.304 -//
1.305 -// assert js.getFetchedCount() == 1 : "One person loaded: " + js.getFetchedCount();
1.306 -//
1.307 -// Person p = js.getFetched();
1.308 -//
1.309 -// assert p != null : "We should get our person back: " + p;
1.310 -// assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
1.311 -// assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
1.312 -// }
1.313 -
1.314 - @Http(@Http.Resource(
1.315 - content = "{'age':[1, 2, 3]}",
1.316 - path="/people8.json",
1.317 - mimeType = "application/json"
1.318 - ))
1.319 - @BrwsrTest public void loadAndParseArrayOfIntegers() throws InterruptedException {
1.320 - if (js == null) {
1.321 - js = new JSONik();
1.322 - js.applyBindings();
1.323 -
1.324 - js.fetchPeopleAge("people8.json");
1.325 - }
1.326 -
1.327 - if (0 == js.getFetchedCount()) {
1.328 - throw new InterruptedException();
1.329 - }
1.330 -
1.331 - assert js.getFetchedCount() == 6 : "1 + 2 + 3 is " + js.getFetchedCount();
1.332 - }
1.333 -
1.334 - @OnReceive(url="/{url}")
1.335 - static void fetchPeopleSex(People p, JSONik model) {
1.336 - model.setFetchedCount(1);
1.337 - model.getFetchedSex().addAll(p.getSex());
1.338 - }
1.339 -
1.340 -
1.341 - @Http(@Http.Resource(
1.342 - content = "{'sex':['FEMALE', 'MALE', 'MALE']}",
1.343 - path="/people11.json",
1.344 - mimeType = "application/json"
1.345 - ))
1.346 - @BrwsrTest public void loadAndParseArrayOfEnums() throws InterruptedException {
1.347 - if (js == null) {
1.348 - js = new JSONik();
1.349 - js.applyBindings();
1.350 -
1.351 - js.fetchPeopleSex("people11.json");
1.352 - }
1.353 -
1.354 - if (0 == js.getFetchedCount()) {
1.355 - throw new InterruptedException();
1.356 - }
1.357 -
1.358 - assert js.getFetchedCount() == 1 : "Loaded";
1.359 -
1.360 - assert js.getFetchedSex().size() == 3 : "Three values " + js.getFetchedSex();
1.361 - assert js.getFetchedSex().get(0) == Sex.FEMALE : "Female first " + js.getFetchedSex();
1.362 - assert js.getFetchedSex().get(1) == Sex.MALE : "male 2nd " + js.getFetchedSex();
1.363 - assert js.getFetchedSex().get(2) == Sex.MALE : "male 3rd " + js.getFetchedSex();
1.364 - }
1.365 -
1.366 - @Http(@Http.Resource(
1.367 - content = "[{'firstName': 'Gitar', 'sex': 'FEMALE'},"
1.368 - + "{'firstName': 'Peter', 'sex': 'MALE'}"
1.369 - + "]",
1.370 - path="/person13.json",
1.371 - mimeType = "application/json"
1.372 - ))
1.373 - @BrwsrTest public void loadAndParseJSONArray() throws InterruptedException {
1.374 - if (js == null) {
1.375 - js = new JSONik();
1.376 - js.applyBindings();
1.377 - js.fetchArray("person13.json");
1.378 - }
1.379 -
1.380 -
1.381 - Person p = js.getFetched();
1.382 - if (p == null) {
1.383 - throw new InterruptedException();
1.384 - }
1.385 -
1.386 - assert js.getFetchedCount() == 2 : "We got two values: " + js.getFetchedCount();
1.387 - assert p != null : "We should get our person back: " + p;
1.388 - assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
1.389 - assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
1.390 - }
1.391 -
1.392 - @Factory public static Object[] create() {
1.393 - return VMTest.create(JSONTest.class);
1.394 - }
1.395 -
1.396 -}