1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.springframework.richclient.command;
17
18 import junit.framework.TestCase;
19
20 import java.beans.PropertyChangeEvent;
21 import java.beans.PropertyChangeListener;
22
23
24
25
26
27 public abstract class AbstractCommandTests extends TestCase {
28
29 private TestListener testListener;
30
31 protected void setUp() throws Exception {
32 testListener = new TestListener();
33 }
34
35 public void testEnabledState() throws Exception {
36 TestAbstractCommand command = new TestAbstractCommand();
37 command.addEnabledListener(testListener);
38
39 command.secondaryEnabledState = false;
40 assertFalse(command.isEnabled());
41 command.setSecondaryEnabledState(false);
42 assertEquals(0, testListener.eventCount);
43 assertFalse(command.isEnabled());
44
45
46 testListener.eventCount = 0;
47 command.setSecondaryEnabledState(true);
48 assertEquals(1, testListener.eventCount);
49 assertTrue(command.isEnabled());
50
51
52 command = new TestAbstractCommand();
53 command.addEnabledListener(testListener);
54 testListener.eventCount = 0;
55 command.secondaryEnabledState = true;
56 assertTrue(command.isEnabled());
57 command.setSecondaryEnabledState(false);
58 assertEquals(1, testListener.eventCount);
59 assertFalse(command.isEnabled());
60 }
61
62 public void testVisibleState() throws Exception {
63 TestAbstractCommand command = new TestAbstractCommand();
64 command.addPropertyChangeListener("visible", testListener);
65
66 command.secondaryVisibleState = false;
67 command.setSecondaryVisibleState(false);
68 assertEquals(0, testListener.eventCount);
69 assertFalse(command.isVisible());
70
71
72 testListener.eventCount = 0;
73 command.setSecondaryVisibleState(true);
74 assertEquals(1, testListener.eventCount);
75 assertTrue(command.isVisible());
76
77
78 command = new TestAbstractCommand();
79 command.addPropertyChangeListener("visible", testListener);
80 testListener.eventCount = 0;
81 command.secondaryVisibleState = true;
82 assertTrue(command.isVisible());
83 command.setSecondaryVisibleState(false);
84 assertEquals(1, testListener.eventCount);
85 assertFalse(command.isVisible());
86 }
87
88 protected class TestListener implements PropertyChangeListener {
89
90 int eventCount = 0;
91
92 public void propertyChange(PropertyChangeEvent evt) {
93 eventCount++;
94 }
95
96 }
97
98 protected class TestAbstractCommand extends AbstractCommand {
99
100 boolean secondaryEnabledState;
101
102 boolean secondaryVisibleState;
103
104 public boolean isEnabled() {
105 return super.isEnabled() && secondaryEnabledState;
106 }
107
108 public boolean isVisible() {
109 return super.isVisible() && secondaryVisibleState;
110 }
111
112 public void setSecondaryEnabledState(boolean secondaryEnabledState) {
113 if (secondaryEnabledState != this.secondaryEnabledState) {
114 this.secondaryEnabledState = secondaryEnabledState;
115 updatedEnabledState();
116 }
117 }
118
119 public void setSecondaryVisibleState(boolean secondaryVisibleState) {
120 if (secondaryVisibleState != this.secondaryVisibleState) {
121 this.secondaryVisibleState = secondaryVisibleState;
122 updatedVisibleState();
123 }
124 }
125
126 public void execute() {
127 }
128 }
129 }