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.Test;
30 * @author Jaroslav Tulach <jtulach@netbeans.org>
32 @Page(xhtml = "Empty.html", className = "Model", properties = {
33 @Property(name = "value", type = int.class),
34 @Property(name = "unrelated", type = long.class)
36 public class ModelTest {
37 @Test public void classGeneratedWithSetterGetter() {
38 Class<?> c = Model.class;
39 assertNotNull(c, "Class for empty page generated");
41 assertEquals(10, Model.getValue(), "Value changed");
44 @Test public void computedMethod() {
46 assertEquals(16, Model.getPowerValue());
49 @Test public void derivedPropertiesAreNotified() {
50 MockKnockout my = new MockKnockout();
51 MockKnockout.next = my;
53 Model.applyBindings();
57 assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
58 assertTrue(my.mutated.contains("powerValue"), "Power value is in there: " + my.mutated);
59 assertTrue(my.mutated.contains("value"), "Simple value is in there: " + my.mutated);
63 Model.setUnrelated(44);
64 assertEquals(my.mutated.size(), 1, "One property changed");
65 assertTrue(my.mutated.contains("unrelated"), "Its name is unrelated");
68 @Test public void computedPropertyCannotWriteToModel() {
70 String res = Model.getNotAllowedWrite();
71 fail("We should not be allowed to write to the model: " + res);
72 } catch (IllegalStateException ex) {
77 @Test public void computedPropertyCannotReadToModel() {
79 String res = Model.getNotAllowedRead();
80 fail("We should not be allowed to read from the model: " + res);
81 } catch (IllegalStateException ex) {
87 static int powerValue(int value) {
92 static String notAllowedRead() {
93 return "Not allowed callback: " + Model.getUnrelated();
97 static String notAllowedWrite() {
98 Model.setUnrelated(11);
99 return "Not allowed callback!";
102 static class MockKnockout extends Knockout {
103 List<String> mutated = new ArrayList<String>();
106 public void valueHasMutated(String prop) {