001    /*
002     * Copyright 2002-2004 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.command;
017    
018    import junit.framework.Assert;
019    import junit.framework.TestCase;
020    
021    import org.easymock.EasyMock;
022    import org.springframework.richclient.application.PropertyNotSetException;
023    import org.springframework.richclient.command.config.CommandConfigurer;
024    
025    
026    /**
027     * Provides a suite of unit tests for the {@link CommandGroupFactoryBean} class.
028     *
029     * @author Kevin Stembridge
030     * @since 0.3
031     *
032     */
033    public class CommandGroupFactoryBeanTests extends TestCase {
034        
035        private AbstractCommand noOpCommand = new AbstractCommand() {
036            public void execute() {
037                //do nothing
038            }
039    
040            /**
041             * {@inheritDoc}
042             */
043            public String getId() {
044                return "noOpCommand";
045            }
046            
047        };
048        
049        private ToggleCommand toggleCommand = new ToggleCommand() {
050            
051            /**
052             * {@inheritDoc}
053             */
054            public String getId() {
055                return "toggleCommand";
056            }
057            
058        };
059    
060        /**
061         * Creates a new uninitialized {@code CommandGroupFactoryBeanTests}.
062         */
063        public CommandGroupFactoryBeanTests() {
064            super();
065        }
066        
067        /**
068         * Confirms that an exception is thrown from the afterPropertiesSet method if the
069         * encodedMembers property has not been set.
070         */
071        public void testForEncodedMembersNotSet() {
072            
073            CommandGroupFactoryBean factoryBean = new CommandGroupFactoryBean();
074            
075            try {
076                factoryBean.afterPropertiesSet();
077                Assert.fail("Should have thrown a PropertyNotSetException");
078            }
079            catch (PropertyNotSetException e) {
080                Assert.assertEquals("members", e.getPropertyName());
081            }
082            
083        }
084    
085        /**
086         * Tests the constructor that takes the group id and members array.
087         * @throws Exception 
088         */
089        public final void testConstructorTakingGroupIdAndMembersArray() throws Exception {
090            
091            String groupId = "groupId";
092            Object[] members = null;
093            
094            try {
095                new CommandGroupFactoryBean(groupId, members);
096                Assert.fail("Should have thrown an IllegalArgumentException");
097            }
098            catch(IllegalArgumentException e) {
099                //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    }