1   /*
2    * Copyright 1999-2004 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of 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,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.chain.impl;
17  
18  
19  import junit.framework.Test;
20  import junit.framework.TestCase;
21  import junit.framework.TestSuite;
22  import org.apache.commons.chain.Catalog;
23  import org.apache.commons.chain.Command;
24  
25  import java.util.Iterator;
26  
27  
28  /***
29   * <p>Test case for the <code>CatalogBase</code> class.</p>
30   *
31   * @author Craig R. McClanahan
32   * @version $Revision: 155403 $ $Date: 2005-02-26 12:52:46 +0000 (Sat, 26 Feb 2005) $
33   */
34  
35  public class CatalogBaseTestCase extends TestCase {
36  
37  
38      // ---------------------------------------------------- Instance Variables
39  
40  
41      /***
42       * The {@link Catalog} instance under test.
43       */
44      protected CatalogBase catalog = null;
45  
46  
47      // ---------------------------------------------------------- Constructors
48  
49      /***
50       * Construct a new instance of this test case.
51       *
52       * @param name Name of the test case
53       */
54      public CatalogBaseTestCase(String name) {
55          super(name);
56      }
57  
58  
59      // -------------------------------------------------- Overall Test Methods
60  
61  
62      /***
63       * Set up instance variables required by this test case.
64       */
65      public void setUp() {
66          catalog = new CatalogBase();
67      }
68  
69  
70      /***
71       * Return the tests included in this test suite.
72       */
73      public static Test suite() {
74          return (new TestSuite(CatalogBaseTestCase.class));
75      }
76  
77      /***
78       * Tear down instance variables required by this test case.
79       */
80      public void tearDown() {
81          catalog = null;
82      }
83  
84  
85      // ------------------------------------------------ Individual Test Methods
86  
87  
88      // Test adding commands
89      public void testAddCommand() {
90          addCommands();
91          checkCommandCount(8);
92      }
93  
94  
95      // Test getting commands
96      public void testGetCommand() {
97  
98          addCommands();
99          Command command = null;
100 
101         command = catalog.getCommand("AddingCommand");
102         assertNotNull(command);
103         assertTrue(command instanceof AddingCommand);
104 
105         command = catalog.getCommand("DelegatingCommand");
106         assertNotNull(command);
107         assertTrue(command instanceof DelegatingCommand);
108 
109         command = catalog.getCommand("DelegatingFilter");
110         assertNotNull(command);
111         assertTrue(command instanceof DelegatingFilter);
112 
113         command = catalog.getCommand("ExceptionCommand");
114         assertNotNull(command);
115         assertTrue(command instanceof ExceptionCommand);
116 
117         command = catalog.getCommand("ExceptionFilter");
118         assertNotNull(command);
119         assertTrue(command instanceof ExceptionFilter);
120 
121         command = catalog.getCommand("NonDelegatingCommand");
122         assertNotNull(command);
123         assertTrue(command instanceof NonDelegatingCommand);
124 
125         command = catalog.getCommand("NonDelegatingFilter");
126         assertNotNull(command);
127         assertTrue(command instanceof NonDelegatingFilter);
128 
129         command = catalog.getCommand("ChainBase");
130         assertNotNull(command);
131         assertTrue(command instanceof ChainBase);
132 
133     }
134 
135 
136     // The getNames() method is implicitly tested by checkCommandCount()
137 
138 
139     // Test pristine instance
140     public void testPristine() {
141         checkCommandCount(0);
142         assertNull(catalog.getCommand("AddingCommand"));
143         assertNull(catalog.getCommand("DelegatingCommand"));
144         assertNull(catalog.getCommand("DelegatingFilter"));
145         assertNull(catalog.getCommand("ExceptionCommand"));
146         assertNull(catalog.getCommand("ExceptionFilter"));
147         assertNull(catalog.getCommand("NonDelegatingCommand"));
148         assertNull(catalog.getCommand("NonDelegatingFilter"));
149         assertNull(catalog.getCommand("ChainBase"));
150     }
151 
152 
153 
154 
155     // -------------------------------------------------------- Support Methods
156 
157 
158     // Add an interesting set of commands to the catalog
159     protected void addCommands() {
160         catalog.addCommand("AddingCommand", new AddingCommand("", null));
161         catalog.addCommand("DelegatingCommand", new DelegatingCommand(""));
162         catalog.addCommand("DelegatingFilter", new DelegatingFilter("", ""));
163         catalog.addCommand("ExceptionCommand", new ExceptionCommand(""));
164         catalog.addCommand("ExceptionFilter", new ExceptionFilter("", ""));
165         catalog.addCommand("NonDelegatingCommand", new NonDelegatingCommand(""));
166         catalog.addCommand("NonDelegatingFilter", new NonDelegatingFilter("", ""));
167         catalog.addCommand("ChainBase", new ChainBase());
168     }
169 
170 
171     // Verify the number of configured commands
172     protected void checkCommandCount(int expected) {
173         int n = 0;
174         Iterator names = catalog.getNames();
175         while (names.hasNext()) {
176             String name = (String) names.next();
177             n++;
178             assertNotNull(name + " exists", catalog.getCommand(name));
179         }
180         assertEquals("Correct command count", expected, n);
181     }
182 
183 
184 }