javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/JSONTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 09 Apr 2013 10:06:19 +0200
branchmodel
changeset 960 4887e22cb810
parent 954 6448c284fe21
child 963 62d77cc38117
permissions -rw-r--r--
Support for enums
     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     @Property(name = "fetchedCount", type = int.class),
    41     @Property(name = "fetchedSex", type = Sex.class, array = true)
    42 })
    43 public class JSONTest {
    44     private JSONik js;
    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     }
   130 
   131     @OnReceive(url="/{url}")
   132     static void fetchArray(Person[] p, JSONik model) {
   133         model.setFetchedCount(p.length);
   134         model.setFetched(p[0]);
   135     }
   136     
   137     @OnReceive(url="/{url}")
   138     static void fetchPeople(People p, JSONik model) {
   139         model.setFetchedCount(p.getInfo().size());
   140         model.setFetched(p.getInfo().get(0));
   141     }
   142 
   143     @OnReceive(url="/{url}")
   144     static void fetchPeopleAge(People p, JSONik model) {
   145         int sum = 0;
   146         for (int a : p.getAge()) {
   147             sum += a;
   148         }
   149         model.setFetchedCount(sum);
   150     }
   151     
   152     @Http(@Http.Resource(
   153         content = "{'firstName': 'Sitar', 'sex': 'MALE'}", 
   154         path="/person.json", 
   155         mimeType = "application/json"
   156     ))
   157     @BrwsrTest public void loadAndParseJSON() throws InterruptedException {
   158         if (js == null) {
   159             js = new JSONik();
   160             js.applyBindings();
   161 
   162             js.fetch("person.json");
   163         }
   164     
   165         Person p = js.getFetched();
   166         if (p == null) {
   167             throw new InterruptedException();
   168         }
   169         
   170         assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
   171         assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
   172     }
   173     
   174     @OnReceive(url="/{url}?callme={me}", jsonp = "me")
   175     static void fetchViaJSONP(Person p, JSONik model) {
   176         model.setFetched(p);
   177     }
   178     
   179     @Http(@Http.Resource(
   180         content = "$0({'firstName': 'Mitar', 'sex': 'MALE'})", 
   181         path="/person.json", 
   182         mimeType = "application/javascript",
   183         parameters = { "callme" }
   184     ))
   185     @BrwsrTest public void loadAndParseJSONP() throws InterruptedException {
   186         if (js == null) {
   187             js = new JSONik();
   188             js.applyBindings();
   189 
   190             js.fetchViaJSONP("person.json");
   191         }
   192     
   193         Person p = js.getFetched();
   194         if (p == null) {
   195             throw new InterruptedException();
   196         }
   197         
   198         assert "Mitar".equals(p.getFirstName()) : "Unexpected: " + p.getFirstName();
   199         assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
   200     }
   201 
   202     @Http(@Http.Resource(
   203         content = "{'firstName': 'Sitar', 'sex': 'MALE'}", 
   204         path="/person.json", 
   205         mimeType = "application/json"
   206     ))
   207     @BrwsrTest public void loadAndParseJSONSentToArray() throws InterruptedException {
   208         if (js == null) {
   209             js = new JSONik();
   210             js.applyBindings();
   211 
   212             js.fetchArray("person.json");
   213         }
   214         
   215         Person p = js.getFetched();
   216         if (p == null) {
   217             throw new InterruptedException();
   218         }
   219         
   220         assert p != null : "We should get our person back: " + p;
   221         assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
   222         assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
   223     }
   224     
   225     @Http(@Http.Resource(
   226         content = "[{'firstName': 'Gitar', 'sex': 'FEMALE'}]", 
   227         path="/person.json", 
   228         mimeType = "application/json"
   229     ))
   230     @BrwsrTest public void loadAndParseJSONArraySingle() throws InterruptedException {
   231         if (js == null) {
   232             js = new JSONik();
   233             js.applyBindings();
   234         
   235             js.fetch("person.json");
   236         }
   237         
   238         Person p = js.getFetched();
   239         if (p == null) {
   240             throw new InterruptedException();
   241         }
   242         
   243         assert p != null : "We should get our person back: " + p;
   244         assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
   245         assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
   246     }
   247     
   248     @Http(@Http.Resource(
   249         content = "{'info':[{'firstName': 'Gitar', 'sex': 'FEMALE'}]}", 
   250         path="/people.json", 
   251         mimeType = "application/json"
   252     ))
   253     @BrwsrTest public void loadAndParseArrayInPeople() throws InterruptedException {
   254         if (js == null) {
   255             js = new JSONik();
   256             js.applyBindings();
   257         
   258             js.fetchPeople("people.json");
   259         }
   260         
   261         if (0 == js.getFetchedCount()) {
   262             throw new InterruptedException();
   263         }
   264 
   265         assert js.getFetchedCount() == 1 : "One person loaded: " + js.getFetchedCount();
   266         
   267         Person p = js.getFetched();
   268         
   269         assert p != null : "We should get our person back: " + p;
   270         assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
   271         assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
   272     }
   273     
   274     @Http(@Http.Resource(
   275         content = "{'age':[1, 2, 3]}", 
   276         path="/people.json", 
   277         mimeType = "application/json"
   278     ))
   279     @BrwsrTest public void loadAndParseArrayOfIntegers() throws InterruptedException {
   280         if (js == null) {
   281             js = new JSONik();
   282             js.applyBindings();
   283         
   284             js.fetchPeopleAge("people.json");
   285         }
   286         
   287         if (0 == js.getFetchedCount()) {
   288             throw new InterruptedException();
   289         }
   290 
   291         assert js.getFetchedCount() == 6 : "1 + 2 + 3 is " + js.getFetchedCount();
   292     }
   293     
   294     @OnReceive(url="/{url}")
   295     static void fetchPeopleSex(People p, JSONik model) {
   296         model.setFetchedCount(1);
   297         model.getFetchedSex().addAll(p.getSex());
   298     }
   299     
   300     
   301     @Http(@Http.Resource(
   302         content = "{'sex':['FEMALE', 'MALE', 'MALE']}", 
   303         path="/people.json", 
   304         mimeType = "application/json"
   305     ))
   306     @BrwsrTest public void loadAndParseArrayOfEnums() throws InterruptedException {
   307         if (js == null) {
   308             js = new JSONik();
   309             js.applyBindings();
   310         
   311             js.fetchPeopleSex("people.json");
   312         }
   313         
   314         if (0 == js.getFetchedCount()) {
   315             throw new InterruptedException();
   316         }
   317 
   318         assert js.getFetchedCount() == 1 : "Loaded";
   319         
   320         assert js.getFetchedSex().size() == 3 : "Three values " + js.getFetchedSex();
   321         assert js.getFetchedSex().get(0) == Sex.FEMALE : "Female first " + js.getFetchedSex();
   322         assert js.getFetchedSex().get(1) == Sex.MALE : "male 2nd " + js.getFetchedSex();
   323         assert js.getFetchedSex().get(2) == Sex.MALE : "male 3rd " + js.getFetchedSex();
   324     }
   325     
   326     @Http(@Http.Resource(
   327         content = "[{'firstName': 'Gitar', 'sex': 'FEMALE'},"
   328         + "{'firstName': 'Peter', 'sex': 'MALE'}"
   329         + "]", 
   330         path="/person.json", 
   331         mimeType = "application/json"
   332     ))
   333     @BrwsrTest public void loadAndParseJSONArray() throws InterruptedException {
   334         if (js == null) {
   335             js = new JSONik();
   336             js.applyBindings();
   337             js.fetchArray("person.json");
   338         }
   339         
   340         
   341         Person p = js.getFetched();
   342         if (p == null) {
   343             throw new InterruptedException();
   344         }
   345         
   346         assert js.getFetchedCount() == 2 : "We got two values: " + js.getFetchedCount();
   347         assert p != null : "We should get our person back: " + p;
   348         assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
   349         assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
   350     }
   351 
   352     @Factory public static Object[] create() {
   353         return VMTest.create(JSONTest.class);
   354     }
   355     
   356 }