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