json-tck/src/main/java/net/java/html/json/tests/JSONTest.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Fri, 07 Feb 2014 07:44:34 +0100
changeset 551 7ca2253fa86d
parent 528 289d1a80c244
child 569 245637e6d8db
permissions -rw-r--r--
Updating copyright headers to mention current year
jaroslav@34
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@34
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 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@551
    30
 * Software is Oracle. Portions Copyright 2013-2014 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@507
    46
import java.io.ByteArrayOutputStream;
jaroslav@507
    47
import java.io.PrintStream;
jaroslav@110
    48
import net.java.html.BrwsrCtx;
jaroslav@34
    49
import net.java.html.json.Model;
jaroslav@240
    50
import net.java.html.json.ModelOperation;
jaroslav@77
    51
import net.java.html.json.Models;
jaroslav@34
    52
import net.java.html.json.OnReceive;
jaroslav@34
    53
import net.java.html.json.Property;
jaroslav@137
    54
import org.apidesign.html.json.tck.KOTest;
jaroslav@34
    55
jaroslav@34
    56
/** Need to verify that models produce reasonable JSON objects.
jaroslav@34
    57
 *
jaroslav@34
    58
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@34
    59
 */
jaroslav@34
    60
@Model(className = "JSONik", properties = {
jaroslav@34
    61
    @Property(name = "fetched", type = Person.class),
jaroslav@34
    62
    @Property(name = "fetchedCount", type = int.class),
jaroslav@73
    63
    @Property(name = "fetchedResponse", type = String.class),
jaroslav@34
    64
    @Property(name = "fetchedSex", type = Sex.class, array = true)
jaroslav@34
    65
})
jaroslav@34
    66
