json-beans/src/test/java/net/java/html/beans/MapModelGCTest.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;
jtulach@790
    46
import java.io.IOException;
jtulach@790
    47
import java.io.InputStream;
jtulach@790
    48
import java.lang.reflect.InvocationTargetException;
jtulach@790
    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
 */
jtulach@790
    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
    
jtulach@790
    87
    @Test public void isThereABinding() throws Exception {
jtulach@808
    88
        Person p = Models.bind(new Person(), c);
jtulach@808
    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);
jtulach@790
    94
        Object v = m.get("firstName");
jtulach@790
    95
        assertNotNull(v, "Value should be in the map");
jtulach@790
    96
        assertEquals(v.getClass(), One.class, "It is instance of One");
jtulach@790
    97
        One o = (One)v;
jtulach@790
    98
        assertEquals(o.changes, 1, "One change so far");
jtulach@790
    99
        assertFalse(o.pb.isReadOnly(), "Mutable property");
jaroslav@9
   100
        
jtulach@790
   101
        assertEquals(o.get(), "Jarda", "Value should be in the map");
jaroslav@10
   102
        
jtulach@790
   103
        o.set("Karle");
jaroslav@10
   104
        
jtulach@790
   105
        assertEquals(p.getFirstName(), "Karle", "Model value updated");
jtulach@790
   106
        assertEquals(o.changes, 2, "Snd change");
jtulach@790
   107
    }
jtulach@790
   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
    }
jtulach@790
   114
    
jtulach@790
   115
    @Test public void dontNotifySameProperty() throws Exception {
jtulach@790
   116
        Person p = Models.bind(new Person(), c);
jtulach@790
   117
        p.setFirstName("Jirka");
jtulach@790
   118
        
jtulach@790
   119
        Map m = (Map)Models.toRaw(p);
jtulach@790
   120
        Object v = m.get("firstName");
jtulach@790
   121
        assertNotNull(v, "Value should be in the map");
jtulach@790
   122
        assertEquals(v.getClass(), One.class, "It is instance of One");
jtulach@790
   123
        One o = (One)v;
jtulach@790
   124
        assertEquals(o.changes, 0, "No change so far the only one change happened before we connected");
jtulach@790
   125
        
jtulach@790
   126
        p.setFirstName(new String("Jirka"));
jtulach@790
   127
        assertEquals(o.changes, 0, "No change so far, the value is the same");
jtulach@790
   128
        
jtulach@790
   129
        p.setFirstName("Jarda");
jtulach@790
   130
        assertFalse(o.pb.isReadOnly(), "Mutable property");
jtulach@790
   131
        
jtulach@790
   132
        assertEquals(o.get(), "Jarda", "Value should be in the map");
jtulach@790
   133
        
jtulach@790
   134
        o.set("Karle");
jtulach@790
   135
        
jtulach@790
   136
        assertEquals(p.getFirstName(), "Karle", "Model value updated");
jtulach@790
   137
        assertEquals(o.changes, 2, "Snd change");
jtulach@790
   138
    }
jtulach@790
   139
    
jtulach@790
   140
    @Test public void canSetEnumAsString() throws Exception {
jtulach@790
   141
        Person p = Models.bind(new Person(), c);
jtulach@790
   142
        p.setFirstName("Jirka");
jtulach@790
   143
        p.setSex(Sex.MALE);
jtulach@790
   144
        
jtulach@790
   145
        Map m = (Map)Models.toRaw(p);
jtulach@790
   146
        Object v = m.get("sex");
jtulach@790
   147
        assertNotNull(v, "Value should be in the map");
jtulach@790
   148
        assertEquals(v.getClass(), One.class, "It is instance of One");
jtulach@790
   149
        One o = (One)v;
jtulach@790
   150
        
jtulach@790
   151
        o.set("FEMALE");
jtulach@790
   152
jtulach@790
   153
        assertEquals(p.getSex(), Sex.FEMALE, "Changed to female");
jtulach@790
   154
    }
jtulach@790
   155
    
