javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ModelTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 22 Feb 2013 08:59:40 +0100
branchmodel
changeset 770 26513bd377b9
parent 766 06d89ffe489e
child 879 af170d42b5b3
permissions -rw-r--r--
Introducing @Model for complex record-like types
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.htmlpage;
    19 
    20 import java.util.ArrayList;
    21 import java.util.Collections;
    22 import java.util.Iterator;
    23 import java.util.List;
    24 import java.util.ListIterator;
    25 import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
    26 import org.apidesign.bck2brwsr.htmlpage.api.Page;
    27 import org.apidesign.bck2brwsr.htmlpage.api.Property;
    28 import static org.testng.Assert.*;
    29 import org.testng.annotations.BeforeMethod;
    30 import org.testng.annotations.Test;
    31 
    32 /**
    33  *
    34  * @author Jaroslav Tulach <jtulach@netbeans.org>
    35  */
    36 @Page(xhtml = "Empty.html", className = "Modelik", properties = {
    37     @Property(name = "value", type = int.class),
    38     @Property(name = "count", type = int.class),
    39     @Property(name = "unrelated", type = long.class),
    40     @Property(name = "names", type = String.class, array = true),
    41     @Property(name = "values", type = int.class, array = true),
    42     @Property(name = "people", type = PersonImpl.class, array = true)
    43 })
    44 public class ModelTest {
    45     private Modelik model;
    46     private static Modelik leakedModel;
    47     
    48     @BeforeMethod
    49     public void createModel() {
    50         model = new Modelik();
    51     }
    52     
    53     @Test public void classGeneratedWithSetterGetter() {
    54         model.setValue(10);
    55         assertEquals(10, model.getValue(), "Value changed");
    56     }
    57     
    58     @Test public void computedMethod() {
    59         model.setValue(4);
    60         assertEquals(16, model.getPowerValue());
    61     }
    62     
    63     @Test public void arrayIsMutable() {
    64         assertEquals(model.getNames().size(), 0, "Is empty");
    65         model.getNames().add("Jarda");
    66         assertEquals(model.getNames().size(), 1, "One element");
    67     }
    68     
    69     @Test public void arrayChangesNotified() {
    70         MockKnockout my = new MockKnockout();
    71         MockKnockout.next = my;
    72         
    73         model.applyBindings();
    74         
    75         model.getNames().add("Hello");
    76         
    77         assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
    78         assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
    79 
    80         my.mutated.clear();
    81         
    82         Iterator<String> it = model.getNames().iterator();
    83         assertEquals(it.next(), "Hello");
    84         it.remove();
    85         
    86         assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
    87         assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
    88 
    89         my.mutated.clear();
    90         
    91         ListIterator<String> lit = model.getNames().listIterator();
    92         lit.add("Jarda");
    93         
    94         assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
    95         assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
    96     }
    97     
    98     @Test public void autoboxedArray() {
    99         MockKnockout my = new MockKnockout();
   100         MockKnockout.next = my;
   101         
   102         model.applyBindings();
   103         
   104         model.getValues().add(10);
   105         
   106         assertEquals(model.getValues().get(0), Integer.valueOf(10), "Really ten");
   107     }
   108 
   109     @Test public void derivedArrayProp() {
   110         MockKnockout my = new MockKnockout();
   111         MockKnockout.next = my;
   112         
   113         model.applyBindings();
   114         
   115         model.setCount(10);
   116         
   117         List<String> arr = model.getRepeat();
   118         assertEquals(arr.size(), 10, "Ten items: " + arr);
   119         
   120         my.mutated.clear();
   121         
   122         model.setCount(5);
   123         
   124         arr = model.getRepeat();
   125         assertEquals(arr.size(), 5, "Five items: " + arr);
   126 
   127         assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
   128         assertTrue(my.mutated.contains("repeat"), "Array is in there: " + my.mutated);
   129         assertTrue(my.mutated.contains("count"), "Count is in there: " + my.mutated);
   130     }
   131     
   132     @Test public void derivedPropertiesAreNotified() {
   133         MockKnockout my = new MockKnockout();
   134         MockKnockout.next = my;
   135         
   136         model.applyBindings();
   137         
   138         model.setValue(33);
   139         
   140         assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
   141         assertTrue(my.mutated.contains("powerValue"), "Power value is in there: " + my.mutated);
   142         assertTrue(my.mutated.contains("value"), "Simple value is in there: " + my.mutated);
   143         
   144         my.mutated.clear();
   145         
   146         model.setUnrelated(44);
   147         assertEquals(my.mutated.size(), 1, "One property changed");
   148         assertTrue(my.mutated.contains("unrelated"), "Its name is unrelated");
   149     }
   150     
   151     @Test public void computedPropertyCannotWriteToModel() {
   152         leakedModel = model;
   153         try {
   154             String res = model.getNotAllowedWrite();
   155             fail("We should not be allowed to write to the model: " + res);
   156         } catch (IllegalStateException ex) {
   157             // OK, we can't read
   158         }
   159     }
   160 
   161     @Test public void computedPropertyCannotReadToModel() {
   162         leakedModel = model;
   163         try {
   164             String res = model.getNotAllowedRead();
   165             fail("We should not be allowed to read from the model: " + res);
   166         } catch (IllegalStateException ex) {
   167             // OK, we can't read
   168         }
   169     }
   170     
   171     @ComputedProperty
   172     static int powerValue(int value) {
   173         return value * value;
   174     }
   175     
   176     @ComputedProperty
   177     static String notAllowedRead() {
   178         return "Not allowed callback: " + leakedModel.getUnrelated();
   179     }
   180 
   181     @ComputedProperty
   182     static String notAllowedWrite() {
   183         leakedModel.setUnrelated(11);
   184         return "Not allowed callback!";
   185     }
   186     
   187     @ComputedProperty
   188     static List<String> repeat(int count) {
   189         return Collections.nCopies(count, "Hello");
   190     }
   191     
   192     static class MockKnockout extends Knockout {
   193         List<String> mutated = new ArrayList<>();
   194         
   195         @Override
   196         public void valueHasMutated(String prop) {
   197             mutated.add(prop);
   198         }
   199     }
   200     
   201     public @Test void hasPersonPropertyAndComputedFullName() {
   202         List<Person> arr = model.getPeople();
   203         assertEquals(arr.size(), 0, "By default empty");
   204         Person p = null;
   205         if (p != null) {
   206             String fullNameGenerated = p.getFullName();
   207             assertNotNull(fullNameGenerated);
   208         }
   209     }
   210 }