Moving towards testability: Model class does not have any public fields, rather instance onces
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)
37 public class ModelTest {
39 private static Model leakedModel;
42 public void createModel() {
46 @Test public void classGeneratedWithSetterGetter() {
48 assertEquals(10, model.getValue(), "Value changed");
51 @Test public void computedMethod() {
53 assertEquals(16, model.getPowerValue());
56 @Test public void derivedPropertiesAreNotified() {
57 MockKnockout my = new MockKnockout();
58 MockKnockout.next = my;
60 model.applyBindings();
64 assertEquals(my.mutated.size(), 2, "Two properties changed: " + my.mutated);
65 assertTrue(my.mutated.contains("powerValue"), "Power value is in there: " + my.mutated);
66 assertTrue(my.mutated.contains("value"), "Simple value is in there: " + my.mutated);
70 model.setUnrelated(44);
71 assertEquals(my.mutated.size(), 1, "One property changed");
72 assertTrue(my.mutated.contains("unrelated"), "Its name is unrelated");
75 @Test public void computedPropertyCannotWriteToModel() {
78 String res = model.getNotAllowedWrite();
79 fail("We should not be allowed to write to the model: " + res);
80 } catch (IllegalStateException ex) {
85 @Test public void computedPropertyCannotReadToModel() {
88 String res = model.getNotAllowedRead();
89 fail("We should not be allowed to read from the model: " + res);
90 } catch (IllegalStateException ex) {
96 static int powerValue(int value) {
101 static String notAllowedRead() {
102 return "Not allowed callback: " + leakedModel.getUnrelated();
106 static String notAllowedWrite() {
107 leakedModel.setUnrelated(11);
108 return "Not allowed callback!";
111 static class MockKnockout extends Knockout {
112 List<String> mutated = new ArrayList<String>();
115 public void valueHasMutated(String prop) {