javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/JSONTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 07 Apr 2013 21:41:57 +0200
branchmodel
changeset 944 1e2b0dcc8326
parent 943 28aae214c202
child 954 6448c284fe21
permissions -rw-r--r--
Can fetch JSON numbers in an array
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.bck2brwsr.htmlpage;
    19 
    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;
    35 
    36 /** Need to verify that models produce reasonable JSON objects.
    37  *
    38  * @author Jaroslav Tulach <jtulach@netbeans.org>
    39  */
    40 @Page(xhtml = "Empty.html", className = "JSONik", properties = {
    41     @Property(name = "fetched", type = PersonImpl.class),
    42     @Property(name = "fetchedCount", type = int.class)
    43 })
    44 public class JSONTest {
    45     private JSONik js;
    46     
    47     @Test public void personToString() throws JSONException {
    48         Person p = new Person();
    49         p.setSex(Sex.MALE);
    50         p.setFirstName("Jarda");
    51         p.setLastName("Tulach");
    52         
    53         JSONTokener t = new JSONTokener(p.toString());
    54         JSONObject o;
    55         try {
    56             o = new JSONObject(t);
    57         } catch (JSONException ex) {
    58             throw new AssertionError("Can't parse " + p.toString(), ex);
    59         }
    60         
    61         Iterator it = o.sortedKeys();
    62         assertEquals(it.next(), "firstName");
    63         assertEquals(it.next(), "lastName");
    64         assertEquals(it.next(), "sex");
    65         
    66         assertEquals(o.getString("firstName"), "Jarda");
    67         assertEquals(o.getString("lastName"), "Tulach");
    68         assertEquals(o.getString("sex"), "MALE");
    69     }
    70     
    71     @Test public void personWithWildCharactersAndNulls() throws JSONException {
    72         Person p = new Person();
    73         p.setFirstName("'\"\n");
    74         p.setLastName("\t\r\u0002");
    75         
    76         JSONTokener t = new JSONTokener(p.toString());
    77         JSONObject o;
    78         try {
    79             o = new JSONObject(t);
    80         } catch (JSONException ex) {
    81             throw new AssertionError("Can't parse " + p.toString(), ex);
    82         }
    83         
    84         Iterator it = o.sortedKeys();
    85         assertEquals(it.next(), "firstName");
    86         assertEquals(it.next(), "lastName");
    87         assertEquals(it.next(), "sex");
    88         
    89         assertEquals(o.getString("firstName"), p.getFirstName());
    90         assertEquals(o.getString("lastName"), p.getLastName());
    91         assertEquals(o.get("sex"), JSONObject.NULL);
    92     }
    93     
    94     @Test public void personsInArray() throws JSONException {
    95         Person p1 = new Person();
    96         p1.setFirstName("One");
    97 
    98         Person p2 = new Person();
    99         p2.setFirstName("Two");
   100         
   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);
   109         
   110         
   111         final String json = arr.toString();
   112         
   113         JSONTokener t = new JSONTokener(json);
   114         JSONObject o;
   115         try {
   116             o = new JSONObject(t);
   117         } catch (JSONException ex) {
   118             throw new AssertionError("Can't parse " + json, ex);
   119         }
   120 
   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);
   124     }
   125     
   126     
   127     @OnReceive(url="/{url}")
   128     static void fetch(Person p, JSONik model) {
   129         model.setFetched(p);
   130     }
   131 
   132     @OnReceive(url="/{url}")
   133     static void fetchArray(Person[] p, JSONik model) {
   134         model.setFetchedCount(p.length);
   135         model.setFetched(p[0]);
   136     }
   137     
   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));
   142     }
   143 
   144     @OnReceive(url="/{url}")
   145     static void fetchPeopleAge(People p, JSONik model) {
   146         int sum = 0;
   147         for (int a : p.getAge()) {
   148             sum += a;
   149         }
   150         model.setFetchedCount(sum);
   151     }
   152     
   153     @Http(@Http.Resource(
   154         content = "{'firstName': 'Sitar', 'sex': 'MALE'}", 
   155         path="/person.json", 
   156         mimeType = "application/json"
   157     ))
   158     @BrwsrTest public void loadAndParseJSON() throws InterruptedException {
   159         if (js == null) {
   160             js = new JSONik();
   161             js.applyBindings();
   162 
   163             js.fetch("person.json");
   164         }
   165     
   166         Person p = js.getFetched();
   167         if (p == null) {
   168             throw new InterruptedException();
   169         }
   170         
   171         assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
   172       //  assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
   173     }
   174 
   175     @Http(@Http.Resource(
   176         content = "{'firstName': 'Sitar', 'sex': 'MALE'}", 
   177         path="/person.json", 
   178         mimeType = "application/json"
   179     ))
   180     @BrwsrTest public void loadAndParseJSONSentToArray() throws InterruptedException {
   181         if (js == null) {
   182             js = new JSONik();
   183             js.applyBindings();
   184 
   185             js.fetchArray("person.json");
   186         }
   187         
   188         Person p = js.getFetched();
   189         if (p == null) {
   190             throw new InterruptedException();
   191         }
   192         
   193         assert p != null : "We should get our person back: " + p;
   194         assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
   195 //        assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
   196     }
   197     
   198     @Http(@Http.Resource(
   199         content = "[{'firstName': 'Gitar', 'sex': 'FEMALE'}]", 
   200         path="/person.json", 
   201         mimeType = "application/json"
   202     ))
   203     @BrwsrTest public void loadAndParseJSONArraySingle() throws InterruptedException {
   204         if (js == null) {
   205             js = new JSONik();
   206             js.applyBindings();
   207         
   208             js.fetch("person.json");
   209         }
   210         
   211         Person p = js.getFetched();
   212         if (p == null) {
   213             throw new InterruptedException();
   214         }
   215         
   216         assert p != null : "We should get our person back: " + p;
   217         assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
   218 //        assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
   219     }
   220     
   221     @Http(@Http.Resource(
   222         content = "{'info':[{'firstName': 'Gitar', 'sex': 'FEMALE'}]}", 
   223         path="/people.json", 
   224         mimeType = "application/json"
   225     ))
   226     @BrwsrTest public void loadAndParseArrayInPeople() throws InterruptedException {
   227         if (js == null) {
   228             js = new JSONik();
   229             js.applyBindings();
   230         
   231             js.fetchPeople("people.json");
   232         }
   233         
   234         if (0 == js.getFetchedCount()) {
   235             throw new InterruptedException();
   236         }
   237 
   238         assert js.getFetchedCount() == 1 : "One person loaded: " + js.getFetchedCount();
   239         
   240         Person p = js.getFetched();
   241         
   242         assert p != null : "We should get our person back: " + p;
   243         assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
   244 //        assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
   245     }
   246     
   247     @Http(@Http.Resource(
   248         content = "{'age':[1, 2, 3]}", 
   249         path="/people.json", 
   250         mimeType = "application/json"
   251     ))
   252     @BrwsrTest public void loadAndParseArrayOfIntegers() throws InterruptedException {
   253         if (js == null) {
   254             js = new JSONik();
   255             js.applyBindings();
   256         
   257             js.fetchPeopleAge("people.json");
   258         }
   259         
   260         if (0 == js.getFetchedCount()) {
   261             throw new InterruptedException();
   262         }
   263 
   264         assert js.getFetchedCount() == 6 : "1 + 2 + 3 is " + js.getFetchedCount();
   265     }
   266     
   267     @Http(@Http.Resource(
   268         content = "[{'firstName': 'Gitar', 'sex': 'FEMALE'},"
   269         + "{'firstName': 'Peter', 'sex': 'MALE'}"
   270         + "]", 
   271         path="/person.json", 
   272         mimeType = "application/json"
   273     ))
   274     @BrwsrTest public void loadAndParseJSONArray() throws InterruptedException {
   275         if (js == null) {
   276             js = new JSONik();
   277             js.applyBindings();
   278             js.fetchArray("person.json");
   279         }
   280         
   281         
   282         Person p = js.getFetched();
   283         if (p == null) {
   284             throw new InterruptedException();
   285         }
   286         
   287         assert js.getFetchedCount() == 2 : "We got two values: " + js.getFetchedCount();
   288         assert p != null : "We should get our person back: " + p;
   289         assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
   290 //        assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
   291     }
   292 
   293     @Factory public static Object[] create() {
   294         return VMTest.create(JSONTest.class);
   295     }
   296     
   297 }