json/src/test/java/net/java/html/json/ModelTest.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Thu, 30 Jan 2014 11:36:32 +0100
branchunion
changeset 506 4b66c5141f84
parent 453 5e90ec3cd61a
permissions -rw-r--r--
Property changes are delivered when in-a union property changes
     1 /**
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2013-2013 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.util.ArrayList;
    47 import java.util.Collections;
    48 import java.util.Iterator;
    49 import java.util.List;
    50 import java.util.ListIterator;
    51 import org.apidesign.html.context.spi.Contexts;
    52 import org.apidesign.html.json.spi.FunctionBinding;
    53 import org.apidesign.html.json.spi.PropertyBinding;
    54 import org.apidesign.html.json.spi.Technology;
    55 import static org.testng.Assert.*;
    56 import org.testng.annotations.BeforeMethod;
    57 import org.testng.annotations.Test;
    58 
    59 /**
    60  *
    61  * @author Jaroslav Tulach <jtulach@netbeans.org>
    62  */
    63 @Model(className = "Modelik", properties = {
    64     @Property(name = "value", type = int.class),
    65     @Property(name = "count", type = int.class),
    66     @Property(name = "unrelated", type = long.class),
    67     @Property(name = "names", type = String.class, array = true),
    68     @Property(name = "values", type = int.class, array = true),
    69     @Property(name = "people", type = Person.class, array = true),
    70     @Property(name = "changedProperty", type=String.class)
    71 })
    72 public class ModelTest {
    73     private MockTechnology my;
    74     private Modelik model;
    75     private static Modelik leakedModel;
    76     
    77     @BeforeMethod
    78     public void createModel() {
    79         my = new MockTechnology();
    80         final BrwsrCtx c = Contexts.newBuilder().register(Technology.class, my, 1).build();
    81         model = Models.bind(new Modelik(), c);
    82     }
    83     
    84     @Test public void classGeneratedWithSetterGetter() {
    85         model.setValue(10);
    86         assertEquals(10, model.getValue(), "Value changed");
    87     }
    88     
    89     @Test public void computedMethod() {
    90         model.setValue(4);
    91         assertEquals(16, model.getPowerValue());
    92     }
    93     
    94     @Test public void equalsAndHashCode() {
    95         Modelik m1 = new Modelik(10, 20, 30, "changed", "firstName");
    96         Modelik m2 = new Modelik(10, 20, 30, "changed", "firstName");
    97         
    98         assertTrue(m1.equals(m2), "They are the same");
    99         assertEquals(m1.hashCode(), m2.hashCode(), "Hashcode is the same");
   100         
   101         m1.setCount(33);
   102         
   103         assertFalse(m1.equals(m2), "No longer the same");
   104         assertFalse(m1.hashCode() == m2.hashCode(), "No longe is hashcode is the same");
   105     }
   106     
   107     @Test public void arrayIsMutable() {
   108         assertEquals(model.getNames().size(), 0, "Is empty");
   109         model.getNames().add("Jarda");
   110         assertEquals(model.getNames().size(), 1, "One element");
   111     }
   112 
   113     @Test public void arrayChangesNotNotifiedUntilInitied() {
   114         model.getNames().add("Hello");
   115         assertTrue(my.mutated.isEmpty(), "No change now " + my.mutated);
   116         model.getNames().remove("Hello");
   117         assertTrue(my.mutated.isEmpty(), "No change still " + my.mutated);
   118         assertTrue(model.getNames().isEmpty(), "No empty");
   119     }
   120     
   121     @Test public void arrayChangesNotified() {
   122         Models.applyBindings(model);
   123         model.getNames().add("Hello");
   124         
   125         assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
   126         assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
   127 
   128         my.mutated.clear();
   129         
   130         Iterator<String> it = model.getNames().iterator();
   131         assertEquals(it.next(), "Hello");
   132         it.remove();
   133         
   134         assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
   135         assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
   136 
   137         my.mutated.clear();
   138         
   139         ListIterator<String> lit = model.getNames().listIterator();
   140         lit.add("Jarda");
   141         
   142         assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
   143         assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
   144     }
   145 
   146     @Test public void autoboxedArray() {
   147         model.getValues().add(10);
   148         
   149         assertEquals(model.getValues().get(0), Integer.valueOf(10), "Really ten");
   150     }
   151 
   152     @Test public void derivedArrayProp() {
   153         model.applyBindings();
   154         model.setCount(10);
   155         
   156         List<String> arr = model.getRepeat();
   157         assertEquals(arr.size(), 10, "Ten items: " + arr);
   158         
   159         my.mutated.clear();
   160         
   161         model.setCount(5);
   162         
   163         arr = model.getRepeat();
   164         assertEquals(arr.size(), 5, "Five items: " + arr);
   165 
   166         assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
   167         assertTrue(my.mutated.contains("repeat"), "Array is in there: " + my.mutated);
   168         assertTrue(my.mutated.contains("count"), "Count is in there: " + my.mutated);
   169     }
   170     
   171     @Test public void derivedPropertiesAreNotified() {
   172         model.applyBindings();
   173         
   174         model.setValue(33);
   175         
   176         // not interested in change of this property
   177         my.mutated.remove("changedProperty");
   178         my.mutated.remove("changedProperty");
   179         
   180         assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
   181         assertTrue(my.mutated.contains("powerValue"), "Power value is in there: " + my.mutated);
   182         assertTrue(my.mutated.contains("value"), "Simple value is in there: " + my.mutated);
   183         
   184         my.mutated.clear();
   185         
   186         model.setUnrelated(44);
   187         
   188         
   189         // not interested in change of this property
   190         my.mutated.remove("changedProperty");
   191         assertEquals(my.mutated.size(), 1, "One property changed: " + my.mutated);
   192         assertTrue(my.mutated.contains("unrelated"), "Its name is unrelated");
   193     }
   194 
   195     @Test public void computedPropertyCannotWriteToModel() {
   196         leakedModel = model;
   197         try {
   198             String res = model.getNotAllowedWrite();
   199             fail("We should not be allowed to write to the model: " + res);
   200         } catch (IllegalStateException ex) {
   201             // OK, we can't read
   202         }
   203     }
   204 
   205     @Test public void computedPropertyCannotReadToModel() {
   206         leakedModel = model;
   207         try {
   208             String res = model.getNotAllowedRead();
   209             fail("We should not be allowed to read from the model: " + res);
   210         } catch (IllegalStateException ex) {
   211             // OK, we can't read
   212         }
   213     }
   214     
   215     @OnReceive(url = "{protocol}://{host}?query={query}", data = Person.class, onError = "errorState")
   216     static void loadPeople(Modelik thiz, People p) {
   217         Modelik m = null;
   218         m.applyBindings();
   219         m.loadPeople("http", "apidesign.org", "query", new Person());
   220     }
   221     
   222     static void errorState(Modelik thiz, Exception ex) {
   223         
   224     }
   225 
   226     @OnReceive(url = "{protocol}://{host}?callback={back}&query={query}", jsonp = "back")
   227     static void loadPeopleViaJSONP(Modelik thiz, People p) {
   228         Modelik m = null;
   229         m.applyBindings();
   230         m.loadPeopleViaJSONP("http", "apidesign.org", "query");
   231     }
   232     
   233     @Function 
   234     static void doSomething() {
   235     }
   236     
   237     @ComputedProperty
   238     static int powerValue(int value) {
   239         return value * value;
   240     }
   241     
   242     @OnPropertyChange({ "powerValue", "unrelated" })
   243     static void aPropertyChanged(Modelik m, String name) {
   244         m.setChangedProperty(name);
   245     }
   246 
   247     @OnPropertyChange({ "values" })
   248     static void anArrayPropertyChanged(String name, Modelik m) {
   249         m.setChangedProperty(name);
   250     }
   251     
   252     @Test public void changeAnything() {
   253         model.setCount(44);
   254         assertNull(model.getChangedProperty(), "No observed value change");
   255     }
   256     @Test public void changeValue() {
   257         model.setValue(33);
   258         assertEquals(model.getChangedProperty(), "powerValue", "power property changed");
   259     }
   260     @Test public void changeUnrelated() {
   261         model.setUnrelated(333);
   262         assertEquals(model.getChangedProperty(), "unrelated", "unrelated changed");
   263     }
   264 
   265     @Test public void changeInArray() {
   266         model.getValues().add(10);
   267         assertNull(model.getChangedProperty(), "No change before applyBindings");
   268         model.applyBindings();
   269         model.getValues().add(10);
   270         assertEquals(model.getChangedProperty(), "values", "Something added into the array");
   271     }
   272     
   273     @ComputedProperty
   274     static String notAllowedRead() {
   275         return "Not allowed callback: " + leakedModel.getUnrelated();
   276     }
   277 
   278     @ComputedProperty
   279     static String notAllowedWrite() {
   280         leakedModel.setUnrelated(11);
   281         return "Not allowed callback!";
   282     }
   283     
   284     @ComputedProperty
   285     static List<String> repeat(int count) {
   286         return Collections.nCopies(count, "Hello");
   287     }
   288     
   289     public @Test void hasPersonPropertyAndComputedFullName() {
   290         List<Person> arr = model.getPeople();
   291         assertEquals(arr.size(), 0, "By default empty");
   292         Person p = null;
   293         if (p != null) {
   294             String fullNameGenerated = p.getFullName();
   295             assertNotNull(fullNameGenerated);
   296         }
   297     }
   298     
   299     private static class MockTechnology implements Technology<Object> {
   300         private final List<String> mutated = new ArrayList<String>();
   301 
   302         @Override
   303         public Object wrapModel(Object model) {
   304             return this;
   305         }
   306 
   307         @Override
   308         public void valueHasMutated(Object data, String propertyName) {
   309             mutated.add(propertyName);
   310         }
   311 
   312         @Override
   313         public void bind(PropertyBinding b, Object model, Object data) {
   314         }
   315 
   316         @Override
   317         public void expose(FunctionBinding fb, Object model, Object d) {
   318         }
   319 
   320         @Override
   321         public void applyBindings(Object data) {
   322         }
   323 
   324         @Override
   325         public Object wrapArray(Object[] arr) {
   326             return arr;
   327         }
   328 
   329         @Override
   330         public <M> M toModel(Class<M> modelClass, Object data) {
   331             return modelClass.cast(data);
   332         }
   333 
   334         @Override
   335         public void runSafe(Runnable r) {
   336             throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
   337         }
   338     }
   339 }