json-tck/src/main/java/net/java/html/json/tests/ConvertTypesTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 26 Aug 2014 18:13:30 +0200
changeset 838 bdc3d696dd4a
parent 790 30f20d9c0986
child 934 bbbdf003a99b
permissions -rw-r--r--
During the API review process (bug 246133) the reviewers decided that in order to include html4j to NetBeans Platform, we need to stop using org.apidesign namespace and switch to NetBeans one. Repackaging all SPI packages into org.netbeans.html.smthng.spi.
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@60
    45
import java.io.ByteArrayInputStream;
jtulach@746
    46
import java.io.EOFException;
jaroslav@60
    47
import java.io.InputStream;
jaroslav@60
    48
import java.io.UnsupportedEncodingException;
jtulach@747
    49
import java.util.ArrayList;
jaroslav@35
    50
import java.util.HashMap;
jtulach@747
    51
import java.util.List;
jaroslav@35
    52
import java.util.Map;
jaroslav@110
    53
import net.java.html.BrwsrCtx;
jaroslav@60
    54
import net.java.html.json.Models;
jtulach@838
    55
import org.netbeans.html.json.tck.KOTest;
jaroslav@34
    56
jaroslav@34
    57
/**
jaroslav@34
    58
 *
jtulach@790
    59
 * @author Jaroslav Tulach
jaroslav@34
    60
 */
jaroslav@34
    61
