2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.bck2brwsr.htmlpage;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.ListIterator;
25 import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
26 import org.apidesign.bck2brwsr.htmlpage.api.Page;
27 import org.apidesign.bck2brwsr.htmlpage.api.Property;
28 import static org.testng.Assert.*;
29 import org.testng.annotations.BeforeMethod;
30 import org.testng.annotations.Test;
34 * @author Jaroslav Tulach <jtulach@netbeans.org>
36 @Page(xhtml = "Empty.html", className = "Model", properties = {
37 @Property(name = "value", type = int.class),
38 @Property(name = "count", type = int.class),
39 @Property(name = "unrelated", type = long.class),
40 @Property(name = "names", type = String.class, array = true),
41 @Property(name = "values", type = int.class, array = true)
43 public class ModelTest {
45 private static Model leakedModel;
48 public void createModel() {
52 @Test public void classGeneratedWithSetterGetter() {
54 assertEquals(10, model.getValue(), "Value changed");
57 @Test public void computedMethod() {
59 assertEquals(16, model.getPowerValue());
62 @Test public void arrayIsMutable() {
63 assertEquals(model.getNames().size(), 0, "Is empty");
64 model.getNames().add("Jarda");
65 assertEquals(model.getNames().size(), 1, "One element");
68 @Test public void arrayChangesNotified() {
69 MockKnockout my = new MockKnockout();
70 MockKnockout.next = my;
72 model.applyBindings();
74 model.getNames().add("Hello");
76 assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
77 assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
81 Iterator<String> it = model.getNames().iterator();
82 assertEquals(it.next(), "Hello");
85 assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
86 assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
90 ListIterator<String> lit = model.getNames().listIterator();
93 assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
94 assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
97 @Test public void autoboxedArray() {
98 MockKnockout my = new MockKnockout();
99 MockKnockout.next = my;
101 model.applyBindings();
103 model.getValues().add(10);
105 assertEquals(model.getValues().get(0), Integer.valueOf(10), "Really ten");
108 @Test public void derivedArrayProp() {
109 MockKnockout my = new MockKnockout();
110 MockKnockout.next = my;
112 model.applyBindings();
116 List<String> arr = model.getRepeat();
117 assertEquals(arr.size(), 10, "Ten items: " + arr);
123 arr = model.getRepeat();
124 assertEquals(arr.size(), 5, "Five items: " + arr);
126 assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
127 assertTrue(my.mutated.contains("repeat"), "Array is in there: " + my.mutated);
128 assertTrue(my.mutated.contains("count"), "Count is in there: " + my.mutated);
131 @Test public void derivedPropertiesAreNotified() {
132 MockKnockout my = new MockKnockout();
133 MockKnockout.next = my;
135 model.applyBindings();
139 assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
140 assertTrue(my.mutated.contains("powerValue"), "Power value is in there: " + my.mutated);
141 assertTrue(my.mutated.contains("value"), "Simple value is in there: " + my.mutated);
145 model.setUnrelated(44);
146 assertEquals(my.mutated.size(), 1, "One property changed");
147 assertTrue(my.mutated.contains("unrelated"), "Its name is unrelated");
150 @Test public void computedPropertyCannotWriteToModel() {
153 String res = model.getNotAllowedWrite();
154 fail("We should not be allowed to write to the model: " + res);
155 } catch (IllegalStateException ex) {
160 @Test public void computedPropertyCannotReadToModel() {
163 String res = model.getNotAllowedRead();
164 fail("We should not be allowed to read from the model: " + res);
165 } catch (IllegalStateException ex) {
171 static int powerValue(int value) {
172 return value * value;
176 static String notAllowedRead() {
177 return "Not allowed callback: " + leakedModel.getUnrelated();
181 static String notAllowedWrite() {
182 leakedModel.setUnrelated(11);
183 return "Not allowed callback!";
187 static List<String> repeat(int count) {
188 return Collections.nCopies(count, "Hello");
191 static class MockKnockout extends Knockout {
192 List<String> mutated = new ArrayList<String>();
195 public void valueHasMutated(String prop) {