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