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.support;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    
021    import org.springframework.richclient.command.AbstractCommand;
022    import org.springframework.richclient.command.AbstractCommandRegistryTests;
023    import org.springframework.richclient.command.ActionCommand;
024    import org.springframework.richclient.command.ActionCommandExecutor;
025    import org.springframework.richclient.command.CommandGroup;
026    import org.springframework.richclient.command.CommandNotOfRequiredTypeException;
027    import org.springframework.richclient.command.CommandRegistry;
028    import org.springframework.richclient.command.CommandRegistryListener;
029    
030    /**
031     * Provides unit tests for the {@link DefaultCommandRegistry} class.
032     * 
033     * @author Peter De Bruycker
034     * @author Kevin Stembridge
035     */
036    public class DefaultCommandRegistryTests extends AbstractCommandRegistryTests {
037        
038        /**
039         * {@inheritDoc}
040         */
041        protected CommandRegistry getCommandRegistry() {
042            return new DefaultCommandRegistry();
043        }
044    
045        public void testConstructor() {
046            DefaultCommandRegistry registry = new DefaultCommandRegistry();
047            assertNull("parent must be null", registry.getParent());
048    
049            TestCommandRegistry parent = new TestCommandRegistry();
050            registry = new DefaultCommandRegistry(parent);
051            assertEquals("parent not set", parent, registry.getParent());
052            assertEquals("registry not added to parent", 1, parent.addedListeners.size());
053            assertTrue("registry not added to parent", parent.addedListeners.contains(registry));
054        }
055    
056        public void testSetParent() {
057            TestCommandRegistry parent = new TestCommandRegistry();
058            TestCommandRegistry parent2 = new TestCommandRegistry();
059    
060            DefaultCommandRegistry registry = new DefaultCommandRegistry(parent);
061    
062            registry.setParent(parent2);
063    
064            assertEquals("parent not set", parent2, registry.getParent());
065            assertEquals("registry not removed from parent", 1, parent.removedListeners.size());
066            assertTrue("registry not removed from parent", parent.removedListeners.contains(registry));
067            assertEquals("registry not added to parent", 1, parent2.addedListeners.size());
068            assertTrue("registry not added to parent", parent2.addedListeners.contains(registry));
069    
070            // set same parent, nothing should happen
071            registry.setParent(parent2);
072            assertEquals("registry added twice to same parent", 1, parent2.addedListeners.size());
073            assertTrue("registry removed from same parent", parent2.removedListeners.isEmpty());
074    
075            parent2.reset();
076    
077            // set parent to null
078            registry.setParent(null);
079            assertNull("parent not set to null", registry.getParent());
080            assertEquals("registry not removed from parent", 1, parent2.removedListeners.size());
081        }
082    
083        private static class TestCommandRegistry implements CommandRegistry {
084            private List addedListeners = new ArrayList();
085    
086            private List removedListeners = new ArrayList();
087    
088            public ActionCommand getActionCommand(String commandId) {
089                return null;
090            }
091    
092            public CommandGroup getCommandGroup(String groupId) {
093                return null;
094            }
095    
096            public boolean containsActionCommand(String commandId) {
097                return false;
098            }
099    
100            public boolean containsCommandGroup(String groupId) {
101                return false;
102            }
103    
104            public void registerCommand(AbstractCommand command) {
105            }
106    
107            public void setTargetableActionCommandExecutor(String targetableCommandId, ActionCommandExecutor commandExecutor) {
108            }
109    
110            public void addCommandRegistryListener(CommandRegistryListener l) {
111                addedListeners.add(l);
112            }
113    
114            public void removeCommandRegistryListener(CommandRegistryListener l) {
115                removedListeners.add(l);
116            }
117    
118            public void reset() {
119                addedListeners.clear();
120                removedListeners.clear();
121            }
122    
123            /**
124             * {@inheritDoc}
125             */
126            public boolean containsCommand(String commandId) {
127                return false;
128            }
129    
130            /**
131             * {@inheritDoc}
132             */
133            public Object getCommand(String commandId) {
134                return null;
135            }
136    
137            /**
138             * {@inheritDoc}
139             */
140            public Object getCommand(String commandId, Class requiredType) throws CommandNotOfRequiredTypeException {
141                return null;
142            }
143    
144            /**
145             * {@inheritDoc}
146             */
147            public Class getType(String commandId) {
148                return null;
149            }
150    
151            /**
152             * {@inheritDoc}
153             */
154            public boolean isTypeMatch(String commandId, Class targetType) {
155                return false;
156            }
157            
158        }
159        
160    }