json/src/test/java/net/java/html/json/MapModelTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 16 Dec 2014 21:03:16 +0100
branchApplyId
changeset 908 ee7a0b3b2d4c
parent 901 4d20596b35bc
permissions -rw-r--r--
Providing an ability to apply model to element with certain id
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;
jtulach@878
    50
import java.util.Iterator;
jtulach@878
    51
import java.util.ListIterator;
jaroslav@9
    52
import java.util.Map;
jtulach@838
    53
import org.netbeans.html.context.spi.Contexts;
jtulach@838
    54
import org.netbeans.html.json.spi.FunctionBinding;
jtulach@838
    55
import org.netbeans.html.json.spi.JSONCall;
jtulach@838
    56
import org.netbeans.html.json.spi.PropertyBinding;
jtulach@838
    57
import org.netbeans.html.json.spi.Technology;
jtulach@838
    58
import org.netbeans.html.json.spi.Transfer;
jaroslav@9
    59
import org.testng.annotations.BeforeMethod;
jaroslav@9
    60
import org.testng.annotations.Test;
jaroslav@9
    61
import static org.testng.Assert.*;
jaroslav@9
    62
jaroslav@9
    63
/**
jaroslav@9
    64
 *
jtulach@790
    65
 * @author Jaroslav Tulach
jaroslav@9
    66
 */
jaroslav@9
    67
