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
jtulach@1054
     1
/**
jtulach@1054
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jtulach@1054
     3
 *
jtulach@1054
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jtulach@1054
     5
 *
jtulach@1054
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jtulach@1054
     7
 * Other names may be trademarks of their respective owners.
jtulach@1054
     8
 *
jtulach@1054
     9
 * The contents of this file are subject to the terms of either the GNU
jtulach@1054
    10
 * General Public License Version 2 only ("GPL") or the Common
jtulach@1054
    11
 * Development and Distribution License("CDDL") (collectively, the
jtulach@1054
    12
 * "License"). You may not use this file except in compliance with the
jtulach@1054
    13
 * License. You can obtain a copy of the License at
jtulach@1054
    14
 * http://www.netbeans.org/cddl-gplv2.html
jtulach@1054
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jtulach@1054
    16
 * specific language governing permissions and limitations under the
jtulach@1054
    17
 * License.  When distributing the software, include this License Header
jtulach@1054
    18
 * Notice in each file and include the License file at
jtulach@1054
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jtulach@1054
    20
 * particular file as subject to the "Classpath" exception as provided
jtulach@1054
    21
 * by Oracle in the GPL Version 2 section of the License file that
jtulach@1054
    22
 * accompanied this code. If applicable, add the following below the
jtulach@1054
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jtulach@1054
    24
 * your own identifying information:
jtulach@1054
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jtulach@1054
    26
 *
jtulach@1054
    27
 * Contributor(s):
jtulach@1054
    28
 *
jtulach@1054
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jtulach@1054
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jtulach@1054
    31
 *
jtulach@1054
    32
 * If you wish your version of this file to be governed by only the CDDL
jtulach@1054
    33
 * or only the GPL Version 2, indicate your decision by adding
jtulach@1054
    34
 * "[Contributor] elects to include this software in this distribution
jtulach@1054
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jtulach@1054
    36
 * single choice of license, a recipient has the option to distribute
jtulach@1054
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jtulach@1054
    38
 * to extend the choice of license to its licensees as provided above.
jtulach@1054
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jtulach@1054
    40
 * Version 2 license, then the option applies only if the new code is
jtulach@1054
    41
 * made subject to such option by the copyright holder.
jtulach@1054
    42
 */
jtulach@1054
    43
package net.java.html.json;
jtulach@1054
    44
jtulach@1054
    45
import java.util.Map;
jtulach@1054
    46
import net.java.html.BrwsrCtx;
jtulach@1054
    47
import org.netbeans.html.context.spi.Contexts;
jtulach@1054
    48
import org.netbeans.html.json.spi.Technology;
jtulach@1054
    49
import org.netbeans.html.json.spi.Transfer;
jtulach@1054
    50
import static org.testng.Assert.assertEquals;
jtulach@1054
    51
import static org.testng.Assert.assertFalse;
jtulach@1054
    52
import static org.testng.Assert.assertNotNull;
jtulach@1054
    53
import static org.testng.Assert.fail;
jtulach@1054
    54
import org.testng.annotations.BeforeMethod;
jtulach@1054
    55
import org.testng.annotations.Test;
jtulach@1054
    56
jtulach@1054
    57
@Model(className = "ConstantValues", properties = {
jtulach@1054
    58
    @Property(name = "byteNumber", type = byte.class, mutable = false),
jtulach@1054
    59
    @Property(name = "shortNumber", type = short.class, mutable = false),
jtulach@1054
    60
    @Property(name = "intNumber", type = int.class, mutable = false),
jtulach@1054
    61
    @Property(name = "longNumber", type = long.class, mutable = false),
jtulach@1054
    62
    @Property(name = "floatNumber", type = float.class, mutable = false),
jtulach@1054
    63
    @Property(name = "doubleNumber", type = double.class, mutable = false),
jtulach@1054
    64
    @Property(name = "stringValue", type = String.class, mutable = false),
jtulach@1054
    65
    @Property(name = "byteArray", type = byte.class, mutable = false, array = true),
jtulach@1054
    66
    @Property(name = "shortArray", type = short.class, mutable = false, array = true),
jtulach@1054
    67
    @Property(name = "intArray", type = int.class, mutable = false, array = true),
jtulach@1054
    68
    @Property(name = "longArray", type = long.class, mutable = false, array = true),
jtulach@1054
    69
    @Property(name = "floatArray", type = float.class, mutable = false, array = true),
jtulach@1054
    70
    @Property(name = "doubleArray", type = double.class, mutable = false, array = true),
jtulach@1054
    71
    @Property(name = "stringArray", type = String.class, mutable = false, array = true),
jtulach@1054
    72
})
jtulach@1054
    73
