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.
     1 /**
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     5  *
     6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
     7  * Other names may be trademarks of their respective owners.
     8  *
     9  * The contents of this file are subject to the terms of either the GNU
    10  * General Public License Version 2 only ("GPL") or the Common
    11  * Development and Distribution License("CDDL") (collectively, the
    12  * "License"). You may not use this file except in compliance with the
    13  * License. You can obtain a copy of the License at
    14  * http://www.netbeans.org/cddl-gplv2.html
    15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    16  * specific language governing permissions and limitations under the
    17  * License.  When distributing the software, include this License Header
    18  * Notice in each file and include the License file at
    19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    20  * particular file as subject to the "Classpath" exception as provided
    21  * by Oracle in the GPL Version 2 section of the License file that
    22  * accompanied this code. If applicable, add the following below the
    23  * License Header, with the fields enclosed by brackets [] replaced by
    24  * your own identifying information:
    25  * "Portions Copyrighted [year] [name of copyright owner]"
    26  *
    27  * Contributor(s):
    28  *
    29  * The Original Software is NetBeans. The Initial Developer of the Original
    30  * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    31  *
    32  * If you wish your version of this file to be governed by only the CDDL
    33  * or only the GPL Version 2, indicate your decision by adding
    34  * "[Contributor] elects to include this software in this distribution
    35  * under the [CDDL or GPL Version 2] license." If you do not indicate a
    36  * single choice of license, a recipient has the option to distribute
    37  * your version of this file under either the CDDL, the GPL Version 2 or
    38  * to extend the choice of license to its licensees as provided above.
    39  * However, if you add GPL Version 2 code and therefore, elected the GPL
    40  * Version 2 license, then the option applies only if the new code is
    41  * made subject to such option by the copyright holder.
    42  */
    43 package net.java.html.json.tests;
    44 
    45 import java.io.ByteArrayInputStream;
    46 import java.io.EOFException;
    47 import java.io.InputStream;
    48 import java.io.UnsupportedEncodingException;
    49 import java.util.ArrayList;
    50 import java.util.HashMap;
    51 import java.util.List;
    52 import java.util.Map;
    53 import net.java.html.BrwsrCtx;
    54 import net.java.html.json.Models;
    55 import org.netbeans.html.json.tck.KOTest;
    56 
    57 /**
    58  *
    59  * @author Jaroslav Tulach
    60  */
    61 public final class ConvertTypesTest {
    62     private static InputStream createIS(boolean includeSex, boolean includeAddress, int array) 
    63     throws UnsupportedEncodingException {
    64         StringBuilder sb = new StringBuilder();
    65         int repeat;
    66         if (array != -1) {
    67             sb.append("[\n");
    68             repeat = array;
    69         } else {
    70             repeat = 1;
    71         }
    72         for (int i = 0; i < repeat; i++) {
    73             sb.append("{ \"firstName\" : \"son\",\n");
    74             sb.append("  \"lastName\" : \"dj\" \n");
    75             if (includeSex) {
    76                 sb.append(",  \"sex\" : \"MALE\" \n");
    77             }
    78             if (includeAddress) {
    79                 sb.append(",  \"address\" : { \"street\" : \"Schnirchova\" } \n");
    80             }
    81             sb.append("}\n");
    82             if (i < array - 1) {
    83                 sb.append(",");
    84             }
    85         }
    86         if (array != -1) {
    87             sb.append(']');
    88         }
    89         return new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
    90     }
    91     private static Object createJSON(boolean includeSex) 
    92     throws UnsupportedEncodingException {
    93         Map<String,Object> map = new HashMap<String,Object>();
    94         map.put("firstName", "son");
    95         map.put("lastName", "dj");
    96         if (includeSex) {
    97             map.put("sex", "MALE");
    98         }
    99         return Utils.createObject(map, ConvertTypesTest.class);
   100     }
   101     
   102     @KOTest
   103     public void testConvertToPeople() throws Exception {
   104         final Object o = createJSON(true);
   105         
   106         Person p = Models.fromRaw(newContext(), Person.class, o);
   107         
   108         assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
   109         assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
   110         assert Sex.MALE.equals(p.getSex()) : "Sex: " + p.getSex();
   111     }
   112 
   113     @KOTest
   114     public void parseConvertToPeople() throws Exception {
   115         final BrwsrCtx c = newContext();
   116         final InputStream o = createIS(true, false, -1);
   117         
   118         Person p = Models.parse(c, Person.class, o);
   119         
   120         assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
   121         assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
   122         assert Sex.MALE.equals(p.getSex()) : "Sex: " + p.getSex();
   123     }
   124     
   125     @KOTest
   126     public void parseConvertToPeopleWithAddress() throws Exception {
   127         final BrwsrCtx c = newContext();
   128         final InputStream o = createIS(true, true, -1);
   129         
   130         Person p = Models.parse(c, Person.class, o);
   131         
   132         assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
   133         assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
   134         assert Sex.MALE.equals(p.getSex()) : "Sex: " + p.getSex();
   135         assert p.getAddress() != null : "Some address provided";
   136         assert p.getAddress().getStreet().equals("Schnirchova") : "Is Schnirchova: " + p.getAddress();
   137     }
   138 
   139     @KOTest
   140     public void parseConvertToPeopleWithAddressIntoAnArray() throws Exception {
   141         final BrwsrCtx c = newContext();
   142         final InputStream o = createIS(true, true, -1);
   143         
   144         List<Person> arr = new ArrayList<Person>();
   145         Models.parse(c, Person.class, o, arr);
   146         
   147         assert arr.size() == 1 : "There is one item in " + arr;
   148         
   149         Person p = arr.get(0);
   150         assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
   151         assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
   152         assert Sex.MALE.equals(p.getSex()) : "Sex: " + p.getSex();
   153         assert p.getAddress() != null : "Some address provided";
   154         assert p.getAddress().getStreet().equals("Schnirchova") : "Is Schnirchova: " + p.getAddress();
   155     }
   156     
   157     @KOTest 
   158     public void parseNullValue() throws Exception {
   159         final BrwsrCtx c = newContext();
   160         
   161         StringBuilder sb = new StringBuilder();
   162         sb.append("{ \"firstName\" : \"son\",\n");
   163         sb.append("  \"lastName\" : null } \n");  
   164         
   165         final ByteArrayInputStream is = new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
   166         Person p = Models.parse(c, Person.class, is);
   167 
   168         assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
   169         assert null == p.getLastName() : "Last name: " + p.getLastName();
   170     }
   171 
   172     @KOTest 
   173     public void parseNullArrayValue() throws Exception {
   174         final BrwsrCtx c = newContext();
   175         
   176         StringBuilder sb = new StringBuilder();
   177         sb.append("[ null, { \"firstName\" : \"son\",\n");
   178         sb.append("  \"lastName\" : null } ]\n");  
   179         
   180         final ByteArrayInputStream is = new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
   181         List<Person> arr = new ArrayList<Person>();
   182         Models.parse(c, Person.class, is, arr);
   183         
   184         assert arr.size() == 2 : "There are two items in " + arr;
   185         assert arr.get(0) == null : "first is null " + arr;
   186         
   187         Person p = arr.get(1);
   188         assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
   189         assert null == p.getLastName() : "Last name: " + p.getLastName();
   190     }
   191 
   192     @KOTest
   193     public void testConvertToPeopleWithoutSex() throws Exception {
   194         final Object o = createJSON(false);
   195         
   196         Person p = Models.fromRaw(newContext(), Person.class, o);
   197         
   198         assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
   199         assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
   200         assert p.getSex() == null : "No sex: " + p.getSex();
   201     }
   202     
   203     @KOTest
   204     public void parseConvertToPeopleWithoutSex() throws Exception {
   205         final BrwsrCtx c = newContext();
   206         final InputStream o = createIS(false, false, -1);
   207         Person p = Models.parse(c, Person.class, o);
   208         
   209         assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
   210         assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
   211         assert p.getSex() == null : "No sex: " + p.getSex();
   212     }
   213     
   214     @KOTest
   215     public void parseConvertToPeopleWithAddressOnArray() throws Exception {
   216         final BrwsrCtx c = newContext();
   217         final InputStream o = createIS(true, true, 1);
   218         
   219         Person p = Models.parse(c, Person.class, o);
   220         
   221         assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
   222         assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
   223         assert Sex.MALE.equals(p.getSex()) : "Sex: " + p.getSex();
   224         assert p.getAddress() != null : "Some address provided";
   225         assert p.getAddress().getStreet().equals("Schnirchova") : "Is Schnirchova: " + p.getAddress();
   226     }
   227 
   228     @KOTest
   229     public void parseConvertToPeopleWithoutSexOnArray() throws Exception {
   230         final BrwsrCtx c = newContext();
   231         final InputStream o = createIS(false, false, 1);
   232         Person p = Models.parse(c, Person.class, o);
   233         
   234         assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
   235         assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
   236         assert p.getSex() == null : "No sex: " + p.getSex();
   237     }
   238 
   239     @KOTest
   240     public void parseFirstElementFromAbiggerArray() throws Exception {
   241         final BrwsrCtx c = newContext();
   242         final InputStream o = createIS(false, false, 5);
   243         Person p = Models.parse(c, Person.class, o);
   244         
   245         assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
   246         assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
   247         assert p.getSex() == null : "No sex: " + p.getSex();
   248     }
   249 
   250     @KOTest
   251     public void parseAllElementFromAbiggerArray() throws Exception {
   252         final BrwsrCtx c = newContext();
   253         final InputStream o = createIS(false, false, 5);
   254         
   255         List<Person> res = new ArrayList<Person>();
   256         Models.parse(c, Person.class, o, res);
   257         
   258         assert res.size() == 5 : "Five elements found" + res;
   259         
   260         for (Person p : res) {
   261             assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
   262             assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
   263             assert p.getSex() == null : "No sex: " + p.getSex();
   264         }
   265     }
   266     
   267     @KOTest
   268     public void parseOnEmptyArray() throws Exception {
   269         final BrwsrCtx c = newContext();
   270         final InputStream o = createIS(false, false, 0);
   271         
   272         try {
   273             Models.parse(c, Person.class, o);
   274         } catch (EOFException ex) {
   275             // OK
   276             return;
   277         }
   278         throw new IllegalStateException("Should throw end of file exception, as the array is empty");
   279     }
   280     
   281     private static BrwsrCtx newContext() {
   282         return Utils.newContext(ConvertTypesTest.class);
   283     }
   284 }