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