json/src/test/java/net/java/html/json/MapModelTest.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Fri, 07 Feb 2014 07:44:34 +0100
changeset 551 7ca2253fa86d
parent 450 bc89281a90d5
child 755 d396bc480560
permissions -rw-r--r--
Updating copyright headers to mention current year
jaroslav@9
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@9
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jaroslav@9
     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@9
     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@9
    42
 */
jaroslav@9
    43
package net.java.html.json;
jaroslav@9
    44
jaroslav@110
    45
import net.java.html.BrwsrCtx;
jaroslav@60
    46
import java.io.IOException;
jaroslav@60
    47
import java.io.InputStream;
jaroslav@9
    48
import java.lang.reflect.InvocationTargetException;
jaroslav@9
    49
import java.util.HashMap;
jaroslav@9
    50
import java.util.Map;
jaroslav@110
    51
import org.apidesign.html.context.spi.Contexts;
jaroslav@11
    52
import org.apidesign.html.json.spi.FunctionBinding;
jaroslav@24
    53
import org.apidesign.html.json.spi.JSONCall;
jaroslav@9
    54
import org.apidesign.html.json.spi.PropertyBinding;
jaroslav@9
    55
import org.apidesign.html.json.spi.Technology;
jaroslav@23
    56
import org.apidesign.html.json.spi.Transfer;
jaroslav@414
    57
import org.netbeans.html.json.impl.JSON;
jaroslav@9
    58
import org.testng.annotations.BeforeMethod;
jaroslav@9
    59
import org.testng.annotations.Test;
jaroslav@9
    60
import static org.testng.Assert.*;
jaroslav@9
    61
jaroslav@9
    62
/**
jaroslav@9
    63
 *
jaroslav@9
    64
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@9
    65
 */
jaroslav@9
    66
