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.
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
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]"
29 * The Original Software is NetBeans. The Initial Developer of the Original
30 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
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.
43 package net.java.html.json.tests;
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;
53 import net.java.html.BrwsrCtx;
54 import net.java.html.json.Models;
55 import org.netbeans.html.json.tck.KOTest;
59 * @author Jaroslav Tulach
61 public final class ConvertTypesTest {
62 private static InputStream createIS(boolean includeSex, boolean includeAddress, int array)
63 throws UnsupportedEncodingException {
64 StringBuilder sb = new StringBuilder();
72 for (int i = 0; i < repeat; i++) {
73 sb.append("{ \"firstName\" : \"son\",\n");
74 sb.append(" \"lastName\" : \"dj\" \n");
76 sb.append(", \"sex\" : \"MALE\" \n");
79 sb.append(", \"address\" : { \"street\" : \"Schnirchova\" } \n");
89 return new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
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");
97 map.put("sex", "MALE");
99 return Utils.createObject(map, ConvertTypesTest.class);
103 public void testConvertToPeople() throws Exception {
104 final Object o = createJSON(true);
106 Person p = Models.fromRaw(newContext(), Person.class, o);
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();
114 public void parseConvertToPeople() throws Exception {
115 final BrwsrCtx c = newContext();
116 final InputStream o = createIS(true, false, -1);
118 Person p = Models.parse(c, Person.class, o);
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();
126 public void parseConvertToPeopleWithAddress() throws Exception {
127 final BrwsrCtx c = newContext();
128 final InputStream o = createIS(true, true, -1);
130 Person p = Models.parse(c, Person.class, o);
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();
140 public void parseConvertToPeopleWithAddressIntoAnArray() throws Exception {
141 final BrwsrCtx c = newContext();
142 final InputStream o = createIS(true, true, -1);
144 List<Person> arr = new ArrayList<Person>();
145 Models.parse(c, Person.class, o, arr);
147 assert arr.size() == 1 : "There is one item in " + arr;
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();
158 public void parseNullValue() throws Exception {
159 final BrwsrCtx c = newContext();
161 StringBuilder sb = new StringBuilder();
162 sb.append("{ \"firstName\" : \"son\",\n");
163 sb.append(" \"lastName\" : null } \n");
165 final ByteArrayInputStream is = new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
166 Person p = Models.parse(c, Person.class, is);
168 assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
169 assert null == p.getLastName() : "Last name: " + p.getLastName();
173 public void parseNullArrayValue() throws Exception {
174 final BrwsrCtx c = newContext();
176 StringBuilder sb = new StringBuilder();
177 sb.append("[ null, { \"firstName\" : \"son\",\n");
178 sb.append(" \"lastName\" : null } ]\n");
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);
184 assert arr.size() == 2 : "There are two items in " + arr;
185 assert arr.get(0) == null : "first is null " + arr;
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();
193 public void testConvertToPeopleWithoutSex() throws Exception {
194 final Object o = createJSON(false);
196 Person p = Models.fromRaw(newContext(), Person.class, o);
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();
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);
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();
215 public void parseConvertToPeopleWithAddressOnArray() throws Exception {
216 final BrwsrCtx c = newContext();
217 final InputStream o = createIS(true, true, 1);
219 Person p = Models.parse(c, Person.class, o);
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();
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);
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();
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);
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();
251 public void parseAllElementFromAbiggerArray() throws Exception {
252 final BrwsrCtx c = newContext();
253 final InputStream o = createIS(false, false, 5);
255 List<Person> res = new ArrayList<Person>();
256 Models.parse(c, Person.class, o, res);
258 assert res.size() == 5 : "Five elements found" + res;
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();
268 public void parseOnEmptyArray() throws Exception {
269 final BrwsrCtx c = newContext();
270 final InputStream o = createIS(false, false, 0);
273 Models.parse(c, Person.class, o);
274 } catch (EOFException ex) {
278 throw new IllegalStateException("Should throw end of file exception, as the array is empty");
281 private static BrwsrCtx newContext() {
282 return Utils.newContext(ConvertTypesTest.class);