javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/JSONTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 05 Apr 2013 17:55:23 +0200
branchmodel
changeset 936 6fe5b2b4b82e
parent 935 2cd6f67472c4
child 941 d1e482f73507
permissions -rw-r--r--
@OnReceive is more ready to receive an array of values
     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     
    46     @Test public void personToString() throws JSONException {
    47         Person p = new Person();
    48         p.setSex(Sex.MALE);
    49         p.setFirstName("Jarda");
    50         p.setLastName("Tulach");
    51         
    52         JSONTokener t = new JSONTokener(p.toString());
    53         JSONObject o;
    54         try {
    55             o = new JSONObject(t);
    56         } catch (JSONException ex) {
    57             throw new AssertionError("Can't parse " + p.toString(), ex);
    58         }
    59         
    60         Iterator it = o.sortedKeys();
    61         assertEquals(it.next(), "firstName");
    62         assertEquals(it.next(), "lastName");
    63         assertEquals(it.next(), "sex");
    64         
    65         assertEquals(o.getString("firstName"), "Jarda");
    66         assertEquals(o.getString("lastName"), "Tulach");
    67         assertEquals(o.getString("sex"), "MALE");
    68     }
    69     
    70     @Test public void personWithWildCharactersAndNulls() throws JSONException {
    71         Person p = new Person();
    72         p.setFirstName("'\"\n");
    73         p.setLastName("\t\r\u0002");
    74         
    75         JSONTokener t = new JSONTokener(p.toString());
    76         JSONObject o;
    77         try {
    78             o = new JSONObject(t);
    79         } catch (JSONException ex) {
    80             throw new AssertionError("Can't parse " + p.toString(), ex);
    81         }
    82         
    83         Iterator it = o.sortedKeys();
    84         assertEquals(it.next(), "firstName");
    85         assertEquals(it.next(), "lastName");
    86         assertEquals(it.next(), "sex");
    87         
    88         assertEquals(o.getString("firstName"), p.getFirstName());
    89         assertEquals(o.getString("lastName"), p.getLastName());
    90         assertEquals(o.get("sex"), JSONObject.NULL);
    91     }
    92     
    93     @Test public void personsInArray() throws JSONException {
    94         Person p1 = new Person();
    95         p1.setFirstName("One");
    96 
    97         Person p2 = new Person();
    98         p2.setFirstName("Two");
    99         
   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);
   108         
   109         
   110         final String json = arr.toString();
   111         
   112         JSONTokener t = new JSONTokener(json);
   113         JSONObject o;
   114         try {
   115             o = new JSONObject(t);
   116         } catch (JSONException ex) {
   117             throw new AssertionError("Can't parse " + json, ex);
   118         }
   119 
   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);
   123     }
   124     
   125     
   126     @OnReceive(url="/{url}")
   127     static void fetch(Person p, JSONik model) {
   128         model.setFetched(p);
   129         throw new IllegalStateException("Got him: " + 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         throw new IllegalStateException("Got in array him: " + Arrays.asList(p));
   137     }
   138     
   139     @Http(@Http.Resource(
   140         content = "{'firstName': 'Sitar', 'sex': 'MALE'}", 
   141         path="/person.json", 
   142         mimeType = "application/json"
   143     ))
   144     @BrwsrTest public void loadAndParseJSON() {
   145         JSONik js = new JSONik();
   146         js.applyBindings();
   147         
   148         js.fetch("person.json");
   149         
   150         Person p = null;
   151         for (int i = 0; i < 10000000; i++) {
   152             if (js.getFetched() != null) {
   153                 p = js.getFetched();
   154             }
   155         }
   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();
   159     }
   160     
   161     @Http(@Http.Resource(
   162         content = "{'firstName': 'Sitar', 'sex': 'MALE'}", 
   163         path="/person.json", 
   164         mimeType = "application/json"
   165     ))
   166     @BrwsrTest public void loadAndParseJSONSentToArray() {
   167         JSONik js = new JSONik();
   168         js.applyBindings();
   169         
   170         js.fetchArray("person.json");
   171         
   172         Person p = null;
   173         for (int i = 0; i < 10000000; i++) {
   174             if (js.getFetched() != null) {
   175                 p = js.getFetched();
   176             }
   177         }
   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();
   181     }
   182     
   183     @Http(@Http.Resource(
   184         content = "[{'firstName': 'Gitar', 'sex': 'FEMALE'}]", 
   185         path="/person.json", 
   186         mimeType = "application/json"
   187     ))
   188     @BrwsrTest public void loadAndParseJSONArraySingle() {
   189         JSONik js = new JSONik();
   190         js.applyBindings();
   191         
   192         js.fetch("person.json");
   193         
   194         Person p = null;
   195         for (int i = 0; i < 10000000; i++) {
   196             if (js.getFetched() != null) {
   197                 p = js.getFetched();
   198             }
   199         }
   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();
   203     }
   204     
   205     
   206     @Http(@Http.Resource(
   207         content = "[{'firstName': 'Gitar', 'sex': 'FEMALE'}"
   208         + "{'firstName': 'Peter', 'sex': 'MALE'}"
   209         + "]", 
   210         path="/person.json", 
   211         mimeType = "application/json"
   212     ))
   213     @BrwsrTest public void loadAndParseJSONArray() {
   214         JSONik js = new JSONik();
   215         js.applyBindings();
   216         
   217         js.fetchArray("person.json");
   218         
   219         Person p = null;
   220         for (int i = 0; i < 10000000; i++) {
   221             if (js.getFetched() != null) {
   222                 p = js.getFetched();
   223             }
   224         }
   225         
   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();
   230     }
   231     
   232     @Factory public static Object[] create() {
   233         return VMTest.create(JSONTest.class);
   234     }
   235     
   236 }