public class MapModelTest {
jaroslav@9
    67
    private MapTechnology t;
jaroslav@110
    68
    private BrwsrCtx c;
jaroslav@9
    69
jaroslav@9
    70
    @BeforeMethod public void initTechnology() {
jaroslav@9
    71
        t = new MapTechnology();
jaroslav@110
    72
        c = Contexts.newBuilder().register(Technology.class, t, 1).
jaroslav@110
    73
            register(Transfer.class, t, 1).build();
jaroslav@9
    74
    }
jaroslav@9
    75
    
jaroslav@9
    76
    @Test public void isThereABinding() throws Exception {
jaroslav@280
    77
        Person p = Models.bind(new Person(), c).applyBindings();
jaroslav@9
    78
        p.setFirstName("Jarda");
jaroslav@9
    79
        
jaroslav@450
    80
        Map m = (Map)Models.toRaw(p);
jaroslav@9
    81
        Object v = m.get("firstName");
jaroslav@9
    82
        assertNotNull(v, "Value should be in the map");
jaroslav@9
    83
        assertEquals(v.getClass(), One.class, "It is instance of One");
jaroslav@9
    84
        One o = (One)v;
jaroslav@9
    85
        assertEquals(o.changes, 1, "One change so far");
jaroslav@17
    86
        assertFalse(o.pb.isReadOnly(), "Mutable property");
jaroslav@9
    87
        
jaroslav@9
    88
        assertEquals(o.get(), "Jarda", "Value should be in the map");
jaroslav@10
    89
        
jaroslav@10
    90
        o.set("Karle");
jaroslav@10
    91
        
jaroslav@10
    92
        assertEquals(p.getFirstName(), "Karle", "Model value updated");
jaroslav@10
    93
        assertEquals(o.changes, 2, "Snd change");
jaroslav@9
    94
    }
jaroslav@9
    95
    
jaroslav@280
    96
    
jaroslav@280
    97
    @Test public void dontNotifySameProperty() throws Exception {
jaroslav@280
    98
        Person p = Models.bind(new Person(), c);
jaroslav@280
    99
        p.setFirstName("Jirka");
jaroslav@280
   100
        
jaroslav@450
   101
        Map m = (Map)Models.toRaw(p);
jaroslav@280
   102
        Object v = m.get("firstName");
jaroslav@280
   103
        assertNotNull(v, "Value should be in the map");
jaroslav@280
   104
        assertEquals(v.getClass(), One.class, "It is instance of One");
jaroslav@280
   105
        One o = (One)v;
jaroslav@280
   106
        assertEquals(o.changes, 0, "No change so far the only one change happened before we connected");
jaroslav@280
   107
        
jaroslav@280
   108
        p.setFirstName(new String("Jirka"));
jaroslav@280
   109
        assertEquals(o.changes, 0, "No change so far, the value is the same");
jaroslav@280
   110
        
jaroslav@280
   111
        p.setFirstName("Jarda");
jaroslav@280
   112
        assertFalse(o.pb.isReadOnly(), "Mutable property");
jaroslav@280
   113
        
jaroslav@280
   114
        assertEquals(o.get(), "Jarda", "Value should be in the map");
jaroslav@280
   115
        
jaroslav@280
   116
        o.set("Karle");
jaroslav@280
   117
        
jaroslav@280
   118
        assertEquals(p.getFirstName(), "Karle", "Model value updated");
jaroslav@280
   119
        assertEquals(o.changes, 2, "Snd change");
jaroslav@280
   120
    }
jaroslav@280
   121
    
jaroslav@17
   122
    @Test public void derivedProperty() throws Exception {
jaroslav@111
   123
        Person p = Models.bind(new Person(), c);
jaroslav@17
   124
        
jaroslav@450
   125
        Map m = (Map)Models.toRaw(p);
jaroslav@17
   126
        Object v = m.get("fullName");
jaroslav@17
   127
        assertNotNull(v, "Value should be in the map");
jaroslav@17
   128
        assertEquals(v.getClass(), One.class, "It is instance of One");
jaroslav@17
   129
        One o = (One)v;
jaroslav@17
   130
        assertTrue(o.pb.isReadOnly(), "Mutable property");
jaroslav@17
   131
    }
jaroslav@17
   132
    
jaroslav@11
   133
    @Test public void changeSex() {
jaroslav@111
   134
        Person p = Models.bind(new Person(), c);
jaroslav@11
   135
        p.setFirstName("Trans");
jaroslav@11
   136
        p.setSex(Sex.MALE);
jaroslav@11
   137
        
jaroslav@450
   138
        Map m = (Map)Models.toRaw(p);
jaroslav@11
   139
        Object o = m.get("changeSex");
jaroslav@11
   140
        assertNotNull(o, "Function registered in the model");
jaroslav@20
   141
        assertEquals(o.getClass(), One.class);
jaroslav@20
   142
        
jaroslav@20
   143
        One one = (One)o;
jaroslav@20
   144
        assertNotNull(one.fb, "Function binding specified");
jaroslav@20
   145
        
jaroslav@21
   146
        one.fb.call(null, null);
jaroslav@21
   147
        
jaroslav@21
   148
        assertEquals(p.getSex(), Sex.FEMALE, "Changed");
jaroslav@21
   149
    }
jaroslav@21
   150
    
jaroslav@21
   151
    @Test public void setSex() {
jaroslav@111
   152
        Person p = Models.bind(new Person(), c);
jaroslav@21
   153
        p.setFirstName("Trans");
jaroslav@21
   154
        
jaroslav@450
   155
        Map m = (Map)Models.toRaw(p);
jaroslav@21
   156
        Object o = m.get("changeSex");
jaroslav@21
   157
        assertNotNull(o, "Function registered in the model");
jaroslav@21
   158
        assertEquals(o.getClass(), One.class);
jaroslav@21
   159
        
jaroslav@21
   160
        One one = (One)o;
jaroslav@21
   161
        assertNotNull(one.fb, "Function binding specified");
jaroslav@21
   162
        
jaroslav@21
   163
        one.fb.call("FEMALE", new Object());
jaroslav@20
   164
        
jaroslav@20
   165
        assertEquals(p.getSex(), Sex.FEMALE, "Changed");
jaroslav@11
   166
    }
jaroslav@9
   167
jaroslav@22
   168
    static final class One {
jaroslav@9
   169
        int changes;
jaroslav@17
   170
        final PropertyBinding pb;
jaroslav@20
   171
        final FunctionBinding fb;
jaroslav@9
   172
    
jaroslav@17
   173
        One(Object m, PropertyBinding pb) throws NoSuchMethodException {
jaroslav@17
   174
            this.pb = pb;
jaroslav@20
   175
            this.fb = null;
jaroslav@20
   176
        }
jaroslav@20
   177
        One(Object m, FunctionBinding fb) throws NoSuchMethodException {
jaroslav@20
   178
            this.pb = null;
jaroslav@20
   179
            this.fb = fb;
jaroslav@9
   180
        }
jaroslav@9
   181
        
jaroslav@9
   182
        Object get() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
jaroslav@17
   183
            return pb.getValue();
jaroslav@9
   184
        }
jaroslav@10
   185
        
jaroslav@10
   186
        void set(Object v) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
jaroslav@17
   187
            pb.setValue(v);
jaroslav@10
   188
        }
