javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ModelTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 21 Jan 2013 13:43:40 +0100
branchmodel
changeset 505 4198be34b516
parent 500 f9e80d48e9b4
child 760 4bd6f3bc6c64
child 851 c285a78302af
permissions -rw-r--r--
Moving towards testability: Model class does not have any public fields, rather instance onces
     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 })
    37 public class ModelTest {
    38     private Model model;
    39     private static Model leakedModel;
    40     
    41     @BeforeMethod
    42     public void createModel() {
    43         model = new Model();
    44     }
    45     
    46     @Test public void classGeneratedWithSetterGetter() {
    47         model.setValue(10);
    48         assertEquals(10, model.getValue(), "Value changed");
    49     }
    50     
    51     @Test public void computedMethod() {
    52         model.setValue(4);
    53         assertEquals(16, model.getPowerValue());
    54     }
    55     
    56     @Test public void derivedPropertiesAreNotified() {
    57         MockKnockout my = new MockKnockout();
    58         MockKnockout.next = my;
    59         
    60         model.applyBindings();
    61         
    62         model.setValue(33);
    63         
    64         assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
    65         assertTrue(my.mutated.contains("powerValue"), "Power value is in there: " + my.mutated);
    66         assertTrue(my.mutated.contains("value"), "Simple value is in there: " + my.mutated);
    67         
    68         my.mutated.clear();
    69         
    70         model.setUnrelated(44);
    71         assertEquals(my.mutated.size(), 1, "One property changed");
    72         assertTrue(my.mutated.contains("unrelated"), "Its name is unrelated");
    73     }
    74     
    75     @Test public void computedPropertyCannotWriteToModel() {
    76         leakedModel = model;
    77         try {
    78             String res = model.getNotAllowedWrite();
    79             fail("We should not be allowed to write to the model: " + res);
    80         } catch (IllegalStateException ex) {
    81             // OK, we can't read
    82         }
    83     }
    84 
    85     @Test public void computedPropertyCannotReadToModel() {
    86         leakedModel = model;
    87         try {
    88             String res = model.getNotAllowedRead();
    89             fail("We should not be allowed to read from the model: " + res);
    90         } catch (IllegalStateException ex) {
    91             // OK, we can't read
    92         }
    93     }
    94     
    95     @ComputedProperty
    96     static int powerValue(int value) {
    97         return value * value;
    98     }
    99     
   100     @ComputedProperty
   101     static String notAllowedRead() {
   102         return "Not allowed callback: " + leakedModel.getUnrelated();
   103     }
   104 
   105     @ComputedProperty
   106     static String notAllowedWrite() {
   107         leakedModel.setUnrelated(11);
   108         return "Not allowed callback!";
   109     }
   110     
   111     static class MockKnockout extends Knockout {
   112         List<String> mutated = new ArrayList<String>();
   113         
   114         @Override
   115         public void valueHasMutated(String prop) {
   116             mutated.add(prop);
   117         }
   118     }
   119 }