public class MapModelNotMutableTest {
jtulach@1054
    74
    private BrwsrCtx c;
jtulach@1054
    75
jtulach@1054
    76
    @BeforeMethod
jtulach@1054
    77
    public void initTechnology() {
jtulach@1054
    78
        MapModelTest.MapTechnology t = new MapModelTest.MapTechnology();
jtulach@1054
    79
        c = Contexts.newBuilder().register(Technology.class, t, 1).
jtulach@1054
    80
            register(Transfer.class, t, 1).build();
jtulach@1054
    81
    }
jtulach@1054
    82
jtulach@1054
    83
    @Test
jtulach@1054
    84
    public void byteConstant() throws Exception {
jtulach@1054
    85
        ConstantValues value = Models.bind(new ConstantValues(), c);
jtulach@1054
    86
        value.setByteNumber((byte)13);
jtulach@1054
    87
jtulach@1054
    88
        Map m = (Map) Models.toRaw(value);
jtulach@1054
    89
        Object v = m.get("byteNumber");
jtulach@1054
    90
        assertNotNull(v, "Value should be in the map");
jtulach@1054
    91
        assertEquals(v.getClass(), MapModelTest.One.class, "It is instance of One");
jtulach@1054
    92
        MapModelTest.One o = (MapModelTest.One) v;
jtulach@1054
    93
        assertEquals(o.changes, 0, "No change so far the only one change happened before we connected");
jtulach@1054
    94
        assertEquals((byte)13, o.get());
jtulach@1054
    95
jtulach@1054
    96
        try {
jtulach@1054
    97
            value.setByteNumber((byte)15);
jtulach@1054
    98
            fail("Changing value shouldn't succeed!");
jtulach@1054
    99
        } catch (IllegalStateException ex) {
jtulach@1054
   100
            // OK
jtulach@1054
   101
        }
jtulach@1054
   102
        assertEquals(o.get(), (byte)13, "Old value should still be in the map");
jtulach@1054
   103
        assertEquals(o.changes, 0, "No change");
jtulach@1054
   104
        assertFalse(o.pb.isReadOnly(), "Mutable property");
jtulach@1054
   105
    }
jtulach@1054
   106
jtulach@1054
   107
    @Test
jtulach@1054
   108
    public void shortConstant() throws Exception {
jtulach@1054
   109
        ConstantValues value = Models.bind(new ConstantValues(), c);
jtulach@1054
   110
        value.setShortNumber((short)13);
jtulach@1054
   111
jtulach@1054
   112
        Map m = (Map) Models.toRaw(value);
jtulach@1054
   113
        Object v = m.get("shortNumber");
jtulach@1054
   114
        assertNotNull(v, "Value should be in the map");
jtulach@1054
   115
        assertEquals(v.getClass(), MapModelTest.One.class, "It is instance of One");
jtulach@1054
   116
        MapModelTest.One o = (MapModelTest.One) v;
jtulach@1054
   117
        assertEquals(o.changes, 0, "No change so far the only one change happened before we connected");
jtulach@1054
   118
        assertEquals((short)13, o.get());
jtulach@1054
   119
jtulach@1054
   120
        try {
jtulach@1054
   121
            value.setShortNumber((short)15);
jtulach@1054
   122
            fail("Changing value shouldn't succeed!");
jtulach@1054
   123
        } catch (IllegalStateException ex) {
jtulach@1054
   124
            // OK
jtulach@1054
   125
        }
jtulach@1054
   126
        assertEquals(o.get(), (short)13, "Old value should still be in the map");
jtulach@1054
   127
        assertEquals(o.changes, 0, "No change");
jtulach@1054
   128
        assertFalse(o.pb.isReadOnly(), "Mutable property");
jtulach@1054
   129
    }
jtulach@1054
   130
jtulach@1054
   131
    @Test
jtulach@1054
   132
    public void intConstant() throws Exception {
jtulach@1054
   133
        ConstantValues value = Models.bind(new ConstantValues(), c);
jtulach@1054
   134
        value.setIntNumber(13);
jtulach@1054
   135
jtulach@1054
   136
        Map m = (Map) Models.toRaw(value);
jtulach@1054
   137
        Object v = m.get("intNumber");
jtulach@1054
   138
        assertNotNull(v, "Value should be in the map");
jtulach@1054
   139
        assertEquals(v.getClass(), MapModelTest.One.class, "It is instance of One");
jtulach@1054
   140
        MapModelTest.One o = (MapModelTest.One) v;
jtulach@1054
   141
        assertEquals(o.changes, 0, "No change so far the only one change happened before we connected");
jtulach@1054
   142
        assertEquals(13, o.get());
jtulach@1054
   143
jtulach@1054
   144
        try {
jtulach@1054
   145
            value.setIntNumber(15);
jtulach@1054
   146
            fail("Changing value shouldn't succeed!");
jtulach@1054
   147
        } catch (IllegalStateException ex) {
jtulach@1054
   148
            // OK
jtulach@1054
   149
        }
jtulach@1054
   150
        assertEquals(o.get(), 13, "Old value should still be in the map");
jtulach@1054
   151
        assertEquals(o.changes, 0, "No change");
jtulach@1054
   152
        assertFalse(o.pb.isReadOnly(), "Mutable property");
jtulach@1054
   153
    }
jtulach@1054
   154
jtulach@1054
   155
    @Test
jtulach@1054
   156
    public void doubleConstant() throws Exception {
jtulach@1054
   157
        ConstantValues value = Models.bind(new ConstantValues(), c);
jtulach@1054
   158
        value.setDoubleNumber(13);
jtulach@1054
   159
jtulach@1054
   160
        Map m = (Map) Models.toRaw(value);
jtulach@1054
   161
        Object v = m.get("doubleNumber");
jtulach@1054
   162
        assertNotNull(v, "Value should be in the map");
jtulach@1054
   163
        assertEquals(v.getClass(), MapModelTest.One.class, "It is instance of One");
jtulach@1054
   164
        MapModelTest.One o = (MapModelTest.One) v;
jtulach@1054
   165
        assertEquals(o.changes, 0, "No change so far the only one change happened before we connected");
jtulach@1054
   166
        assertEquals(13.0, o.get());
jtulach@1054
   167
jtulach@1054
   168
        try {
jtulach@1054
   169
            value.setDoubleNumber(15);
jtulach@1054
   170
            fail("Changing value shouldn't succeed!");
jtulach@1054
   171
        } catch (IllegalStateException ex) {
jtulach@1054
   172
            // OK
jtulach@1054
   173
        }
jtulach@1054
   174
        assertEquals(o.get(), 13.0, "Old value should still be in the map");
jtulach@1054
   175
        assertEquals(o.changes, 0, "No change");
jtulach@1054
   176
        assertFalse(o.pb.isReadOnly(), "Mutable property");
jtulach@1054
   177
    }
jtulach@1054
   178
jtulach@1054
   179
    @Test
jtulach@1054
   180
    public void stringConstant() throws Exception {
jtulach@1054
   181
        ConstantValues value = Models.bind(new ConstantValues(), c);
jtulach@1054
   182
        value.setStringValue("Hi");
jtulach@1054
   183
jtulach@1054
   184
        Map m = (Map) Models.toRaw(value);
jtulach@1054
   185
        Object v = m.get("stringValue");
jtulach@1054
   186
        assertNotNull(v, "Value should be in the map");
jtulach@1054
   187
        assertEquals(v.getClass(), MapModelTest.One.class, "It is instance of One");
jtulach@1054
   188
        MapModelTest.One o = (MapModelTest.One) v;
jtulach@1054
   189
        assertEquals(o.changes, 0, "No change so far the only one change happened before we connected");
jtulach@1054
   190
        assertEquals("Hi", o.get());
jtulach@1054
   191
jtulach@1054
   192
        try {
jtulach@1054
   193
            value.setStringValue("Hello");
jtulach@1054
   194
            fail("Changing value shouldn't succeed!");
jtulach@1054
   195
        } catch (IllegalStateException ex) {
jtulach@1054
   196
            // OK
jtulach@1054
   197
        }
jtulach@1054
   198
        assertEquals(o.get(), "Hi", "Old value should still be in the map");
jtulach@1054
   199
        assertEquals(o.changes, 0, "No change");
jtulach@1054
   200
        assertFalse(o.pb.isReadOnly(), "Mutable property");
jtulach@1054
   201
    }
jtulach@1054
   202
jtulach@1054
   203
    @Test
jtulach@1054
   204
    public void stringArray() throws Exception {
jtulach@1054
   205
        ConstantValues value = Models.bind(new ConstantValues(), c);
jtulach@1054
   206
        value.getStringArray().add("Hi");
jtulach@1054
   207
jtulach@1054
   208
        Map m = (Map) Models.toRaw(value);
jtulach@1054
   209
        Object v = m.get("stringArray");
jtulach@1054
   210
        assertNotNull(v, "Value should be in the map");
jtulach@1054
   211
        assertEquals(v.getClass(), MapModelTest.One.class, "It is instance of One");
jtulach@1054
   212
        MapModelTest.One o = (MapModelTest.One) v;
jtulach@1054
   213
        assertEquals(o.changes, 0, "No change so far the only one change happened before we connected");
jtulach@1054
   214
        assertEquals(o.get(), new String[] { "Hi" }, "One element");
jtulach@1054
   215
jtulach@1054
   216
        try {
jtulach@1054
   217
            value.getStringArray().add("Hello");
jtulach@1054
   218
            fail("Changing value shouldn't succeed!");
jtulach@1054
   219
        } catch (UnsupportedOperationException ex) {
jtulach@1054
   220
            // OK
jtulach@1054
   221
        }
jtulach@1054
   222
        assertEquals(o.get(), new String[] { "Hi" }, "Old value should still be in the map");
jtulach@1054
   223
        assertEquals(o.changes, 0, "No change");
jtulach@1054
   224
        assertFalse(o.pb.isReadOnly(), "Mutable property");
jtulach@1054
   225
    }
jtulach@1054
   226
jtulach@1054
   227
}