json/src/test/java/org/netbeans/html/json/impl/JSONListTest.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Fri, 07 Feb 2014 07:44:34 +0100
changeset 551 7ca2253fa86d
parent 479 72fca24897e7
child 790 30f20d9c0986
permissions -rw-r--r--
Updating copyright headers to mention current year
jaroslav@15
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@15
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jaroslav@15
     5
 *
jaroslav@358
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@358
     7
 * Other names may be trademarks of their respective owners.
jaroslav@15
     8
 *
jaroslav@358
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@358
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@358
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@358
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@358
    13
 * License. You can obtain a copy of the License at
jaroslav@358
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@358
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@358
    16
 * specific language governing permissions and limitations under the
jaroslav@358
    17
 * License.  When distributing the software, include this License Header
jaroslav@358
    18
 * Notice in each file and include the License file at
jaroslav@358
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@358
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@358
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@358
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@358
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@358
    24
 * your own identifying information:
jaroslav@358
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@358
    26
 *
jaroslav@358
    27
 * Contributor(s):
jaroslav@358
    28
 *
jaroslav@358
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jaroslav@551
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jaroslav@358
    31
 *
jaroslav@358
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@358
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@358
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@358
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@358
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@358
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@358
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@358
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@358
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@358
    41
 * made subject to such option by the copyright holder.
jaroslav@15
    42
 */
jaroslav@362
    43
package org.netbeans.html.json.impl;
jaroslav@15
    44
jaroslav@17
    45
import java.util.HashMap;
jaroslav@17
    46
import java.util.Map;
jaroslav@110
    47
import net.java.html.BrwsrCtx;
jaroslav@479
    48
import net.java.html.json.Model;
jaroslav@111
    49
import net.java.html.json.Models;
jaroslav@15
    50
import net.java.html.json.People;
jaroslav@15
    51
import net.java.html.json.Person;
jaroslav@479
    52
import net.java.html.json.Property;
jaroslav@15
    53
import net.java.html.json.Sex;
jaroslav@110
    54
import org.apidesign.html.context.spi.Contexts;
jaroslav@15
    55
import org.apidesign.html.json.spi.FunctionBinding;
jaroslav@15
    56
import org.apidesign.html.json.spi.PropertyBinding;
jaroslav@15
    57
import org.apidesign.html.json.spi.Technology;
jaroslav@15
    58
import static org.testng.Assert.*;
jaroslav@17
    59
import org.testng.annotations.BeforeMethod;
jaroslav@15
    60
import org.testng.annotations.Test;
jaroslav@15
    61
jaroslav@15
    62
/**
jaroslav@15
    63
 *
jaroslav@15
    64
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@15
    65
 */
jaroslav@479
    66
@Model(className = "JSNLst", properties = {
jaroslav@479
    67
    @Property(name = "names", type = String.class, array = true)
jaroslav@479
    68
})
jaroslav@15
    69
