javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ModelTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 19 Feb 2013 15:42:41 +0100
branchmodel
changeset 766 06d89ffe489e
parent 764 605791f059b0
child 770 26513bd377b9
permissions -rw-r--r--
Derived properties can return list
     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 = "Model", 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 })
    43 public class ModelTest {
    44     private Model model;
    45     private static Model leakedModel;
    46     
    47     @BeforeMethod
    48     public void createModel() {
    49         model = new Model();
    50     }
    51     
    52     @Test public void classGeneratedWithSetterGetter() {
    53         model.setValue(10);
    54         assertEquals(10, model.getValue(), "Value changed");
    55     }
    56     
    57     @Test public void computedMethod() {
    58         model.setValue(4);
    59         assertEquals(16, model.getPowerValue());
    60     }
    61     
    62     @Test public void arrayIsMutable() {
    63         assertEquals(model.getNames().size(), 0, "Is empty");
    64         model.getNames().add("Jarda");
    65         assertEquals(model.getNames().size(), 1, "One element");
    66     }
    67     
    68     @Test public void arrayChangesNotified() {
    69         MockKnockout my = new MockKnockout();
    70         MockKnockout.next = my;
    71         
    72         model.applyBindings();
    73         
    74         model.getNames().add("Hello");
    75         
    76         assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
    77         assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
    78 
    79         my.mutated.clear();
    80         
    81         Iterator<String> it = model.getNames().iterator();
    82         assertEquals(it.next(), "Hello");
    83         it.remove();
    84         
    85         assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
    86         assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
    87 
    88         my.mutated.clear();
    89         
    90         ListIterator<String> lit = model.getNames().listIterator();
    91         lit.add("Jarda");
    92         
    93         assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
    94         assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
    95     }
    96     
    97     @Test public void autoboxedArray() {
    98         MockKnockout my = new MockKnockout();
    99         MockKnockout.next = my;
   100         
   101         model.applyBindings();
   102         
   103         model.getValues().add(10);
   104         
   105         assertEquals(model.getValues().get(0), Integer.valueOf(10), "Really ten");
   106     }
   107 
   108     @Test public void derivedArrayProp() {
   109         MockKnockout my = new MockKnockout();
   110         MockKnockout.next = my;
   111         
   112         model.applyBindings();
   113         
   114         model.setCount(10);
   115         
   116         List<String> arr = model.getRepeat();
   117         assertEquals(arr.size(), 10, "Ten items: " + arr);
   118         
   119         my.mutated.clear();
   120         
   121         model.setCount(5);
   122         
   123         arr = model.getRepeat();
   124         assertEquals(arr.size(), 5, "Five items: " + arr);
   125 
   126         assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
   127         assertTrue(my.mutated.contains("repeat"), "Array is in there: " + my.mutated);
   128         assertTrue(my.mutated.contains("count"), "Count is in there: " + my.mutated);
   129     }
   130     
   131     @Test public void derivedPropertiesAreNotified() {
   132         MockKnockout my = new MockKnockout();
   133         MockKnockout.next = my;
   134         
   135         model.applyBindings();
   136         
   137         model.setValue(33);
   138         
   139         assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
   140         assertTrue(my.mutated.contains("powerValue"), "Power value is in there: " + my.mutated);
   141         assertTrue(my.mutated.contains("value"), "Simple value is in there: " + my.mutated);
   142         
   143         my.mutated.clear();
   144         
   145         model.setUnrelated(44);
   146         assertEquals(my.mutated.size(), 1, "One property changed");
   147         assertTrue(my.mutated.contains("unrelated"), "Its name is unrelated");
   148     }
   149     
   150     @Test public void computedPropertyCannotWriteToModel() {
   151         leakedModel = model;
   152         try {
   153             String res = model.getNotAllowedWrite();
   154             fail("We should not be allowed to write to the model: " + res);
   155         } catch (IllegalStateException ex) {
   156             // OK, we can't read
   157         }
   158     }
   159 
   160     @Test public void computedPropertyCannotReadToModel() {
   161         leakedModel = model;
   162         try {
   163             String res = model.getNotAllowedRead();
   164             fail("We should not be allowed to read from the model: " + res);
   165         } catch (IllegalStateException ex) {
   166             // OK, we can't read
   167         }
   168     }
   169     
   170     @ComputedProperty
   171     static int powerValue(int value) {
   172         return value * value;
   173     }
   174     
   175     @ComputedProperty
   176     static String notAllowedRead() {
   177         return "Not allowed callback: " + leakedModel.getUnrelated();
   178     }
   179 
   180     @ComputedProperty
   181     static String notAllowedWrite() {
   182         leakedModel.setUnrelated(11);
   183         return "Not allowed callback!";
   184     }
   185     
   186     @ComputedProperty
   187     static List<String> repeat(int count) {
   188         return Collections.nCopies(count, "Hello");
   189     }
   190     
   191     static class MockKnockout extends Knockout {
   192         List<String> mutated = new ArrayList<String>();
   193         
   194         @Override
   195         public void valueHasMutated(String prop) {
   196             mutated.add(prop);
   197         }
   198     }
   199 }