javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/JSONTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 18 Apr 2013 17:18:10 +0200
branchfx
changeset 1013 32502264b896
parent 1012 b22c86471837
permissions -rw-r--r--
scriptElements - another native method
jaroslav@919
     1
/**
jaroslav@919
     2
 * Back 2 Browser Bytecode Translator
jaroslav@919
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@919
     4
 *
jaroslav@919
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@919
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@919
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@919
     8
 *
jaroslav@919
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@919
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@919
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@919
    12
 * GNU General Public License for more details.
jaroslav@919
    13
 *
jaroslav@919
    14
 * You should have received a copy of the GNU General Public License
jaroslav@919
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@919
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@919
    17
 */
jaroslav@919
    18
package org.apidesign.bck2brwsr.htmlpage;
jaroslav@919
    19
jaroslav@919
    20
import java.util.Iterator;
jaroslav@1013
    21
import javafx.scene.web.WebEngine;
jaroslav@963
    22
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@934
    23
import org.apidesign.bck2brwsr.htmlpage.api.OnReceive;
jaroslav@934
    24
import org.apidesign.bck2brwsr.htmlpage.api.Page;
jaroslav@934
    25
import org.apidesign.bck2brwsr.htmlpage.api.Property;
jaroslav@934
    26
import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
jaroslav@934
    27
import org.apidesign.bck2brwsr.vmtest.Http;
jaroslav@934
    28
import org.apidesign.bck2brwsr.vmtest.VMTest;
jaroslav@919
    29
import org.json.JSONException;
jaroslav@919
    30
import org.json.JSONObject;
jaroslav@919
    31
import org.json.JSONTokener;
jaroslav@919
    32
import org.testng.annotations.Test;
jaroslav@919
    33
import static org.testng.Assert.*;
jaroslav@934
    34
import org.testng.annotations.Factory;
jaroslav@919
    35
jaroslav@919
    36
/** Need to verify that models produce reasonable JSON objects.
jaroslav@919
    37
 *
jaroslav@919
    38
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@919
    39
 */
jaroslav@934
    40
@Page(xhtml = "Empty.html", className = "JSONik", properties = {
jaroslav@1012
    41
    @Property(name = "fetched", type = Person.class),
jaroslav@960
    42
    @Property(name = "fetchedCount", type = int.class),
jaroslav@960
    43
    @Property(name = "fetchedSex", type = Sex.class, array = true)
jaroslav@934
    44
})
jaroslav@919
    45
