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.List;
22 import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
23 import org.apidesign.bck2brwsr.htmlpage.api.Page;
24 import org.apidesign.bck2brwsr.htmlpage.api.Property;
25 import static org.testng.Assert.*;
26 import org.testng.annotations.BeforeMethod;
27 import org.testng.annotations.Test;
31 * @author Jaroslav Tulach <jtulach@netbeans.org>
33 @Page(xhtml = "Empty.html", className = "Model", properties = {
34 @Property(name = "value", type = int.class),
35 @Property(name = "unrelated", type = long.class),
36 @Property(name = "names", type = String.class, array = true)
38 public class ModelTest {
40 private static Model leakedModel;
43 public void createModel() {
47 @Test public void classGeneratedWithSetterGetter() {
49 assertEquals(10, model.getValue(), "Value changed");
52 @Test public void computedMethod() {
54 assertEquals(16, model.getPowerValue());
57 @Test public void arrayIsMutable() {
58 assertEquals(model.getNames().size(), 0, "Is empty");
59 model.getNames().add("Jarda");
60 assertEquals(model.getNames().size(), 1, "One element");
63 @Test public void derivedPropertiesAreNotified() {
64 MockKnockout my = new MockKnockout();
65 MockKnockout.next = my;
67 model.applyBindings();
71 assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
72 assertTrue(my.mutated.contains("powerValue"), "Power value is in there: " + my.mutated);
73 assertTrue(my.mutated.contains("value"), "Simple value is in there: " + my.mutated);
77 model.setUnrelated(44);
78 assertEquals(my.mutated.size(), 1, "One property changed");
79 assertTrue(my.mutated.contains("unrelated"), "Its name is unrelated");
82 @Test public void computedPropertyCannotWriteToModel() {
85 String res = model.getNotAllowedWrite();
86 fail("We should not be allowed to write to the model: " + res);
87 } catch (IllegalStateException ex) {
92 @Test public void computedPropertyCannotReadToModel() {
95 String res = model.getNotAllowedRead();
96 fail("We should not be allowed to read from the model: " + res);
97 } catch (IllegalStateException ex) {
103 static int powerValue(int value) {
104 return value * value;
108 static String notAllowedRead() {
109 return "Not allowed callback: " + leakedModel.getUnrelated();
113 static String notAllowedWrite() {
114 leakedModel.setUnrelated(11);
115 return "Not allowed callback!";
118 static class MockKnockout extends Knockout {
119 List<String> mutated = new ArrayList<String>();
122 public void valueHasMutated(String prop) {