jtulach@790
   156
    @Test public void derivedProperty() throws Exception {
jtulach@790
   157
        Person p = Models.bind(new Person(), c);
jtulach@790
   158
        
jtulach@790
   159
        Map m = (Map)Models.toRaw(p);
jtulach@790
   160
        Object v = m.get("fullName");
jtulach@790
   161
        assertNotNull(v, "Value should be in the map");
jtulach@790
   162
        assertEquals(v.getClass(), One.class, "It is instance of One");
jtulach@790
   163
        One o = (One)v;
jtulach@790
   164
        assertTrue(o.pb.isReadOnly(), "Mutable property");
jtulach@790
   165
    }
jtulach@790
   166
    
jtulach@790
   167
    @Test public void changeSex() {
jtulach@790
   168
        Person p = Models.bind(new Person(), c);
jtulach@790
   169
        p.setFirstName("Trans");
jtulach@790
   170
        p.setSex(Sex.MALE);
jtulach@790
   171
        
jtulach@790
   172
        Map m = (Map)Models.toRaw(p);
jtulach@790
   173
        Object o = m.get("changeSex");
jtulach@790
   174
        assertNotNull(o, "Function registered in the model");
jtulach@790
   175
        assertEquals(o.getClass(), One.class);
jtulach@790
   176
        
jtulach@790
   177
        One one = (One)o;
jtulach@790
   178
        assertNotNull(one.fb, "Function binding specified");
jtulach@790
   179
        
jtulach@790
   180
        one.fb.call(null, null);
jtulach@790
   181
        
jtulach@790
   182
        assertEquals(p.getSex(), Sex.FEMALE, "Changed");
jtulach@790
   183
    }
jtulach@790
   184
    
jtulach@790
   185
    @Test public void setSex() {
jtulach@790
   186
        Person p = Models.bind(new Person(), c);
jtulach@790
   187
        p.setFirstName("Trans");
jtulach@790
   188
        
jtulach@790
   189
        Map m = (Map)Models.toRaw(p);
jtulach@790
   190
        Object o = m.get("changeSex");
jtulach@790
   191
        assertNotNull(o, "Function registered in the model");
jtulach@790
   192
        assertEquals(o.getClass(), One.class);
jtulach@790
   193
        
jtulach@790
   194
        One one = (One)o;
jtulach@790
   195
        assertNotNull(one.fb, "Function binding specified");
jtulach@790
   196
        
jtulach@790
   197
        one.fb.call("FEMALE", new Object());
jtulach@790
   198
        
jtulach@790
   199
        assertEquals(p.getSex(), Sex.FEMALE, "Changed");
jtulach@808
   200
    }
jtulach@808
   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");
jaroslav@9
   218
    }
jaroslav@9
   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@878
   231
        
jtulach@878
   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@878
   255
        assertEquals(o.getClass(), One.class);
jtulach@878
   256
        One one = (One)o;
jtulach@878
   257
        
jtulach@878
   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@878
   274
    }
jtulach@790
   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
    
jtulach@790
   307
    static final class One {
jtulach@790
   308
        int changes;
jtulach@790
   309
        final PropertyBinding pb;
jtulach@790
   310
        final FunctionBinding fb;
jtulach@790
   311
    
jtulach@790
   312
        One(Object m, PropertyBinding pb) throws NoSuchMethodException {
jtulach@790
   313
            this.pb = pb;
jtulach@790
   314
            this.fb = null;
jtulach@790
   315
        }
jtulach@790
   316
        One(Object m, FunctionBinding fb) throws NoSuchMethodException {
jtulach@790
   317
            this.pb = null;
jtulach@790
   318
            this.fb = fb;
jtulach@790
   319
        }
jtulach@790
   320
        
jtulach@790
   321
        Object get() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
jtulach@790
   322
            return pb.getValue();
jtulach@790
   323
        }
jtulach@790
   324
        
jtulach@790
   325
        void set(Object v) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
jtulach@790
   326
            pb.setValue(v);
jtulach@808
   327
        }
