json/src/test/java/org/netbeans/html/json/impl/PlainEnumTest.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Thu, 30 Jan 2014 11:36:32 +0100
branchunion
changeset 506 4b66c5141f84
parent 505 501413c8638c
permissions -rw-r--r--
Property changes are delivered when in-a union property changes
jaroslav@490
     1
/**
jaroslav@490
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@490
     3
 *
jaroslav@490
     4
 * Copyright 2013-2013 Oracle and/or its affiliates. All rights reserved.
jaroslav@490
     5
 *
jaroslav@490
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@490
     7
 * Other names may be trademarks of their respective owners.
jaroslav@490
     8
 *
jaroslav@490
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@490
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@490
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@490
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@490
    13
 * License. You can obtain a copy of the License at
jaroslav@490
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@490
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@490
    16
 * specific language governing permissions and limitations under the
jaroslav@490
    17
 * License.  When distributing the software, include this License Header
jaroslav@490
    18
 * Notice in each file and include the License file at
jaroslav@490
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@490
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@490
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@490
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@490
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@490
    24
 * your own identifying information:
jaroslav@490
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@490
    26
 *
jaroslav@490
    27
 * Contributor(s):
jaroslav@490
    28
 *
jaroslav@490
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jaroslav@490
    30
 * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
jaroslav@490
    31
 *
jaroslav@490
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@490
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@490
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@490
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@490
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@490
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@490
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@490
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@490
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@490
    41
 * made subject to such option by the copyright holder.
jaroslav@490
    42
 */
jaroslav@490
    43
package org.netbeans.html.json.impl;
jaroslav@490
    44
jaroslav@503
    45
import java.util.Map;
jaroslav@503
    46
import net.java.html.BrwsrCtx;
jaroslav@503
    47
import net.java.html.json.MapModelTest;
jaroslav@490
    48
import net.java.html.json.Model;
jaroslav@494
    49
import net.java.html.json.Models;
jaroslav@505
    50
import net.java.html.json.OnPropertyChange;
jaroslav@490
    51
import net.java.html.json.Property;
jaroslav@503
    52
import org.apidesign.html.context.spi.Contexts;
jaroslav@503
    53
import org.apidesign.html.json.spi.Technology;
jaroslav@490
    54
import static org.testng.Assert.*;
jaroslav@490
    55
import org.testng.annotations.Test;
jaroslav@490
    56
jaroslav@490
    57
/**
jaroslav@490
    58
 *
jaroslav@490
    59
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@490
    60
 */
jaroslav@490
    61
