json-tck/src/main/java/net/java/html/json/tests/JSONTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 16 Dec 2013 16:59:43 +0100
branchnetbeans
changeset 362 92fb71afdc0e
parent 358 80702021b851
child 365 5c93ad8c7a15
permissions -rw-r--r--
Moving implementation classes into org.netbeans.html namespace
jaroslav@34
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@34
     3
 *
jaroslav@358
     4
 * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
jaroslav@34
     5
 *
jaroslav@358
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@358
     7
 * Other names may be trademarks of their respective owners.
jaroslav@34
     8
 *
jaroslav@358
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@358
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@358
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@358
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@358
    13
 * License. You can obtain a copy of the License at
jaroslav@358
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@358
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@358
    16
 * specific language governing permissions and limitations under the
jaroslav@358
    17
 * License.  When distributing the software, include this License Header
jaroslav@358
    18
 * Notice in each file and include the License file at
jaroslav@358
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@358
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@358
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@358
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@358
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@358
    24
 * your own identifying information:
jaroslav@358
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@358
    26
 *
jaroslav@358
    27
 * Contributor(s):
jaroslav@358
    28
 *
jaroslav@358
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jaroslav@358
    30
 * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
jaroslav@358
    31
 *
jaroslav@358
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@358
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@358
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@358
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@358
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@358
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@358
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@358
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@358
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@358
    41
 * made subject to such option by the copyright holder.
jaroslav@34
    42
 */
jaroslav@34
    43
package net.java.html.json.tests;
jaroslav@34
    44
jaroslav@77
    45
import java.io.ByteArrayInputStream;
jaroslav@110
    46
import net.java.html.BrwsrCtx;
jaroslav@34
    47
import net.java.html.json.Model;
jaroslav@240
    48
import net.java.html.json.ModelOperation;
jaroslav@77
    49
import net.java.html.json.Models;
jaroslav@34
    50
import net.java.html.json.OnReceive;
jaroslav@34
    51
import net.java.html.json.Property;
jaroslav@362
    52
import org.netbeans.html.json.impl.JSON;
jaroslav@137
    53
import org.apidesign.html.json.tck.KOTest;
jaroslav@34
    54
jaroslav@34
    55
/** Need to verify that models produce reasonable JSON objects.
jaroslav@34
    56
 *
jaroslav@34
    57
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@34
    58
 */
jaroslav@34
    59
@Model(className = "JSONik", properties = {
jaroslav@34
    60
    @Property(name = "fetched", type = Person.class),
jaroslav@34
    61
    @Property(name = "fetchedCount", type = int.class),
jaroslav@73
    62
    @Property(name = "fetchedResponse", type = String.class),
jaroslav@34
    63
    @Property(name = "fetchedSex", type = Sex.class, array = true)
jaroslav@34
    64
})
jaroslav@34
    65
