json/src/test/java/net/java/html/json/MapModelNotMutableTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Mon, 22 Feb 2016 06:09:33 +0100
branchNonMutable258088
changeset 1054 4c40ceb185e5
permissions -rw-r--r--
#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
     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;
    44 
    45 import java.util.Map;
    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;
    56 
    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),
    72 })
    73 public class MapModelNotMutableTest {
    74     private BrwsrCtx c;
    75 
    76     @BeforeMethod
    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();
    81     }
    82 
    83     @Test
    84     public void byteConstant() throws Exception {
    85         ConstantValues value = Models.bind(new ConstantValues(), c);
    86         value.setByteNumber((byte)13);
    87 
    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());
    95 
    96         try {
    97             value.setByteNumber((byte)15);
    98             fail("Changing value shouldn't succeed!");
    99         } catch (IllegalStateException ex) {
   100             // OK
   101         }
   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");
   105     }
   106 
   107     @Test
   108     public void shortConstant() throws Exception {
   109         ConstantValues value = Models.bind(new ConstantValues(), c);
   110         value.setShortNumber((short)13);
   111 
   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());
   119 
   120         try {
   121             value.setShortNumber((short)15);
   122             fail("Changing value shouldn't succeed!");
   123         } catch (IllegalStateException ex) {
   124             // OK
   125         }
   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");
   129     }
   130 
   131     @Test
   132     public void intConstant() throws Exception {
   133         ConstantValues value = Models.bind(new ConstantValues(), c);
   134         value.setIntNumber(13);
   135 
   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());
   143 
   144         try {
   145             value.setIntNumber(15);
   146             fail("Changing value shouldn't succeed!");
   147         } catch (IllegalStateException ex) {
   148             // OK
   149         }
   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");
   153     }
   154 
   155     @Test
   156     public void doubleConstant() throws Exception {
   157         ConstantValues value = Models.bind(new ConstantValues(), c);
   158         value.setDoubleNumber(13);
   159 
   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());
   167 
   168         try {
   169             value.setDoubleNumber(15);
   170             fail("Changing value shouldn't succeed!");
   171         } catch (IllegalStateException ex) {
   172             // OK
   173         }
   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");
   177     }
   178 
   179     @Test
   180     public void stringConstant() throws Exception {
   181         ConstantValues value = Models.bind(new ConstantValues(), c);
   182         value.setStringValue("Hi");
   183 
   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());
   191 
   192         try {
   193             value.setStringValue("Hello");
   194             fail("Changing value shouldn't succeed!");
   195         } catch (IllegalStateException ex) {
   196             // OK
   197         }
   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");
   201     }
   202 
   203     @Test
   204     public void stringArray() throws Exception {
   205         ConstantValues value = Models.bind(new ConstantValues(), c);
   206         value.getStringArray().add("Hi");
   207 
   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");
   215 
   216         try {
   217             value.getStringArray().add("Hello");
   218             fail("Changing value shouldn't succeed!");
   219         } catch (UnsupportedOperationException ex) {
   220             // OK
   221         }
   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");
   225     }
   226 
   227 }