001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.chain.impl;
018    
019    
020    import junit.framework.Test;
021    import junit.framework.TestCase;
022    import junit.framework.TestSuite;
023    import org.apache.commons.chain.Catalog;
024    import org.apache.commons.chain.Command;
025    
026    import java.util.Iterator;
027    
028    
029    /**
030     * <p>Test case for the <code>CatalogBase</code> class.</p>
031     *
032     * @author Craig R. McClanahan
033     * @version $Revision: 480477 $ $Date: 2006-11-29 08:34:52 +0000 (Wed, 29 Nov 2006) $
034     */
035    
036    public class CatalogBaseTestCase extends TestCase {
037    
038    
039        // ---------------------------------------------------- Instance Variables
040    
041    
042        /**
043         * The {@link Catalog} instance under test.
044         */
045        protected CatalogBase catalog = null;
046    
047    
048        // ---------------------------------------------------------- Constructors
049    
050        /**
051         * Construct a new instance of this test case.
052         *
053         * @param name Name of the test case
054         */
055        public CatalogBaseTestCase(String name) {
056            super(name);
057        }
058    
059    
060        // -------------------------------------------------- Overall Test Methods
061    
062    
063        /**
064         * Set up instance variables required by this test case.
065         */
066        public void setUp() {
067            catalog = new CatalogBase();
068        }
069    
070    
071        /**
072         * Return the tests included in this test suite.
073         */
074        public static Test suite() {
075            return (new TestSuite(CatalogBaseTestCase.class));
076        }
077    
078        /**
079         * Tear down instance variables required by this test case.
080         */
081        public void tearDown() {
082            catalog = null;
083        }
084    
085    
086        // ------------------------------------------------ Individual Test Methods
087    
088    
089        // Test adding commands
090        public void testAddCommand() {
091            addCommands();
092            checkCommandCount(8);
093        }
094    
095    
096        // Test getting commands
097        public void testGetCommand() {
098    
099            addCommands();
100            Command command = null;
101    
102            command = catalog.getCommand("AddingCommand");
103            assertNotNull(command);
104            assertTrue(command instanceof AddingCommand);
105    
106            command = catalog.getCommand("DelegatingCommand");
107            assertNotNull(command);
108            assertTrue(command instanceof DelegatingCommand);
109    
110            command = catalog.getCommand("DelegatingFilter");
111            assertNotNull(command);
112            assertTrue(command instanceof DelegatingFilter);
113    
114            command = catalog.getCommand("ExceptionCommand");
115            assertNotNull(command);
116            assertTrue(command instanceof ExceptionCommand);
117    
118            command = catalog.getCommand("ExceptionFilter");
119            assertNotNull(command);
120            assertTrue(command instanceof ExceptionFilter);
121    
122            command = catalog.getCommand("NonDelegatingCommand");
123            assertNotNull(command);
124            assertTrue(command instanceof NonDelegatingCommand);
125    
126            command = catalog.getCommand("NonDelegatingFilter");
127            assertNotNull(command);
128            assertTrue(command instanceof NonDelegatingFilter);
129    
130            command = catalog.getCommand("ChainBase");
131            assertNotNull(command);
132            assertTrue(command instanceof ChainBase);
133    
134        }
135    
136    
137        // The getNames() method is implicitly tested by checkCommandCount()
138    
139    
140        // Test pristine instance
141        public void testPristine() {
142            checkCommandCount(0);
143            assertNull(catalog.getCommand("AddingCommand"));
144            assertNull(catalog.getCommand("DelegatingCommand"));
145            assertNull(catalog.getCommand("DelegatingFilter"));
146            assertNull(catalog.getCommand("ExceptionCommand"));
147            assertNull(catalog.getCommand("ExceptionFilter"));
148            assertNull(catalog.getCommand("NonDelegatingCommand"));
149            assertNull(catalog.getCommand("NonDelegatingFilter"));
150            assertNull(catalog.getCommand("ChainBase"));
151        }
152    
153    
154    
155    
156        // -------------------------------------------------------- Support Methods
157    
158    
159        // Add an interesting set of commands to the catalog
160        protected void addCommands() {
161            catalog.addCommand("AddingCommand", new AddingCommand("", null));
162            catalog.addCommand("DelegatingCommand", new DelegatingCommand(""));
163            catalog.addCommand("DelegatingFilter", new DelegatingFilter("", ""));
164            catalog.addCommand("ExceptionCommand", new ExceptionCommand(""));
165            catalog.addCommand("ExceptionFilter", new ExceptionFilter("", ""));
166            catalog.addCommand("NonDelegatingCommand", new NonDelegatingCommand(""));
167            catalog.addCommand("NonDelegatingFilter", new NonDelegatingFilter("", ""));
168            catalog.addCommand("ChainBase", new ChainBase());
169        }
170    
171    
172        // Verify the number of configured commands
173        protected void checkCommandCount(int expected) {
174            int n = 0;
175            Iterator names = catalog.getNames();
176            while (names.hasNext()) {
177                String name = (String) names.next();
178                n++;
179                assertNotNull(name + " exists", catalog.getCommand(name));
180            }
181            assertEquals("Correct command count", expected, n);
182        }
183    
184    
185    }