public class MapModelTest {
jaroslav@9
    68
    private MapTechnology t;
jaroslav@110
    69
    private BrwsrCtx c;
jaroslav@9
    70
jaroslav@9
    71
    @BeforeMethod public void initTechnology() {
jaroslav@9
    72
        t = new MapTechnology();
jaroslav@110
    73
        c = Contexts.newBuilder().register(Technology.class, t, 1).
jaroslav@110
    74
            register(Transfer.class, t, 1).build();
jaroslav@9
    75
    }
jaroslav@9
    76
    
jaroslav@9
    77
    @Test public void isThereABinding() throws Exception {
jaroslav@280
    78
        Person p = Models.bind(new Person(), c).applyBindings();
jtulach@908
    79
        assertNull(t.appliedId, "Applied globally");
jaroslav@9
    80
        p.setFirstName("Jarda");
jaroslav@9
    81
        
jaroslav@450
    82
        Map m = (Map)Models.toRaw(p);
jaroslav@9
    83
        Object v = m.get("firstName");
jaroslav@9
    84
        assertNotNull(v, "Value should be in the map");
jaroslav@9
    85
        assertEquals(v.getClass(), One.class, "It is instance of One");
jaroslav@9
    86
        One o = (One)v;
jaroslav@9
    87
        assertEquals(o.changes, 1, "One change so far");
jaroslav@17
    88
        assertFalse(o.pb.isReadOnly(), "Mutable property");
jaroslav@9
    89
        
jaroslav@9
    90
        assertEquals(o.get(), "Jarda", "Value should be in the map");
jaroslav@10
    91
        
jaroslav@10
    92
        o.set("Karle");
jaroslav@10
    93
        
jaroslav@10
    94
        assertEquals(p.getFirstName(), "Karle", "Model value updated");
jaroslav@10
    95
        assertEquals(o.changes, 2, "Snd change");
jaroslav@9
    96
    }
jaroslav@9
    97
    
jtulach@908
    98
    @Test public void applyLocally() throws Exception {
jtulach@908
    99
        Person p = Models.bind(new Person(), c).applyBindings("local");
jtulach@908
   100
        assertEquals(t.appliedId, "local", "Applied locally");
jtulach@908
   101
    }
jaroslav@280
   102
    
jaroslav@280
   103
    @Test public void dontNotifySameProperty() throws Exception {
jaroslav@280
   104
        Person p = Models.bind(new Person(), c);
jaroslav@280
   105
        p.setFirstName("Jirka");
jaroslav@280
   106
        
jaroslav@450
   107
        Map m = (Map)Models.toRaw(p);
jaroslav@280
   108
        Object v = m.get("firstName");
jaroslav@280
   109
        assertNotNull(v, "Value should be in the map");
jaroslav@280
   110
        assertEquals(v.getClass(), One.class, "It is instance of One");
jaroslav@280
   111
        One o = (One)v;
jaroslav@280
   112
        assertEquals(o.changes, 0, "No change so far the only one change happened before we connected");
jaroslav@280
   113
        
jaroslav@280
   114
        p.setFirstName(new String("Jirka"));
jaroslav@280
   115
        assertEquals(o.changes, 0, "No change so far, the value is the same");
jaroslav@280
   116
        
jaroslav@280
   117
        p.setFirstName("Jarda");
jaroslav@280
   118
        assertFalse(o.pb.isReadOnly(), "Mutable property");
jaroslav@280
   119
        
jaroslav@280
   120
        assertEquals(o.get(), "Jarda", "Value should be in the map");
jaroslav@280
   121
        
jaroslav@280
   122
        o.set("Karle");
jaroslav@280
   123
        
jaroslav@280
   124
        assertEquals(p.getFirstName(), "Karle", "Model value updated");
jaroslav@280
   125
        assertEquals(o.changes, 2, "Snd change");
jaroslav@280
   126
    }
jaroslav@280
   127
    
jtulach@755
   128
    @Test public void canSetEnumAsString() throws Exception {
jtulach@755
   129
        Person p = Models.bind(new Person(), c);
jtulach@755
   130
        p.setFirstName("Jirka");
jtulach@755
   131
        p.setSex(Sex.MALE);
jtulach@755
   132
        
jtulach@755
   133
        Map m = (Map)Models.toRaw(p);
jtulach@755
   134
        Object v = m.get("sex");
jtulach@755
   135
        assertNotNull(v, "Value should be in the map");
jtulach@755
   136
        assertEquals(v.getClass(), One.class, "It is instance of One");
jtulach@755
   137
        One o = (One)v;
jtulach@755
   138
        
jtulach@755
   139
        o.set("FEMALE");
jtulach@755
   140
jtulach@755
   141
        assertEquals(p.getSex(), Sex.FEMALE, "Changed to female");
jtulach@755
   142
    }
jtulach@755
   143
    
jaroslav@17
   144
    @Test public void derivedProperty() throws Exception {
jaroslav@111
   145
        Person p = Models.bind(new Person(), c);
jaroslav@17
   146
        
jaroslav@450
   147
        Map m = (Map)Models.toRaw(p);
jaroslav@17
   148
        Object v = m.get("fullName");
jaroslav@17
   149
        assertNotNull(v, "Value should be in the map");
jaroslav@17
   150
        assertEquals(v.getClass(), One.class, "It is instance of One");
jaroslav@17
   151
        One o = (One)v;
jaroslav@17
   152
        assertTrue(o.pb.isReadOnly(), "Mutable property");
jaroslav@17
   153
    }
jaroslav@17
   154
    
jaroslav@11
   155
    @Test public void changeSex() {
jaroslav@111
   156
        Person p = Models.bind(new Person(), c);
jaroslav@11
   157
        p.setFirstName("Trans");
jaroslav@11
   158
        p.setSex(Sex.MALE);
jaroslav@11
   159
        
jaroslav@450
   160
        Map m = (Map)Models.toRaw(p);
jaroslav@11
   161
        Object o = m.get("changeSex");
jaroslav@11
   162
        assertNotNull(o, "Function registered in the model");
jaroslav@20
   163
        assertEquals(o.getClass(), One.class);
jaroslav@20
   164
        
jaroslav@20
   165
        One one = (One)o;
jaroslav@20
   166
        assertNotNull(one.fb, "Function binding specified");
jaroslav@20
   167
        
jaroslav@21
   168
        one.fb.call(null, null);
jaroslav@21
   169
        
jaroslav@21
   170
        assertEquals(p.getSex(), Sex.FEMALE, "Changed");
jaroslav@21
   171
    }
jaroslav@21
   172
    
jaroslav@21
   173
    @Test public void setSex() {
jaroslav@111
   174
        Person p = Models.bind(new Person(), c);
jaroslav@21
   175
        p.setFirstName("Trans");
jaroslav@21
   176
        
jaroslav@450
   177
        Map m = (Map)Models.toRaw(p);
jaroslav@21
   178
        Object o = m.get("changeSex");
jaroslav@21
   179
        assertNotNull(o, "Function registered in the model");
jaroslav@21
   180
        assertEquals(o.getClass(), One.class);
jaroslav@21
   181
        
jaroslav@21
   182
        One one = (One)o;
jaroslav@21
   183
        assertNotNull(one.fb, "Function binding specified");
jaroslav@21
   184
        
jaroslav@21
   185
        one.fb.call("FEMALE", new Object());
jaroslav@20
   186
        
jaroslav@20
   187
        assertEquals(p.getSex(), Sex.FEMALE, "Changed");
jaroslav@11
   188
    }
jtulach@878
   189
    
jtulach@878
   190
    @Test public void removeViaIterator() {
jtulach@878
   191
        People p = Models.bind(new People(), c);
jtulach@878
   192
        p.getNicknames().add("One");
jtulach@878
   193
        p.getNicknames().add("Two");
jtulach@878
   194
        p.getNicknames().add("Three");
jtulach@878
   195
jtulach@878
   196
        Map m = (Map)Models.toRaw(p);
jtulach@878
   197
        Object o = m.get("nicknames");
jtulach@878
   198
        assertNotNull(o, "List registered in the model");
jtulach@878
   199
        assertEquals(o.getClass(), One.class);
jtulach@878
   200
        One one = (One)o;
jtulach@878
   201
        
jtulach@878
   202
        
jtulach@878
   203
        assertEquals(one.changes, 0, "No change");
jtulach@878
   204
        
jtulach@878
   205
        Iterator<String> it = p.getNicknames().iterator();
jtulach@878
   206
        assertEquals(it.next(), "One");
jtulach@878
   207
        assertEquals(it.next(), "Two");
jtulach@878
   208
        it.remove();
jtulach@878
   209
        assertEquals(it.next(), "Three");
jtulach@878
   210
        assertFalse(it.hasNext());
jtulach@878
   211
        
jtulach@878
   212
        
jtulach@878
   213
        assertEquals(one.changes, 1, "One change");
jtulach@878
   214
    }
jtulach@878
   215
    
jtulach@878
   216
    @Test public void removeViaListIterator() {
jtulach@878
   217
        People p = Models.bind(new People(), c);
jtulach@878
   218
        p.getNicknames().add("One");
jtulach@878
   219
        p.getNicknames().add("Two");
jtulach@878
   220
        p.getNicknames().add("Three");
jtulach@878
   221
jtulach@878
   222
        Map m = (Map)Models.toRaw(p);
jtulach@878
   223
        Object o = m.get("nicknames");
jtulach@878
   224
        assertNotNull(o, "List registered in the model");
jtulach@878
   225
        assertEquals(o.getClass(), One.class);
jtulach@878
   226
        One one = (One)o;
jtulach@878
   227
        
jtulach@878
   228
        
jtulach@878
   229
        assertEquals(one.changes, 0, "No change");
jtulach@878
   230
        
jtulach@878
   231
        ListIterator<String> it = p.getNicknames().listIterator(1);
jtulach@878
   232
        assertEquals(it.next(), "Two");
jtulach@878
   233
        it.remove();
jtulach@878
   234
        assertEquals(it.next(), "Three");
jtulach@878
   235
        assertFalse(it.hasNext());
jtulach@878
   236
        
jtulach@878
   237
        
jtulach@878
   238
        assertEquals(one.changes, 1, "One change");
jtulach@879
   239
        
jtulach@879
   240
        it.set("3");
jtulach@879
   241
        assertEquals(p.getNicknames().get(1), "3");
jtulach@879
   242
        
jtulach@879
   243
        assertEquals(one.changes, 2, "Snd change");
jtulach@878
   244
    }
jaroslav@9
   245
jtulach@901
   246
    @Test public void functionWithParameters() {
jtulach@901
   247
        People p = Models.bind(new People(), c);
jtulach@901
   248
        p.getNicknames().add("One");
jtulach@901
   249
        p.getNicknames().add("Two");
jtulach@901
   250
        p.getNicknames().add("Three");
jtulach@901
   251
jtulach@901
   252
        Map m = (Map)Models.toRaw(p);
jtulach@901
   253
        Object o = m.get("inInnerClass");
jtulach@901
   254
        assertNotNull(o, "functiton is available");
jtulach@901
   255
        assertEquals(o.getClass(), One.class);
jtulach@901
   256
        One one = (One)o;
jtulach@901
   257
        
jtulach@901
   258
        Map<String,Object> obj = new HashMap<String, Object>();
jtulach@901
   259
        obj.put("nick", "newNick");
jtulach@901
   260
        obj.put("x", 42);
jtulach@901
   261
        obj.put("y", 7.7f);
jtulach@901
   262
        final Person data = new Person("a", "b", Sex.MALE);
jtulach@901
   263
        
jtulach@901
   264
        one.fb.call(data, obj);
jtulach@901
   265
jtulach@901
   266
        assertEquals(p.getInfo().size(), 1, "a+b is there: " + p.getInfo());
jtulach@901
   267
        assertEquals(p.getInfo().get(0), data, "Expecting data: " + p.getInfo());
jtulach@901
   268
        
jtulach@901
   269
        assertEquals(p.getNicknames().size(), 4, "One more nickname: " + p.getNicknames());
jtulach@901
   270
        assertEquals(p.getNicknames().get(3), "newNick");
jtulach@901
   271
        
jtulach@901
   272
        assertEquals(p.getAge().size(), 2, "Two new values: " + p.getAge());
jtulach@901
   273
        assertEquals(p.getAge().get(0).intValue(), 42);
jtulach@901
   274
        assertEquals(p.getAge().get(1).intValue(), 7);
jtulach@901
   275
    }
jtulach@901
   276
    
jaroslav@22
   277
    static final class One {
jaroslav@9
   278
        int changes;
jaroslav@17
   279
        final PropertyBinding pb;
jaroslav@20
   280
        final FunctionBinding fb;
jaroslav@9
   281
    
jaroslav@17
   282
        One(Object m, PropertyBinding pb) throws NoSuchMethodException {
jaroslav@17
   283
            this.pb = pb;
jaroslav@20
   284
            this.fb = null;
jaroslav@20
   285
        }
jaroslav@20
   286
        One(Object m, FunctionBinding fb) throws NoSuchMethodException {
jaroslav@20
   287
            this.pb = null;
jaroslav@20
   288
            this.fb = fb;
jaroslav@9
   289
        }
jaroslav@9
   290
        
jaroslav@9
   291
        Object get() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
jaroslav@17
   292
            return pb.getValue();
jaroslav@9
   293
        }
jaroslav@10
   294
        
jaroslav@10
   295
        void set(Object v) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
jaroslav@17
   296
            pb.setValue(v);
jaroslav@10
   297
        }
