json/src/test/java/org/netbeans/html/json/impl/ToDoTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 02 Aug 2014 07:54:15 +0200
branchDeepWatch
changeset 788 f8ac4d547ad3
parent 787 a0e8f185c0d4
child 838 bdc3d696dd4a
permissions -rw-r--r--
Deep checking of @ComputedProperties is now default if one depends on another model class
jtulach@781
     1
/**
jtulach@781
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jtulach@781
     3
 *
jtulach@781
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jtulach@781
     5
 *
jtulach@781
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jtulach@781
     7
 * Other names may be trademarks of their respective owners.
jtulach@781
     8
 *
jtulach@781
     9
 * The contents of this file are subject to the terms of either the GNU
jtulach@781
    10
 * General Public License Version 2 only ("GPL") or the Common
jtulach@781
    11
 * Development and Distribution License("CDDL") (collectively, the
jtulach@781
    12
 * "License"). You may not use this file except in compliance with the
jtulach@781
    13
 * License. You can obtain a copy of the License at
jtulach@781
    14
 * http://www.netbeans.org/cddl-gplv2.html
jtulach@781
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jtulach@781
    16
 * specific language governing permissions and limitations under the
jtulach@781
    17
 * License.  When distributing the software, include this License Header
jtulach@781
    18
 * Notice in each file and include the License file at
jtulach@781
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jtulach@781
    20
 * particular file as subject to the "Classpath" exception as provided
jtulach@781
    21
 * by Oracle in the GPL Version 2 section of the License file that
jtulach@781
    22
 * accompanied this code. If applicable, add the following below the
jtulach@781
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jtulach@781
    24
 * your own identifying information:
jtulach@781
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jtulach@781
    26
 *
jtulach@781
    27
 * Contributor(s):
jtulach@781
    28
 *
jtulach@781
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jtulach@781
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jtulach@781
    31
 *
jtulach@781
    32
 * If you wish your version of this file to be governed by only the CDDL
jtulach@781
    33
 * or only the GPL Version 2, indicate your decision by adding
jtulach@781
    34
 * "[Contributor] elects to include this software in this distribution
jtulach@781
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jtulach@781
    36
 * single choice of license, a recipient has the option to distribute
jtulach@781
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jtulach@781
    38
 * to extend the choice of license to its licensees as provided above.
jtulach@781
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jtulach@781
    40
 * Version 2 license, then the option applies only if the new code is
jtulach@781
    41
 * made subject to such option by the copyright holder.
jtulach@781
    42
 */
jtulach@787
    43
package org.netbeans.html.json.impl;
jtulach@781
    44
jtulach@781
    45
import java.util.List;
jtulach@781
    46
import java.util.Map;
jtulach@781
    47
import net.java.html.BrwsrCtx;
jtulach@787
    48
import net.java.html.json.ComputedProperty;
jtulach@787
    49
import net.java.html.json.Model;
jtulach@787
    50
import net.java.html.json.Models;
jtulach@787
    51
import net.java.html.json.Property;
jtulach@781
    52
import org.apidesign.html.context.spi.Contexts;
jtulach@781
    53
import org.apidesign.html.json.spi.Technology;
jtulach@781
    54
import org.apidesign.html.json.spi.Transfer;
jtulach@787
    55
import org.netbeans.html.json.impl.DeepChangeTest.MapTechnology;
jtulach@787
    56
import org.netbeans.html.json.impl.DeepChangeTest.One;
jtulach@781
    57
import static org.testng.Assert.*;
jtulach@781
    58
import org.testng.annotations.BeforeMethod;
jtulach@781
    59
import org.testng.annotations.Test;
jtulach@781
    60
jtulach@781
    61
/**
jtulach@781
    62
 *
jtulach@781
    63
 * @author Jaroslav Tulach
jtulach@781
    64
 */
jtulach@781
    65
@Model(className = "TodoUI", properties = {
jtulach@781
    66
    @Property(name = "todos", type = Todo.class, array = true),
jtulach@781
    67
    @Property(name = "todoText", type = String.class)
jtulach@781
    68
})
jtulach@781
    69
public class ToDoTest {
jtulach@781
    70
    @Model(className = "Todo", properties = {
jtulach@781
    71
        @Property(name = "text", type = String.class),
jtulach@781
    72
        @Property(name = "done", type = boolean.class)
jtulach@781
    73
    })
jtulach@781
    74
    static class ItemCtrl {
jtulach@781
    75
    }
jtulach@781
    76
jtulach@788
    77
    @ComputedProperty
jtulach@781
    78
    static int remaining(
jtulach@781
    79
        List<Todo> todos, String todoText
jtulach@781
    80
    ) {
jtulach@781
    81
        int count = 0;
jtulach@781
    82
        for (Todo d : todos) {
jtulach@781
    83
            if (!d.isDone()) {
jtulach@781
    84
                count++;
jtulach@781
    85
            }
jtulach@781
    86
        }
jtulach@781
    87
        return count;
jtulach@781
    88
    }
jtulach@781
    89
    
jtulach@787
    90
    private MapTechnology t;
jtulach@781
    91
    private BrwsrCtx c;
jtulach@781
    92
jtulach@781
    93
    @BeforeMethod
jtulach@781
    94
    public void initTechnology() {
jtulach@787
    95
        t = new MapTechnology();
jtulach@781
    96
        c = Contexts.newBuilder().register(Technology.class, t, 1).
jtulach@781
    97
                register(Transfer.class, t, 1).build();
jtulach@781
    98
    }
jtulach@781
    99
    
jtulach@781
   100
    
jtulach@781
   101
    @Test public void checkAndUncheckFirstItem() throws Exception {
jtulach@781
   102
        TodoUI ui = Models.bind(
jtulach@781
   103
                new TodoUI(
jtulach@781
   104
                    null,
jtulach@781
   105
                    new Todo("First", false),
jtulach@781
   106
                    new Todo("2nd", true),
jtulach@781
   107
                    new Todo("Third", false)
jtulach@781
   108
                ), c).applyBindings();
jtulach@781
   109
jtulach@781
   110
        Map m = (Map) Models.toRaw(ui);
jtulach@781
   111
        Object v = m.get("remaining");
jtulach@781
   112
        assertNotNull(v, "Value should be in the map");
jtulach@781
   113
        assertEquals(v.getClass(), One.class, "It is instance of One");
jtulach@781
   114
        One o = (One) v;
jtulach@781
   115
        assertEquals(o.changes, 0, "No changes so far");
jtulach@781
   116
        assertTrue(o.pb.isReadOnly(), "Derived property");
jtulach@781
   117
        assertEquals(o.get(), 2);
jtulach@781
   118
jtulach@781
   119
        ui.getTodos().get(0).setDone(true);
jtulach@781
   120
jtulach@781
   121
        assertEquals(o.get(), 1);
jtulach@781
   122
        assertEquals(o.changes, 1, "One change so far");
jtulach@781
   123
jtulach@781
   124
        ui.getTodos().get(0).setDone(false);
jtulach@781
   125
jtulach@781
   126
        assertEquals(o.get(), 2);
jtulach@781
   127
        assertEquals(o.changes, 2, "2nd change so far");
jtulach@781
   128
        
jtulach@781
   129
    }
jtulach@781
   130
    
jtulach@781
   131
}