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@489
|
26 |
import org.apidesign.bck2brwsr.htmlpage.api.Page;
|
jaroslav@490
|
27 |
import org.apidesign.bck2brwsr.htmlpage.api.Property;
|
jaroslav@489
|
28 |
import static org.testng.Assert.*;
|
jaroslav@505
|
29 |
import org.testng.annotations.BeforeMethod;
|
jaroslav@489
|
30 |
import org.testng.annotations.Test;
|
jaroslav@489
|
31 |
|
jaroslav@489
|
32 |
/**
|
jaroslav@489
|
33 |
*
|
jaroslav@489
|
34 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@489
|
35 |
*/
|
jaroslav@490
|
36 |
@Page(xhtml = "Empty.html", className = "Model", properties = {
|
jaroslav@499
|
37 |
@Property(name = "value", type = int.class),
|
jaroslav@766
|
38 |
@Property(name = "count", type = int.class),
|
jaroslav@760
|
39 |
@Property(name = "unrelated", type = long.class),
|
jaroslav@764
|
40 |
@Property(name = "names", type = String.class, array = true),
|
jaroslav@764
|
41 |
@Property(name = "values", type = int.class, array = true)
|
jaroslav@490
|
42 |
})
|
jaroslav@489
|
43 |
public class ModelTest {
|
jaroslav@505
|
44 |
private Model model;
|
jaroslav@505
|
45 |
private static Model leakedModel;
|
jaroslav@505
|
46 |
|
jaroslav@505
|
47 |
@BeforeMethod
|
jaroslav@505
|
48 |
public void createModel() {
|
jaroslav@505
|
49 |
model = new Model();
|
jaroslav@505
|
50 |
}
|
jaroslav@505
|
51 |
|
jaroslav@490
|
52 |
@Test public void classGeneratedWithSetterGetter() {
|
jaroslav@505
|
53 |
model.setValue(10);
|
jaroslav@505
|
54 |
assertEquals(10, model.getValue(), "Value changed");
|
jaroslav@489
|
55 |
}
|
jaroslav@491
|
56 |
|
jaroslav@491
|
57 |
@Test public void computedMethod() {
|
jaroslav@505
|
58 |
model.setValue(4);
|
jaroslav@505
|
59 |
assertEquals(16, model.getPowerValue());
|
jaroslav@491
|
60 |
}
|
jaroslav@491
|
61 |
|
jaroslav@760
|
62 |
@Test public void arrayIsMutable() {
|
jaroslav@760
|
63 |
assertEquals(model.getNames().size(), 0, "Is empty");
|
jaroslav@760
|
64 |
model.getNames().add("Jarda");
|
jaroslav@760
|
65 |
assertEquals(model.getNames().size(), 1, "One element");
|
jaroslav@760
|
66 |
}
|
jaroslav@760
|
67 |
|
jaroslav@761
|
68 |
@Test public void arrayChangesNotified() {
|
jaroslav@761
|
69 |
MockKnockout my = new MockKnockout();
|
jaroslav@761
|
70 |
MockKnockout.next = my;
|
jaroslav@761
|
71 |
|
jaroslav@761
|
72 |
model.applyBindings();
|
jaroslav@761
|
73 |
|
jaroslav@761
|
74 |
model.getNames().add("Hello");
|
jaroslav@761
|
75 |
|
jaroslav@761
|
76 |
assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
|
jaroslav@761
|
77 |
assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
|
jaroslav@761
|
78 |
|
jaroslav@761
|
79 |
my.mutated.clear();
|
jaroslav@761
|
80 |
|
jaroslav@761
|
81 |
Iterator<String> it = model.getNames().iterator();
|
jaroslav@761
|
82 |
assertEquals(it.next(), "Hello");
|
jaroslav@761
|
83 |
it.remove();
|
jaroslav@761
|
84 |
|
jaroslav@761
|
85 |
assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
|
jaroslav@761
|
86 |
assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
|
jaroslav@761
|
87 |
|
jaroslav@761
|
88 |
my.mutated.clear();
|
jaroslav@761
|
89 |
|
jaroslav@761
|
90 |
ListIterator<String> lit = model.getNames().listIterator();
|
jaroslav@761
|
91 |
lit.add("Jarda");
|
jaroslav@761
|
92 |
|
jaroslav@761
|
93 |
assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
|
jaroslav@761
|
94 |
assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
|
jaroslav@761
|
95 |
}
|
jaroslav@761
|
96 |
|
jaroslav@764
|
97 |
@Test public void autoboxedArray() {
|
jaroslav@764
|
98 |
MockKnockout my = new MockKnockout();
|
jaroslav@764
|
99 |
MockKnockout.next = my;
|
jaroslav@764
|
100 |
|
jaroslav@764
|
101 |
model.applyBindings();
|
jaroslav@764
|
102 |
|
jaroslav@764
|
103 |
model.getValues().add(10);
|
jaroslav@764
|
104 |
|
jaroslav@764
|
105 |
assertEquals(model.getValues().get(0), Integer.valueOf(10), "Really ten");
|
jaroslav@764
|
106 |
}
|
jaroslav@766
|
107 |
|
jaroslav@766
|
108 |
@Test public void derivedArrayProp() {
|
jaroslav@766
|
109 |
MockKnockout my = new MockKnockout();
|
jaroslav@766
|
110 |
MockKnockout.next = my;
|
jaroslav@766
|
111 |
|
jaroslav@766
|
112 |
model.applyBindings();
|
jaroslav@766
|
113 |
|
jaroslav@766
|
114 |
model.setCount(10);
|
jaroslav@766
|
115 |
|
jaroslav@766
|
116 |
List<String> arr = model.getRepeat();
|
jaroslav@766
|
117 |
assertEquals(arr.size(), 10, "Ten items: " + arr);
|
jaroslav@766
|
118 |
|
jaroslav@766
|
119 |
my.mutated.clear();
|
jaroslav@766
|
120 |
|
jaroslav@766
|
121 |
model.setCount(5);
|
jaroslav@766
|
122 |
|
jaroslav@766
|
123 |
arr = model.getRepeat();
|
jaroslav@766
|
124 |
assertEquals(arr.size(), 5, "Five items: " + arr);
|
jaroslav@766
|
125 |
|
jaroslav@766
|
126 |
assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
|
jaroslav@766
|
127 |
assertTrue(my.mutated.contains("repeat"), "Array is in there: " + my.mutated);
|
jaroslav@766
|
128 |
assertTrue(my.mutated.contains("count"), "Count is in there: " + my.mutated);
|
jaroslav@766
|
129 |
}
|
jaroslav@764
|
130 |
|
jaroslav@499
|
131 |
@Test public void derivedPropertiesAreNotified() {
|
jaroslav@499
|
132 |
MockKnockout my = new MockKnockout();
|
jaroslav@499
|
133 |
MockKnockout.next = my;
|
jaroslav@499
|
134 |
|
jaroslav@505
|
135 |
model.applyBindings();
|
jaroslav@499
|
136 |
|
jaroslav@505
|
137 |
model.setValue(33);
|
jaroslav@499
|
138 |
|
jaroslav@499
|
139 |
assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
|
jaroslav@499
|
140 |
assertTrue(my.mutated.contains("powerValue"), "Power value is in there: " + my.mutated);
|
jaroslav@499
|
141 |
assertTrue(my.mutated.contains("value"), "Simple value is in there: " + my.mutated);
|
jaroslav@499
|
142 |
|
jaroslav@499
|
143 |
my.mutated.clear();
|
jaroslav@499
|
144 |
|
jaroslav@505
|
145 |
model.setUnrelated(44);
|
jaroslav@499
|
146 |
assertEquals(my.mutated.size(), 1, "One property changed");
|
jaroslav@499
|
147 |
assertTrue(my.mutated.contains("unrelated"), "Its name is unrelated");
|
jaroslav@499
|
148 |
}
|
jaroslav@499
|
149 |
|
jaroslav@500
|
150 |
@Test public void computedPropertyCannotWriteToModel() {
|
jaroslav@505
|
151 |
leakedModel = model;
|
jaroslav@500
|
152 |
try {
|
jaroslav@505
|
153 |
String res = model.getNotAllowedWrite();
|
jaroslav@500
|
154 |
fail("We should not be allowed to write to the model: " + res);
|
jaroslav@500
|
155 |
} catch (IllegalStateException ex) {
|
jaroslav@500
|
156 |
// OK, we can't read
|
jaroslav@500
|
157 |
}
|
jaroslav@500
|
158 |
}
|
jaroslav@500
|
159 |
|
jaroslav@500
|
160 |
@Test public void computedPropertyCannotReadToModel() {
|
jaroslav@505
|
161 |
leakedModel = model;
|
jaroslav@500
|
162 |
try {
|
jaroslav@505
|
163 |
String res = model.getNotAllowedRead();
|
jaroslav@500
|
164 |
fail("We should not be allowed to read from the model: " + res);
|
jaroslav@500
|
165 |
} catch (IllegalStateException ex) {
|
jaroslav@500
|
166 |
// OK, we can't read
|
jaroslav@500
|
167 |
}
|
jaroslav@500
|
168 |
}
|
jaroslav@500
|
169 |
|
jaroslav@491
|
170 |
@ComputedProperty
|
jaroslav@491
|
171 |
static int powerValue(int value) {
|
jaroslav@491
|
172 |
return value * value;
|
jaroslav@491
|
173 |
}
|
jaroslav@499
|
174 |
|
jaroslav@500
|
175 |
@ComputedProperty
|
jaroslav@500
|
176 |
static String notAllowedRead() {
|
jaroslav@505
|
177 |
return "Not allowed callback: " + leakedModel.getUnrelated();
|
jaroslav@500
|
178 |
}
|
jaroslav@500
|
179 |
|
jaroslav@500
|
180 |
@ComputedProperty
|
jaroslav@500
|
181 |
static String notAllowedWrite() {
|
jaroslav@505
|
182 |
leakedModel.setUnrelated(11);
|
jaroslav@500
|
183 |
return "Not allowed callback!";
|
jaroslav@500
|
184 |
}
|
jaroslav@500
|
185 |
|
jaroslav@766
|
186 |
@ComputedProperty
|
jaroslav@766
|
187 |
static List<String> repeat(int count) {
|
jaroslav@766
|
188 |
return Collections.nCopies(count, "Hello");
|
jaroslav@766
|
189 |
}
|
jaroslav@766
|
190 |
|
jaroslav@499
|
191 |
static class MockKnockout extends Knockout {
|
jaroslav@499
|
192 |
List<String> mutated = new ArrayList<String>();
|
jaroslav@499
|
193 |
|
jaroslav@499
|
194 |
@Override
|
jaroslav@499
|
195 |
public void valueHasMutated(String prop) {
|
jaroslav@499
|
196 |
mutated.add(prop);
|
jaroslav@499
|
197 |
}
|
jaroslav@499
|
198 |
}
|
jaroslav@489
|
199 |
}
|