jaroslav@9
   298
    }
jaroslav@9
   299
    
jaroslav@23
   300
    static final class MapTechnology 
jtulach@908
   301
    implements Technology.ApplyId<Map<String,One>>, Transfer {
jtulach@908
   302
        private Map<String, One> appliedData;
jtulach@908
   303
        private String appliedId;
jaroslav@9
   304
jaroslav@9
   305
        @Override
jaroslav@9
   306
        public Map<String, One> wrapModel(Object model) {
jaroslav@100
   307
            return new HashMap<String, One>();
jaroslav@9
   308
        }
jaroslav@9
   309
jaroslav@9
   310
        @Override
jaroslav@9
   311
        public void valueHasMutated(Map<String, One> data, String propertyName) {
jaroslav@9
   312
            One p = data.get(propertyName);
jaroslav@9
   313
            if (p != null) {
jaroslav@9
   314
                p.changes++;
jaroslav@9
   315
            }
jaroslav@9
   316
        }
jaroslav@9
   317
jaroslav@9
   318
        @Override
jaroslav@9
   319
        public void bind(PropertyBinding b, Object model, Map<String, One> data) {
jaroslav@9
   320
            try {
jaroslav@17
   321
                One o = new One(model, b);
jaroslav@9
   322
                data.put(b.getPropertyName(), o);
jaroslav@9
   323
            } catch (NoSuchMethodException ex) {
jaroslav@9
   324
                throw new IllegalStateException(ex);
jaroslav@9
   325
            }
jaroslav@9
   326
        }
jaroslav@11
   327
jaroslav@11
   328
        @Override
jaroslav@11
   329
        public void expose(FunctionBinding fb, Object model, Map<String, One> data) {
jaroslav@11
   330
            try {
jaroslav@20
   331
                data.put(fb.getFunctionName(), new One(model, fb));
jaroslav@11
   332
            } catch (NoSuchMethodException ex) {
jaroslav@11
   333
                throw new IllegalStateException(ex);
jaroslav@11
   334
            }
jaroslav@11
   335
        }
jaroslav@14
   336
jaroslav@14
   337
        @Override
jaroslav@14
   338
        public void applyBindings(Map<String, One> data) {
jtulach@908
   339
            throw new UnsupportedOperationException("Never called!");
jaroslav@14
   340
        }
jaroslav@17
   341
jaroslav@17
   342
        @Override
jaroslav@17
   343
        public Object wrapArray(Object[] arr) {
jaroslav@17
   344
            return arr;
jaroslav@17
   345
        }
jaroslav@22
   346
jaroslav@22
   347
        @Override
jaroslav@22
   348
        public void extract(Object obj, String[] props, Object[] values) {
jaroslav@22
   349
            Map<?,?> map = obj instanceof Map ? (Map<?,?>)obj : null;
jaroslav@22
   350
            for (int i = 0; i < Math.min(props.length, values.length); i++) {
jaroslav@22
   351
                if (map == null) {
jaroslav@22
   352
                    values[i] = null;
jaroslav@22
   353
                } else {
jaroslav@22
   354
                    values[i] = map.get(props[i]);
jaroslav@22
   355
                    if (values[i] instanceof One) {
jaroslav@22
   356
                        values[i] = ((One)values[i]).pb.getValue();
jaroslav@22
   357
                    }
jaroslav@22
   358
                }
jaroslav@22
   359
            }
jaroslav@22
   360
        }
jaroslav@24
   361
jaroslav@24
   362
        @Override
jaroslav@24
   363
        public void loadJSON(JSONCall call) {
jaroslav@24
   364
            call.notifyError(new UnsupportedOperationException());
jaroslav@24
   365
        }
jaroslav@38
   366
jaroslav@38
   367
        @Override
jaroslav@38
   368
        public <M> M toModel(Class<M> modelClass, Object data) {
jaroslav@38
   369
            return modelClass.cast(data);
jaroslav@38
   370
        }
jaroslav@60
   371
jaroslav@60
   372
        @Override
jaroslav@60
   373
        public Object toJSON(InputStream is) throws IOException {
jaroslav@60
   374
            throw new IOException();
jaroslav@60
   375
        }
jaroslav@240
   376
jaroslav@240
   377
        @Override
jaroslav@240
   378
        public void runSafe(Runnable r) {
jaroslav@240
   379
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
jaroslav@240
   380
        }
jtulach@908
   381
jtulach@908
   382
        @Override
jtulach@908
   383
        public void applyBindings(String id, Map<String, One> data) {
jtulach@908
   384
            this.appliedId = id;
jtulach@908
   385
            this.appliedData = data;
jtulach@908
   386
        }
jaroslav@9
   387
    }
jaroslav@9
   388
}