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)
40 public class ModelTest {
42 private static Model leakedModel;
45 public void createModel() {
49 @Test public void classGeneratedWithSetterGetter() {
51 assertEquals(10, model.getValue(), "Value changed");
54 @Test public void computedMethod() {
56 assertEquals(16, model.getPowerValue());
59 @Test public void arrayIsMutable() {
60 assertEquals(model.getNames().size(), 0, "Is empty");
61 model.getNames().add("Jarda");
62 assertEquals(model.getNames().size(), 1, "One element");
65 @Test public void arrayChangesNotified() {
66 MockKnockout my = new MockKnockout();
67 MockKnockout.next = my;
69 model.applyBindings();
71 model.getNames().add("Hello");
73 assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
74 assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
78 Iterator<String> it = model.getNames().iterator();
79 assertEquals(it.next(), "Hello");
82 assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
83 assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
87 ListIterator<String> lit = model.getNames().listIterator();
90 assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
91 assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
94 @Test public void derivedPropertiesAreNotified() {
95 MockKnockout my = new MockKnockout();
96 MockKnockout.next = my;
98 model.applyBindings();
102 assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
103 assertTrue(my.mutated.contains("powerValue"), "Power value is in there: " + my.mutated);
104 assertTrue(my.mutated.contains("value"), "Simple value is in there: " + my.mutated);
108 model.setUnrelated(44);
109 assertEquals(my.mutated.size(), 1, "One property changed");
110 assertTrue(my.mutated.contains("unrelated"), "Its name is unrelated");
113 @Test public void computedPropertyCannotWriteToModel() {
116 String res = model.getNotAllowedWrite();
117 fail("We should not be allowed to write to the model: " + res);
118 } catch (IllegalStateException ex) {
123 @Test public void computedPropertyCannotReadToModel() {
126 String res = model.getNotAllowedRead();
127 fail("We should not be allowed to read from the model: " + res);
128 } catch (IllegalStateException ex) {
134 static int powerValue(int value) {
135 return value * value;
139 static String notAllowedRead() {
140 return "Not allowed callback: " + leakedModel.getUnrelated();
144 static String notAllowedWrite() {
145 leakedModel.setUnrelated(11);
146 return "Not allowed callback!";
149 static class MockKnockout extends Knockout {
150 List<String> mutated = new ArrayList<String>();
153 public void valueHasMutated(String prop) {