json/src/test/java/org/netbeans/html/json/impl/JSONListTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 16 Dec 2013 16:59:43 +0100
branchnetbeans
changeset 362 92fb71afdc0e
parent 358 json/src/test/java/org/apidesign/html/json/impl/JSONListTest.java@80702021b851
child 365 5c93ad8c7a15
permissions -rw-r--r--
Moving implementation classes into org.netbeans.html namespace
jaroslav@15
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@15
     3
 *
jaroslav@358
     4
 * Copyright 1997-2010 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@358
    30
 * Software is Oracle. Portions Copyright 2013-2013 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@111
    48
import net.java.html.json.Models;
jaroslav@15
    49
import net.java.html.json.People;
jaroslav@15
    50
import net.java.html.json.Person;
jaroslav@15
    51
import net.java.html.json.Sex;
jaroslav@110
    52
import org.apidesign.html.context.spi.Contexts;
jaroslav@15
    53
import org.apidesign.html.json.spi.FunctionBinding;
jaroslav@15
    54
import org.apidesign.html.json.spi.PropertyBinding;
jaroslav@15
    55
import org.apidesign.html.json.spi.Technology;
jaroslav@15
    56
import static org.testng.Assert.*;
jaroslav@17
    57
import org.testng.annotations.BeforeMethod;
jaroslav@15
    58
import org.testng.annotations.Test;
jaroslav@15
    59
jaroslav@15
    60
/**
jaroslav@15
    61
 *
jaroslav@15
    62
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@15
    63
 */
jaroslav@15
    64