public final class JSONTest {
jaroslav@34
    67
    private JSONik js;
jaroslav@34
    68
    private Integer orig;
jaroslav@138
    69
    private String url;
jaroslav@34
    70
    
jaroslav@240
    71
    @ModelOperation static void assignFetched(JSONik m, Person p) {
jaroslav@240
    72
        m.setFetched(p);
jaroslav@240
    73
    }
jaroslav@240
    74
    
jaroslav@137
    75
    @KOTest public void toJSONInABrowser() throws Throwable {
jaroslav@121
    76
        Person p = Models.bind(new Person(), newContext());
jaroslav@34
    77
        p.setSex(Sex.MALE);
jaroslav@34
    78
        p.setFirstName("Jarda");
jaroslav@34
    79
        p.setLastName("Tulach");
jaroslav@34
    80
jaroslav@34
    81
        Object json;
jaroslav@34
    82
        try {
jaroslav@34
    83
            json = parseJSON(p.toString());
jaroslav@34
    84
        } catch (Throwable ex) {
jaroslav@34
    85
            throw new IllegalStateException("Can't parse " + p).initCause(ex);
jaroslav@34
    86
        }
jaroslav@34
    87
        
jaroslav@388
    88
        Person p2 = Models.fromRaw(newContext(), Person.class, json);
jaroslav@34
    89
        
jaroslav@34
    90
        assert p2.getFirstName().equals(p.getFirstName()) : 
jaroslav@34
    91
            "Should be the same: " + p.getFirstName() + " != " + p2.getFirstName();
jaroslav@34
    92
    }
jaroslav@34
    93
    
jaroslav@329
    94
    @KOTest public void toJSONWithEscapeCharactersInABrowser() throws Throwable {
jaroslav@329
    95
        Person p = Models.bind(new Person(), newContext());
jaroslav@329
    96
        p.setSex(Sex.MALE);
jaroslav@329
    97
        p.setFirstName("/*\n * Copyright (c) 2013");
jaroslav@329
    98
jaroslav@329
    99
        
jaroslav@329
   100
        final String txt = p.toString();
jaroslav@329
   101
        Object json;
jaroslav@329
   102
        try {
jaroslav@329
   103
            json = parseJSON(txt);
jaroslav@329
   104
        } catch (Throwable ex) {
jaroslav@329
   105
            throw new IllegalStateException("Can't parse " + txt).initCause(ex);
jaroslav@329
   106
        }
jaroslav@329
   107
        
jaroslav@388
   108
        Person p2 = Models.fromRaw(newContext(), Person.class, json);
jaroslav@329
   109
        
jaroslav@329
   110
        assert p2.getFirstName().equals(p.getFirstName()) : 
jaroslav@329
   111
            "Should be the same: " + p.getFirstName() + " != " + p2.getFirstName();
jaroslav@329
   112
    }
jaroslav@329
   113
    
jaroslav@329
   114
    @KOTest public void toJSONWithDoubleSlashInABrowser() throws Throwable {
jaroslav@329
   115
        Person p = Models.bind(new Person(), newContext());
jaroslav@329
   116
        p.setSex(Sex.MALE);
jaroslav@329
   117
        p.setFirstName("/*\\n * Copyright (c) 2013");
jaroslav@329
   118
jaroslav@329
   119
        
jaroslav@329
   120
        final String txt = p.toString();
jaroslav@329
   121
        Object json;
jaroslav@329
   122
        try {
jaroslav@329
   123
            json = parseJSON(txt);
jaroslav@329
   124
        } catch (Throwable ex) {
jaroslav@329
   125
            throw new IllegalStateException("Can't parse " + txt).initCause(ex);
jaroslav@329
   126
        }
jaroslav@329
   127
        
jaroslav@388
   128
        Person p2 = Models.fromRaw(newContext(), Person.class, json);
jaroslav@329
   129
        
jaroslav@329
   130
        assert p2.getFirstName().equals(p.getFirstName()) : 
jaroslav@329
   131
            "Should be the same: " + p.getFirstName() + " != " + p2.getFirstName();
jaroslav@329
   132
    }
jaroslav@329
   133
    
jaroslav@338
   134
    @KOTest public void toJSONWithApostrophInABrowser() throws Throwable {
jaroslav@338
   135
        Person p = Models.bind(new Person(), newContext());
jaroslav@338
   136
        p.setSex(Sex.MALE);
jaroslav@338
   137
        p.setFirstName("Jimmy 'Jim' Rambo");
jaroslav@338
   138
jaroslav@338
   139
        
jaroslav@338
   140
        final String txt = p.toString();
jaroslav@338
   141
        Object json;
jaroslav@338
   142
        try {
jaroslav@338
   143
            json = parseJSON(txt);
jaroslav@338
   144
        } catch (Throwable ex) {
jaroslav@338
   145
            throw new IllegalStateException("Can't parse " + txt).initCause(ex);
jaroslav@338
   146
        }
jaroslav@338
   147
        
jaroslav@388
   148
        Person p2 = Models.fromRaw(newContext(), Person.class, json);
jaroslav@338
   149
        
jaroslav@338
   150
        assert p2.getFirstName().equals(p.getFirstName()) : 
jaroslav@338
   151
            "Should be the same: " + p.getFirstName() + " != " + p2.getFirstName();
jaroslav@338
   152
    }
jaroslav@338
   153
    
jaroslav@138
   154
    @OnReceive(url="{url}")
jaroslav@34
   155
    static void fetch(Person p, JSONik model) {
jaroslav@34
   156
        model.setFetched(p);
jaroslav@34
   157
    }
jaroslav@34
   158
jaroslav@245
   159
    @OnReceive(url="{url}", onError = "setMessage")
jaroslav@34
   160
    static void fetchArray(Person[] p, JSONik model) {
jaroslav@34
   161
        model.setFetchedCount(p.length);
jaroslav@34
   162
        model.setFetched(p[0]);
jaroslav@34
   163
    }
jaroslav@34
   164
    
jaroslav@245
   165
    static void setMessage(JSONik m, Exception t) {
jaroslav@245
   166
        assert t != null;
jaroslav@245
   167
        m.setFetchedResponse("Exception");
jaroslav@245
   168
    }
jaroslav@245
   169
    
jaroslav@138
   170
    @OnReceive(url="{url}")
jaroslav@34
   171
    static void fetchPeople(People p, JSONik model) {
jaroslav@528
   172
        final int size = p.getInfo().size();
jaroslav@528
   173
        if (size > 0) {
jaroslav@528
   174
            model.setFetched(p.getInfo().get(0));
jaroslav@528
   175
        }
jaroslav@528
   176
        model.setFetchedCount(size);
jaroslav@34
   177
    }
jaroslav@34
   178
jaroslav@138
   179
    @OnReceive(url="{url}")
jaroslav@34
   180
    static void fetchPeopleAge(People p, JSONik model) {
jaroslav@34
   181
        int sum = 0;
jaroslav@34
   182
        for (int a : p.getAge()) {
jaroslav@34
   183
            sum += a;
jaroslav@34
   184
        }
jaroslav@34
   185
        model.setFetchedCount(sum);
jaroslav@34
   186
    }
jaroslav@34
   187
    
jaroslav@137
   188
    @KOTest public void loadAndParseJSON() throws InterruptedException {
jaroslav@34
   189
        if (js == null) {
jaroslav@138
   190
            url = Utils.prepareURL(
jaroslav@138
   191
                JSONTest.class, "{'firstName': 'Sitar', 'sex': 'MALE'}",
jaroslav@138
   192
                "application/json"
jaroslav@138
   193
            );
jaroslav@121
   194
            js = Models.bind(new JSONik(), newContext());
jaroslav@34
   195
            js.applyBindings();
jaroslav@34
   196
jaroslav@234
   197
            js.setFetched(null);
jaroslav@138
   198
            js.fetch(url);
jaroslav@34
   199
        }
jaroslav@34
   200
    
jaroslav@34
   201
        Person p = js.getFetched();
jaroslav@34
   202
        if (p == null) {
jaroslav@34
   203
            throw new InterruptedException();
jaroslav@34
   204
        }
jaroslav@34
   205
        
jaroslav@34
   206
        assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
jaroslav@34
   207
        assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
jaroslav@34
   208
    }
jaroslav@34
   209
    
jaroslav@139
   210
    @OnReceive(url="{url}?callme={me}", jsonp = "me")
jaroslav@34
   211
    static void fetchViaJSONP(Person p, JSONik model) {
jaroslav@34
   212
        model.setFetched(p);
jaroslav@34
   213
    }
jaroslav@34
   214
    
jaroslav@137
   215
    @KOTest public void loadAndParseJSONP() throws InterruptedException, Exception {
jaroslav@34
   216
        if (js == null) {
jaroslav@138
   217
            url = Utils.prepareURL(
jaroslav@138
   218
                JSONTest.class, "$0({'firstName': 'Mitar', 'sex': 'MALE'})", 
jaroslav@138
   219
                "application/javascript",
jaroslav@138
   220
                "callme"
jaroslav@138
   221
            );
jaroslav@34
   222
            orig = scriptElements();
jaroslav@34
   223
            assert orig > 0 : "There should be some scripts on the page";
jaroslav@34
   224
            
jaroslav@121
   225
            js = Models.bind(new JSONik(), newContext());
jaroslav@34
   226
            js.applyBindings();
jaroslav@34
   227
jaroslav@234
   228
            js.setFetched(null);
jaroslav@138
   229
            js.fetchViaJSONP(url);
jaroslav@34
   230
        }
jaroslav@34
   231
    
jaroslav@34
   232
        Person p = js.getFetched();
jaroslav@34
   233
        if (p == null) {
jaroslav@34
   234
            throw new InterruptedException();
jaroslav@34
   235
        }
jaroslav@34
   236
        
jaroslav@34
   237
        assert "Mitar".equals(p.getFirstName()) : "Unexpected: " + p.getFirstName();
jaroslav@34
   238
        assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
jaroslav@34
   239
        
jaroslav@34
   240
        int now = scriptElements();
jaroslav@34
   241
        
jaroslav@34
   242
        assert orig == now : "The set of elements is unchanged. Delta: " + (now - orig);
jaroslav@34
   243
    }
jaroslav@34
   244
    
jaroslav@72
   245
    
jaroslav@72
   246
    
jaroslav@72
   247
    @OnReceive(url="{url}", method = "PUT", data = Person.class)
jaroslav@72
   248
    static void putPerson(JSONik model, String reply) {
jaroslav@72
   249
        model.setFetchedCount(1);
jaroslav@73
   250
        model.setFetchedResponse(reply);
jaroslav@72
   251
    }
jaroslav@138
   252
jaroslav@137
   253
    @KOTest public void putPeopleUsesRightMethod() throws InterruptedException, Exception {
jaroslav@72
   254
        if (js == null) {
jaroslav@138
   255
            url = Utils.prepareURL(
jaroslav@138
   256
                JSONTest.class, "$0\n$1", 
jaroslav@138
   257
                "text/plain",
jaroslav@138
   258
                "http.method", "http.requestBody"
jaroslav@138
   259
            );
jaroslav@72
   260
            orig = scriptElements();
jaroslav@72
   261
            assert orig > 0 : "There should be some scripts on the page";
jaroslav@72
   262
            
jaroslav@121
   263
            js = Models.bind(new JSONik(), newContext());
jaroslav@72
   264
            js.applyBindings();
jaroslav@72
   265
jaroslav@115
   266
            Person p = Models.bind(new Person(), BrwsrCtx.EMPTY);
jaroslav@72
   267
            p.setFirstName("Jarda");
jaroslav@138
   268
            js.putPerson(url, p);
jaroslav@72
   269
        }
jaroslav@72
   270
    
jaroslav@72
   271
        int cnt = js.getFetchedCount();
jaroslav@72
   272
        if (cnt == 0) {
jaroslav@72
   273
            throw new InterruptedException();
jaroslav@72
   274
        }
jaroslav@73
   275
        String res = js.getFetchedResponse();
jaroslav@73
   276
        int line = res.indexOf('\n');
jaroslav@73
   277
        String msg;
jaroslav@73
   278
        if (line >= 0) {
jaroslav@75
   279
            msg = res.substring(line + 1);
jaroslav@73
   280
            res = res.substring(0, line);
jaroslav@73
   281
        } else {
jaroslav@73
   282
            msg = res;
jaroslav@73
   283
        }
jaroslav@73
   284
        
jaroslav@73
   285
        assert "PUT".equals(res) : "Server was queried with PUT method: " + js.getFetchedResponse();
jaroslav@73
   286
        
jaroslav@73
   287
        assert msg.contains("Jarda") : "Data transferred to the server: " + msg;
jaroslav@72
   288
    }
jaroslav@72
   289
    
jaroslav@36
   290
    private static int scriptElements() throws Exception {
jaroslav@121
   291
        return ((Number)Utils.executeScript(
jaroslav@121
   292
            JSONTest.class, 
jaroslav@121
   293
            "return window.document.getElementsByTagName('script').length;")).intValue();
jaroslav@36
   294
    }
jaroslav@34
   295
jaroslav@36
   296
    private static Object parseJSON(String s) throws Exception {
jaroslav@121
   297
        return Utils.executeScript(
jaroslav@121
   298
            JSONTest.class, 
jaroslav@121
   299
            "return window.JSON.parse(arguments[0]);", s);
jaroslav@36
   300
    }
jaroslav@34
   301
    
jaroslav@137
   302
    @KOTest public void loadAndParseJSONSentToArray() throws InterruptedException {
jaroslav@34
   303
        if (js == null) {
jaroslav@138
   304
            url = Utils.prepareURL(
jaroslav@138
   305
                JSONTest.class, "{'firstName': 'Sitar', 'sex': 'MALE'}", 
jaroslav@138
   306
                "application/json"
jaroslav@138
   307
            );
jaroslav@138
   308
            
jaroslav@121
   309
            js = Models.bind(new JSONik(), newContext());
jaroslav@34
   310
            js.applyBindings();
jaroslav@34
   311
jaroslav@234
   312
            js.setFetched(null);
jaroslav@138
   313
            js.fetchArray(url);
jaroslav@34
   314
        }
jaroslav@34
   315
        
jaroslav@34
   316
        Person p = js.getFetched();
jaroslav@34
   317
        if (p == null) {
jaroslav@34
   318
            throw new InterruptedException();
jaroslav@34
   319
        }
jaroslav@34
   320
        
jaroslav@34
   321
        assert "Sitar".equals(p.getFirstName()) : "Expecting Sitar: " + p.getFirstName();
jaroslav@34
   322
        assert Sex.MALE.equals(p.getSex()) : "Expecting MALE: " + p.getSex();
jaroslav@34
   323
    }
jaroslav@34
   324
    
jaroslav@137
   325
    @KOTest public void loadAndParseJSONArraySingle() throws InterruptedException {
jaroslav@34
   326
        if (js == null) {
jaroslav@138
   327
            url = Utils.prepareURL(
jaroslav@138
   328
                JSONTest.class, "[{'firstName': 'Gitar', 'sex': 'FEMALE'}]", 
jaroslav@138
   329
                "application/json"
jaroslav@138
   330
            );
jaroslav@121
   331
            js = Models.bind(new JSONik(), newContext());
jaroslav@34
   332
            js.applyBindings();
jaroslav@34
   333
        
jaroslav@234
   334
            js.setFetched(null);
jaroslav@138
   335
            js.fetch(url);
jaroslav@34
   336
        }
jaroslav@34
   337
        
jaroslav@34
   338
        Person p = js.getFetched();
jaroslav@34
   339
        if (p == null) {
jaroslav@34
   340
            throw new InterruptedException();
jaroslav@34
   341
        }
jaroslav@34
   342
        
jaroslav@34
   343
        assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
jaroslav@34
   344
        assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
jaroslav@34
   345
    }
jaroslav@34
   346
    
jaroslav@137
   347
    @KOTest public void loadAndParseArrayInPeople() throws InterruptedException {
jaroslav@34
   348
        if (js == null) {
jaroslav@138
   349
            url = Utils.prepareURL(
jaroslav@138
   350
                JSONTest.class, "{'info':[{'firstName': 'Gitar', 'sex': 'FEMALE'}]}", 
jaroslav@138
   351
                "application/json"
jaroslav@138
   352
            );
jaroslav@121
   353
            js = Models.bind(new JSONik(), newContext());
jaroslav@34
   354
            js.applyBindings();
jaroslav@34
   355
        
jaroslav@138
   356
            js.fetchPeople(url);
jaroslav@34
   357
        }
jaroslav@34
   358
        
jaroslav@34
   359
        if (0 == js.getFetchedCount()) {
jaroslav@34
   360
            throw new InterruptedException();
jaroslav@34
   361
        }
jaroslav@34
   362
jaroslav@34
   363
        assert js.getFetchedCount() == 1 : "One person loaded: " + js.getFetchedCount();
jaroslav@34
   364
        
jaroslav@34
   365
        Person p = js.getFetched();
jaroslav@34
   366
        
jaroslav@34
   367
        assert p != null : "We should get our person back: " + p;
jaroslav@34
   368
        assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
jaroslav@34
   369
        assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
jaroslav@34
   370
    }
jaroslav@34
   371
    
jaroslav@137
   372
    @KOTest public void loadAndParseArrayOfIntegers() throws InterruptedException {
jaroslav@34
   373
        if (js == null) {
jaroslav@138
   374
            url = Utils.prepareURL(
jaroslav@138
   375
                JSONTest.class, "{'age':[1, 2, 3]}", 
jaroslav@138
   376
                "application/json"
jaroslav@138
   377
            );
jaroslav@121
   378
            js = Models.bind(new JSONik(), newContext());
jaroslav@34
   379
            js.applyBindings();
jaroslav@34
   380
        
jaroslav@138
   381
            js.fetchPeopleAge(url);
jaroslav@34
   382
        }
jaroslav@34
   383
        
jaroslav@34
   384
        if (0 == js.getFetchedCount()) {
jaroslav@34
   385
            throw new InterruptedException();
jaroslav@34
   386
        }
jaroslav@34
   387
jaroslav@34
   388
        assert js.getFetchedCount() == 6 : "1 + 2 + 3 is " + js.getFetchedCount();
jaroslav@34
   389
    }
jaroslav@34
   390
    
jaroslav@139
   391
    @OnReceive(url="{url}")
jaroslav@34
   392
    static void fetchPeopleSex(People p, JSONik model) {
jaroslav@34
   393
        model.setFetchedCount(1);
jaroslav@34
   394
        model.getFetchedSex().addAll(p.getSex());
jaroslav@34
   395
    }
jaroslav@34
   396
    
jaroslav@137
   397
    @KOTest public void loadAndParseArrayOfEnums() throws InterruptedException {
jaroslav@34
   398
        if (js == null) {
jaroslav@138
   399
            url = Utils.prepareURL(
jaroslav@138
   400
                JSONTest.class, "{'sex':['FEMALE', 'MALE', 'MALE']}", 
jaroslav@138
   401
                "application/json"
jaroslav@138
   402
            );
jaroslav@121
   403
            js = Models.bind(new JSONik(), newContext());
jaroslav@34
   404
            js.applyBindings();
jaroslav@34
   405
        
jaroslav@138
   406
            js.fetchPeopleSex(url);
jaroslav@34
   407
        }
jaroslav@34
   408
        
jaroslav@34
   409
        if (0 == js.getFetchedCount()) {
jaroslav@34
   410
            throw new InterruptedException();
jaroslav@34
   411
        }
jaroslav@34
   412
jaroslav@34
   413
        assert js.getFetchedCount() == 1 : "Loaded";
jaroslav@34
   414
        
jaroslav@34
   415
        assert js.getFetchedSex().size() == 3 : "Three values " + js.getFetchedSex();
jaroslav@34
   416
        assert js.getFetchedSex().get(0) == Sex.FEMALE : "Female first " + js.getFetchedSex();
jaroslav@34
   417
        assert js.getFetchedSex().get(1) == Sex.MALE : "male 2nd " + js.getFetchedSex();
jaroslav@34
   418
        assert js.getFetchedSex().get(2) == Sex.MALE : "male 3rd " + js.getFetchedSex();
jaroslav@34
   419
    }
jaroslav@34
   420
    
jaroslav@137
   421
    @KOTest public void loadAndParseJSONArray() throws InterruptedException {
jaroslav@34
   422
        if (js == null) {
jaroslav@138
   423
            url = Utils.prepareURL(
jaroslav@138
   424
                JSONTest.class, "[{'firstName': 'Gitar', 'sex': 'FEMALE'},"
jaroslav@138
   425
                + "{'firstName': 'Peter', 'sex': 'MALE'}"
jaroslav@138
   426
                + "]", 
jaroslav@138
   427
                "application/json"
jaroslav@138
   428
            );
jaroslav@121
   429
            js = Models.bind(new JSONik(), newContext());
jaroslav@34
   430
            js.applyBindings();
jaroslav@234
   431
            js.setFetched(null);
jaroslav@234
   432
            
jaroslav@138
   433
            js.fetchArray(url);
jaroslav@34
   434
        }
jaroslav@34
   435
        
jaroslav@34
   436
        
jaroslav@34
   437
        Person p = js.getFetched();
jaroslav@34
   438
        if (p == null) {
jaroslav@34
   439
            throw new InterruptedException();
jaroslav@34
   440
        }
jaroslav@34
   441
        
jaroslav@34
   442
        assert js.getFetchedCount() == 2 : "We got two values: " + js.getFetchedCount();
jaroslav@34
   443
        assert "Gitar".equals(p.getFirstName()) : "Expecting Gitar: " + p.getFirstName();
jaroslav@34
   444
        assert Sex.FEMALE.equals(p.getSex()) : "Expecting FEMALE: " + p.getSex();
jaroslav@34
   445
    }
jaroslav@77
   446
    
jaroslav@245
   447
    @KOTest public void loadError() throws InterruptedException {
jaroslav@245
   448
        if (js == null) {
jaroslav@245
   449
            js = Models.bind(new JSONik(), newContext());
jaroslav@245
   450
            js.applyBindings();
jaroslav@245
   451
            js.setFetched(null);
jaroslav@245
   452
            
jaroslav@268
   453
            js.fetchArray("http://127.0.0.1:54253/does/not/exist.txt");
jaroslav@245
   454
        }
jaroslav@245
   455
        
jaroslav@245
   456
        
jaroslav@245
   457
        if (js.getFetchedResponse() == null) {
jaroslav@245
   458
            throw new InterruptedException();
jaroslav@245
   459
        }
jaroslav@245
   460
jaroslav@245
   461
        assert "Exception".equals(js.getFetchedResponse()) : js.getFetchedResponse();
jaroslav@245
   462
    }
jaroslav@245
   463
    
jaroslav@77
   464
    @Model(className = "NameAndValue", properties = {
jaroslav@77
   465
        @Property(name = "name", type = String.class),
jaroslav@77
   466
        @Property(name = "value", type = long.class),
jaroslav@77
   467
        @Property(name = "small", type = byte.class)
jaroslav@77
   468
    })
jaroslav@77
   469
    static class NandV {
jaroslav@77
   470
    }
jaroslav@77
   471
    
jaroslav@137
   472
    @KOTest public void parseNullNumber() throws Exception {
jaroslav@77
   473
        String txt = "{ \"name\":\"M\" }";
jaroslav@77
   474
        ByteArrayInputStream is = new ByteArrayInputStream(txt.getBytes("UTF-8"));
jaroslav@121
   475
        NameAndValue v = Models.parse(newContext(), NameAndValue.class, is);
jaroslav@77
   476
        assert "M".equals(v.getName()) : "Name is 'M': " + v.getName();
jaroslav@77
   477
        assert 0 == v.getValue() : "Value is empty: " + v.getValue();
jaroslav@77
   478
        assert 0 == v.getSmall() : "Small value is empty: " + v.getSmall();
jaroslav@77
   479
    }
jaroslav@340
   480
jaroslav@340
   481
    @KOTest public void deserializeWrongEnum() throws Exception {
jaroslav@507
   482
        PrintStream prev = null;
jaroslav@507
   483
        ByteArrayOutputStream err = new ByteArrayOutputStream();
jaroslav@507
   484
        try {
jaroslav@507
   485
            prev = System.err;
jaroslav@507
   486
            System.setErr(new PrintStream(err));
jaroslav@518
   487
        } catch (SecurityException e) {
jaroslav@518
   488
            err = null;
jaroslav@518
   489
            prev = null;
jaroslav@507
   490
        } catch (LinkageError e) {
jaroslav@507
   491
            err = null;
jaroslav@507
   492
            prev = null;
jaroslav@507
   493
        }
jaroslav@507
   494
        
jaroslav@340
   495
        String str = "{ \"sex\" : \"unknown\" }";
jaroslav@340
   496
        ByteArrayInputStream is = new ByteArrayInputStream(str.getBytes("UTF-8"));
jaroslav@340
   497
        Person p = Models.parse(newContext(), Person.class, is);
jaroslav@340
   498
        assert p.getSex() == null : "Wrong sex means null, but was: " + p.getSex();
jaroslav@507
   499
        
jaroslav@507
   500
        if (err != null) {
jaroslav@526
   501
            assert err.toString().contains("unknown") && err.toString().contains("Sex"): "Expecting error: " + err.toString();
jaroslav@507
   502
        }
jaroslav@507
   503
        if (prev != null) {
jaroslav@507
   504
            try {
jaroslav@507
   505
                System.setErr(prev);
jaroslav@507
   506
            } catch (LinkageError e) {
jaroslav@507
   507
                // ignore
jaroslav@507
   508
            }
jaroslav@507
   509
        }
jaroslav@340
   510
    }
jaroslav@340
   511
jaroslav@240
   512
    
jaroslav@121
   513
    private static BrwsrCtx newContext() {
jaroslav@121
   514
        return Utils.newContext(JSONTest.class);
jaroslav@121
   515
    }
jaroslav@77
   516
    
jaroslav@34
   517
}