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