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
     1 /**
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2013-2013 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-2013 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 org.netbeans.html.json.impl;
    44 
    45 import java.util.Map;
    46 import net.java.html.BrwsrCtx;
    47 import net.java.html.json.MapModelTest;
    48 import net.java.html.json.Model;
    49 import net.java.html.json.Models;
    50 import net.java.html.json.OnPropertyChange;
    51 import net.java.html.json.Property;
    52 import org.apidesign.html.context.spi.Contexts;
    53 import org.apidesign.html.json.spi.Technology;
    54 import static org.testng.Assert.*;
    55 import org.testng.annotations.Test;
    56 
    57 /**
    58  *
    59  * @author Jaroslav Tulach <jtulach@netbeans.org>
    60  */
    61 public class PlainEnumTest {
    62     @Test public void unionA() {
    63         Union on = new Union(new Union.A(9), 11, 1.1);
    64         assertEquals(on.getX(), 11);
    65         assertEquals(on.getY(), 1.1);
    66         
    67         assertEquals(on.getAbc(), Abc.A);
    68         assertNotNull(on.getA());
    69         assertEquals(on.getA().getUa().size(), 1);
    70         assertEquals(on.getA().getUa().get(0), Integer.valueOf(9));
    71         assertNull(on.getB());
    72     }
    73 
    74     @Test public void unionB() {
    75         Union on = new Union(new Union.B("9.9"), 11, 1.1);
    76         assertEquals(on.getX(), 11);
    77         assertEquals(on.getY(), 1.1);
    78         
    79         assertEquals(on.getAbc(), Abc.B);
    80         assertNull(on.getA());
    81         assertNotNull(on.getB());
    82         assertEquals(on.getB().getUb(), "9.9");
    83     }
    84 
    85     @Test public void cloneUnionB() {
    86         Union old = new Union(new Union.B("9.9"), 11, 1.1);
    87         Union on = old.clone();
    88         assertNotSame(old.getB(), on.getB());
    89         
    90         assertEquals(on.getX(), 11);
    91         assertEquals(on.getY(), 1.1);
    92         
    93         assertEquals(on.getAbc(), Abc.B);
    94         assertNull(on.getA());
    95         assertNotNull(on.getB());
    96         assertEquals(on.getB().getUb(), "9.9");
    97     }
    98 
    99     @Model(className = "Union", properties = {
   100         @Property(name = "x", type = int.class),
   101         @Property(name = "y", type = double.class)
   102     })
   103     enum Abc {
   104         @Model(className = "A", properties = { 
   105             @Property(name = "ua", type = int.class, array = true)
   106         })
   107         A, 
   108         @Model(className = "B", properties = { 
   109             @Property(name = "ub", type = String.class)
   110         })
   111         B;
   112         
   113         @OnPropertyChange({ "ua", "ub" })
   114         static void onChange(Union u) {
   115             u.setX(u.getX() + 1);
   116         }
   117     }
   118     
   119     @Model(className = "EnumAndModel", properties = {
   120         @Property(name = "enm", type = Abc.class),
   121         @Property(name = "mdl", type = Union.class),
   122     })
   123     static class EnumAndModelModel {
   124     }
   125     
   126     @Test public void enumIsTreatedAsEnum() {
   127         EnumAndModel em = new EnumAndModel();
   128         assertNull(em.getEnm(), "No enum set, yet");
   129         assertNotNull(em.getMdl(), "Model is not null");
   130     }
   131     
   132     @Test public void serializeAsASingleObject() {
   133         Union on = new Union(new Union.B("9.9"), 11, 1.1);
   134         String json = on.toString();
   135         assertNotEquals(json.indexOf("\"9.9\""), -1, "The string 9.9 should be in the output");
   136         assertNotEquals(json.indexOf("Abc"), -1, "The property 'Abc' should be in");
   137         assertNotEquals(json.indexOf("\"Abc\""), -1, "The string 'Abc' should be assigned");
   138         
   139         int first = json.indexOf('{');
   140         assertEquals(first, 0, "Begins with {");
   141         
   142         int next = json.indexOf('{', 1);
   143         assertEquals(next, -1, "No other {");
   144     }
   145     
   146     @Test public void unionOfProperties() {
   147         MapModelTest.MapTechnology mt = new MapModelTest.MapTechnology();
   148         BrwsrCtx ctx = Contexts.newBuilder().register(Technology.class, mt, 1).build();
   149 
   150         Union on = new Union(new Union.B("9.9"), 11, 1.1);
   151         Union u = Models.bind(on, ctx);
   152         
   153         Map<?,?> map = (Map<?,?>)Models.toRaw(u);
   154         assertNotNull(map.get("x"), "Four properties: " + map);
   155         assertNotNull(map.get("y"), "Four properties: " + map);
   156         MapModelTest.One ub;
   157         assertNotNull(map.get("ua"), "Four properties: " + map);
   158         assertNotNull(ub = (MapModelTest.One) map.get("ub"), "Four properties: " + map);
   159         
   160         assertEquals(u.getX(), 11, "Eleven");
   161         
   162         assertEquals(ub.getChanges(), 0, "No changes yet");
   163         u.getB().setUb("10");
   164         assertEquals(u.getX(), 12, "Change increases X");
   165         assertEquals(ub.getChanges(), 1, "One change");
   166     }
   167     
   168     @Test public void changeInUnionArray() {
   169         MapModelTest.MapTechnology mt = new MapModelTest.MapTechnology();
   170         BrwsrCtx ctx = Contexts.newBuilder().register(Technology.class, mt, 1).build();
   171 
   172         Union on = new Union(new Union.A(10, 20, 30), 0, 1.1);
   173         Union u = Models.bind(on, ctx);
   174 
   175         Map<?, ?> map = (Map<?, ?>) Models.toRaw(u);
   176         MapModelTest.One ua;
   177         assertNotNull(ua = (MapModelTest.One) map.get("ua"), "Array prop found: " + map);
   178         
   179         assertEquals(u.getX(), 0, "Zero");
   180         assertEquals(ua.getChanges(), 0, "No changes yet");
   181         u.getA().getUa().add(40);
   182         assertEquals(u.getX(), 1, "Incremented once");
   183         assertEquals(ua.getChanges(), 1, "One change");
   184     }
   185 }