javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/JSONTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 05 Apr 2013 12:43:17 +0200
branchmodel
changeset 934 19b4ddc302a6
parent 921 cd0a40987abb
child 935 2cd6f67472c4
permissions -rw-r--r--
@OnReceive annotation can obtain and process single JSON object
     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.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;
    33 
    34 /** Need to verify that models produce reasonable JSON objects.
    35  *
    36  * @author Jaroslav Tulach <jtulach@netbeans.org>
    37  */
    38 @Page(xhtml = "Empty.html", className = "JSONik", properties = {
    39     @Property(name = "fetched", type = PersonImpl.class)
    40 })
    41 public class JSONTest {
    42     
    43     @Test public void personToString() throws JSONException {
    44         Person p = new Person();
    45         p.setSex(Sex.MALE);
    46         p.setFirstName("Jarda");
    47         p.setLastName("Tulach");
    48         
    49         JSONTokener t = new JSONTokener(p.toString());
    50         JSONObject o;
    51         try {
    52             o = new JSONObject(t);
    53         } catch (JSONException ex) {
    54             throw new AssertionError("Can't parse " + p.toString(), ex);
    55         }
    56         
    57         Iterator it = o.sortedKeys();
    58         assertEquals(it.next(), "firstName");
    59         assertEquals(it.next(), "lastName");
    60         assertEquals(it.next(), "sex");
    61         
    62         assertEquals(o.getString("firstName"), "Jarda");
    63         assertEquals(o.getString("lastName"), "Tulach");
    64         assertEquals(o.getString("sex"), "MALE");
    65     }
    66     
    67     @Test public void personWithWildCharactersAndNulls() throws JSONException {
    68         Person p = new Person();
    69         p.setFirstName("'\"\n");
    70         p.setLastName("\t\r\u0002");
    71         
    72         JSONTokener t = new JSONTokener(p.toString());
    73         JSONObject o;
    74         try {
    75             o = new JSONObject(t);
    76         } catch (JSONException ex) {
    77             throw new AssertionError("Can't parse " + p.toString(), ex);
    78         }
    79         
    80         Iterator it = o.sortedKeys();
    81         assertEquals(it.next(), "firstName");
    82         assertEquals(it.next(), "lastName");
    83         assertEquals(it.next(), "sex");
    84         
    85         assertEquals(o.getString("firstName"), p.getFirstName());
    86         assertEquals(o.getString("lastName"), p.getLastName());
    87         assertEquals(o.get("sex"), JSONObject.NULL);
    88     }
    89     
    90     @Test public void personsInArray() throws JSONException {
    91         Person p1 = new Person();
    92         p1.setFirstName("One");
    93 
    94         Person p2 = new Person();
    95         p2.setFirstName("Two");
    96         
    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);
   105         
   106         
   107         final String json = arr.toString();
   108         
   109         JSONTokener t = new JSONTokener(json);
   110         JSONObject o;
   111         try {
   112             o = new JSONObject(t);
   113         } catch (JSONException ex) {
   114             throw new AssertionError("Can't parse " + json, ex);
   115         }
   116 
   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);
   120     }
   121     
   122     
   123     @OnReceive(url="/{url}")
   124     static void fetch(Person p, JSONik model) {
   125         model.setFetched(p);
   126         throw new IllegalStateException("Got him: " + p);
   127     }
   128     
   129     @Http(@Http.Resource(
   130         content = "{'firstName': 'Sitar', 'sex': 'MALE'}", 
   131         path="/person.json", 
   132         mimeType = "application/json"
   133     ))
   134     @BrwsrTest public void loadAndParseJSON() {
   135         JSONik js = new JSONik();
   136         js.applyBindings();
   137         
   138         js.fetch("person.json");
   139         
   140         Person p = null;
   141         for (int i = 0; i < 10000000; i++) {
   142             if (js.getFetched() != null) {
   143                 p = js.getFetched();
   144             }
   145         }
   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();
   149     }
   150     
   151     @Http(@Http.Resource(
   152         content = "[{'firstName': 'Sitar', 'sex': 'MALE'}]", 
   153         path="/person.json", 
   154         mimeType = "application/json"
   155     ))
   156     @BrwsrTest public void loadAndParseJSONArray() {
   157         JSONik js = new JSONik();
   158         js.applyBindings();
   159         
   160         js.fetch("person.json");
   161         
   162         Person p = null;
   163         for (int i = 0; i < 10000000; i++) {
   164             if (js.getFetched() != null) {
   165                 p = js.getFetched();
   166             }
   167         }
   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();
   171     }
   172     
   173     @Factory public static Object[] create() {
   174         return VMTest.create(JSONTest.class);
   175     }
   176     
   177 }