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 = "Modelik", 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),
42 @Property(name = "people", type = PersonImpl.class, array = true)
44 public class ModelTest {
45 private Modelik model;
46 private static Modelik leakedModel;
49 public void createModel() {
50 model = new Modelik();
53 @Test public void classGeneratedWithSetterGetter() {
55 assertEquals(10, model.getValue(), "Value changed");
58 @Test public void computedMethod() {
60 assertEquals(16, model.getPowerValue());
63 @Test public void arrayIsMutable() {
64 assertEquals(model.getNames().size(), 0, "Is empty");
65 model.getNames().add("Jarda");
66 assertEquals(model.getNames().size(), 1, "One element");
69 @Test public void arrayChangesNotified() {
70 MockKnockout my = new MockKnockout();
71 MockKnockout.next = my;
73 model.applyBindings();
75 model.getNames().add("Hello");
77 assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
78 assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
82 Iterator<String> it = model.getNames().iterator();
83 assertEquals(it.next(), "Hello");
86 assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
87 assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
91 ListIterator<String> lit = model.getNames().listIterator();
94 assertFalse(my.mutated.isEmpty(), "There was a change" + my.mutated);
95 assertTrue(my.mutated.contains("names"), "Change in names property: " + my.mutated);
98 @Test public void autoboxedArray() {
99 MockKnockout my = new MockKnockout();
100 MockKnockout.next = my;
102 model.applyBindings();
104 model.getValues().add(10);
106 assertEquals(model.getValues().get(0), Integer.valueOf(10), "Really ten");
109 @Test public void derivedArrayProp() {
110 MockKnockout my = new MockKnockout();
111 MockKnockout.next = my;
113 model.applyBindings();
117 List<String> arr = model.getRepeat();
118 assertEquals(arr.size(), 10, "Ten items: " + arr);
124 arr = model.getRepeat();
125 assertEquals(arr.size(), 5, "Five items: " + arr);
127 assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
128 assertTrue(my.mutated.contains("repeat"), "Array is in there: " + my.mutated);
129 assertTrue(my.mutated.contains("count"), "Count is in there: " + my.mutated);
132 @Test public void derivedPropertiesAreNotified() {
133 MockKnockout my = new MockKnockout();
134 MockKnockout.next = my;
136 model.applyBindings();
140 assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
141 assertTrue(my.mutated.contains("powerValue"), "Power value is in there: " + my.mutated);
142 assertTrue(my.mutated.contains("value"), "Simple value is in there: " + my.mutated);
146 model.setUnrelated(44);
147 assertEquals(my.mutated.size(), 1, "One property changed");
148 assertTrue(my.mutated.contains("unrelated"), "Its name is unrelated");
151 @Test public void computedPropertyCannotWriteToModel() {
154 String res = model.getNotAllowedWrite();
155 fail("We should not be allowed to write to the model: " + res);
156 } catch (IllegalStateException ex) {
161 @Test public void computedPropertyCannotReadToModel() {
164 String res = model.getNotAllowedRead();
165 fail("We should not be allowed to read from the model: " + res);
166 } catch (IllegalStateException ex) {
172 static int powerValue(int value) {
173 return value * value;
177 static String notAllowedRead() {
178 return "Not allowed callback: " + leakedModel.getUnrelated();
182 static String notAllowedWrite() {
183 leakedModel.setUnrelated(11);
184 return "Not allowed callback!";
188 static List<String> repeat(int count) {
189 return Collections.nCopies(count, "Hello");
192 static class MockKnockout extends Knockout {
193 List<String> mutated = new ArrayList<>();
196 public void valueHasMutated(String prop) {
201 public @Test void hasPersonPropertyAndComputedFullName() {
202 List<Person> arr = model.getPeople();
203 assertEquals(arr.size(), 0, "By default empty");
206 String fullNameGenerated = p.getFullName();
207 assertNotNull(fullNameGenerated);