1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/json/src/test/java/net/java/html/json/MapModelNotMutableTest.java Mon Feb 22 06:09:33 2016 +0100
1.3 @@ -0,0 +1,227 @@
1.4 +/**
1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 + *
1.7 + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
1.8 + *
1.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
1.10 + * Other names may be trademarks of their respective owners.
1.11 + *
1.12 + * The contents of this file are subject to the terms of either the GNU
1.13 + * General Public License Version 2 only ("GPL") or the Common
1.14 + * Development and Distribution License("CDDL") (collectively, the
1.15 + * "License"). You may not use this file except in compliance with the
1.16 + * License. You can obtain a copy of the License at
1.17 + * http://www.netbeans.org/cddl-gplv2.html
1.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.19 + * specific language governing permissions and limitations under the
1.20 + * License. When distributing the software, include this License Header
1.21 + * Notice in each file and include the License file at
1.22 + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
1.23 + * particular file as subject to the "Classpath" exception as provided
1.24 + * by Oracle in the GPL Version 2 section of the License file that
1.25 + * accompanied this code. If applicable, add the following below the
1.26 + * License Header, with the fields enclosed by brackets [] replaced by
1.27 + * your own identifying information:
1.28 + * "Portions Copyrighted [year] [name of copyright owner]"
1.29 + *
1.30 + * Contributor(s):
1.31 + *
1.32 + * The Original Software is NetBeans. The Initial Developer of the Original
1.33 + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
1.34 + *
1.35 + * If you wish your version of this file to be governed by only the CDDL
1.36 + * or only the GPL Version 2, indicate your decision by adding
1.37 + * "[Contributor] elects to include this software in this distribution
1.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
1.39 + * single choice of license, a recipient has the option to distribute
1.40 + * your version of this file under either the CDDL, the GPL Version 2 or
1.41 + * to extend the choice of license to its licensees as provided above.
1.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
1.43 + * Version 2 license, then the option applies only if the new code is
1.44 + * made subject to such option by the copyright holder.
1.45 + */
1.46 +package net.java.html.json;
1.47 +
1.48 +import java.util.Map;
1.49 +import net.java.html.BrwsrCtx;
1.50 +import org.netbeans.html.context.spi.Contexts;
1.51 +import org.netbeans.html.json.spi.Technology;
1.52 +import org.netbeans.html.json.spi.Transfer;
1.53 +import static org.testng.Assert.assertEquals;
1.54 +import static org.testng.Assert.assertFalse;
1.55 +import static org.testng.Assert.assertNotNull;
1.56 +import static org.testng.Assert.fail;
1.57 +import org.testng.annotations.BeforeMethod;
1.58 +import org.testng.annotations.Test;
1.59 +
1.60 +@Model(className = "ConstantValues", properties = {
1.61 + @Property(name = "byteNumber", type = byte.class, mutable = false),
1.62 + @Property(name = "shortNumber", type = short.class, mutable = false),
1.63 + @Property(name = "intNumber", type = int.class, mutable = false),
1.64 + @Property(name = "longNumber", type = long.class, mutable = false),
1.65 + @Property(name = "floatNumber", type = float.class, mutable = false),
1.66 + @Property(name = "doubleNumber", type = double.class, mutable = false),
1.67 + @Property(name = "stringValue", type = String.class, mutable = false),
1.68 + @Property(name = "byteArray", type = byte.class, mutable = false, array = true),
1.69 + @Property(name = "shortArray", type = short.class, mutable = false, array = true),
1.70 + @Property(name = "intArray", type = int.class, mutable = false, array = true),
1.71 + @Property(name = "longArray", type = long.class, mutable = false, array = true),
1.72 + @Property(name = "floatArray", type = float.class, mutable = false, array = true),
1.73 + @Property(name = "doubleArray", type = double.class, mutable = false, array = true),
1.74 + @Property(name = "stringArray", type = String.class, mutable = false, array = true),
1.75 +})
1.76 +public class MapModelNotMutableTest {
1.77 + private BrwsrCtx c;
1.78 +
1.79 + @BeforeMethod
1.80 + public void initTechnology() {
1.81 + MapModelTest.MapTechnology t = new MapModelTest.MapTechnology();
1.82 + c = Contexts.newBuilder().register(Technology.class, t, 1).
1.83 + register(Transfer.class, t, 1).build();
1.84 + }
1.85 +
1.86 + @Test
1.87 + public void byteConstant() throws Exception {
1.88 + ConstantValues value = Models.bind(new ConstantValues(), c);
1.89 + value.setByteNumber((byte)13);
1.90 +
1.91 + Map m = (Map) Models.toRaw(value);
1.92 + Object v = m.get("byteNumber");
1.93 + assertNotNull(v, "Value should be in the map");
1.94 + assertEquals(v.getClass(), MapModelTest.One.class, "It is instance of One");
1.95 + MapModelTest.One o = (MapModelTest.One) v;
1.96 + assertEquals(o.changes, 0, "No change so far the only one change happened before we connected");
1.97 + assertEquals((byte)13, o.get());
1.98 +
1.99 + try {
1.100 + value.setByteNumber((byte)15);
1.101 + fail("Changing value shouldn't succeed!");
1.102 + } catch (IllegalStateException ex) {
1.103 + // OK
1.104 + }
1.105 + assertEquals(o.get(), (byte)13, "Old value should still be in the map");
1.106 + assertEquals(o.changes, 0, "No change");
1.107 + assertFalse(o.pb.isReadOnly(), "Mutable property");
1.108 + }
1.109 +
1.110 + @Test
1.111 + public void shortConstant() throws Exception {
1.112 + ConstantValues value = Models.bind(new ConstantValues(), c);
1.113 + value.setShortNumber((short)13);
1.114 +
1.115 + Map m = (Map) Models.toRaw(value);
1.116 + Object v = m.get("shortNumber");
1.117 + assertNotNull(v, "Value should be in the map");
1.118 + assertEquals(v.getClass(), MapModelTest.One.class, "It is instance of One");
1.119 + MapModelTest.One o = (MapModelTest.One) v;
1.120 + assertEquals(o.changes, 0, "No change so far the only one change happened before we connected");
1.121 + assertEquals((short)13, o.get());
1.122 +
1.123 + try {
1.124 + value.setShortNumber((short)15);
1.125 + fail("Changing value shouldn't succeed!");
1.126 + } catch (IllegalStateException ex) {
1.127 + // OK
1.128 + }
1.129 + assertEquals(o.get(), (short)13, "Old value should still be in the map");
1.130 + assertEquals(o.changes, 0, "No change");
1.131 + assertFalse(o.pb.isReadOnly(), "Mutable property");
1.132 + }
1.133 +
1.134 + @Test
1.135 + public void intConstant() throws Exception {
1.136 + ConstantValues value = Models.bind(new ConstantValues(), c);
1.137 + value.setIntNumber(13);
1.138 +
1.139 + Map m = (Map) Models.toRaw(value);
1.140 + Object v = m.get("intNumber");
1.141 + assertNotNull(v, "Value should be in the map");
1.142 + assertEquals(v.getClass(), MapModelTest.One.class, "It is instance of One");
1.143 + MapModelTest.One o = (MapModelTest.One) v;
1.144 + assertEquals(o.changes, 0, "No change so far the only one change happened before we connected");
1.145 + assertEquals(13, o.get());
1.146 +
1.147 + try {
1.148 + value.setIntNumber(15);
1.149 + fail("Changing value shouldn't succeed!");
1.150 + } catch (IllegalStateException ex) {
1.151 + // OK
1.152 + }
1.153 + assertEquals(o.get(), 13, "Old value should still be in the map");
1.154 + assertEquals(o.changes, 0, "No change");
1.155 + assertFalse(o.pb.isReadOnly(), "Mutable property");
1.156 + }
1.157 +
1.158 + @Test
1.159 + public void doubleConstant() throws Exception {
1.160 + ConstantValues value = Models.bind(new ConstantValues(), c);
1.161 + value.setDoubleNumber(13);
1.162 +
1.163 + Map m = (Map) Models.toRaw(value);
1.164 + Object v = m.get("doubleNumber");
1.165 + assertNotNull(v, "Value should be in the map");
1.166 + assertEquals(v.getClass(), MapModelTest.One.class, "It is instance of One");
1.167 + MapModelTest.One o = (MapModelTest.One) v;
1.168 + assertEquals(o.changes, 0, "No change so far the only one change happened before we connected");
1.169 + assertEquals(13.0, o.get());
1.170 +
1.171 + try {
1.172 + value.setDoubleNumber(15);
1.173 + fail("Changing value shouldn't succeed!");
1.174 + } catch (IllegalStateException ex) {
1.175 + // OK
1.176 + }
1.177 + assertEquals(o.get(), 13.0, "Old value should still be in the map");
1.178 + assertEquals(o.changes, 0, "No change");
1.179 + assertFalse(o.pb.isReadOnly(), "Mutable property");
1.180 + }
1.181 +
1.182 + @Test
1.183 + public void stringConstant() throws Exception {
1.184 + ConstantValues value = Models.bind(new ConstantValues(), c);
1.185 + value.setStringValue("Hi");
1.186 +
1.187 + Map m = (Map) Models.toRaw(value);
1.188 + Object v = m.get("stringValue");
1.189 + assertNotNull(v, "Value should be in the map");
1.190 + assertEquals(v.getClass(), MapModelTest.One.class, "It is instance of One");
1.191 + MapModelTest.One o = (MapModelTest.One) v;
1.192 + assertEquals(o.changes, 0, "No change so far the only one change happened before we connected");
1.193 + assertEquals("Hi", o.get());
1.194 +
1.195 + try {
1.196 + value.setStringValue("Hello");
1.197 + fail("Changing value shouldn't succeed!");
1.198 + } catch (IllegalStateException ex) {
1.199 + // OK
1.200 + }
1.201 + assertEquals(o.get(), "Hi", "Old value should still be in the map");
1.202 + assertEquals(o.changes, 0, "No change");
1.203 + assertFalse(o.pb.isReadOnly(), "Mutable property");
1.204 + }
1.205 +
1.206 + @Test
1.207 + public void stringArray() throws Exception {
1.208 + ConstantValues value = Models.bind(new ConstantValues(), c);
1.209 + value.getStringArray().add("Hi");
1.210 +
1.211 + Map m = (Map) Models.toRaw(value);
1.212 + Object v = m.get("stringArray");
1.213 + assertNotNull(v, "Value should be in the map");
1.214 + assertEquals(v.getClass(), MapModelTest.One.class, "It is instance of One");
1.215 + MapModelTest.One o = (MapModelTest.One) v;
1.216 + assertEquals(o.changes, 0, "No change so far the only one change happened before we connected");
1.217 + assertEquals(o.get(), new String[] { "Hi" }, "One element");
1.218 +
1.219 + try {
1.220 + value.getStringArray().add("Hello");
1.221 + fail("Changing value shouldn't succeed!");
1.222 + } catch (UnsupportedOperationException ex) {
1.223 + // OK
1.224 + }
1.225 + assertEquals(o.get(), new String[] { "Hi" }, "Old value should still be in the map");
1.226 + assertEquals(o.changes, 0, "No change");
1.227 + assertFalse(o.pb.isReadOnly(), "Mutable property");
1.228 + }
1.229 +
1.230 +}