public final class JSONTest {
jaroslav@34
    66
    private JSONik js;
jaroslav@34
    67
    private Integer orig;
jaroslav@138
    68
    private String url;
jaroslav@34
    69
    
jaroslav@240
    70
    @ModelOperation static void assignFetched(JSONik m, Person p) {
jaroslav@240
    71
        m.setFetched(p);
jaroslav@240
    72
    }
jaroslav@240
    73
    
jaroslav@137
    74
    @KOTest public void toJSONInABrowser() throws Throwable {
jaroslav@121
    75
        Person p = Models.bind(new Person(), newContext());
jaroslav@34
    76
        p.setSex(Sex.MALE);
jaroslav@34
    77
        p.setFirstName("Jarda");
jaroslav@34
    78
        p.setLastName("Tulach");
jaroslav@34
    79
jaroslav@34
    80
        Object json;
jaroslav@34
    81
        try {
jaroslav@34
    82
            json = parseJSON(p.toString());
jaroslav@34
    83
        } catch (Throwable ex) {
jaroslav@34
    84
            throw new IllegalStateException("Can't parse " + p).initCause(ex);
jaroslav@34
    85
        }
jaroslav@34
    86
        
jaroslav@121
    87
        Person p2 = JSON.read(newContext(), Person.class, json);
jaroslav@34
    88
        
jaroslav@34
    89
        assert p2.getFirstName().equals(p.getFirstName()) : 
jaroslav@34
    90
            "Should be the same: " + p.getFirstName() + " != " + p2.getFirstName();
jaroslav@34
    91
    }
jaroslav@34
    92
    
jaroslav@329
    93
    @KOTest public void toJSONWithEscapeCharactersInABrowser() throws Throwable {
jaroslav@329
    94
        Person p = Models.bind(new Person(), newContext());
jaroslav@329
    95
        p.setSex(Sex.MALE);
jaroslav@329
    96
        p.setFirstName("/*\n * Copyright (c) 2013");
jaroslav@329
    97
jaroslav@329
    98
        
jaroslav@329
    99
        final String txt = p.toString();
jaroslav@329
   100
        Object json;
jaroslav@329
   101
        try {
jaroslav@329
   102
            json = parseJSON(txt);
jaroslav@329
   103
        } catch (Throwable ex) {
jaroslav@329
   104
            throw new IllegalStateException("Can't parse " + txt).initCause(ex);
jaroslav@329
   105
        }
jaroslav@329
   106
        
jaroslav@329
   107
        Person p2 = JSON.read(newContext(), Person.class, json);
jaroslav@329
   108
        
jaroslav@329
   109
        assert p2.getFirstName().equals(p.getFirstName()) : 
jaroslav@329
   110
            "Should be the same: " + p.getFirstName() + " != " + p2.getFirstName();
jaroslav@329
   111
    }
jaroslav@329
   112
    
jaroslav@329
   113
    @KOTest public void toJSONWithDoubleSlashInABrowser() throws Throwable {
jaroslav@329
   114
        Person p = Models.bind(new Person(), newContext());
jaroslav@329
   115
        p.setSex(Sex.MALE);
jaroslav@329
   116
        p.setFirstName("/*\\n * Copyright (c) 2013");
jaroslav@329
   117
jaroslav@329
   118
        
jaroslav@329
   119
        final String txt = p.toString();
jaroslav@329
   120
        Object json;
jaroslav@329
   121
        try {
jaroslav@329
   122
            json = parseJSON(txt);
jaroslav@329
   123
        } catch (Throwable ex) {
jaroslav@329
   124
            throw new IllegalStateException("Can't parse " + txt).initCause(ex);
jaroslav@329
   125
        }
jaroslav@329
   126
        
jaroslav@329
   127
        Person p2 = JSON.read(newContext(), Person.class, json);
jaroslav@329
   128
        
jaroslav@329
   129
        assert p2.getFirstName().equals(p.getFirstName()) : 
jaroslav@329
   130
            "Should be the same: " + p.getFirstName() + " != " + p2.getFirstName();
jaroslav@329
   131
    }
jaroslav@329
   132
    
jaroslav@338
   133
    @KOTest public void toJSONWithApostrophInABrowser() throws Throwable {
jaroslav@338
   134
        Person p = Models.bind(new Person(), newContext());
jaroslav@338
   135
        p.setSex(Sex.MALE);
jaroslav@338
   136
        p.setFirstName("Jimmy 'Jim' Rambo");
jaroslav@338
   137
jaroslav@338
   138
        
jaroslav@338
   139
        final String txt = p.toString();
jaroslav@338
   140
        Object json;
jaroslav@338
   141
        try {
jaroslav@338
   142
            json = parseJSON(txt);
jaroslav@338
   143
        } catch (Throwable ex) {
jaroslav@338
   144
            throw new IllegalStateException("Can't parse " + txt).initCause(ex);
jaroslav@338
   145
        }
jaroslav@338
   146
        
jaroslav@338
   147
        Person p2 = JSON.read(newContext(), Person.class, json);
jaroslav@338
   148
        
jaroslav@338
   149
        assert p2.getFirstName().equals(p.getFirstName()) : 
jaroslav@338
   150
            "Should be the same: " + p.getFirstName() + " != " + p2.getFirstName();
jaroslav@338
   151
    }
jaroslav@338
   152
    
jaroslav@138
   153
    @OnReceive(url="{url}")
jaroslav@34
   154
    static void fetch(Person p, JSONik model) {
jaroslav@34
   155
        model.setFetched(p);
jaroslav@34
   156
    }
jaroslav@34
   157
jaroslav@245
   158
    @OnReceive(url="{url}", onError = "setMessage")
jaroslav@34
   159
    static void fetchArray(Person[] p, JSONik model) {
jaroslav@34
   160
        model.setFetchedCount(p.length);
jaroslav@34
   161
        model.setFetched(p[0]);
jaroslav@34
   162
    }
jaroslav@34
   163
    
jaroslav@245
   164
    static void setMessage(JSONik m, Exception t) {
jaroslav@245
   165
        assert t != null;
jaroslav@245
   166
        m.setFetchedResponse("Exception");
jaroslav@245
   167
    }
jaroslav@245
   168
    
jaroslav@138
   169
    @OnReceive(url="{url}")
jaroslav@34
   170
    static void fetchPeople(People p, JSONik model) {
jaroslav@34
   171
        model.setFetchedCount(p.getInfo().size());
jaroslav@34
   172
        model.setFetched(p.getInfo().get(0));
jaroslav@34
   173
    }
jaroslav@34
   174
jaroslav@138
   175
    @OnReceive(url="{url}")
jaroslav@34
   176
    static void fetchPeopleAge(People p, JSONik model) {
jaroslav@34
   177
        int sum = 0;
jaroslav@34
   178
        for (int a : p.getAge()) {
jaroslav@34
   179
            sum += a;
jaroslav@34
   180
        }
jaroslav@34
   181
        model.setFetchedCount(sum);
jaroslav@34
   182
    }
jaroslav@34
   183
    
jaroslav@137
   184
    @KOTest public void loadAndParseJSON() throws InterruptedException {
jaroslav@34
   185
        if (js == null) {
jaroslav@138
   186
            url = Utils.prepareURL(
jaroslav@138
   187
                JSONTest.class, "{'firstName': 'Sitar', 'sex': 'MALE'}",
jaroslav@138
   188
                "application/json"
jaroslav@138
   189
            );
jaroslav@121
   190
            js = Models.bind(new JSONik(), newContext());
jaroslav@34
   191
            js.applyBindings();
jaroslav@34
   192
jaroslav@234
   193
            js.setFetched(null);
jaroslav@138
   194
            js.fetch(url);
jaroslav@34
   195
        }
jaroslav@34
   196
    
jaroslav@34
   197
        Person p = js.getFetched();
jaroslav@34
   198
        if (p == null) {
jaroslav@34
   199
            throw new InterruptedException();
jaroslav@34
   200
        }
jaroslav@34
   201
        
jaroslav@34
   202
        assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
jaroslav@34
   203
        assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
jaroslav@34
   204
    }
jaroslav@34
   205
    
jaroslav@139
   206
    @OnReceive(url="{url}?callme={me}", jsonp = "me")
jaroslav@34
   207
    static void fetchViaJSONP(Person p, JSONik model) {
jaroslav@34
   208
        model.setFetched(p);
jaroslav@34
   209
    }
jaroslav@34
   210
    
jaroslav@137
   211
    @KOTest public void loadAndParseJSONP() throws InterruptedException, Exception {
jaroslav@34
   212
        if (js == null) {
jaroslav@138
   213
            url = Utils.prepareURL(
jaroslav@138
   214
                JSONTest.class, "$0({'firstName': 'Mitar', 'sex': 'MALE'})", 
jaroslav@138
   215
                "application/javascript",
jaroslav@138
   216
                "callme"
jaroslav@138
   217
            );
jaroslav@34
   218
            orig = scriptElements();
jaroslav@34
   219
            assert orig > 0 : "There should be some scripts on the page";
jaroslav@34
   220
            
jaroslav@121
   221
            js = Models.bind(new JSONik(), newContext());
jaroslav@34
   222
            js.applyBindings();
jaroslav@34
   223
jaroslav@234
   224
            js.setFetched(null);
jaroslav@138
   225
            js.fetchViaJSONP(url);
jaroslav@34
   226
        }
jaroslav@34
   227
    
jaroslav@34
   228
        Person p = js.getFetched();
jaroslav@34
   229
        if (p == null) {
jaroslav@34
   230
            throw new InterruptedException();
jaroslav@34
   231
        }
jaroslav@34
   232
        
jaroslav@34
   233
        assert "Mitar".equals(p.getFirstName()) : "Unexpected: " + p.getFirstName();
jaroslav@34
   234
        assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
jaroslav@34
   235
        
jaroslav@34
   236
        int now = scriptElements();
jaroslav@34
   237
        
jaroslav@34
   238
        assert orig == now : "The set of elements is unchanged. Delta: " + (now - orig);
jaroslav@34
   239
    }
jaroslav@34
   240
    
jaroslav@72
   241
    
jaroslav@72
   242
    
jaroslav@72
   243
    @OnReceive(url="{url}", method = "PUT", data = Person.class)
jaroslav@72
   244
    static void putPerson(JSONik model, String reply) {
jaroslav@72
   245
        model.setFetchedCount(1);
jaroslav@73
   246
        model.setFetchedResponse(reply);
jaroslav@72
   247
    }
jaroslav@138
   248
jaroslav@137
   249
    @KOTest public void putPeopleUsesRightMethod() throws InterruptedException, Exception {
jaroslav@72
   250
        if (js == null) {
jaroslav@138
   251
            url = Utils.prepareURL(
jaroslav@138
   252
                JSONTest.class, "$0\n$1", 
jaroslav@138
   253
                "text/plain",
jaroslav@138
   254
                "http.method", "http.requestBody"
jaroslav@138
   255
            );
jaroslav@72
   256
            orig = scriptElements();
jaroslav@72
   257
            assert orig > 0 : "There should be some scripts on the page";
jaroslav@72
   258
            
jaroslav@121
   259
            js = Models.bind(new JSONik(), newContext());
jaroslav@72
   260
            js.applyBindings();
jaroslav@72
   261
jaroslav@115
   262
            Person p = Models.bind(new Person(), BrwsrCtx.EMPTY);
jaroslav@72
   263
            p.setFirstName("Jarda");
jaroslav@138
   264
            js.putPerson(url, p);
jaroslav@72
   265
        }
jaroslav@72
   266
    
jaroslav@72
   267
        int cnt = js.getFetchedCount();
jaroslav@72
   268
        if (cnt == 0) {
jaroslav@72
   269
            throw new InterruptedException();
jaroslav@72
   270
        }
jaroslav@73
   271
        String res = js.getFetchedResponse();
jaroslav@73
   272
        int line = res.indexOf('\n');
jaroslav@73
   273
        String msg;
jaroslav@73
   274
        if (line >= 0) {
jaroslav@75
   275
            msg = res.substring(line + 1);
jaroslav@73
   276
            res = res.substring(0, line);
jaroslav@73
   277
        } else {
jaroslav@73
   278
            msg = res;
jaroslav@73
   279
        }
jaroslav@73
   280
        
jaroslav@73
   281
        assert "PUT".equals(res) : "Server was queried with PUT method: " + js.getFetchedResponse();
jaroslav@73
   282
        
jaroslav@73
   283
        assert msg.contains("Jarda") : "Data transferred to the server: " + msg;
jaroslav@72
   284
    }
jaroslav@72
   285
    
jaroslav@36
   286
    private static int scriptElements() throws Exception {
jaroslav@121
   287
        return ((Number)Utils.executeScript(
jaroslav@121
   288
            JSONTest.class, 
jaroslav@121
   289
            "return window.document.getElementsByTagName('script').length;")).intValue();
jaroslav@36
   290
    }
jaroslav@34
   291
jaroslav@36
   292
    private static Object parseJSON(String s) throws Exception {
jaroslav@121
   293
        return Utils.executeScript(
jaroslav@121
   294
            JSONTest.class, 
jaroslav@121
   295
            "return window.JSON.parse(arguments[0]);", s);
jaroslav@36
   296
    }
jaroslav@34
   297
    
jaroslav@137
   298
    @KOTest public void loadAndParseJSONSentToArray() throws InterruptedException {
jaroslav@34
   299
        if (js == null) {
jaroslav@138
   300
            url = Utils.prepareURL(
jaroslav@138
   301
                JSONTest.class, "{'firstName': 'Sitar', 'sex': 'MALE'}", 
jaroslav@138
   302
                "application/json"
jaroslav@138
   303
            );
jaroslav@138
   304
            
jaroslav@121
   305
            js = Models.bind(new JSONik(), newContext());
jaroslav@34
   306
            js.applyBindings();
jaroslav@34
   307
jaroslav@234
   308
            js.setFetched(null);
jaroslav@138
   309
            js.fetchArray(url);
jaroslav@34
   310
        }
jaroslav@34
   311
        
jaroslav@34
   312
        Person p = js.getFetched();
jaroslav@34
   313
        if (p == null) {
jaroslav@34
   314
            throw new InterruptedException();
jaroslav@34
   315
        }
jaroslav@34
   316
        
jaroslav@34
   317
        assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
jaroslav@34
   318
        assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
jaroslav@34
   319
    }
jaroslav@34
   320
    
jaroslav@137
   321
    @KOTest public void loadAndParseJSONArraySingle() throws InterruptedException {
jaroslav@34
   322
        if (js == null) {
jaroslav@138
   323
            url = Utils.prepareURL(
jaroslav@138
   324
                JSONTest.class, "[{'firstName': 'Gitar', 'sex': 'FEMALE'}]", 
jaroslav@138
   325
                "application/json"
jaroslav@138
   326
            );
jaroslav@121
   327
            js = Models.bind(new JSONik(), newContext());
jaroslav@34
   328
            js.applyBindings();
jaroslav@34
   329
        
jaroslav@234
   330
            js.setFetched(null);
jaroslav@138
   331
            js.fetch(url);
jaroslav@34
   332
        }
jaroslav@34
   333
        
jaroslav@34
   334
        Person p = js.getFetched();
jaroslav@34
   335
        if (p == null) {
jaroslav@34
   336
            throw new InterruptedException();
jaroslav@34
   337
        }
jaroslav@34
   338
        
jaroslav@34
   339
        assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
jaroslav@34
   340
        assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
jaroslav@34
   341
    }
jaroslav@34
   342
    
jaroslav@137
   343
    @KOTest public void loadAndParseArrayInPeople() throws InterruptedException {
jaroslav@34
   344
        if (js == null) {
jaroslav@138
   345
            url = Utils.prepareURL(
jaroslav@138
   346
                JSONTest.class, "{'info':[{'firstName': 'Gitar', 'sex': 'FEMALE'}]}", 
jaroslav@138
   347
                "application/json"
jaroslav@138
   348
            );
jaroslav@121
   349
            js = Models.bind(new JSONik(), newContext());
jaroslav@34
   350
            js.applyBindings();
jaroslav@34
   351
        
jaroslav@138
   352
            js.fetchPeople(url);
jaroslav@34
   353
        }
jaroslav@34
   354
        
jaroslav@34
   355
        if (0 == js.getFetchedCount()) {
jaroslav@34
   356
            throw new InterruptedException();
jaroslav@34
   357
        }
jaroslav@34
   358
jaroslav@34
   359
        assert js.getFetchedCount() == 1 : "One person loaded: " + js.getFetchedCount();
jaroslav@34
   360
        
jaroslav@34
   361
        Person p = js.getFetched();
jaroslav@34
   362
        
jaroslav@34
   363
        assert p != null : "We should get our person back: " + p;
jaroslav@34
   364
        assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
jaroslav@34
   365
        assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
jaroslav@34
   366
    }
jaroslav@34
   367
    
jaroslav@137
   368
    @KOTest public void loadAndParseArrayOfIntegers() throws InterruptedException {
jaroslav@34
   369
        if (js == null) {
jaroslav@138
   370
            url = Utils.prepareURL(
jaroslav@138
   371
                JSONTest.class, "{'age':[1, 2, 3]}", 
jaroslav@138
   372
                "application/json"
jaroslav@138
   373
            );
jaroslav@121
   374
            js = Models.bind(new JSONik(), newContext());
jaroslav@34
   375
            js.applyBindings();
jaroslav@34
   376
        
jaroslav@138
   377
            js.fetchPeopleAge(url);
jaroslav@34
   378
        }
jaroslav@34
   379
        
jaroslav@34
   380
        if (0 == js.getFetchedCount()) {
jaroslav@34
   381
            throw new InterruptedException();
jaroslav@34
   382
        }
jaroslav@34
   383
jaroslav@34
   384
        assert js.getFetchedCount() == 6 : "1 + 2 + 3 is " + js.getFetchedCount();
jaroslav@34
   385
    }
jaroslav@34
   386
    
jaroslav@139
   387
    @OnReceive(url="{url}")
jaroslav@34
   388
    static void fetchPeopleSex(People p, JSONik model) {
jaroslav@34
   389
        model.setFetchedCount(1);
jaroslav@34
   390
        model.getFetchedSex().addAll(p.getSex());
jaroslav@34
   391
    }
jaroslav@34
   392
    
jaroslav@137
   393
    @KOTest public void loadAndParseArrayOfEnums() throws InterruptedException {
jaroslav@34
   394
        if (js == null) {
jaroslav@138
   395
            url = Utils.prepareURL(
jaroslav@138
   396
                JSONTest.class, "{'sex':['FEMALE', 'MALE', 'MALE']}", 
jaroslav@138
   397
                "application/json"
jaroslav@138
   398
            );
jaroslav@121
   399
            js = Models.bind(new JSONik(), newContext());
jaroslav@34
   400
            js.applyBindings();
jaroslav@34
   401
        
jaroslav@138
   402
            js.fetchPeopleSex(url);
jaroslav@34
   403
        }
jaroslav@34
   404
        
jaroslav@34
   405
        if (0 == js.getFetchedCount()) {
jaroslav@34
   406
            throw new InterruptedException();
jaroslav@34
   407
        }
jaroslav@34
   408
jaroslav@34
   409
        assert js.getFetchedCount() == 1 : "Loaded";
jaroslav@34
   410
        
jaroslav@34
   411
        assert js.getFetchedSex().size() == 3 : "Three values " + js.getFetchedSex();
jaroslav@34
   412
        assert js.getFetchedSex().get(0) == Sex.FEMALE : "Female first " + js.getFetchedSex();
jaroslav@34
   413
        assert js.getFetchedSex().get(1) == Sex.MALE : "male 2nd " + js.getFetchedSex();
jaroslav@34
   414
        assert js.getFetchedSex().get(2) == Sex.MALE : "male 3rd " + js.getFetchedSex();
jaroslav@34
   415
    }
jaroslav@34
   416
    
jaroslav@137
   417
    @KOTest public void loadAndParseJSONArray() throws InterruptedException {
jaroslav@34
   418
        if (js == null) {
jaroslav@138
   419
            url = Utils.prepareURL(
jaroslav@138
   420
                JSONTest.class, "[{'firstName': 'Gitar', 'sex': 'FEMALE'},"
jaroslav@138
   421
                + "{'firstName': 'Peter', 'sex': 'MALE'}"
jaroslav@138
   422
                + "]", 
jaroslav@138
   423
                "application/json"
jaroslav@138
   424
            );
jaroslav@121
   425
            js = Models.bind(new JSONik(), newContext());
jaroslav@34
   426
            js.applyBindings();
jaroslav@234
   427
            js.setFetched(null);
jaroslav@234
   428
            
jaroslav@138
   429
            js.fetchArray(url);
jaroslav@34
   430
        }
jaroslav@34
   431
        
jaroslav@34
   432
        
jaroslav@34
   433
        Person p = js.getFetched();
jaroslav@34
   434
        if (p == null) {
jaroslav@34
   435
            throw new InterruptedException();
jaroslav@34
   436
        }
jaroslav@34
   437
        
jaroslav@34
   438
        assert js.getFetchedCount() == 2 : "We got two values: " + js.getFetchedCount();
jaroslav@34
   439
        assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
jaroslav@34
   440
        assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
jaroslav@34
   441
    }
jaroslav@77
   442
    
jaroslav@245
   443
    @KOTest public void loadError() throws InterruptedException {
jaroslav@245
   444
        if (js == null) {
jaroslav@245
   445
            js = Models.bind(new JSONik(), newContext());
jaroslav@245
   446
            js.applyBindings();
jaroslav@245
   447
            js.setFetched(null);
jaroslav@245
   448
            
jaroslav@268
   449
            js.fetchArray("http://127.0.0.1:54253/does/not/exist.txt");
jaroslav@245
   450
        }
jaroslav@245
   451
        
jaroslav@245
   452
        
jaroslav@245
   453
        if (js.getFetchedResponse() == null) {
jaroslav@245
   454
            throw new InterruptedException();
jaroslav@245
   455
        }
jaroslav@245
   456
jaroslav@245
   457
        assert "Exception".equals(js.getFetchedResponse()) : js.getFetchedResponse();
jaroslav@245
   458
    }
jaroslav@245
   459
    
jaroslav@77
   460
    @Model(className = "NameAndValue", properties = {
jaroslav@77
   461
        @Property(name = "name", type = String.class),
jaroslav@77
   462
        @Property(name = "value", type = long.class),
jaroslav@77
   463
        @Property(name = "small", type = byte.class)
jaroslav@77
   464
    })
jaroslav@77
   465
    static class NandV {
jaroslav@77
   466
    }
jaroslav@77
   467
    
jaroslav@137
   468
    @KOTest public void parseNullNumber() throws Exception {
jaroslav@77
   469
        String txt = "{ \"name\":\"M\" }";
jaroslav@77
   470
        ByteArrayInputStream is = new ByteArrayInputStream(txt.getBytes("UTF-8"));
jaroslav@121
   471
        NameAndValue v = Models.parse(newContext(), NameAndValue.class, is);
jaroslav@77
   472
        assert "M".equals(v.getName()) : "Name is 'M': " + v.getName();
jaroslav@77
   473
        assert 0 == v.getValue() : "Value is empty: " + v.getValue();
jaroslav@77
   474
        assert 0 == v.getSmall() : "Small value is empty: " + v.getSmall();
jaroslav@77
   475
    }
jaroslav@340
   476
jaroslav@340
   477
    @KOTest public void deserializeWrongEnum() throws Exception {
jaroslav@340
   478
        String str = "{ \"sex\" : \"unknown\" }";
jaroslav@340
   479
        ByteArrayInputStream is = new ByteArrayInputStream(str.getBytes("UTF-8"));
jaroslav@340
   480
        Person p = Models.parse(newContext(), Person.class, is);
jaroslav@340
   481
        assert p.getSex() == null : "Wrong sex means null, but was: " + p.getSex();
jaroslav@340
   482
    }
jaroslav@340
   483
jaroslav@240
   484
    
jaroslav@121
   485
    private static BrwsrCtx newContext() {
jaroslav@121
   486
        return Utils.newContext(JSONTest.class);
jaroslav@121
   487
    }
jaroslav@77
   488
    
jaroslav@34
   489
}