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.support;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.springframework.richclient.command.AbstractCommand;
22  import org.springframework.richclient.command.AbstractCommandRegistryTests;
23  import org.springframework.richclient.command.ActionCommand;
24  import org.springframework.richclient.command.ActionCommandExecutor;
25  import org.springframework.richclient.command.CommandGroup;
26  import org.springframework.richclient.command.CommandNotOfRequiredTypeException;
27  import org.springframework.richclient.command.CommandRegistry;
28  import org.springframework.richclient.command.CommandRegistryListener;
29  
30  /**
31   * Provides unit tests for the {@link DefaultCommandRegistry} class.
32   * 
33   * @author Peter De Bruycker
34   * @author Kevin Stembridge
35   */
36  public class DefaultCommandRegistryTests extends AbstractCommandRegistryTests {
37      
38      /**
39       * {@inheritDoc}
40       */
41      protected CommandRegistry getCommandRegistry() {
42          return new DefaultCommandRegistry();
43      }
44  
45      public void testConstructor() {
46          DefaultCommandRegistry registry = new DefaultCommandRegistry();
47          assertNull("parent must be null", registry.getParent());
48  
49          TestCommandRegistry parent = new TestCommandRegistry();
50          registry = new DefaultCommandRegistry(parent);
51          assertEquals("parent not set", parent, registry.getParent());
52          assertEquals("registry not added to parent", 1, parent.addedListeners.size());
53          assertTrue("registry not added to parent", parent.addedListeners.contains(registry));
54      }
55  
56      public void testSetParent() {
57          TestCommandRegistry parent = new TestCommandRegistry();
58          TestCommandRegistry parent2 = new TestCommandRegistry();
59  
60          DefaultCommandRegistry registry = new DefaultCommandRegistry(parent);
61  
62          registry.setParent(parent2);
63  
64          assertEquals("parent not set", parent2, registry.getParent());
65          assertEquals("registry not removed from parent", 1, parent.removedListeners.size());
66          assertTrue("registry not removed from parent", parent.removedListeners.contains(registry));
67          assertEquals("registry not added to parent", 1, parent2.addedListeners.size());
68          assertTrue("registry not added to parent", parent2.addedListeners.contains(registry));
69  
70          // set same parent, nothing should happen
71          registry.setParent(parent2);
72          assertEquals("registry added twice to same parent", 1, parent2.addedListeners.size());
73          assertTrue("registry removed from same parent", parent2.removedListeners.isEmpty());
74  
75          parent2.reset();
76  
77          // set parent to null
78          registry.setParent(null);
79          assertNull("parent not set to null", registry.getParent());
80          assertEquals("registry not removed from parent", 1, parent2.removedListeners.size());
81      }
82  
83      private static class TestCommandRegistry implements CommandRegistry {
84          private List addedListeners = new ArrayList();
85  
86          private List removedListeners = new ArrayList();
87  
88          public ActionCommand getActionCommand(String commandId) {
89              return null;
90          }
91  
92          public CommandGroup getCommandGroup(String groupId) {
93              return null;
94          }
95  
96          public boolean containsActionCommand(String commandId) {
97              return false;
98          }
99  
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 }