public class PlainEnumTest {
jaroslav@490
    62
    @Test public void unionA() {
jaroslav@498
    63
        Union on = new Union(new Union.A(9), 11, 1.1);
jaroslav@490
    64
        assertEquals(on.getX(), 11);
jaroslav@490
    65
        assertEquals(on.getY(), 1.1);
jaroslav@490
    66
        
jaroslav@490
    67
        assertEquals(on.getAbc(), Abc.A);
jaroslav@490
    68
        assertNotNull(on.getA());
jaroslav@506
    69
        assertEquals(on.getA().getUa().size(), 1);
jaroslav@506
    70
        assertEquals(on.getA().getUa().get(0), Integer.valueOf(9));
jaroslav@490
    71
        assertNull(on.getB());
jaroslav@490
    72
    }
jaroslav@490
    73
jaroslav@490
    74
    @Test public void unionB() {
jaroslav@498
    75
        Union on = new Union(new Union.B("9.9"), 11, 1.1);
jaroslav@490
    76
        assertEquals(on.getX(), 11);
jaroslav@490
    77
        assertEquals(on.getY(), 1.1);
jaroslav@490
    78
        
jaroslav@491
    79
        assertEquals(on.getAbc(), Abc.B);
jaroslav@490
    80
        assertNull(on.getA());
jaroslav@490
    81
        assertNotNull(on.getB());
jaroslav@490
    82
        assertEquals(on.getB().getUb(), "9.9");
jaroslav@490
    83
    }
jaroslav@490
    84
jaroslav@491
    85
    @Test public void cloneUnionB() {
jaroslav@498
    86
        Union old = new Union(new Union.B("9.9"), 11, 1.1);
jaroslav@493
    87
        Union on = old.clone();
jaroslav@493
    88
        assertNotSame(old.getB(), on.getB());
jaroslav@493
    89
        
jaroslav@491
    90
        assertEquals(on.getX(), 11);
jaroslav@491
    91
        assertEquals(on.getY(), 1.1);
jaroslav@491
    92
        
jaroslav@491
    93
        assertEquals(on.getAbc(), Abc.B);
jaroslav@491
    94
        assertNull(on.getA());
jaroslav@491
    95
        assertNotNull(on.getB());
jaroslav@491
    96
        assertEquals(on.getB().getUb(), "9.9");
jaroslav@491
    97
    }
jaroslav@490
    98
jaroslav@490
    99
    @Model(className = "Union", properties = {
jaroslav@490
   100
        @Property(name = "x", type = int.class),
jaroslav@490
   101
        @Property(name = "y", type = double.class)
jaroslav@490
   102
    })
jaroslav@490
   103
    enum Abc {
jaroslav@490
   104
        @Model(className = "A", properties = { 
jaroslav@506
   105
            @Property(name = "ua", type = int.class, array = true)
jaroslav@490
   106
        })
jaroslav@490
   107
        A, 
jaroslav@490
   108
        @Model(className = "B", properties = { 
jaroslav@490
   109
            @Property(name = "ub", type = String.class)
jaroslav@490
   110
        })
jaroslav@505
   111
        B;
jaroslav@505
   112
        
jaroslav@505
   113
        @OnPropertyChange({ "ua", "ub" })
jaroslav@505
   114
        static void onChange(Union u) {
jaroslav@505
   115
            u.setX(u.getX() + 1);
jaroslav@505
   116
        }
jaroslav@490
   117
    }
jaroslav@494
   118
    
jaroslav@494
   119
    @Model(className = "EnumAndModel", properties = {
jaroslav@494
   120
        @Property(name = "enm", type = Abc.class),
jaroslav@494
   121
        @Property(name = "mdl", type = Union.class),
jaroslav@494
   122
    })
jaroslav@494
   123
    static class EnumAndModelModel {
jaroslav@494
   124
    }
jaroslav@494
   125
    
jaroslav@494
   126
    @Test public void enumIsTreatedAsEnum() {
jaroslav@494
   127
        EnumAndModel em = new EnumAndModel();
jaroslav@494
   128
        assertNull(em.getEnm(), "No enum set, yet");
jaroslav@494
   129
        assertNotNull(em.getMdl(), "Model is not null");
jaroslav@494
   130
    }
jaroslav@494
   131
    
jaroslav@494
   132
    @Test public void serializeAsASingleObject() {
jaroslav@498
   133
        Union on = new Union(new Union.B("9.9"), 11, 1.1);
jaroslav@494
   134
        String json = on.toString();
jaroslav@494
   135
        assertNotEquals(json.indexOf("\"9.9\""), -1, "The string 9.9 should be in the output");
jaroslav@495
   136
        assertNotEquals(json.indexOf("Abc"), -1, "The property 'Abc' should be in");
jaroslav@495
   137
        assertNotEquals(json.indexOf("\"Abc\""), -1, "The string 'Abc' should be assigned");
jaroslav@495
   138
        
jaroslav@495
   139
        int first = json.indexOf('{');
jaroslav@495
   140
        assertEquals(first, 0, "Begins with {");
jaroslav@495
   141
        
jaroslav@495
   142
        int next = json.indexOf('{', 1);
jaroslav@495
   143
        assertEquals(next, -1, "No other {");
jaroslav@494
   144
    }
jaroslav@503
   145
    
jaroslav@503
   146
    @Test public void unionOfProperties() {
jaroslav@503
   147
        MapModelTest.MapTechnology mt = new MapModelTest.MapTechnology();
jaroslav@503
   148
        BrwsrCtx ctx = Contexts.newBuilder().register(Technology.class, mt, 1).build();
jaroslav@503
   149
jaroslav@503
   150
        Union on = new Union(new Union.B("9.9"), 11, 1.1);
jaroslav@503
   151
        Union u = Models.bind(on, ctx);
jaroslav@503
   152
        
jaroslav@503
   153
        Map<?,?> map = (Map<?,?>)Models.toRaw(u);
jaroslav@503
   154
        assertNotNull(map.get("x"), "Four properties: " + map);
jaroslav@503
   155
        assertNotNull(map.get("y"), "Four properties: " + map);
jaroslav@505
   156
        MapModelTest.One ub;
jaroslav@503
   157
        assertNotNull(map.get("ua"), "Four properties: " + map);
jaroslav@505
   158
        assertNotNull(ub = (MapModelTest.One) map.get("ub"), "Four properties: " + map);
jaroslav@505
   159
        
jaroslav@505
   160
        assertEquals(u.getX(), 11, "Eleven");
jaroslav@505
   161
        
jaroslav@505
   162
        assertEquals(ub.getChanges(), 0, "No changes yet");
jaroslav@505
   163
        u.getB().setUb("10");
jaroslav@505
   164
        assertEquals(u.getX(), 12, "Change increases X");
jaroslav@505
   165
        assertEquals(ub.getChanges(), 1, "One change");
jaroslav@503
   166
    }
jaroslav@506
   167
    
jaroslav@506
   168
    @Test public void changeInUnionArray() {
jaroslav@506
   169
        MapModelTest.MapTechnology mt = new MapModelTest.MapTechnology();
jaroslav@506
   170
        BrwsrCtx ctx = Contexts.newBuilder().register(Technology.class, mt, 1).build();
jaroslav@506
   171
jaroslav@506
   172
        Union on = new Union(new Union.A(10, 20, 30), 0, 1.1);
jaroslav@506
   173
        Union u = Models.bind(on, ctx);
jaroslav@506
   174
jaroslav@506
   175
        Map<?, ?> map = (Map<?, ?>) Models.toRaw(u);
jaroslav@506
   176
        MapModelTest.One ua;
jaroslav@506
   177
        assertNotNull(ua = (MapModelTest.One) map.get("ua"), "Array prop found: " + map);
jaroslav@506
   178
        
jaroslav@506
   179
        assertEquals(u.getX(), 0, "Zero");
jaroslav@506
   180
        assertEquals(ua.getChanges(), 0, "No changes yet");
jaroslav@506
   181
        u.getA().getUa().add(40);
jaroslav@506
   182
        assertEquals(u.getX(), 1, "Incremented once");
jaroslav@506
   183
        assertEquals(ua.getChanges(), 1, "One change");
jaroslav@506
   184
    }
jaroslav@490
   185
}