1   /*
2    * Copyright 2002-2004 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
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   * Provides a suite of unit tests for the {@link CommandGroupFactoryBean} class.
28   *
29   * @author Kevin Stembridge
30   * @since 0.3
31   *
32   */
33  public class CommandGroupFactoryBeanTests extends TestCase {
34      
35      private AbstractCommand noOpCommand = new AbstractCommand() {
36          public void execute() {
37              //do nothing
38          }
39  
40          /**
41           * {@inheritDoc}
42           */
43          public String getId() {
44              return "noOpCommand";
45          }
46          
47      };
48      
49      private ToggleCommand toggleCommand = new ToggleCommand() {
50          
51          /**
52           * {@inheritDoc}
53           */
54          public String getId() {
55              return "toggleCommand";
56          }
57          
58      };
59  
60      /**
61       * Creates a new uninitialized {@code CommandGroupFactoryBeanTests}.
62       */
63      public CommandGroupFactoryBeanTests() {
64          super();
65      }
66      
67      /**
68       * Confirms that an exception is thrown from the afterPropertiesSet method if the
69       * encodedMembers property has not been set.
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       * Tests the constructor that takes the group id and members array.
87       * @throws Exception 
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              //do nothing, test passes
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      * Test method for {@link CommandGroupFactoryBean#setMembers(java.lang.Object[])}.
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             //test passes
124         }
125         
126         factoryBean.setMembers(new Object[] {});
127         
128     }
129 
130     /**
131      * Test method for {@link org.springframework.richclient.command.CommandGroupFactoryBean#setBeanName(java.lang.String)}.
132      * @throws Exception 
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         //confirm that setting the beanName will override the groupId
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      * Confirms that an exception is thrown if the 'group:' prefix appears in the members list
154      * with no following command name.
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      * Confirms that an exception is thrown if the 'command:' prefix appears in the members list
175      * with no following command name.
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      * Test method for {@link CommandGroupFactoryBean#createCommandGroup()}.
196      * @throws Exception 
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      * Test method for {@link CommandGroupFactoryBean#configureIfNecessary(AbstractCommand)}.
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             //test passes
235         }
236         
237         AbstractCommand command = new AbstractCommand() {
238             public void execute() {
239                 //do nothing
240             }
241         };
242         
243         //no configurer has been set, confirming that this doesn't throw an exception
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      * Test method for {@link CommandGroupFactoryBean#getObjectType()}.
260      */
261     public final void testGetObjectType() {
262         Assert.assertEquals(CommandGroup.class, new CommandGroupFactoryBean().getObjectType());
263     }
264 
265     /**
266      * Confirms that the command group is assigned the security controller id of the factory bean.
267      * @throws Exception 
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 }