public class JSONListTest implements Technology<Object> {
jaroslav@17
    70
    private boolean replaceArray;
jaroslav@100
    71
    private final Map<String,PropertyBinding> bindings = new HashMap<String,PropertyBinding>();
jaroslav@15
    72
    
jaroslav@15
    73
    public JSONListTest() {
jaroslav@15
    74
    }
jaroslav@17
    75
    
jaroslav@17
    76
    @BeforeMethod public void clear() {
jaroslav@17
    77
        replaceArray = false;
jaroslav@17
    78
    }
jaroslav@15
    79
jaroslav@15
    80
    @Test public void testConvertorOnAnObject() {
jaroslav@110
    81
        BrwsrCtx c = Contexts.newBuilder().register(Technology.class, this, 1).build();
jaroslav@15
    82
        
jaroslav@111
    83
        Person p = Models.bind(new Person(), c);
jaroslav@15
    84
        p.setFirstName("1");
jaroslav@15
    85
        p.setLastName("2");
jaroslav@15
    86
        p.setSex(Sex.MALE);
jaroslav@15
    87
jaroslav@450
    88
        Object real = Models.toRaw(p);
jaroslav@15
    89
        assertEquals(this, real, "I am the right model");
jaroslav@15
    90
    }
jaroslav@15
    91
    
jaroslav@15
    92
    @Test public void testConvertorOnAnArray() {
jaroslav@110
    93
        BrwsrCtx c = Contexts.newBuilder().register(Technology.class, this, 1).build();
jaroslav@15
    94
        
jaroslav@111
    95
        Person p = Models.bind(new Person(), c);
jaroslav@15
    96
        p.setFirstName("1");
jaroslav@15
    97
        p.setLastName("2");
jaroslav@15
    98
        p.setSex(Sex.MALE);
jaroslav@15
    99
        
jaroslav@278
   100
        People people = Models.bind(new People(p), c).applyBindings();
jaroslav@18
   101
        assertEquals(people.getInfo().toString(), "[{\"firstName\":\"1\",\"lastName\":\"2\",\"sex\":\"MALE\"}]", "Converted to real JSON");
jaroslav@17
   102
        
jaroslav@17
   103
        PropertyBinding pb = bindings.get("info");
jaroslav@17
   104
        assertNotNull(pb, "Binding for info found");
jaroslav@17
   105
        
jaroslav@17
   106
        Object real = pb.getValue();
jaroslav@16
   107
        assertTrue(real instanceof Object[], "It is an array: " + real);
jaroslav@16
   108
        Object[] arr = (Object[])real;
jaroslav@16
   109
        assertEquals(arr.length, 1, "Size is one");
jaroslav@16
   110
        assertEquals(this, arr[0], "I am the right model");
jaroslav@15
   111
    }
jaroslav@17
   112
    
jaroslav@19
   113
    @Test public void testNicknames() {
jaroslav@110
   114
        BrwsrCtx c = Contexts.newBuilder().register(Technology.class, this, 1).build();
jaroslav@19
   115
        
jaroslav@278
   116
        People people = Models.bind(new People(), c).applyBindings();
jaroslav@19
   117
        people.getNicknames().add("One");
jaroslav@19
   118
        people.getNicknames().add("Two");
jaroslav@19
   119
        
jaroslav@19
   120
        PropertyBinding pb = bindings.get("nicknames");
jaroslav@19
   121
        assertNotNull(pb, "Binding for info found");
jaroslav@19
   122
        
jaroslav@19
   123
        Object real = pb.getValue();
jaroslav@19
   124
        assertTrue(real instanceof Object[], "It is an array: " + real);
jaroslav@19
   125
        Object[] arr = (Object[])real;
jaroslav@19
   126
        assertEquals(arr.length, 2, "Length two");
jaroslav@19
   127
        assertEquals(arr[0], "One", "Text should be in the model");
jaroslav@19
   128
        assertEquals(arr[1], "Two", "2nd text in the model");
jaroslav@19
   129
    }
jaroslav@19
   130
    
jaroslav@17
   131
    @Test public void testConvertorOnAnArrayWithWrapper() {
jaroslav@17
   132
        this.replaceArray = true;
jaroslav@110
   133
        BrwsrCtx c = Contexts.newBuilder().register(Technology.class, this, 1).build();
jaroslav@17
   134
        
jaroslav@111
   135
        Person p = Models.bind(new Person(), c);
jaroslav@17
   136
        p.setFirstName("1");
jaroslav@17
   137
        p.setLastName("2");
jaroslav@17
   138
        p.setSex(Sex.MALE);
jaroslav@17
   139
        
jaroslav@278
   140
        People people = Models.bind(new People(), c).applyBindings();
jaroslav@17
   141
        people.getInfo().add(p);
jaroslav@278
   142
        
jaroslav@414
   143
        Object real = JSON.find(people.getInfo());
jaroslav@17
   144
        assertEquals(real, this, "I am the model of the array");
jaroslav@17
   145
    }
jaroslav@17
   146
jaroslav@17
   147
    @Test public void bindingsOnArray() {
jaroslav@17
   148
        this.replaceArray = true;
jaroslav@110
   149
        BrwsrCtx c = Contexts.newBuilder().register(Technology.class, this, 1).build();
jaroslav@17
   150
        
jaroslav@278
   151
        People p = Models.bind(new People(), c).applyBindings();
jaroslav@17
   152
        p.getAge().add(30);
jaroslav@17
   153
        
jaroslav@17
   154
        PropertyBinding pb = bindings.get("age");
jaroslav@17
   155
        assertNotNull(pb, "There is a binding for age list");
jaroslav@17
   156
        
jaroslav@17
   157
        assertEquals(pb.getValue(), this, "I am the model of the array");
jaroslav@17
   158
    }
jaroslav@479
   159
    
jaroslav@479
   160
    @Test public void toStringOnArrayOfStrings() {
jaroslav@479
   161
        JSNLst l = new JSNLst("Jarda", "Jirka", "Parda");
jaroslav@479
   162
        assertEquals(l.toString(), "{\"names\":[\"Jarda\",\"Jirka\",\"Parda\"]}", "Properly quoted");
jaroslav@479
   163
    }
jaroslav@15
   164
jaroslav@15
   165
    @Override
jaroslav@15
   166
    public Object wrapModel(Object model) {
jaroslav@15
   167
        return this;
jaroslav@15
   168
    }
jaroslav@15
   169
jaroslav@15
   170
    @Override
jaroslav@15
   171
    public void bind(PropertyBinding b, Object model, Object data) {
jaroslav@17
   172
        bindings.put(b.getPropertyName(), b);
jaroslav@15
   173
    }
jaroslav@15
   174
jaroslav@15
   175
    @Override
jaroslav@15
   176
    public void valueHasMutated(Object data, String propertyName) {
jaroslav@15
   177
    }
jaroslav@15
   178
jaroslav@15
   179
    @Override
jaroslav@15
   180
    public void expose(FunctionBinding fb, Object model, Object d) {
jaroslav@15
   181
    }
jaroslav@15
   182
jaroslav@15
   183
    @Override
jaroslav@15
   184
    public void applyBindings(Object data) {
jaroslav@15
   185
    }
jaroslav@17
   186
jaroslav@17
   187
    @Override
jaroslav@17
   188
    public Object wrapArray(Object[] arr) {
jaroslav@17
   189
        return replaceArray ? this : arr;
jaroslav@17
   190
    }
jaroslav@22
   191
jaroslav@38
   192
    @Override
jaroslav@38
   193
    public <M> M toModel(Class<M> modelClass, Object data) {
jaroslav@38
   194
        return modelClass.cast(data);
jaroslav@38
   195
    }
jaroslav@38
   196
jaroslav@240
   197
    @Override
jaroslav@240
   198
    public void runSafe(Runnable r) {
jaroslav@240
   199
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
jaroslav@240
   200
    }
jaroslav@240
   201
jaroslav@15
   202
}