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.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;
33 * @author Jaroslav Tulach <jtulach@netbeans.org>
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)
41 public class ModelTest {
43 private static Model leakedModel;
46 public void createModel() {
50 @Test public void classGeneratedWithSetterGetter() {
52 assertEquals(10, model.getValue(), "Value changed");
55 @Test public void computedMethod() {
57 assertEquals(16, model.getPowerValue());
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");
66 @Test public void arrayChangesNotified() {
67 MockKnockout my = new MockKnockout();
68 MockKnockout.next = my;
70 model.applyBindings();
72 model.getNames().add("Hello");
74 assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
75 assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
79 Iterator<String> it = model.getNames().iterator();
80 assertEquals(it.next(), "Hello");
83 assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
84 assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
88 ListIterator<String> lit = model.getNames().listIterator();
91 assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
92 assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
95 @Test public void autoboxedArray() {
96 MockKnockout my = new MockKnockout();
97 MockKnockout.next = my;
99 model.applyBindings();
101 model.getValues().add(10);
103 assertEquals(model.getValues().get(0), Integer.valueOf(10), "Really ten");
106 @Test public void derivedPropertiesAreNotified() {
107 MockKnockout my = new MockKnockout();
108 MockKnockout.next = my;
110 model.applyBindings();
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);
120 model.setUnrelated(44);
121 assertEquals(my.mutated.size(), 1, "One property changed");
122 assertTrue(my.mutated.contains("unrelated"), "Its name is unrelated");
125 @Test public void computedPropertyCannotWriteToModel() {
128 String res = model.getNotAllowedWrite();
129 fail("We should not be allowed to write to the model: " + res);
130 } catch (IllegalStateException ex) {
135 @Test public void computedPropertyCannotReadToModel() {
138 String res = model.getNotAllowedRead();
139 fail("We should not be allowed to read from the model: " + res);
140 } catch (IllegalStateException ex) {
146 static int powerValue(int value) {
147 return value * value;
151 static String notAllowedRead() {
152 return "Not allowed callback: " + leakedModel.getUnrelated();
156 static String notAllowedWrite() {
157 leakedModel.setUnrelated(11);
158 return "Not allowed callback!";
161 static class MockKnockout extends Knockout {
162 List<String> mutated = new ArrayList<String>();
165 public void valueHasMutated(String prop) {