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.Assert;
19 import junit.framework.TestCase;
20
21 import org.easymock.EasyMock;
22 import org.springframework.richclient.application.PropertyNotSetException;
23 import org.springframework.richclient.command.config.CommandConfigurer;
24
25
26
27
28
29
30
31
32
33 public class CommandGroupFactoryBeanTests extends TestCase {
34
35 private AbstractCommand noOpCommand = new AbstractCommand() {
36 public void execute() {
37
38 }
39
40
41
42
43 public String getId() {
44 return "noOpCommand";
45 }
46
47 };
48
49 private ToggleCommand toggleCommand = new ToggleCommand() {
50
51
52
53
54 public String getId() {
55 return "toggleCommand";
56 }
57
58 };
59
60
61
62
63 public CommandGroupFactoryBeanTests() {
64 super();
65 }
66
67
68
69
70
71 public void testForEncodedMembersNotSet() {
72
73 CommandGroupFactoryBean factoryBean = new CommandGroupFactoryBean();
74
75 try {
76 factoryBean.afterPropertiesSet();
77 Assert.fail("Should have thrown a PropertyNotSetException");
78 }
79 catch (PropertyNotSetException e) {
80 Assert.assertEquals("members", e.getPropertyName());
81 }
82
83 }
84
85
86
87
88
89 public final void testConstructorTakingGroupIdAndMembersArray() throws Exception {
90
91 String groupId = "groupId";
92 Object[] members = null;
93
94 try {
95 new CommandGroupFactoryBean(groupId, members);
96 Assert.fail("Should have thrown an IllegalArgumentException");
97 }
98 catch(IllegalArgumentException e) {
99
100 }
101
102 members = new Object[] {noOpCommand};
103
104 CommandGroupFactoryBean factoryBean = new CommandGroupFactoryBean(groupId, members);
105 CommandGroup commandGroup = (CommandGroup) factoryBean.getObject();
106 Assert.assertEquals(groupId, commandGroup.getId());
107 Assert.assertEquals(1, commandGroup.size());
108
109 }
110
111
112
113
114 public final void testSetMembers() {
115
116 CommandGroupFactoryBean factoryBean = new CommandGroupFactoryBean();
117
118 try {
119 factoryBean.setMembers(null);
120 Assert.fail("Should have thrown an IllegalArgumentException");
121 }
122 catch (IllegalArgumentException e) {
123
124 }
125
126 factoryBean.setMembers(new Object[] {});
127
128 }
129
130
131
132
133
134 public final void testSetBeanName() throws Exception {
135
136 String groupId = "bogusGroupId";
137 String beanName = "bogusBeanName";
138 Object[] members = new Object[] {noOpCommand};
139
140 CommandGroupFactoryBean factoryBean = new CommandGroupFactoryBean(groupId, members);
141 CommandGroup commandGroup = (CommandGroup) factoryBean.getObject();
142 Assert.assertEquals(groupId, commandGroup.getId());
143
144
145 factoryBean = new CommandGroupFactoryBean(groupId, members);
146 factoryBean.setBeanName(beanName);
147 commandGroup = (CommandGroup) factoryBean.getObject();
148 Assert.assertEquals(beanName, commandGroup.getId());
149
150 }
151
152
153
154
155
156 public void testInvalidGroupPrefix() {
157
158 Object[] members = new Object[] {CommandGroupFactoryBean.GROUP_MEMBER_PREFIX};
159
160 CommandGroupFactoryBean factoryBean = new CommandGroupFactoryBean();
161 factoryBean.setMembers(members);
162
163 try {
164 factoryBean.getCommandGroup();
165 Assert.fail("Should have thrown an InvalidGroupMemberEncodingException");
166 }
167 catch (InvalidGroupMemberEncodingException e) {
168 Assert.assertEquals(CommandGroupFactoryBean.GROUP_MEMBER_PREFIX, e.getEncodedString());
169 }
170
171 }
172
173
174
175
176
177 public void testInvalidCommandPrefix() {
178
179 Object[] members = new Object[] {CommandGroupFactoryBean.COMMAND_MEMBER_PREFIX};
180
181 CommandGroupFactoryBean factoryBean = new CommandGroupFactoryBean();
182 factoryBean.setMembers(members);
183
184 try {
185 factoryBean.getCommandGroup();
186 Assert.fail("Should have thrown an InvalidGroupMemberEncodingException");
187 }
188 catch (InvalidGroupMemberEncodingException e) {
189 Assert.assertEquals(CommandGroupFactoryBean.COMMAND_MEMBER_PREFIX, e.getEncodedString());
190 }
191
192 }
193
194
195
196
197
198 public final void testCreateCommandGroup() throws Exception {
199
200 String groupId = "bogusGroupId";
201 String securityId = "bogusSecurityId";
202 Object[] members = new Object[] {toggleCommand};
203
204 CommandGroupFactoryBean factoryBean = new CommandGroupFactoryBean(groupId, members);
205 factoryBean.setSecurityControllerId(securityId);
206 CommandGroup commandGroup = (CommandGroup) factoryBean.getObject();
207 Assert.assertEquals(securityId , commandGroup.getSecurityControllerId());
208 Assert.assertFalse("Assert command group not exclusive", commandGroup instanceof ExclusiveCommandGroup);
209 Assert.assertEquals(1, commandGroup.size());
210
211 factoryBean = new CommandGroupFactoryBean(groupId, members);
212 factoryBean.setExclusive(true);
213 factoryBean.setAllowsEmptySelection(true);
214 commandGroup = (CommandGroup) factoryBean.getObject();
215 Assert.assertTrue("Assert command group is exclusive", commandGroup instanceof ExclusiveCommandGroup);
216 Assert.assertTrue("Assert allows empty selection is true",
217 ((ExclusiveCommandGroup) commandGroup).getAllowsEmptySelection());
218
219
220 }
221
222
223
224
225 public final void testConfigureIfNecessary() {
226
227 CommandGroupFactoryBean factoryBean = new CommandGroupFactoryBean();
228
229 try {
230 factoryBean.configureIfNecessary(null);
231 Assert.fail("Should have thrown an IllegalArgumentException");
232 }
233 catch (IllegalArgumentException e) {
234
235 }
236
237 AbstractCommand command = new AbstractCommand() {
238 public void execute() {
239
240 }
241 };
242
243
244 factoryBean.configureIfNecessary(command);
245
246 CommandConfigurer configurer = (CommandConfigurer) EasyMock.createMock(CommandConfigurer.class);
247 EasyMock.expect(configurer.configure(command)).andReturn(command);
248
249 EasyMock.replay(configurer);
250
251 factoryBean.setCommandConfigurer(configurer);
252 factoryBean.configureIfNecessary(command);
253
254 EasyMock.verify(configurer);
255
256 }
257
258
259
260
261 public final void testGetObjectType() {
262 Assert.assertEquals(CommandGroup.class, new CommandGroupFactoryBean().getObjectType());
263 }
264
265
266
267
268
269 public final void testSecurityControllerIdIsApplied() throws Exception {
270
271 String groupId = "bogusGroupId";
272 String securityId = "bogusSecurityId";
273 Object[] members = new Object[] {noOpCommand};
274
275 CommandGroupFactoryBean factoryBean = new CommandGroupFactoryBean(groupId, members);
276 factoryBean.setSecurityControllerId(securityId);
277 CommandGroup commandGroup = (CommandGroup) factoryBean.getObject();
278 Assert.assertEquals(securityId , commandGroup.getSecurityControllerId());
279
280 }
281
282 }