#258088: Introducing @Property(mutable=false) and making sure the generated Java code yields exceptions when such properties are modified after their use by the technology
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;
46 import net.java.html.BrwsrCtx;
47 import org.netbeans.html.context.spi.Contexts;
48 import org.netbeans.html.json.spi.Technology;
49 import org.netbeans.html.json.spi.Transfer;
50 import static org.testng.Assert.assertEquals;
51 import static org.testng.Assert.assertFalse;
52 import static org.testng.Assert.assertNotNull;
53 import static org.testng.Assert.fail;
54 import org.testng.annotations.BeforeMethod;
55 import org.testng.annotations.Test;
57 @Model(className = "ConstantValues", properties = {
58 @Property(name = "byteNumber", type = byte.class, mutable = false),
59 @Property(name = "shortNumber", type = short.class, mutable = false),
60 @Property(name = "intNumber", type = int.class, mutable = false),
61 @Property(name = "longNumber", type = long.class, mutable = false),
62 @Property(name = "floatNumber", type = float.class, mutable = false),
63 @Property(name = "doubleNumber", type = double.class, mutable = false),
64 @Property(name = "stringValue", type = String.class, mutable = false),
65 @Property(name = "byteArray", type = byte.class, mutable = false, array = true),
66 @Property(name = "shortArray", type = short.class, mutable = false, array = true),
67 @Property(name = "intArray", type = int.class, mutable = false, array = true),
68 @Property(name = "longArray", type = long.class, mutable = false, array = true),
69 @Property(name = "floatArray", type = float.class, mutable = false, array = true),
70 @Property(name = "doubleArray", type = double.class, mutable = false, array = true),
71 @Property(name = "stringArray", type = String.class, mutable = false, array = true),
73 public class MapModelNotMutableTest {
77 public void initTechnology() {
78 MapModelTest.MapTechnology t = new MapModelTest.MapTechnology();
79 c = Contexts.newBuilder().register(Technology.class, t, 1).
80 register(Transfer.class, t, 1).build();
84 public void byteConstant() throws Exception {
85 ConstantValues value = Models.bind(new ConstantValues(), c);
86 value.setByteNumber((byte)13);
88 Map m = (Map) Models.toRaw(value);
89 Object v = m.get("byteNumber");
90 assertNotNull(v, "Value should be in the map");
91 assertEquals(v.getClass(), MapModelTest.One.class, "It is instance of One");
92 MapModelTest.One o = (MapModelTest.One) v;
93 assertEquals(o.changes, 0, "No change so far the only one change happened before we connected");
94 assertEquals((byte)13, o.get());
97 value.setByteNumber((byte)15);
98 fail("Changing value shouldn't succeed!");
99 } catch (IllegalStateException ex) {
102 assertEquals(o.get(), (byte)13, "Old value should still be in the map");
103 assertEquals(o.changes, 0, "No change");
104 assertFalse(o.pb.isReadOnly(), "Mutable property");
108 public void shortConstant() throws Exception {
109 ConstantValues value = Models.bind(new ConstantValues(), c);
110 value.setShortNumber((short)13);
112 Map m = (Map) Models.toRaw(value);
113 Object v = m.get("shortNumber");
114 assertNotNull(v, "Value should be in the map");
115 assertEquals(v.getClass(), MapModelTest.One.class, "It is instance of One");
116 MapModelTest.One o = (MapModelTest.One) v;
117 assertEquals(o.changes, 0, "No change so far the only one change happened before we connected");
118 assertEquals((short)13, o.get());
121 value.setShortNumber((short)15);
122 fail("Changing value shouldn't succeed!");
123 } catch (IllegalStateException ex) {
126 assertEquals(o.get(), (short)13, "Old value should still be in the map");
127 assertEquals(o.changes, 0, "No change");
128 assertFalse(o.pb.isReadOnly(), "Mutable property");
132 public void intConstant() throws Exception {
133 ConstantValues value = Models.bind(new ConstantValues(), c);
134 value.setIntNumber(13);
136 Map m = (Map) Models.toRaw(value);
137 Object v = m.get("intNumber");
138 assertNotNull(v, "Value should be in the map");
139 assertEquals(v.getClass(), MapModelTest.One.class, "It is instance of One");
140 MapModelTest.One o = (MapModelTest.One) v;
141 assertEquals(o.changes, 0, "No change so far the only one change happened before we connected");
142 assertEquals(13, o.get());
145 value.setIntNumber(15);
146 fail("Changing value shouldn't succeed!");
147 } catch (IllegalStateException ex) {
150 assertEquals(o.get(), 13, "Old value should still be in the map");
151 assertEquals(o.changes, 0, "No change");
152 assertFalse(o.pb.isReadOnly(), "Mutable property");
156 public void doubleConstant() throws Exception {
157 ConstantValues value = Models.bind(new ConstantValues(), c);
158 value.setDoubleNumber(13);
160 Map m = (Map) Models.toRaw(value);
161 Object v = m.get("doubleNumber");
162 assertNotNull(v, "Value should be in the map");
163 assertEquals(v.getClass(), MapModelTest.One.class, "It is instance of One");
164 MapModelTest.One o = (MapModelTest.One) v;
165 assertEquals(o.changes, 0, "No change so far the only one change happened before we connected");
166 assertEquals(13.0, o.get());
169 value.setDoubleNumber(15);
170 fail("Changing value shouldn't succeed!");
171 } catch (IllegalStateException ex) {
174 assertEquals(o.get(), 13.0, "Old value should still be in the map");
175 assertEquals(o.changes, 0, "No change");
176 assertFalse(o.pb.isReadOnly(), "Mutable property");
180 public void stringConstant() throws Exception {
181 ConstantValues value = Models.bind(new ConstantValues(), c);
182 value.setStringValue("Hi");
184 Map m = (Map) Models.toRaw(value);
185 Object v = m.get("stringValue");
186 assertNotNull(v, "Value should be in the map");
187 assertEquals(v.getClass(), MapModelTest.One.class, "It is instance of One");
188 MapModelTest.One o = (MapModelTest.One) v;
189 assertEquals(o.changes, 0, "No change so far the only one change happened before we connected");
190 assertEquals("Hi", o.get());
193 value.setStringValue("Hello");
194 fail("Changing value shouldn't succeed!");
195 } catch (IllegalStateException ex) {
198 assertEquals(o.get(), "Hi", "Old value should still be in the map");
199 assertEquals(o.changes, 0, "No change");
200 assertFalse(o.pb.isReadOnly(), "Mutable property");
204 public void stringArray() throws Exception {
205 ConstantValues value = Models.bind(new ConstantValues(), c);
206 value.getStringArray().add("Hi");
208 Map m = (Map) Models.toRaw(value);
209 Object v = m.get("stringArray");
210 assertNotNull(v, "Value should be in the map");
211 assertEquals(v.getClass(), MapModelTest.One.class, "It is instance of One");
212 MapModelTest.One o = (MapModelTest.One) v;
213 assertEquals(o.changes, 0, "No change so far the only one change happened before we connected");
214 assertEquals(o.get(), new String[] { "Hi" }, "One element");
217 value.getStringArray().add("Hello");
218 fail("Changing value shouldn't succeed!");
219 } catch (UnsupportedOperationException ex) {
222 assertEquals(o.get(), new String[] { "Hi" }, "Old value should still be in the map");
223 assertEquals(o.changes, 0, "No change");
224 assertFalse(o.pb.isReadOnly(), "Mutable property");