jtulach@805
   328
    }
jtulach@790
   329
    
jtulach@790
   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
jtulach@790
   335
        @Override
jtulach@790
   336
        public Map<String, One> wrapModel(Object model) {
jtulach@790
   337
            return new HashMap<String, One>();
jtulach@790
   338
        }
jtulach@790
   339
jtulach@790
   340
        @Override
jtulach@790
   341
        public void valueHasMutated(Map<String, One> data, String propertyName) {
jtulach@790
   342
            One p = data.get(propertyName);
jtulach@790
   343
            if (p != null) {
jtulach@790
   344
                p.changes++;
jtulach@790
   345
            }
jtulach@790
   346
        }
jtulach@790
   347
jtulach@790
   348
        @Override
jtulach@790
   349
        public void bind(PropertyBinding b, Object model, Map<String, One> data) {
jtulach@790
   350
            try {
jtulach@790
   351
                One o = new One(model, b);
jtulach@790
   352
                data.put(b.getPropertyName(), o);
jtulach@790
   353
            } catch (NoSuchMethodException ex) {
jtulach@790
   354
                throw new IllegalStateException(ex);
jtulach@790
   355
            }
jtulach@790
   356
        }
jtulach@790
   357
jtulach@790
   358
        @Override
jtulach@790
   359
        public void expose(FunctionBinding fb, Object model, Map<String, One> data) {
jtulach@790
   360
            try {
jtulach@790
   361
                data.put(fb.getFunctionName(), new One(model, fb));
jtulach@790
   362
            } catch (NoSuchMethodException ex) {
jtulach@790
   363
                throw new IllegalStateException(ex);
jtulach@790
   364
            }
jtulach@790
   365
        }
jtulach@790
   366
jtulach@790
   367
        @Override
jtulach@790
   368
        public void applyBindings(Map<String, One> data) {
jtulach@920
   369
            throw new UnsupportedOperationException("Never called!");
jtulach@790
   370
        }
jtulach@790
   371
jtulach@790
   372
        @Override
jtulach@790
   373
        public Object wrapArray(Object[] arr) {
jtulach@790
   374
            return arr;
jtulach@790
   375
        }
jtulach@790
   376
jtulach@790
   377
        @Override
jtulach@790
   378
        public void extract(Object obj, String[] props, Object[] values) {
jtulach@790
   379
            Map<?,?> map = obj instanceof Map ? (Map<?,?>)obj : null;
jtulach@790
   380
            for (int i = 0; i < Math.min(props.length, values.length); i++) {
jtulach@790
   381
                if (map == null) {
jtulach@790
   382
                    values[i] = null;
jtulach@790
   383
                } else {
jtulach@790
   384
                    values[i] = map.get(props[i]);
jtulach@790
   385
                    if (values[i] instanceof One) {
jtulach@790
   386
                        values[i] = ((One)values[i]).pb.getValue();
jtulach@790
   387
                    }
jtulach@790
   388
                }
jtulach@790
   389
            }
jtulach@790
   390
        }
jtulach@790
   391
jtulach@790
   392
        @Override
jtulach@790
   393
        public void loadJSON(JSONCall call) {
jtulach@790
   394
            call.notifyError(new UnsupportedOperationException());
jtulach@790
   395
        }
jtulach@790
   396
jtulach@790
   397
        @Override
jtulach@790
   398
        public <M> M toModel(Class<M> modelClass, Object data) {
jtulach@790
   399
            return modelClass.cast(data);
jtulach@790
   400
        }
jtulach@790
   401
jtulach@790
   402
        @Override
jtulach@790
   403
        public Object toJSON(InputStream is) throws IOException {
jtulach@790
   404
            throw new IOException();
jtulach@790
   405
        }
jtulach@790
   406
jtulach@790
   407
        @Override
jtulach@790
   408
        public void runSafe(Runnable r) {
jtulach@790
   409
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
jtulach@790
   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;
jaroslav@240
   416
        }
jaroslav@9
   417
    }
jaroslav@9
   418
}