javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ModelTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 21 Jan 2013 11:55:27 +0100
branchmodel
changeset 500 f9e80d48e9b4
parent 499 af027874f93e
child 505 4198be34b516
permissions -rw-r--r--
@ComputedProperty methods can't access the model
     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.List;
    22 import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
    23 import org.apidesign.bck2brwsr.htmlpage.api.Page;
    24 import org.apidesign.bck2brwsr.htmlpage.api.Property;
    25 import static org.testng.Assert.*;
    26 import org.testng.annotations.Test;
    27 
    28 /**
    29  *
    30  * @author Jaroslav Tulach <jtulach@netbeans.org>
    31  */
    32 @Page(xhtml = "Empty.html", className = "Model", properties = {
    33     @Property(name = "value", type = int.class),
    34     @Property(name = "unrelated", type = long.class)
    35 })
    36 public class ModelTest {
    37     @Test public void classGeneratedWithSetterGetter() {
    38         Class<?> c = Model.class;
    39         assertNotNull(c, "Class for empty page generated");
    40         Model.setValue(10);
    41         assertEquals(10, Model.getValue(), "Value changed");
    42     }
    43     
    44     @Test public void computedMethod() {
    45         Model.setValue(4);
    46         assertEquals(16, Model.getPowerValue());
    47     }
    48     
    49     @Test public void derivedPropertiesAreNotified() {
    50         MockKnockout my = new MockKnockout();
    51         MockKnockout.next = my;
    52         
    53         Model.applyBindings();
    54         
    55         Model.setValue(33);
    56         
    57         assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
    58         assertTrue(my.mutated.contains("powerValue"), "Power value is in there: " + my.mutated);
    59         assertTrue(my.mutated.contains("value"), "Simple value is in there: " + my.mutated);
    60         
    61         my.mutated.clear();
    62         
    63         Model.setUnrelated(44);
    64         assertEquals(my.mutated.size(), 1, "One property changed");
    65         assertTrue(my.mutated.contains("unrelated"), "Its name is unrelated");
    66     }
    67     
    68     @Test public void computedPropertyCannotWriteToModel() {
    69         try {
    70             String res = Model.getNotAllowedWrite();
    71             fail("We should not be allowed to write to the model: " + res);
    72         } catch (IllegalStateException ex) {
    73             // OK, we can't read
    74         }
    75     }
    76 
    77     @Test public void computedPropertyCannotReadToModel() {
    78         try {
    79             String res = Model.getNotAllowedRead();
    80             fail("We should not be allowed to read from the model: " + res);
    81         } catch (IllegalStateException ex) {
    82             // OK, we can't read
    83         }
    84     }
    85     
    86     @ComputedProperty
    87     static int powerValue(int value) {
    88         return value * value;
    89     }
    90     
    91     @ComputedProperty
    92     static String notAllowedRead() {
    93         return "Not allowed callback: " + Model.getUnrelated();
    94     }
    95 
    96     @ComputedProperty
    97     static String notAllowedWrite() {
    98         Model.setUnrelated(11);
    99         return "Not allowed callback!";
   100     }
   101     
   102     static class MockKnockout extends Knockout {
   103         List<String> mutated = new ArrayList<String>();
   104         
   105         @Override
   106         public void valueHasMutated(String prop) {
   107             mutated.add(prop);
   108         }
   109     }
   110 }