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