javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ModelTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 18 Feb 2013 13:03:01 +0100
branchmodel
changeset 761 ade90921ede5
parent 760 4bd6f3bc6c64
child 764 605791f059b0
permissions -rw-r--r--
Changes in String array are properly notified
     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.Iterator;
    22 import java.util.List;
    23 import java.util.ListIterator;
    24 import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
    25 import org.apidesign.bck2brwsr.htmlpage.api.Page;
    26 import org.apidesign.bck2brwsr.htmlpage.api.Property;
    27 import static org.testng.Assert.*;
    28 import org.testng.annotations.BeforeMethod;
    29 import org.testng.annotations.Test;
    30 
    31 /**
    32  *
    33  * @author Jaroslav Tulach <jtulach@netbeans.org>
    34  */
    35 @Page(xhtml = "Empty.html", className = "Model", properties = {
    36     @Property(name = "value", type = int.class),
    37     @Property(name = "unrelated", type = long.class),
    38     @Property(name = "names", type = String.class, array = true)
    39 })
    40 public class ModelTest {
    41     private Model model;
    42     private static Model leakedModel;
    43     
    44     @BeforeMethod
    45     public void createModel() {
    46         model = new Model();
    47     }
    48     
    49     @Test public void classGeneratedWithSetterGetter() {
    50         model.setValue(10);
    51         assertEquals(10, model.getValue(), "Value changed");
    52     }
    53     
    54     @Test public void computedMethod() {
    55         model.setValue(4);
    56         assertEquals(16, model.getPowerValue());
    57     }
    58     
    59     @Test public void arrayIsMutable() {
    60         assertEquals(model.getNames().size(), 0, "Is empty");
    61         model.getNames().add("Jarda");
    62         assertEquals(model.getNames().size(), 1, "One element");
    63     }
    64     
    65     @Test public void arrayChangesNotified() {
    66         MockKnockout my = new MockKnockout();
    67         MockKnockout.next = my;
    68         
    69         model.applyBindings();
    70         
    71         model.getNames().add("Hello");
    72         
    73         assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
    74         assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
    75 
    76         my.mutated.clear();
    77         
    78         Iterator<String> it = model.getNames().iterator();
    79         assertEquals(it.next(), "Hello");
    80         it.remove();
    81         
    82         assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
    83         assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
    84 
    85         my.mutated.clear();
    86         
    87         ListIterator<String> lit = model.getNames().listIterator();
    88         lit.add("Jarda");
    89         
    90         assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
    91         assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
    92     }
    93     
    94     @Test public void derivedPropertiesAreNotified() {
    95         MockKnockout my = new MockKnockout();
    96         MockKnockout.next = my;
    97         
    98         model.applyBindings();
    99         
   100         model.setValue(33);
   101         
   102         assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
   103         assertTrue(my.mutated.contains("powerValue"), "Power value is in there: " + my.mutated);
   104         assertTrue(my.mutated.contains("value"), "Simple value is in there: " + my.mutated);
   105         
   106         my.mutated.clear();
   107         
   108         model.setUnrelated(44);
   109         assertEquals(my.mutated.size(), 1, "One property changed");
   110         assertTrue(my.mutated.contains("unrelated"), "Its name is unrelated");
   111     }
   112     
   113     @Test public void computedPropertyCannotWriteToModel() {
   114         leakedModel = model;
   115         try {
   116             String res = model.getNotAllowedWrite();
   117             fail("We should not be allowed to write to the model: " + res);
   118         } catch (IllegalStateException ex) {
   119             // OK, we can't read
   120         }
   121     }
   122 
   123     @Test public void computedPropertyCannotReadToModel() {
   124         leakedModel = model;
   125         try {
   126             String res = model.getNotAllowedRead();
   127             fail("We should not be allowed to read from the model: " + res);
   128         } catch (IllegalStateException ex) {
   129             // OK, we can't read
   130         }
   131     }
   132     
   133     @ComputedProperty
   134     static int powerValue(int value) {
   135         return value * value;
   136     }
   137     
   138     @ComputedProperty
   139     static String notAllowedRead() {
   140         return "Not allowed callback: " + leakedModel.getUnrelated();
   141     }
   142 
   143     @ComputedProperty
   144     static String notAllowedWrite() {
   145         leakedModel.setUnrelated(11);
   146         return "Not allowed callback!";
   147     }
   148     
   149     static class MockKnockout extends Knockout {
   150         List<String> mutated = new ArrayList<String>();
   151         
   152         @Override
   153         public void valueHasMutated(String prop) {
   154             mutated.add(prop);
   155         }
   156     }
   157 }