public final class ConvertTypesTest {
jtulach@747
    62
    private static InputStream createIS(boolean includeSex, boolean includeAddress, int array) 
jaroslav@60
    63
    throws UnsupportedEncodingException {
jaroslav@60
    64
        StringBuilder sb = new StringBuilder();
jtulach@747
    65
        int repeat;
jtulach@747
    66
        if (array != -1) {
jtulach@745
    67
            sb.append("[\n");
jtulach@747
    68
            repeat = array;
jtulach@747
    69
        } else {
jtulach@747
    70
            repeat = 1;
jtulach@745
    71
        }
jtulach@747
    72
        for (int i = 0; i < repeat; i++) {
jtulach@747
    73
            sb.append("{ \"firstName\" : \"son\",\n");
jtulach@747
    74
            sb.append("  \"lastName\" : \"dj\" \n");
jtulach@747
    75
            if (includeSex) {
jtulach@747
    76
                sb.append(",  \"sex\" : \"MALE\" \n");
jtulach@747
    77
            }
jtulach@747
    78
            if (includeAddress) {
jtulach@747
    79
                sb.append(",  \"address\" : { \"street\" : \"Schnirchova\" } \n");
jtulach@747
    80
            }
jtulach@747
    81
            sb.append("}\n");
jtulach@747
    82
            if (i < array - 1) {
jtulach@747
    83
                sb.append(",");
jtulach@747
    84
            }
jaroslav@136
    85
        }
jtulach@747
    86
        if (array != -1) {
jtulach@745
    87
            sb.append(']');
jtulach@745
    88
        }
jaroslav@60
    89
        return new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
jaroslav@60
    90
    }
jaroslav@60
    91
    private static Object createJSON(boolean includeSex) 
jaroslav@60
    92
    throws UnsupportedEncodingException {
jaroslav@100
    93
        Map<String,Object> map = new HashMap<String,Object>();
jaroslav@35
    94
        map.put("firstName", "son");
jaroslav@35
    95
        map.put("lastName", "dj");
jaroslav@35
    96
        if (includeSex) {
jaroslav@35
    97
            map.put("sex", "MALE");
jaroslav@35
    98
        }
jaroslav@121
    99
        return Utils.createObject(map, ConvertTypesTest.class);
jaroslav@35
   100
    }
jaroslav@34
   101
    
jaroslav@137
   102
    @KOTest
jaroslav@34
   103
    public void testConvertToPeople() throws Exception {
jaroslav@34
   104
        final Object o = createJSON(true);
jaroslav@34
   105
        
jaroslav@388
   106
        Person p = Models.fromRaw(newContext(), Person.class, o);
jaroslav@34
   107
        
jaroslav@34
   108
        assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
jaroslav@34
   109
        assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
jaroslav@34
   110
        assert Sex.MALE.equals(p.getSex()) : "Sex: " + p.getSex();
jaroslav@34
   111
    }
jaroslav@34
   112
jaroslav@137
   113
    @KOTest
jaroslav@60
   114
    public void parseConvertToPeople() throws Exception {
jaroslav@121
   115
        final BrwsrCtx c = newContext();
jtulach@747
   116
        final InputStream o = createIS(true, false, -1);
jaroslav@60
   117
        
jaroslav@60
   118
        Person p = Models.parse(c, Person.class, o);
jaroslav@60
   119
        
jaroslav@60
   120
        assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
jaroslav@60
   121
        assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
jaroslav@60
   122
        assert Sex.MALE.equals(p.getSex()) : "Sex: " + p.getSex();
jaroslav@60
   123
    }
jaroslav@136
   124
    
jaroslav@137
   125
    @KOTest
jaroslav@136
   126
    public void parseConvertToPeopleWithAddress() throws Exception {
jaroslav@136
   127
        final BrwsrCtx c = newContext();
jtulach@747
   128
        final InputStream o = createIS(true, true, -1);
jaroslav@136
   129
        
jaroslav@136
   130
        Person p = Models.parse(c, Person.class, o);
jaroslav@136
   131
        
jaroslav@136
   132
        assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
jaroslav@136
   133
        assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
jaroslav@136
   134
        assert Sex.MALE.equals(p.getSex()) : "Sex: " + p.getSex();
jaroslav@136
   135
        assert p.getAddress() != null : "Some address provided";
jaroslav@136
   136
        assert p.getAddress().getStreet().equals("Schnirchova") : "Is Schnirchova: " + p.getAddress();
jaroslav@136
   137
    }
jaroslav@60
   138
jaroslav@137
   139
    @KOTest
jtulach@747
   140
    public void parseConvertToPeopleWithAddressIntoAnArray() throws Exception {
jtulach@747
   141
        final BrwsrCtx c = newContext();
jtulach@747
   142
        final InputStream o = createIS(true, true, -1);
jtulach@747
   143
        
jtulach@747
   144
        List<Person> arr = new ArrayList<Person>();
jtulach@747
   145
        Models.parse(c, Person.class, o, arr);
jtulach@747
   146
        
jtulach@747
   147
        assert arr.size() == 1 : "There is one item in " + arr;
jtulach@747
   148
        
jtulach@747
   149
        Person p = arr.get(0);
jtulach@747
   150
        assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
jtulach@747
   151
        assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
jtulach@747
   152
        assert Sex.MALE.equals(p.getSex()) : "Sex: " + p.getSex();
jtulach@747
   153
        assert p.getAddress() != null : "Some address provided";
jtulach@747
   154
        assert p.getAddress().getStreet().equals("Schnirchova") : "Is Schnirchova: " + p.getAddress();
jtulach@747
   155
    }
jtulach@749
   156
    
jtulach@749
   157
    @KOTest 
jtulach@749
   158
    public void parseNullValue() throws Exception {
jtulach@749
   159
        final BrwsrCtx c = newContext();
jtulach@749
   160
        
jtulach@749
   161
        StringBuilder sb = new StringBuilder();
jtulach@749
   162
        sb.append("{ \"firstName\" : \"son\",\n");
jtulach@749
   163
        sb.append("  \"lastName\" : null } \n");  
jtulach@749
   164
        
jtulach@749
   165
        final ByteArrayInputStream is = new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
jtulach@749
   166
        Person p = Models.parse(c, Person.class, is);
jtulach@749
   167
jtulach@749
   168
        assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
jtulach@749
   169
        assert null == p.getLastName() : "Last name: " + p.getLastName();
jtulach@749
   170
    }
jtulach@747
   171
jtulach@750
   172
    @KOTest 
jtulach@750
   173
    public void parseNullArrayValue() throws Exception {
jtulach@750
   174
        final BrwsrCtx c = newContext();
jtulach@750
   175
        
jtulach@750
   176
        StringBuilder sb = new StringBuilder();
jtulach@750
   177
        sb.append("[ null, { \"firstName\" : \"son\",\n");
jtulach@750
   178
        sb.append("  \"lastName\" : null } ]\n");  
jtulach@750
   179
        
jtulach@750
   180
        final ByteArrayInputStream is = new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
jtulach@750
   181
        List<Person> arr = new ArrayList<Person>();
jtulach@750
   182
        Models.parse(c, Person.class, is, arr);
jtulach@750
   183
        
jtulach@750
   184
        assert arr.size() == 2 : "There are two items in " + arr;
jtulach@750
   185
        assert arr.get(0) == null : "first is null " + arr;
jtulach@750
   186
        
jtulach@750
   187
        Person p = arr.get(1);
jtulach@750
   188
        assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
jtulach@750
   189
        assert null == p.getLastName() : "Last name: " + p.getLastName();
jtulach@750
   190
    }
jtulach@750
   191
jtulach@747
   192
    @KOTest
jaroslav@34
   193
    public void testConvertToPeopleWithoutSex() throws Exception {
jaroslav@34
   194
        final Object o = createJSON(false);
jaroslav@34
   195
        
jaroslav@388
   196
        Person p = Models.fromRaw(newContext(), Person.class, o);
jaroslav@34
   197
        
jaroslav@34
   198
        assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
jaroslav@34
   199
        assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
jaroslav@34
   200
        assert p.getSex() == null : "No sex: " + p.getSex();
jaroslav@34
   201
    }
jaroslav@34
   202
    
jaroslav@137
   203
    @KOTest
jaroslav@60
   204
    public void parseConvertToPeopleWithoutSex() throws Exception {
jaroslav@121
   205
        final BrwsrCtx c = newContext();
jtulach@747
   206
        final InputStream o = createIS(false, false, -1);
jtulach@745
   207
        Person p = Models.parse(c, Person.class, o);
jtulach@745
   208
        
jtulach@745
   209
        assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
jtulach@745
   210
        assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
jtulach@745
   211
        assert p.getSex() == null : "No sex: " + p.getSex();
jtulach@745
   212
    }
jtulach@745
   213
    
jtulach@745
   214
    @KOTest
jtulach@745
   215
    public void parseConvertToPeopleWithAddressOnArray() throws Exception {
jtulach@745
   216
        final BrwsrCtx c = newContext();
jtulach@747
   217
        final InputStream o = createIS(true, true, 1);
jtulach@745
   218
        
jtulach@745
   219
        Person p = Models.parse(c, Person.class, o);
jtulach@745
   220
        
jtulach@745
   221
        assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
jtulach@745
   222
        assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
jtulach@745
   223
        assert Sex.MALE.equals(p.getSex()) : "Sex: " + p.getSex();
jtulach@745
   224
        assert p.getAddress() != null : "Some address provided";
jtulach@745
   225
        assert p.getAddress().getStreet().equals("Schnirchova") : "Is Schnirchova: " + p.getAddress();
jtulach@745
   226
    }
jtulach@745
   227
jtulach@745
   228
    @KOTest
jtulach@745
   229
    public void parseConvertToPeopleWithoutSexOnArray() throws Exception {
jtulach@745
   230
        final BrwsrCtx c = newContext();
jtulach@747
   231
        final InputStream o = createIS(false, false, 1);
jaroslav@60
   232
        Person p = Models.parse(c, Person.class, o);
jaroslav@60
   233
        
jaroslav@60
   234
        assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
jaroslav@60
   235
        assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
jaroslav@60
   236
        assert p.getSex() == null : "No sex: " + p.getSex();
jaroslav@60
   237
    }
jtulach@747
   238
jtulach@747
   239
    @KOTest
jtulach@747
   240
    public void parseFirstElementFromAbiggerArray() throws Exception {
jtulach@747
   241
        final BrwsrCtx c = newContext();
jtulach@747
   242
        final InputStream o = createIS(false, false, 5);
jtulach@747
   243
        Person p = Models.parse(c, Person.class, o);
jtulach@747
   244
        
jtulach@747
   245
        assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
jtulach@747
   246
        assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
jtulach@747
   247
        assert p.getSex() == null : "No sex: " + p.getSex();
jtulach@747
   248
    }
jtulach@747
   249
jtulach@747
   250
    @KOTest
jtulach@747
   251
    public void parseAllElementFromAbiggerArray() throws Exception {
jtulach@747
   252
        final BrwsrCtx c = newContext();
jtulach@747
   253
        final InputStream o = createIS(false, false, 5);
jtulach@747
   254
        
jtulach@747
   255
        List<Person> res = new ArrayList<Person>();
jtulach@747
   256
        Models.parse(c, Person.class, o, res);
jtulach@747
   257
        
jtulach@747
   258
        assert res.size() == 5 : "Five elements found" + res;
jtulach@747
   259
        
jtulach@747
   260
        for (Person p : res) {
jtulach@747
   261
            assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
jtulach@747
   262
            assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
jtulach@747
   263
            assert p.getSex() == null : "No sex: " + p.getSex();
jtulach@747
   264
        }
jtulach@747
   265
    }
jaroslav@60
   266
    
jtulach@746
   267
    @KOTest
jtulach@746
   268
    public void parseOnEmptyArray() throws Exception {
jtulach@746
   269
        final BrwsrCtx c = newContext();
jtulach@747
   270
        final InputStream o = createIS(false, false, 0);
jtulach@746
   271
        
jtulach@746
   272
        try {
jtulach@746
   273
            Models.parse(c, Person.class, o);
jtulach@746
   274
        } catch (EOFException ex) {
jtulach@746
   275
            // OK
jtulach@746
   276
            return;
jtulach@746
   277
        }
jtulach@746
   278
        throw new IllegalStateException("Should throw end of file exception, as the array is empty");
jtulach@746
   279
    }
jtulach@746
   280
    
jaroslav@121
   281
    private static BrwsrCtx newContext() {
jaroslav@121
   282
        return Utils.newContext(ConvertTypesTest.class);
jaroslav@121
   283
    }
jaroslav@34
   284
}