jaroslav@9
   189
    }
jaroslav@9
   190
    
jaroslav@23
   191
    static final class MapTechnology 
jaroslav@23
   192
    implements Technology<Map<String,One>>, Transfer {
jaroslav@9
   193
jaroslav@9
   194
        @Override
jaroslav@9
   195
        public Map<String, One> wrapModel(Object model) {
jaroslav@100
   196
            return new HashMap<String, One>();
jaroslav@9
   197
        }
jaroslav@9
   198
jaroslav@9
   199
        @Override
jaroslav@9
   200
        public void valueHasMutated(Map<String, One> data, String propertyName) {
jaroslav@9
   201
            One p = data.get(propertyName);
jaroslav@9
   202
            if (p != null) {
jaroslav@9
   203
                p.changes++;
jaroslav@9
   204
            }
jaroslav@9
   205
        }
jaroslav@9
   206
jaroslav@9
   207
        @Override
jaroslav@9
   208
        public void bind(PropertyBinding b, Object model, Map<String, One> data) {
jaroslav@9
   209
            try {
jaroslav@17
   210
                One o = new One(model, b);
jaroslav@9
   211
                data.put(b.getPropertyName(), o);
jaroslav@9
   212
            } catch (NoSuchMethodException ex) {
jaroslav@9
   213
                throw new IllegalStateException(ex);
jaroslav@9
   214
            }
jaroslav@9
   215
        }
jaroslav@11
   216
jaroslav@11
   217
        @Override
jaroslav@11
   218
        public void expose(FunctionBinding fb, Object model, Map<String, One> data) {
jaroslav@11
   219
            try {
jaroslav@20
   220
                data.put(fb.getFunctionName(), new One(model, fb));
jaroslav@11
   221
            } catch (NoSuchMethodException ex) {
jaroslav@11
   222
                throw new IllegalStateException(ex);
jaroslav@11
   223
            }
jaroslav@11
   224
        }
jaroslav@14
   225
jaroslav@14
   226
        @Override
jaroslav@14
   227
        public void applyBindings(Map<String, One> data) {
jaroslav@14
   228
        }
jaroslav@17
   229
jaroslav@17
   230
        @Override
jaroslav@17
   231
        public Object wrapArray(Object[] arr) {
jaroslav@17
   232
            return arr;
jaroslav@17
   233
        }
jaroslav@22
   234
jaroslav@22
   235
        @Override
jaroslav@22
   236
        public void extract(Object obj, String[] props, Object[] values) {
jaroslav@22
   237
            Map<?,?> map = obj instanceof Map ? (Map<?,?>)obj : null;
jaroslav@22
   238
            for (int i = 0; i < Math.min(props.length, values.length); i++) {
jaroslav@22
   239
                if (map == null) {
jaroslav@22
   240
                    values[i] = null;
jaroslav@22
   241
                } else {
jaroslav@22
   242
                    values[i] = map.get(props[i]);
jaroslav@22
   243
                    if (values[i] instanceof One) {
jaroslav@22
   244
                        values[i] = ((One)values[i]).pb.getValue();
jaroslav@22
   245
                    }
jaroslav@22
   246
                }
jaroslav@22
   247
            }
jaroslav@22
   248
        }
jaroslav@24
   249
jaroslav@24
   250
        @Override
jaroslav@24
   251
        public void loadJSON(JSONCall call) {
jaroslav@24
   252
            call.notifyError(new UnsupportedOperationException());
jaroslav@24
   253
        }
jaroslav@38
   254
jaroslav@38
   255
        @Override
jaroslav@38
   256
        public <M> M toModel(Class<M> modelClass, Object data) {
jaroslav@38
   257
            return modelClass.cast(data);
jaroslav@38
   258
        }
jaroslav@60
   259
jaroslav@60
   260
        @Override
jaroslav@60
   261
        public Object toJSON(InputStream is) throws IOException {
jaroslav@60
   262
            throw new IOException();
jaroslav@60
   263
        }
jaroslav@240
   264
jaroslav@240
   265
        @Override
jaroslav@240
   266
        public void runSafe(Runnable r) {
jaroslav@240
   267
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
jaroslav@240
   268
        }
jaroslav@9
   269
    }
jaroslav@9
   270
}