jaroslav@489
|
1 |
/**
|
jaroslav@489
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@489
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@489
|
4 |
*
|
jaroslav@489
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@489
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@489
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@489
|
8 |
*
|
jaroslav@489
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@489
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@489
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@489
|
12 |
* GNU General Public License for more details.
|
jaroslav@489
|
13 |
*
|
jaroslav@489
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@489
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@489
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@489
|
17 |
*/
|
jaroslav@489
|
18 |
package org.apidesign.bck2brwsr.htmlpage;
|
jaroslav@489
|
19 |
|
jaroslav@499
|
20 |
import java.util.ArrayList;
|
jaroslav@766
|
21 |
import java.util.Collections;
|
jaroslav@761
|
22 |
import java.util.Iterator;
|
jaroslav@499
|
23 |
import java.util.List;
|
jaroslav@761
|
24 |
import java.util.ListIterator;
|
jaroslav@491
|
25 |
import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
|
jaroslav@879
|
26 |
import org.apidesign.bck2brwsr.htmlpage.api.OnFunction;
|
jaroslav@949
|
27 |
import org.apidesign.bck2brwsr.htmlpage.api.OnPropertyChange;
|
jaroslav@489
|
28 |
import org.apidesign.bck2brwsr.htmlpage.api.Page;
|
jaroslav@490
|
29 |
import org.apidesign.bck2brwsr.htmlpage.api.Property;
|
jaroslav@489
|
30 |
import static org.testng.Assert.*;
|
jaroslav@505
|
31 |
import org.testng.annotations.BeforeMethod;
|
jaroslav@489
|
32 |
import org.testng.annotations.Test;
|
jaroslav@489
|
33 |
|
jaroslav@489
|
34 |
/**
|
jaroslav@489
|
35 |
*
|
jaroslav@489
|
36 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@489
|
37 |
*/
|
jaroslav@770
|
38 |
@Page(xhtml = "Empty.html", className = "Modelik", properties = {
|
jaroslav@499
|
39 |
@Property(name = "value", type = int.class),
|
jaroslav@766
|
40 |
@Property(name = "count", type = int.class),
|
jaroslav@760
|
41 |
@Property(name = "unrelated", type = long.class),
|
jaroslav@764
|
42 |
@Property(name = "names", type = String.class, array = true),
|
jaroslav@770
|
43 |
@Property(name = "values", type = int.class, array = true),
|
jaroslav@949
|
44 |
@Property(name = "people", type = PersonImpl.class, array = true),
|
jaroslav@949
|
45 |
@Property(name = "changedProperty", type=String.class)
|
jaroslav@490
|
46 |
})
|
jaroslav@489
|
47 |
public class ModelTest {
|
jaroslav@770
|
48 |
private Modelik model;
|
jaroslav@770
|
49 |
private static Modelik leakedModel;
|
jaroslav@505
|
50 |
|
jaroslav@505
|
51 |
@BeforeMethod
|
jaroslav@505
|
52 |
public void createModel() {
|
jaroslav@770
|
53 |
model = new Modelik();
|
jaroslav@505
|
54 |
}
|
jaroslav@505
|
55 |
|
jaroslav@490
|
56 |
@Test public void classGeneratedWithSetterGetter() {
|
jaroslav@505
|
57 |
model.setValue(10);
|
jaroslav@505
|
58 |
assertEquals(10, model.getValue(), "Value changed");
|
jaroslav@489
|
59 |
}
|
jaroslav@491
|
60 |
|
jaroslav@491
|
61 |
@Test public void computedMethod() {
|
jaroslav@505
|
62 |
model.setValue(4);
|
jaroslav@505
|
63 |
assertEquals(16, model.getPowerValue());
|
jaroslav@491
|
64 |
}
|
jaroslav@491
|
65 |
|
jaroslav@760
|
66 |
@Test public void arrayIsMutable() {
|
jaroslav@760
|
67 |
assertEquals(model.getNames().size(), 0, "Is empty");
|
jaroslav@760
|
68 |
model.getNames().add("Jarda");
|
jaroslav@760
|
69 |
assertEquals(model.getNames().size(), 1, "One element");
|
jaroslav@760
|
70 |
}
|
jaroslav@760
|
71 |
|
jaroslav@761
|
72 |
@Test public void arrayChangesNotified() {
|
jaroslav@761
|
73 |
MockKnockout my = new MockKnockout();
|
jaroslav@761
|
74 |
MockKnockout.next = my;
|
jaroslav@761
|
75 |
|
jaroslav@761
|
76 |
model.applyBindings();
|
jaroslav@761
|
77 |
|
jaroslav@761
|
78 |
model.getNames().add("Hello");
|
jaroslav@761
|
79 |
|
jaroslav@761
|
80 |
assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
|
jaroslav@761
|
81 |
assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
|
jaroslav@761
|
82 |
|
jaroslav@761
|
83 |
my.mutated.clear();
|
jaroslav@761
|
84 |
|
jaroslav@761
|
85 |
Iterator<String> it = model.getNames().iterator();
|
jaroslav@761
|
86 |
assertEquals(it.next(), "Hello");
|
jaroslav@761
|
87 |
it.remove();
|
jaroslav@761
|
88 |
|
jaroslav@761
|
89 |
assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
|
jaroslav@761
|
90 |
assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
|
jaroslav@761
|
91 |
|
jaroslav@761
|
92 |
my.mutated.clear();
|
jaroslav@761
|
93 |
|
jaroslav@761
|
94 |
ListIterator<String> lit = model.getNames().listIterator();
|
jaroslav@761
|
95 |
lit.add("Jarda");
|
jaroslav@761
|
96 |
|
jaroslav@761
|
97 |
assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
|
jaroslav@761
|
98 |
assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
|
jaroslav@761
|
99 |
}
|
jaroslav@761
|
100 |
|
jaroslav@764
|
101 |
@Test public void autoboxedArray() {
|
jaroslav@764
|
102 |
MockKnockout my = new MockKnockout();
|
jaroslav@764
|
103 |
MockKnockout.next = my;
|
jaroslav@764
|
104 |
|
jaroslav@764
|
105 |
model.applyBindings();
|
jaroslav@764
|
106 |
|
jaroslav@764
|
107 |
model.getValues().add(10);
|
jaroslav@764
|
108 |
|
jaroslav@764
|
109 |
assertEquals(model.getValues().get(0), Integer.valueOf(10), "Really ten");
|
jaroslav@764
|
110 |
}
|
jaroslav@766
|
111 |
|
jaroslav@766
|
112 |
@Test public void derivedArrayProp() {
|
jaroslav@766
|
113 |
MockKnockout my = new MockKnockout();
|
jaroslav@766
|
114 |
MockKnockout.next = my;
|
jaroslav@766
|
115 |
|
jaroslav@766
|
116 |
model.applyBindings();
|
jaroslav@766
|
117 |
|
jaroslav@766
|
118 |
model.setCount(10);
|
jaroslav@766
|
119 |
|
jaroslav@766
|
120 |
List<String> arr = model.getRepeat();
|
jaroslav@766
|
121 |
assertEquals(arr.size(), 10, "Ten items: " + arr);
|
jaroslav@766
|
122 |
|
jaroslav@766
|
123 |
my.mutated.clear();
|
jaroslav@766
|
124 |
|
jaroslav@766
|
125 |
model.setCount(5);
|
jaroslav@766
|
126 |
|
jaroslav@766
|
127 |
arr = model.getRepeat();
|
jaroslav@766
|
128 |
assertEquals(arr.size(), 5, "Five items: " + arr);
|
jaroslav@766
|
129 |
|
jaroslav@766
|
130 |
assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
|
jaroslav@766
|
131 |
assertTrue(my.mutated.contains("repeat"), "Array is in there: " + my.mutated);
|
jaroslav@766
|
132 |
assertTrue(my.mutated.contains("count"), "Count is in there: " + my.mutated);
|
jaroslav@766
|
133 |
}
|
jaroslav@764
|
134 |
|
jaroslav@499
|
135 |
@Test public void derivedPropertiesAreNotified() {
|
jaroslav@499
|
136 |
MockKnockout my = new MockKnockout();
|
jaroslav@499
|
137 |
MockKnockout.next = my;
|
jaroslav@499
|
138 |
|
jaroslav@505
|
139 |
model.applyBindings();
|
jaroslav@499
|
140 |
|
jaroslav@505
|
141 |
model.setValue(33);
|
jaroslav@499
|
142 |
|
jaroslav@949
|
143 |
// not interested in change of this property
|
jaroslav@949
|
144 |
my.mutated.remove("changedProperty");
|
jaroslav@949
|
145 |
|
jaroslav@499
|
146 |
assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
|
jaroslav@499
|
147 |
assertTrue(my.mutated.contains("powerValue"), "Power value is in there: " + my.mutated);
|
jaroslav@499
|
148 |
assertTrue(my.mutated.contains("value"), "Simple value is in there: " + my.mutated);
|
jaroslav@499
|
149 |
|
jaroslav@499
|
150 |
my.mutated.clear();
|
jaroslav@499
|
151 |
|
jaroslav@505
|
152 |
model.setUnrelated(44);
|
jaroslav@949
|
153 |
|
jaroslav@949
|
154 |
|
jaroslav@949
|
155 |
// not interested in change of this property
|
jaroslav@949
|
156 |
my.mutated.remove("changedProperty");
|
jaroslav@949
|
157 |
assertEquals(my.mutated.size(), 1, "One property changed: " + my.mutated);
|
jaroslav@499
|
158 |
assertTrue(my.mutated.contains("unrelated"), "Its name is unrelated");
|
jaroslav@499
|
159 |
}
|
jaroslav@499
|
160 |
|
jaroslav@500
|
161 |
@Test public void computedPropertyCannotWriteToModel() {
|
jaroslav@505
|
162 |
leakedModel = model;
|
jaroslav@500
|
163 |
try {
|
jaroslav@505
|
164 |
String res = model.getNotAllowedWrite();
|
jaroslav@500
|
165 |
fail("We should not be allowed to write to the model: " + res);
|
jaroslav@500
|
166 |
} catch (IllegalStateException ex) {
|
jaroslav@500
|
167 |
// OK, we can't read
|
jaroslav@500
|
168 |
}
|
jaroslav@500
|
169 |
}
|
jaroslav@500
|
170 |
|
jaroslav@500
|
171 |
@Test public void computedPropertyCannotReadToModel() {
|
jaroslav@505
|
172 |
leakedModel = model;
|
jaroslav@500
|
173 |
try {
|
jaroslav@505
|
174 |
String res = model.getNotAllowedRead();
|
jaroslav@500
|
175 |
fail("We should not be allowed to read from the model: " + res);
|
jaroslav@500
|
176 |
} catch (IllegalStateException ex) {
|
jaroslav@500
|
177 |
// OK, we can't read
|
jaroslav@500
|
178 |
}
|
jaroslav@500
|
179 |
}
|
jaroslav@500
|
180 |
|
jaroslav@879
|
181 |
@OnFunction
|
jaroslav@879
|
182 |
static void doSomething() {
|
jaroslav@879
|
183 |
}
|
jaroslav@879
|
184 |
|
jaroslav@491
|
185 |
@ComputedProperty
|
jaroslav@491
|
186 |
static int powerValue(int value) {
|
jaroslav@491
|
187 |
return value * value;
|
jaroslav@491
|
188 |
}
|
jaroslav@499
|
189 |
|
jaroslav@949
|
190 |
@OnPropertyChange({ "powerValue", "unrelated" })
|
jaroslav@949
|
191 |
static void aPropertyChanged(Modelik m, String name) {
|
jaroslav@949
|
192 |
m.setChangedProperty(name);
|
jaroslav@949
|
193 |
}
|
jaroslav@950
|
194 |
|
jaroslav@950
|
195 |
@OnPropertyChange({ "values" })
|
jaroslav@950
|
196 |
static void anArrayPropertyChanged(String name, Modelik m) {
|
jaroslav@950
|
197 |
m.setChangedProperty(name);
|
jaroslav@950
|
198 |
}
|
jaroslav@949
|
199 |
|
jaroslav@949
|
200 |
@Test public void changeAnything() {
|
jaroslav@949
|
201 |
model.setCount(44);
|
jaroslav@949
|
202 |
assertNull(model.getChangedProperty(), "No observed value change");
|
jaroslav@949
|
203 |
}
|
jaroslav@949
|
204 |
@Test public void changeValue() {
|
jaroslav@949
|
205 |
model.setValue(33);
|
jaroslav@949
|
206 |
assertEquals(model.getChangedProperty(), "powerValue", "power property changed");
|
jaroslav@949
|
207 |
}
|
jaroslav@949
|
208 |
@Test public void changeUnrelated() {
|
jaroslav@949
|
209 |
model.setUnrelated(333);
|
jaroslav@949
|
210 |
assertEquals(model.getChangedProperty(), "unrelated", "unrelated changed");
|
jaroslav@949
|
211 |
}
|
jaroslav@950
|
212 |
|
jaroslav@950
|
213 |
@Test public void changeInArray() {
|
jaroslav@950
|
214 |
model.getValues().add(10);
|
jaroslav@950
|
215 |
assertEquals(model.getChangedProperty(), "values", "Something added into the array");
|
jaroslav@950
|
216 |
}
|
jaroslav@949
|
217 |
|
jaroslav@500
|
218 |
@ComputedProperty
|
jaroslav@500
|
219 |
static String notAllowedRead() {
|
jaroslav@505
|
220 |
return "Not allowed callback: " + leakedModel.getUnrelated();
|
jaroslav@500
|
221 |
}
|
jaroslav@500
|
222 |
|
jaroslav@500
|
223 |
@ComputedProperty
|
jaroslav@500
|
224 |
static String notAllowedWrite() {
|
jaroslav@505
|
225 |
leakedModel.setUnrelated(11);
|
jaroslav@500
|
226 |
return "Not allowed callback!";
|
jaroslav@500
|
227 |
}
|
jaroslav@500
|
228 |
|
jaroslav@766
|
229 |
@ComputedProperty
|
jaroslav@766
|
230 |
static List<String> repeat(int count) {
|
jaroslav@766
|
231 |
return Collections.nCopies(count, "Hello");
|
jaroslav@766
|
232 |
}
|
jaroslav@766
|
233 |
|
jaroslav@499
|
234 |
static class MockKnockout extends Knockout {
|
jaroslav@770
|
235 |
List<String> mutated = new ArrayList<>();
|
jaroslav@499
|
236 |
|
jaroslav@909
|
237 |
MockKnockout() {
|
jaroslav@909
|
238 |
super(null);
|
jaroslav@909
|
239 |
}
|
jaroslav@909
|
240 |
|
jaroslav@499
|
241 |
@Override
|
jaroslav@499
|
242 |
public void valueHasMutated(String prop) {
|
jaroslav@499
|
243 |
mutated.add(prop);
|
jaroslav@499
|
244 |
}
|
jaroslav@499
|
245 |
}
|
jaroslav@770
|
246 |
|
jaroslav@770
|
247 |
public @Test void hasPersonPropertyAndComputedFullName() {
|
jaroslav@770
|
248 |
List<Person> arr = model.getPeople();
|
jaroslav@770
|
249 |
assertEquals(arr.size(), 0, "By default empty");
|
jaroslav@770
|
250 |
Person p = null;
|
jaroslav@770
|
251 |
if (p != null) {
|
jaroslav@770
|
252 |
String fullNameGenerated = p.getFullName();
|
jaroslav@770
|
253 |
assertNotNull(fullNameGenerated);
|
jaroslav@770
|
254 |
}
|
jaroslav@770
|
255 |
}
|
jaroslav@489
|
256 |
}
|