public class JSONListTest implements Technology<Object> {
jaroslav@17
    65
    private boolean replaceArray;
jaroslav@100
    66
    private final Map<String,PropertyBinding> bindings = new HashMap<String,PropertyBinding>();
jaroslav@15
    67
    
jaroslav@15
    68
    public JSONListTest() {
jaroslav@15
    69
    }
jaroslav@17
    70
    
jaroslav@17
    71
    @BeforeMethod public void clear() {
jaroslav@17
    72
        replaceArray = false;
jaroslav@17
    73
    }
jaroslav@15
    74
jaroslav@15
    75
    @Test public void testConvertorOnAnObject() {
jaroslav@110
    76
        BrwsrCtx c = Contexts.newBuilder().register(Technology.class, this, 1).build();
jaroslav@15
    77
        
jaroslav@111
    78
        Person p = Models.bind(new Person(), c);
jaroslav@15
    79
        p.setFirstName("1");
jaroslav@15
    80
        p.setLastName("2");
jaroslav@15
    81
        p.setSex(Sex.MALE);
jaroslav@15
    82
jaroslav@15
    83
        Object real = WrapperObject.find(p);
jaroslav@15
    84
        assertEquals(this, real, "I am the right model");
jaroslav@15
    85
    }
jaroslav@15
    86
    
jaroslav@15
    87
    @Test public void testConvertorOnAnArray() {
jaroslav@110
    88
        BrwsrCtx c = Contexts.newBuilder().register(Technology.class, this, 1).build();
jaroslav@15
    89
        
jaroslav@111
    90
        Person p = Models.bind(new Person(), c);
jaroslav@15
    91
        p.setFirstName("1");
jaroslav@15
    92
        p.setLastName("2");
jaroslav@15
    93
        p.setSex(Sex.MALE);
jaroslav@15
    94
        
jaroslav@278
    95
        People people = Models.bind(new People(p), c).applyBindings();
jaroslav@18
    96
        assertEquals(people.getInfo().toString(), "[{\"firstName\":\"1\",\"lastName\":\"2\",\"sex\":\"MALE\"}]", "Converted to real JSON");
jaroslav@17
    97
        
jaroslav@17
    98
        PropertyBinding pb = bindings.get("info");
jaroslav@17
    99
        assertNotNull(pb, "Binding for info found");
jaroslav@17
   100
        
jaroslav@17
   101
        Object real = pb.getValue();
jaroslav@16
   102
        assertTrue(real instanceof Object[], "It is an array: " + real);
jaroslav@16
   103
        Object[] arr = (Object[])real;
jaroslav@16
   104
        assertEquals(arr.length, 1, "Size is one");
jaroslav@16
   105
        assertEquals(this, arr[0], "I am the right model");
jaroslav@15
   106
    }
jaroslav@17
   107
    
jaroslav@19
   108
    @Test public void testNicknames() {
jaroslav@110
   109
        BrwsrCtx c = Contexts.newBuilder().register(Technology.class, this, 1).build();
jaroslav@19
   110
        
jaroslav@278
   111
        People people = Models.bind(new People(), c).applyBindings();
jaroslav@19
   112
        people.getNicknames().add("One");
jaroslav@19
   113
        people.getNicknames().add("Two");
jaroslav@19
   114
        
jaroslav@19
   115
        PropertyBinding pb = bindings.get("nicknames");
jaroslav@19
   116
        assertNotNull(pb, "Binding for info found");
jaroslav@19
   117
        
jaroslav@19
   118
        Object real = pb.getValue();
jaroslav@19
   119
        assertTrue(real instanceof Object[], "It is an array: " + real);
jaroslav@19
   120
        Object[] arr = (Object[])real;
jaroslav@19
   121
        assertEquals(arr.length, 2, "Length two");
jaroslav@19
   122
        assertEquals(arr[0], "One", "Text should be in the model");
jaroslav@19
   123
        assertEquals(arr[1], "Two", "2nd text in the model");
jaroslav@19
   124
    }
jaroslav@19
   125
    
jaroslav@17
   126
    @Test public void testConvertorOnAnArrayWithWrapper() {
jaroslav@17
   127
        this.replaceArray = true;
jaroslav@110
   128
        BrwsrCtx c = Contexts.newBuilder().register(Technology.class, this, 1).build();
jaroslav@17
   129
        
jaroslav@111
   130
        Person p = Models.bind(new Person(), c);
jaroslav@17
   131
        p.setFirstName("1");
jaroslav@17
   132
        p.setLastName("2");
jaroslav@17
   133
        p.setSex(Sex.MALE);
jaroslav@17
   134
        
jaroslav@278
   135
        People people = Models.bind(new People(), c).applyBindings();
jaroslav@17
   136
        people.getInfo().add(p);
jaroslav@278
   137
        
jaroslav@17
   138
        Object real = WrapperObject.find(people.getInfo());
jaroslav@17
   139
        assertEquals(real, this, "I am the model of the array");
jaroslav@17
   140
    }
jaroslav@17
   141
jaroslav@17
   142
    @Test public void bindingsOnArray() {
jaroslav@17
   143
        this.replaceArray = true;
jaroslav@110
   144
        BrwsrCtx c = Contexts.newBuilder().register(Technology.class, this, 1).build();
jaroslav@17
   145
        
jaroslav@278
   146
        People p = Models.bind(new People(), c).applyBindings();
jaroslav@17
   147
        p.getAge().add(30);
jaroslav@17
   148
        
jaroslav@17
   149
        PropertyBinding pb = bindings.get("age");
jaroslav@17
   150
        assertNotNull(pb, "There is a binding for age list");
jaroslav@17
   151
        
jaroslav@17
   152
        assertEquals(pb.getValue(), this, "I am the model of the array");
jaroslav@17
   153
    }
jaroslav@15
   154
jaroslav@15
   155
    @Override
jaroslav@15
   156
    public Object wrapModel(Object model) {
jaroslav@15
   157
        return this;
jaroslav@15
   158
    }
jaroslav@15
   159
jaroslav@15
   160
    @Override
jaroslav@15
   161
    public void bind(PropertyBinding b, Object model, Object data) {
jaroslav@17
   162
        bindings.put(b.getPropertyName(), b);
jaroslav@15
   163
    }
jaroslav@15
   164
jaroslav@15
   165
    @Override
jaroslav@15
   166
    public void valueHasMutated(Object data, String propertyName) {
jaroslav@15
   167
    }
jaroslav@15
   168
jaroslav@15
   169
    @Override
jaroslav@15
   170
    public void expose(FunctionBinding fb, Object model, Object d) {
jaroslav@15
   171
    }
jaroslav@15
   172
jaroslav@15
   173
    @Override
jaroslav@15
   174
    public void applyBindings(Object data) {
jaroslav@15
   175
    }
jaroslav@17
   176
jaroslav@17
   177
    @Override
jaroslav@17
   178
    public Object wrapArray(Object[] arr) {
jaroslav@17
   179
        return replaceArray ? this : arr;
jaroslav@17
   180
    }
jaroslav@22
   181
jaroslav@38
   182
    @Override
jaroslav@38
   183
    public <M> M toModel(Class<M> modelClass, Object data) {
jaroslav@38
   184
        return modelClass.cast(data);
jaroslav@38
   185
    }
jaroslav@38
   186
jaroslav@240
   187
    @Override
jaroslav@240
   188
    public void runSafe(Runnable r) {
jaroslav@240
   189
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
jaroslav@240
   190
    }
jaroslav@240
   191
jaroslav@15
   192
}