2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2013-2013 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-2013 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 org.netbeans.html.json.impl;
46 import net.java.html.BrwsrCtx;
47 import net.java.html.json.MapModelTest;
48 import net.java.html.json.Model;
49 import net.java.html.json.Models;
50 import net.java.html.json.OnPropertyChange;
51 import net.java.html.json.Property;
52 import org.apidesign.html.context.spi.Contexts;
53 import org.apidesign.html.json.spi.Technology;
54 import static org.testng.Assert.*;
55 import org.testng.annotations.Test;
59 * @author Jaroslav Tulach <jtulach@netbeans.org>
61 public class PlainEnumTest {
62 @Test public void unionA() {
63 Union on = new Union(new Union.A(9), 11, 1.1);
64 assertEquals(on.getX(), 11);
65 assertEquals(on.getY(), 1.1);
67 assertEquals(on.getAbc(), Abc.A);
68 assertNotNull(on.getA());
69 assertEquals(on.getA().getUa().size(), 1);
70 assertEquals(on.getA().getUa().get(0), Integer.valueOf(9));
71 assertNull(on.getB());
74 @Test public void unionB() {
75 Union on = new Union(new Union.B("9.9"), 11, 1.1);
76 assertEquals(on.getX(), 11);
77 assertEquals(on.getY(), 1.1);
79 assertEquals(on.getAbc(), Abc.B);
80 assertNull(on.getA());
81 assertNotNull(on.getB());
82 assertEquals(on.getB().getUb(), "9.9");
85 @Test public void cloneUnionB() {
86 Union old = new Union(new Union.B("9.9"), 11, 1.1);
87 Union on = old.clone();
88 assertNotSame(old.getB(), on.getB());
90 assertEquals(on.getX(), 11);
91 assertEquals(on.getY(), 1.1);
93 assertEquals(on.getAbc(), Abc.B);
94 assertNull(on.getA());
95 assertNotNull(on.getB());
96 assertEquals(on.getB().getUb(), "9.9");
99 @Model(className = "Union", properties = {
100 @Property(name = "x", type = int.class),
101 @Property(name = "y", type = double.class)
104 @Model(className = "A", properties = {
105 @Property(name = "ua", type = int.class, array = true)
108 @Model(className = "B", properties = {
109 @Property(name = "ub", type = String.class)
113 @OnPropertyChange({ "ua", "ub" })
114 static void onChange(Union u) {
115 u.setX(u.getX() + 1);
119 @Model(className = "EnumAndModel", properties = {
120 @Property(name = "enm", type = Abc.class),
121 @Property(name = "mdl", type = Union.class),
123 static class EnumAndModelModel {
126 @Test public void enumIsTreatedAsEnum() {
127 EnumAndModel em = new EnumAndModel();
128 assertNull(em.getEnm(), "No enum set, yet");
129 assertNotNull(em.getMdl(), "Model is not null");
132 @Test public void serializeAsASingleObject() {
133 Union on = new Union(new Union.B("9.9"), 11, 1.1);
134 String json = on.toString();
135 assertNotEquals(json.indexOf("\"9.9\""), -1, "The string 9.9 should be in the output");
136 assertNotEquals(json.indexOf("Abc"), -1, "The property 'Abc' should be in");
137 assertNotEquals(json.indexOf("\"Abc\""), -1, "The string 'Abc' should be assigned");
139 int first = json.indexOf('{');
140 assertEquals(first, 0, "Begins with {");
142 int next = json.indexOf('{', 1);
143 assertEquals(next, -1, "No other {");
146 @Test public void unionOfProperties() {
147 MapModelTest.MapTechnology mt = new MapModelTest.MapTechnology();
148 BrwsrCtx ctx = Contexts.newBuilder().register(Technology.class, mt, 1).build();
150 Union on = new Union(new Union.B("9.9"), 11, 1.1);
151 Union u = Models.bind(on, ctx);
153 Map<?,?> map = (Map<?,?>)Models.toRaw(u);
154 assertNotNull(map.get("x"), "Four properties: " + map);
155 assertNotNull(map.get("y"), "Four properties: " + map);
157 assertNotNull(map.get("ua"), "Four properties: " + map);
158 assertNotNull(ub = (MapModelTest.One) map.get("ub"), "Four properties: " + map);
160 assertEquals(u.getX(), 11, "Eleven");
162 assertEquals(ub.getChanges(), 0, "No changes yet");
163 u.getB().setUb("10");
164 assertEquals(u.getX(), 12, "Change increases X");
165 assertEquals(ub.getChanges(), 1, "One change");
168 @Test public void changeInUnionArray() {
169 MapModelTest.MapTechnology mt = new MapModelTest.MapTechnology();
170 BrwsrCtx ctx = Contexts.newBuilder().register(Technology.class, mt, 1).build();
172 Union on = new Union(new Union.A(10, 20, 30), 0, 1.1);
173 Union u = Models.bind(on, ctx);
175 Map<?, ?> map = (Map<?, ?>) Models.toRaw(u);
177 assertNotNull(ua = (MapModelTest.One) map.get("ua"), "Array prop found: " + map);
179 assertEquals(u.getX(), 0, "Zero");
180 assertEquals(ua.getChanges(), 0, "No changes yet");
181 u.getA().getUa().add(40);
182 assertEquals(u.getX(), 1, "Incremented once");
183 assertEquals(ua.getChanges(), 1, "One change");