1.1 --- a/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ModelTest.java Mon Mar 18 17:17:00 2013 +0100
1.2 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ModelTest.java Thu Apr 11 20:44:46 2013 +0200
1.3 @@ -18,8 +18,13 @@
1.4 package org.apidesign.bck2brwsr.htmlpage;
1.5
1.6 import java.util.ArrayList;
1.7 +import java.util.Collections;
1.8 +import java.util.Iterator;
1.9 import java.util.List;
1.10 +import java.util.ListIterator;
1.11 import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
1.12 +import org.apidesign.bck2brwsr.htmlpage.api.OnFunction;
1.13 +import org.apidesign.bck2brwsr.htmlpage.api.OnPropertyChange;
1.14 import org.apidesign.bck2brwsr.htmlpage.api.Page;
1.15 import org.apidesign.bck2brwsr.htmlpage.api.Property;
1.16 import static org.testng.Assert.*;
1.17 @@ -30,17 +35,22 @@
1.18 *
1.19 * @author Jaroslav Tulach <jtulach@netbeans.org>
1.20 */
1.21 -@Page(xhtml = "Empty.html", className = "Model", properties = {
1.22 +@Page(xhtml = "Empty.html", className = "Modelik", properties = {
1.23 @Property(name = "value", type = int.class),
1.24 - @Property(name = "unrelated", type = long.class)
1.25 + @Property(name = "count", type = int.class),
1.26 + @Property(name = "unrelated", type = long.class),
1.27 + @Property(name = "names", type = String.class, array = true),
1.28 + @Property(name = "values", type = int.class, array = true),
1.29 + @Property(name = "people", type = PersonImpl.class, array = true),
1.30 + @Property(name = "changedProperty", type=String.class)
1.31 })
1.32 public class ModelTest {
1.33 - private Model model;
1.34 - private static Model leakedModel;
1.35 + private Modelik model;
1.36 + private static Modelik leakedModel;
1.37
1.38 @BeforeMethod
1.39 public void createModel() {
1.40 - model = new Model();
1.41 + model = new Modelik();
1.42 }
1.43
1.44 @Test public void classGeneratedWithSetterGetter() {
1.45 @@ -53,6 +63,75 @@
1.46 assertEquals(16, model.getPowerValue());
1.47 }
1.48
1.49 + @Test public void arrayIsMutable() {
1.50 + assertEquals(model.getNames().size(), 0, "Is empty");
1.51 + model.getNames().add("Jarda");
1.52 + assertEquals(model.getNames().size(), 1, "One element");
1.53 + }
1.54 +
1.55 + @Test public void arrayChangesNotified() {
1.56 + MockKnockout my = new MockKnockout();
1.57 + MockKnockout.next = my;
1.58 +
1.59 + model.applyBindings();
1.60 +
1.61 + model.getNames().add("Hello");
1.62 +
1.63 + assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
1.64 + assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
1.65 +
1.66 + my.mutated.clear();
1.67 +
1.68 + Iterator<String> it = model.getNames().iterator();
1.69 + assertEquals(it.next(), "Hello");
1.70 + it.remove();
1.71 +
1.72 + assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
1.73 + assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
1.74 +
1.75 + my.mutated.clear();
1.76 +
1.77 + ListIterator<String> lit = model.getNames().listIterator();
1.78 + lit.add("Jarda");
1.79 +
1.80 + assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
1.81 + assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
1.82 + }
1.83 +
1.84 + @Test public void autoboxedArray() {
1.85 + MockKnockout my = new MockKnockout();
1.86 + MockKnockout.next = my;
1.87 +
1.88 + model.applyBindings();
1.89 +
1.90 + model.getValues().add(10);
1.91 +
1.92 + assertEquals(model.getValues().get(0), Integer.valueOf(10), "Really ten");
1.93 + }
1.94 +
1.95 + @Test public void derivedArrayProp() {
1.96 + MockKnockout my = new MockKnockout();
1.97 + MockKnockout.next = my;
1.98 +
1.99 + model.applyBindings();
1.100 +
1.101 + model.setCount(10);
1.102 +
1.103 + List<String> arr = model.getRepeat();
1.104 + assertEquals(arr.size(), 10, "Ten items: " + arr);
1.105 +
1.106 + my.mutated.clear();
1.107 +
1.108 + model.setCount(5);
1.109 +
1.110 + arr = model.getRepeat();
1.111 + assertEquals(arr.size(), 5, "Five items: " + arr);
1.112 +
1.113 + assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
1.114 + assertTrue(my.mutated.contains("repeat"), "Array is in there: " + my.mutated);
1.115 + assertTrue(my.mutated.contains("count"), "Count is in there: " + my.mutated);
1.116 + }
1.117 +
1.118 @Test public void derivedPropertiesAreNotified() {
1.119 MockKnockout my = new MockKnockout();
1.120 MockKnockout.next = my;
1.121 @@ -61,6 +140,9 @@
1.122
1.123 model.setValue(33);
1.124
1.125 + // not interested in change of this property
1.126 + my.mutated.remove("changedProperty");
1.127 +
1.128 assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
1.129 assertTrue(my.mutated.contains("powerValue"), "Power value is in there: " + my.mutated);
1.130 assertTrue(my.mutated.contains("value"), "Simple value is in there: " + my.mutated);
1.131 @@ -68,7 +150,11 @@
1.132 my.mutated.clear();
1.133
1.134 model.setUnrelated(44);
1.135 - assertEquals(my.mutated.size(), 1, "One property changed");
1.136 +
1.137 +
1.138 + // not interested in change of this property
1.139 + my.mutated.remove("changedProperty");
1.140 + assertEquals(my.mutated.size(), 1, "One property changed: " + my.mutated);
1.141 assertTrue(my.mutated.contains("unrelated"), "Its name is unrelated");
1.142 }
1.143
1.144 @@ -92,11 +178,43 @@
1.145 }
1.146 }
1.147
1.148 + @OnFunction
1.149 + static void doSomething() {
1.150 + }
1.151 +
1.152 @ComputedProperty
1.153 static int powerValue(int value) {
1.154 return value * value;
1.155 }
1.156
1.157 + @OnPropertyChange({ "powerValue", "unrelated" })
1.158 + static void aPropertyChanged(Modelik m, String name) {
1.159 + m.setChangedProperty(name);
1.160 + }
1.161 +
1.162 + @OnPropertyChange({ "values" })
1.163 + static void anArrayPropertyChanged(String name, Modelik m) {
1.164 + m.setChangedProperty(name);
1.165 + }
1.166 +
1.167 + @Test public void changeAnything() {
1.168 + model.setCount(44);
1.169 + assertNull(model.getChangedProperty(), "No observed value change");
1.170 + }
1.171 + @Test public void changeValue() {
1.172 + model.setValue(33);
1.173 + assertEquals(model.getChangedProperty(), "powerValue", "power property changed");
1.174 + }
1.175 + @Test public void changeUnrelated() {
1.176 + model.setUnrelated(333);
1.177 + assertEquals(model.getChangedProperty(), "unrelated", "unrelated changed");
1.178 + }
1.179 +
1.180 + @Test public void changeInArray() {
1.181 + model.getValues().add(10);
1.182 + assertEquals(model.getChangedProperty(), "values", "Something added into the array");
1.183 + }
1.184 +
1.185 @ComputedProperty
1.186 static String notAllowedRead() {
1.187 return "Not allowed callback: " + leakedModel.getUnrelated();
1.188 @@ -108,9 +226,14 @@
1.189 return "Not allowed callback!";
1.190 }
1.191
1.192 + @ComputedProperty
1.193 + static List<String> repeat(int count) {
1.194 + return Collections.nCopies(count, "Hello");
1.195 + }
1.196 +
1.197 static class MockKnockout extends Knockout {
1.198 - List<String> mutated = new ArrayList<String>();
1.199 -
1.200 + List<String> mutated = new ArrayList<>();
1.201 +
1.202 public MockKnockout() {
1.203 super(new Object());
1.204 }
1.205 @@ -120,4 +243,14 @@
1.206 mutated.add(prop);
1.207 }
1.208 }
1.209 +
1.210 + public @Test void hasPersonPropertyAndComputedFullName() {
1.211 + List<Person> arr = model.getPeople();
1.212 + assertEquals(arr.size(), 0, "By default empty");
1.213 + Person p = null;
1.214 + if (p != null) {
1.215 + String fullNameGenerated = p.getFullName();
1.216 + assertNotNull(fullNameGenerated);
1.217 + }
1.218 + }
1.219 }