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.
     1 /**
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     5  *
     6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
     7  * Other names may be trademarks of their respective owners.
     8  *
     9  * The contents of this file are subject to the terms of either the GNU
    10  * General Public License Version 2 only ("GPL") or the Common
    11  * Development and Distribution License("CDDL") (collectively, the
    12  * "License"). You may not use this file except in compliance with the
    13  * License. You can obtain a copy of the License at
    14  * http://www.netbeans.org/cddl-gplv2.html
    15  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    16  * specific language governing permissions and limitations under the
    17  * License.  When distributing the software, include this License Header
    18  * Notice in each file and include the License file at
    19  * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    20  * particular file as subject to the "Classpath" exception as provided
    21  * by Oracle in the GPL Version 2 section of the License file that
    22  * accompanied this code. If applicable, add the following below the
    23  * License Header, with the fields enclosed by brackets [] replaced by
    24  * your own identifying information:
    25  * "Portions Copyrighted [year] [name of copyright owner]"
    26  *
    27  * Contributor(s):
    28  *
    29  * The Original Software is NetBeans. The Initial Developer of the Original
    30  * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    31  *
    32  * If you wish your version of this file to be governed by only the CDDL
    33  * or only the GPL Version 2, indicate your decision by adding
    34  * "[Contributor] elects to include this software in this distribution
    35  * under the [CDDL or GPL Version 2] license." If you do not indicate a
    36  * single choice of license, a recipient has the option to distribute
    37  * your version of this file under either the CDDL, the GPL Version 2 or
    38  * to extend the choice of license to its licensees as provided above.
    39  * However, if you add GPL Version 2 code and therefore, elected the GPL
    40  * Version 2 license, then the option applies only if the new code is
    41  * made subject to such option by the copyright holder.
    42  */
    43 package net.java.html.json;
    44 
    45 import net.java.html.BrwsrCtx;
    46 import java.io.IOException;
    47 import java.io.InputStream;
    48 import java.lang.reflect.InvocationTargetException;
    49 import java.util.HashMap;
    50 import java.util.Map;
    51 import org.netbeans.html.context.spi.Contexts;
    52 import org.netbeans.html.json.spi.FunctionBinding;
    53 import org.netbeans.html.json.spi.JSONCall;
    54 import org.netbeans.html.json.spi.PropertyBinding;
    55 import org.netbeans.html.json.spi.Technology;
    56 import org.netbeans.html.json.spi.Transfer;
    57 import org.testng.annotations.BeforeMethod;
    58 import org.testng.annotations.Test;
    59 import static org.testng.Assert.*;
    60 
    61 /**
    62  *
    63  * @author Jaroslav Tulach
    64  */
    65 public class MapModelTest {
    66     private MapTechnology t;
    67     private BrwsrCtx c;
    68 
    69     @BeforeMethod public void initTechnology() {
    70         t = new MapTechnology();
    71         c = Contexts.newBuilder().register(Technology.class, t, 1).
    72             register(Transfer.class, t, 1).build();
    73     }
    74     
    75     @Test public void isThereABinding() throws Exception {
    76         Person p = Models.bind(new Person(), c).applyBindings();
    77         p.setFirstName("Jarda");
    78         
    79         Map m = (Map)Models.toRaw(p);
    80         Object v = m.get("firstName");
    81         assertNotNull(v, "Value should be in the map");
    82         assertEquals(v.getClass(), One.class, "It is instance of One");
    83         One o = (One)v;
    84         assertEquals(o.changes, 1, "One change so far");
    85         assertFalse(o.pb.isReadOnly(), "Mutable property");
    86         
    87         assertEquals(o.get(), "Jarda", "Value should be in the map");
    88         
    89         o.set("Karle");
    90         
    91         assertEquals(p.getFirstName(), "Karle", "Model value updated");
    92         assertEquals(o.changes, 2, "Snd change");
    93     }
    94     
    95     
    96     @Test public void dontNotifySameProperty() throws Exception {
    97         Person p = Models.bind(new Person(), c);
    98         p.setFirstName("Jirka");
    99         
   100         Map m = (Map)Models.toRaw(p);
   101         Object v = m.get("firstName");
   102         assertNotNull(v, "Value should be in the map");
   103         assertEquals(v.getClass(), One.class, "It is instance of One");
   104         One o = (One)v;
   105         assertEquals(o.changes, 0, "No change so far the only one change happened before we connected");
   106         
   107         p.setFirstName(new String("Jirka"));
   108         assertEquals(o.changes, 0, "No change so far, the value is the same");
   109         
   110         p.setFirstName("Jarda");
   111         assertFalse(o.pb.isReadOnly(), "Mutable property");
   112         
   113         assertEquals(o.get(), "Jarda", "Value should be in the map");
   114         
   115         o.set("Karle");
   116         
   117         assertEquals(p.getFirstName(), "Karle", "Model value updated");
   118         assertEquals(o.changes, 2, "Snd change");
   119     }
   120     
   121     @Test public void canSetEnumAsString() throws Exception {
   122         Person p = Models.bind(new Person(), c);
   123         p.setFirstName("Jirka");
   124         p.setSex(Sex.MALE);
   125         
   126         Map m = (Map)Models.toRaw(p);
   127         Object v = m.get("sex");
   128         assertNotNull(v, "Value should be in the map");
   129         assertEquals(v.getClass(), One.class, "It is instance of One");
   130         One o = (One)v;
   131         
   132         o.set("FEMALE");
   133 
   134         assertEquals(p.getSex(), Sex.FEMALE, "Changed to female");
   135     }
   136     
   137     @Test public void derivedProperty() throws Exception {
   138         Person p = Models.bind(new Person(), c);
   139         
   140         Map m = (Map)Models.toRaw(p);
   141         Object v = m.get("fullName");
   142         assertNotNull(v, "Value should be in the map");
   143         assertEquals(v.getClass(), One.class, "It is instance of One");
   144         One o = (One)v;
   145         assertTrue(o.pb.isReadOnly(), "Mutable property");
   146     }
   147     
   148     @Test public void changeSex() {
   149         Person p = Models.bind(new Person(), c);
   150         p.setFirstName("Trans");
   151         p.setSex(Sex.MALE);
   152         
   153         Map m = (Map)Models.toRaw(p);
   154         Object o = m.get("changeSex");
   155         assertNotNull(o, "Function registered in the model");
   156         assertEquals(o.getClass(), One.class);
   157         
   158         One one = (One)o;
   159         assertNotNull(one.fb, "Function binding specified");
   160         
   161         one.fb.call(null, null);
   162         
   163         assertEquals(p.getSex(), Sex.FEMALE, "Changed");
   164     }
   165     
   166     @Test public void setSex() {
   167         Person p = Models.bind(new Person(), c);
   168         p.setFirstName("Trans");
   169         
   170         Map m = (Map)Models.toRaw(p);
   171         Object o = m.get("changeSex");
   172         assertNotNull(o, "Function registered in the model");
   173         assertEquals(o.getClass(), One.class);
   174         
   175         One one = (One)o;
   176         assertNotNull(one.fb, "Function binding specified");
   177         
   178         one.fb.call("FEMALE", new Object());
   179         
   180         assertEquals(p.getSex(), Sex.FEMALE, "Changed");
   181     }
   182 
   183     static final class One {
   184         int changes;
   185         final PropertyBinding pb;
   186         final FunctionBinding fb;
   187     
   188         One(Object m, PropertyBinding pb) throws NoSuchMethodException {
   189             this.pb = pb;
   190             this.fb = null;
   191         }
   192         One(Object m, FunctionBinding fb) throws NoSuchMethodException {
   193             this.pb = null;
   194             this.fb = fb;
   195         }
   196         
   197         Object get() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
   198             return pb.getValue();
   199         }
   200         
   201         void set(Object v) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
   202             pb.setValue(v);
   203         }
   204     }
   205     
   206     static final class MapTechnology 
   207     implements Technology<Map<String,One>>, Transfer {
   208 
   209         @Override
   210         public Map<String, One> wrapModel(Object model) {
   211             return new HashMap<String, One>();
   212         }
   213 
   214         @Override
   215         public void valueHasMutated(Map<String, One> data, String propertyName) {
   216             One p = data.get(propertyName);
   217             if (p != null) {
   218                 p.changes++;
   219             }
   220         }
   221 
   222         @Override
   223         public void bind(PropertyBinding b, Object model, Map<String, One> data) {
   224             try {
   225                 One o = new One(model, b);
   226                 data.put(b.getPropertyName(), o);
   227             } catch (NoSuchMethodException ex) {
   228                 throw new IllegalStateException(ex);
   229             }
   230         }
   231 
   232         @Override
   233         public void expose(FunctionBinding fb, Object model, Map<String, One> data) {
   234             try {
   235                 data.put(fb.getFunctionName(), new One(model, fb));
   236             } catch (NoSuchMethodException ex) {
   237                 throw new IllegalStateException(ex);
   238             }
   239         }
   240 
   241         @Override
   242         public void applyBindings(Map<String, One> data) {
   243         }
   244 
   245         @Override
   246         public Object wrapArray(Object[] arr) {
   247             return arr;
   248         }
   249 
   250         @Override
   251         public void extract(Object obj, String[] props, Object[] values) {
   252             Map<?,?> map = obj instanceof Map ? (Map<?,?>)obj : null;
   253             for (int i = 0; i < Math.min(props.length, values.length); i++) {
   254                 if (map == null) {
   255                     values[i] = null;
   256                 } else {
   257                     values[i] = map.get(props[i]);
   258                     if (values[i] instanceof One) {
   259                         values[i] = ((One)values[i]).pb.getValue();
   260                     }
   261                 }
   262             }
   263         }
   264 
   265         @Override
   266         public void loadJSON(JSONCall call) {
   267             call.notifyError(new UnsupportedOperationException());
   268         }
   269 
   270         @Override
   271         public <M> M toModel(Class<M> modelClass, Object data) {
   272             return modelClass.cast(data);
   273         }
   274 
   275         @Override
   276         public Object toJSON(InputStream is) throws IOException {
   277             throw new IOException();
   278         }
   279 
   280         @Override
   281         public void runSafe(Runnable r) {
   282             throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
   283         }
   284     }
   285 }