public class JSONTest {
jaroslav@941
    46
    private JSONik js;
jaroslav@963
    47
    private Integer orig;
jaroslav@919
    48
    
jaroslav@919
    49
    @Test public void personToString() throws JSONException {
jaroslav@919
    50
        Person p = new Person();
jaroslav@919
    51
        p.setSex(Sex.MALE);
jaroslav@919
    52
        p.setFirstName("Jarda");
jaroslav@919
    53
        p.setLastName("Tulach");
jaroslav@919
    54
        
jaroslav@919
    55
        JSONTokener t = new JSONTokener(p.toString());
jaroslav@919
    56
        JSONObject o;
jaroslav@919
    57
        try {
jaroslav@919
    58
            o = new JSONObject(t);
jaroslav@919
    59
        } catch (JSONException ex) {
jaroslav@919
    60
            throw new AssertionError("Can't parse " + p.toString(), ex);
jaroslav@919
    61
        }
jaroslav@919
    62
        
jaroslav@919
    63
        Iterator it = o.sortedKeys();
jaroslav@919
    64
        assertEquals(it.next(), "firstName");
jaroslav@919
    65
        assertEquals(it.next(), "lastName");
jaroslav@919
    66
        assertEquals(it.next(), "sex");
jaroslav@919
    67
        
jaroslav@919
    68
        assertEquals(o.getString("firstName"), "Jarda");
jaroslav@919
    69
        assertEquals(o.getString("lastName"), "Tulach");
jaroslav@919
    70
        assertEquals(o.getString("sex"), "MALE");
jaroslav@919
    71
    }
jaroslav@919
    72
    
jaroslav@920
    73
    @Test public void personWithWildCharactersAndNulls() throws JSONException {
jaroslav@920
    74
        Person p = new Person();
jaroslav@920
    75
        p.setFirstName("'\"\n");
jaroslav@920
    76
        p.setLastName("\t\r\u0002");
jaroslav@920
    77
        
jaroslav@920
    78
        JSONTokener t = new JSONTokener(p.toString());
jaroslav@920
    79
        JSONObject o;
jaroslav@920
    80
        try {
jaroslav@920
    81
            o = new JSONObject(t);
jaroslav@920
    82
        } catch (JSONException ex) {
jaroslav@920
    83
            throw new AssertionError("Can't parse " + p.toString(), ex);
jaroslav@920
    84
        }
jaroslav@920
    85
        
jaroslav@920
    86
        Iterator it = o.sortedKeys();
jaroslav@920
    87
        assertEquals(it.next(), "firstName");
jaroslav@920
    88
        assertEquals(it.next(), "lastName");
jaroslav@920
    89
        assertEquals(it.next(), "sex");
jaroslav@920
    90
        
jaroslav@920
    91
        assertEquals(o.getString("firstName"), p.getFirstName());
jaroslav@920
    92
        assertEquals(o.getString("lastName"), p.getLastName());
jaroslav@920
    93
        assertEquals(o.get("sex"), JSONObject.NULL);
jaroslav@920
    94
    }
jaroslav@920
    95
    
jaroslav@921
    96
    @Test public void personsInArray() throws JSONException {
jaroslav@921
    97
        Person p1 = new Person();
jaroslav@921
    98
        p1.setFirstName("One");
jaroslav@921
    99
jaroslav@921
   100
        Person p2 = new Person();
jaroslav@921
   101
        p2.setFirstName("Two");
jaroslav@921
   102
        
jaroslav@921
   103
        People arr = new People();
jaroslav@921
   104
        arr.getInfo().add(p1);
jaroslav@921
   105
        arr.getInfo().add(p2);
jaroslav@921
   106
        arr.getNicknames().add("Prvn\u00ed k\u016f\u0148");
jaroslav@921
   107
        final String n2 = "Druh\u00fd hlem\u00fd\u017e\u010f, star\u0161\u00ed";
jaroslav@921
   108
        arr.getNicknames().add(n2);
jaroslav@921
   109
        arr.getAge().add(33);
jaroslav@921
   110
        arr.getAge().add(73);
jaroslav@921
   111
        
jaroslav@921
   112
        
jaroslav@921
   113
        final String json = arr.toString();
jaroslav@921
   114
        
jaroslav@921
   115
        JSONTokener t = new JSONTokener(json);
jaroslav@921
   116
        JSONObject o;
jaroslav@921
   117
        try {
jaroslav@921
   118
            o = new JSONObject(t);
jaroslav@921
   119
        } catch (JSONException ex) {
jaroslav@921
   120
            throw new AssertionError("Can't parse " + json, ex);
jaroslav@921
   121
        }
jaroslav@921
   122
jaroslav@921
   123
        assertEquals(o.getJSONArray("info").getJSONObject(0).getString("firstName"), "One");
jaroslav@921
   124
        assertEquals(o.getJSONArray("nicknames").getString(1), n2);
jaroslav@921
   125
        assertEquals(o.getJSONArray("age").getInt(1), 73);
jaroslav@921
   126
    }
jaroslav@934
   127
    
jaroslav@934
   128
    
jaroslav@934
   129
    @OnReceive(url="/{url}")
jaroslav@934
   130
    static void fetch(Person p, JSONik model) {
jaroslav@934
   131
        model.setFetched(p);
jaroslav@934
   132
    }
jaroslav@936
   133
jaroslav@936
   134
    @OnReceive(url="/{url}")
jaroslav@936
   135
    static void fetchArray(Person[] p, JSONik model) {
jaroslav@936
   136
        model.setFetchedCount(p.length);
jaroslav@936
   137
        model.setFetched(p[0]);
jaroslav@936
   138
    }
jaroslav@934
   139
    
jaroslav@943
   140
    @OnReceive(url="/{url}")
jaroslav@943
   141
    static void fetchPeople(People p, JSONik model) {
jaroslav@943
   142
        model.setFetchedCount(p.getInfo().size());
jaroslav@943
   143
        model.setFetched(p.getInfo().get(0));
jaroslav@943
   144
    }
jaroslav@944
   145
jaroslav@944
   146
    @OnReceive(url="/{url}")
jaroslav@944
   147
    static void fetchPeopleAge(People p, JSONik model) {
jaroslav@944
   148
        int sum = 0;
jaroslav@944
   149
        for (int a : p.getAge()) {
jaroslav@944
   150
            sum += a;
jaroslav@944
   151
        }
jaroslav@944
   152
        model.setFetchedCount(sum);
jaroslav@944
   153
    }
jaroslav@943
   154
    
jaroslav@934
   155
    @Http(@Http.Resource(
jaroslav@934
   156
        content = "{'firstName': 'Sitar', 'sex': 'MALE'}", 
jaroslav@934
   157
        path="/person.json", 
jaroslav@934
   158
        mimeType = "application/json"
jaroslav@934
   159
    ))
jaroslav@941
   160
    @BrwsrTest public void loadAndParseJSON() throws InterruptedException {
jaroslav@941
   161
        if (js == null) {
jaroslav@941
   162
            js = new JSONik();
jaroslav@941
   163
            js.applyBindings();
jaroslav@941
   164
jaroslav@941
   165
            js.fetch("person.json");
jaroslav@941
   166
        }
jaroslav@941
   167
    
jaroslav@941
   168
        Person p = js.getFetched();
jaroslav@941
   169
        if (p == null) {
jaroslav@941
   170
            throw new InterruptedException();
jaroslav@941
   171
        }
jaroslav@934
   172
        
jaroslav@934
   173
        assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
jaroslav@960
   174
        assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
jaroslav@934
   175
    }
jaroslav@954
   176
    
jaroslav@954
   177
    @OnReceive(url="/{url}?callme={me}", jsonp = "me")
jaroslav@954
   178
    static void fetchViaJSONP(Person p, JSONik model) {
jaroslav@954
   179
        model.setFetched(p);
jaroslav@954
   180
    }
jaroslav@954
   181
    
jaroslav@954
   182
    @Http(@Http.Resource(
jaroslav@954
   183
        content = "$0({'firstName': 'Mitar', 'sex': 'MALE'})", 
jaroslav@954
   184
        path="/person.json", 
jaroslav@954
   185
        mimeType = "application/javascript",
jaroslav@954
   186
        parameters = { "callme" }
jaroslav@954
   187
    ))
jaroslav@954
   188
    @BrwsrTest public void loadAndParseJSONP() throws InterruptedException {
jaroslav@963
   189
        
jaroslav@954
   190
        if (js == null) {
jaroslav@963
   191
            orig = scriptElements();
jaroslav@963
   192
            assert orig > 0 : "There should be some scripts on the page";
jaroslav@963
   193
            
jaroslav@954
   194
            js = new JSONik();
jaroslav@954
   195
            js.applyBindings();
jaroslav@954
   196
jaroslav@954
   197
            js.fetchViaJSONP("person.json");
jaroslav@954
   198
        }
jaroslav@954
   199
    
jaroslav@954
   200
        Person p = js.getFetched();
jaroslav@954
   201
        if (p == null) {
jaroslav@954
   202
            throw new InterruptedException();
jaroslav@954
   203
        }
jaroslav@954
   204
        
jaroslav@954
   205
        assert "Mitar".equals(p.getFirstName()) : "Unexpected: " + p.getFirstName();
jaroslav@960
   206
        assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
jaroslav@963
   207
        
jaroslav@963
   208
        int now = scriptElements();
jaroslav@963
   209
        
jaroslav@963
   210
        assert orig == now : "The set of elements is unchanged. Delta: " + (now - orig);
jaroslav@954
   211
    }
jaroslav@963
   212
    
jaroslav@963
   213
    @JavaScriptBody(args = {  }, body = "return window.document.getElementsByTagName('script').length;")
jaroslav@1013
   214
    private static int scriptElements() {
jaroslav@1013
   215
        WebEngine eng = (WebEngine) System.getProperties().get("webEngine");
jaroslav@1013
   216
        return (Integer) eng.executeScript(
jaroslav@1013
   217
            "window.document.getElementsByTagName('script').length"
jaroslav@1013
   218
        );
jaroslav@1013
   219
    }
jaroslav@941
   220
jaroslav@934
   221
    @Http(@Http.Resource(
jaroslav@936
   222
        content = "{'firstName': 'Sitar', 'sex': 'MALE'}", 
jaroslav@936
   223
        path="/person.json", 
jaroslav@936
   224
        mimeType = "application/json"
jaroslav@936
   225
    ))
jaroslav@941
   226
    @BrwsrTest public void loadAndParseJSONSentToArray() throws InterruptedException {
jaroslav@941
   227
        if (js == null) {
jaroslav@941
   228
            js = new JSONik();
jaroslav@941
   229
            js.applyBindings();
jaroslav@941
   230
jaroslav@941
   231
            js.fetchArray("person.json");
jaroslav@941
   232
        }
jaroslav@936
   233
        
jaroslav@941
   234
        Person p = js.getFetched();
jaroslav@941
   235
        if (p == null) {
jaroslav@941
   236
            throw new InterruptedException();
jaroslav@941
   237
        }
jaroslav@936
   238
        
jaroslav@936
   239
        assert p != null : "We should get our person back: " + p;
jaroslav@936
   240
        assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
jaroslav@960
   241
        assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
jaroslav@936
   242
    }
jaroslav@936
   243
    
jaroslav@936
   244
    @Http(@Http.Resource(
jaroslav@935
   245
        content = "[{'firstName': 'Gitar', 'sex': 'FEMALE'}]", 
jaroslav@934
   246
        path="/person.json", 
jaroslav@934
   247
        mimeType = "application/json"
jaroslav@934
   248
    ))
jaroslav@941
   249
    @BrwsrTest public void loadAndParseJSONArraySingle() throws InterruptedException {
jaroslav@941
   250
        if (js == null) {
jaroslav@941
   251
            js = new JSONik();
jaroslav@941
   252
            js.applyBindings();
jaroslav@934
   253
        
jaroslav@941
   254
            js.fetch("person.json");
jaroslav@941
   255
        }
jaroslav@934
   256
        
jaroslav@941
   257
        Person p = js.getFetched();
jaroslav@941
   258
        if (p == null) {
jaroslav@941
   259
            throw new InterruptedException();
jaroslav@934
   260
        }
jaroslav@941
   261
        
jaroslav@934
   262
        assert p != null : "We should get our person back: " + p;
jaroslav@941
   263
        assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
jaroslav@960
   264
        assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
jaroslav@934
   265
    }
jaroslav@934
   266
    
jaroslav@936
   267
    @Http(@Http.Resource(
jaroslav@943
   268
        content = "{'info':[{'firstName': 'Gitar', 'sex': 'FEMALE'}]}", 
jaroslav@943
   269
        path="/people.json", 
jaroslav@943
   270
        mimeType = "application/json"
jaroslav@943
   271
    ))
jaroslav@943
   272
    @BrwsrTest public void loadAndParseArrayInPeople() throws InterruptedException {
jaroslav@943
   273
        if (js == null) {
jaroslav@943
   274
            js = new JSONik();
jaroslav@943
   275
            js.applyBindings();
jaroslav@943
   276
        
jaroslav@943
   277
            js.fetchPeople("people.json");
jaroslav@943
   278
        }
jaroslav@943
   279
        
jaroslav@943
   280
        if (0 == js.getFetchedCount()) {
jaroslav@943
   281
            throw new InterruptedException();
jaroslav@943
   282
        }
jaroslav@943
   283
jaroslav@943
   284
        assert js.getFetchedCount() == 1 : "One person loaded: " + js.getFetchedCount();
jaroslav@943
   285
        
jaroslav@943
   286
        Person p = js.getFetched();
jaroslav@943
   287
        
jaroslav@943
   288
        assert p != null : "We should get our person back: " + p;
jaroslav@943
   289
        assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
jaroslav@960
   290
        assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
jaroslav@943
   291
    }
jaroslav@943
   292
    
jaroslav@943
   293
    @Http(@Http.Resource(
jaroslav@944
   294
        content = "{'age':[1, 2, 3]}", 
jaroslav@944
   295
        path="/people.json", 
jaroslav@944
   296
        mimeType = "application/json"
jaroslav@944
   297
    ))
jaroslav@944
   298
    @BrwsrTest public void loadAndParseArrayOfIntegers() throws InterruptedException {
jaroslav@944
   299
        if (js == null) {
jaroslav@944
   300
            js = new JSONik();
jaroslav@944
   301
            js.applyBindings();
jaroslav@944
   302
        
jaroslav@944
   303
            js.fetchPeopleAge("people.json");
jaroslav@944
   304
        }
jaroslav@944
   305
        
jaroslav@944
   306
        if (0 == js.getFetchedCount()) {
jaroslav@944
   307
            throw new InterruptedException();
jaroslav@944
   308
        }
jaroslav@944
   309
jaroslav@944
   310
        assert js.getFetchedCount() == 6 : "1 + 2 + 3 is " + js.getFetchedCount();
jaroslav@944
   311
    }
jaroslav@944
   312
    
jaroslav@960
   313
    @OnReceive(url="/{url}")
jaroslav@960
   314
    static void fetchPeopleSex(People p, JSONik model) {
jaroslav@960
   315
        model.setFetchedCount(1);
jaroslav@960
   316
        model.getFetchedSex().addAll(p.getSex());
jaroslav@960
   317
    }
jaroslav@960
   318
    
jaroslav@960
   319
    
jaroslav@960
   320
    @Http(@Http.Resource(
jaroslav@960
   321
        content = "{'sex':['FEMALE', 'MALE', 'MALE']}", 
jaroslav@960
   322
        path="/people.json", 
jaroslav@960
   323
        mimeType = "application/json"
jaroslav@960
   324
    ))
jaroslav@960
   325
    @BrwsrTest public void loadAndParseArrayOfEnums() throws InterruptedException {
jaroslav@960
   326
        if (js == null) {
jaroslav@960
   327
            js = new JSONik();
jaroslav@960
   328
            js.applyBindings();
jaroslav@960
   329
        
jaroslav@960
   330
            js.fetchPeopleSex("people.json");
jaroslav@960
   331
        }
jaroslav@960
   332
        
jaroslav@960
   333
        if (0 == js.getFetchedCount()) {
jaroslav@960
   334
            throw new InterruptedException();
jaroslav@960
   335
        }
jaroslav@960
   336
jaroslav@960
   337
        assert js.getFetchedCount() == 1 : "Loaded";
jaroslav@960
   338
        
jaroslav@960
   339
        assert js.getFetchedSex().size() == 3 : "Three values " + js.getFetchedSex();
jaroslav@960
   340
        assert js.getFetchedSex().get(0) == Sex.FEMALE : "Female first " + js.getFetchedSex();
jaroslav@960
   341
        assert js.getFetchedSex().get(1) == Sex.MALE : "male 2nd " + js.getFetchedSex();
jaroslav@960
   342
        assert js.getFetchedSex().get(2) == Sex.MALE : "male 3rd " + js.getFetchedSex();
jaroslav@960
   343
    }
jaroslav@960
   344
    
jaroslav@944
   345
    @Http(@Http.Resource(
jaroslav@941
   346
        content = "[{'firstName': 'Gitar', 'sex': 'FEMALE'},"
jaroslav@936
   347
        + "{'firstName': 'Peter', 'sex': 'MALE'}"
jaroslav@936
   348
        + "]", 
jaroslav@936
   349
        path="/person.json", 
jaroslav@936
   350
        mimeType = "application/json"
jaroslav@936
   351
    ))
jaroslav@941
   352
    @BrwsrTest public void loadAndParseJSONArray() throws InterruptedException {
jaroslav@941
   353
        if (js == null) {
jaroslav@941
   354
            js = new JSONik();
jaroslav@941
   355
            js.applyBindings();
jaroslav@941
   356
            js.fetchArray("person.json");
jaroslav@941
   357
        }
jaroslav@936
   358
        
jaroslav@936
   359
        
jaroslav@941
   360
        Person p = js.getFetched();
jaroslav@941
   361
        if (p == null) {
jaroslav@941
   362
            throw new InterruptedException();
jaroslav@936
   363
        }
jaroslav@936
   364
        
jaroslav@936
   365
        assert js.getFetchedCount() == 2 : "We got two values: " + js.getFetchedCount();
jaroslav@936
   366
        assert p != null : "We should get our person back: " + p;
jaroslav@941
   367
        assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
jaroslav@960
   368
        assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
jaroslav@936
   369
    }
jaroslav@941
   370
jaroslav@934
   371
    @Factory public static Object[] create() {
jaroslav@934
   372
        return VMTest.create(JSONTest.class);
jaroslav@934
   373
    }
jaroslav@934
   374
    
jaroslav@919
   375
}