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