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.CatalogFactory;
023: import org.apache.commons.chain.Command;
024: import org.apache.commons.chain.impl.CatalogBase;
025: import java.util.Iterator;
026:
027: /**
028: * <p>Test case for the <code>CatalogFactoryBase</code> class.</p>
029: *
030: * @author Craig R. McClanahan
031: * @version $Revision: 155403 $ $Date: 2005-02-26 12:52:46 +0000 (Sat, 26 Feb 2005) $
032: */
033:
034: public class CatalogFactoryBaseTestCase extends TestCase {
035:
036: // ---------------------------------------------------- Instance Variables
037:
038: /**
039: * <p>The {@link CatalogFactory} instance under test.</p>
040: */
041: protected CatalogFactory factory = null;
042:
043: // ---------------------------------------------------------- Constructors
044:
045: /**
046: * Construct a new instance of this test case.
047: *
048: * @param name Name of the test case
049: */
050: public CatalogFactoryBaseTestCase(String name) {
051: super (name);
052: }
053:
054: // -------------------------------------------------- Overall Test Methods
055:
056: /**
057: * <p>Set up instance variables required by this test case.</p>
058: */
059: public void setUp() {
060: factory = CatalogFactory.getInstance();
061: }
062:
063: /**
064: * <p>Return the tests included in this test suite.</p>
065: */
066: public static Test suite() {
067: return (new TestSuite(CatalogFactoryBaseTestCase.class));
068: }
069:
070: /**
071: * <p>Tear down instance variables required by this test case.</p>
072: */
073: public void tearDown() {
074: factory = null;
075: }
076:
077: // ------------------------------------------------ Individual Test Methods
078:
079: /**
080: * <p>Test a pristine instance of {@link CatalogFactory}.</p>
081: */
082: public void testPristine() {
083:
084: assertNotNull(factory);
085: assertNull(factory.getCatalog());
086: assertNull(factory.getCatalog("foo"));
087: assertEquals(0, getCatalogCount());
088:
089: }
090:
091: /**
092: * <p>Test the default {@link Catalog} instance.</p>
093: */
094: public void testDefaultCatalog() {
095:
096: Catalog catalog = new CatalogBase();
097: factory.setCatalog(catalog);
098: assertTrue(catalog == factory.getCatalog());
099: assertEquals(0, getCatalogCount());
100:
101: }
102:
103: /**
104: * <p>Test adding a specifically named {@link Catalog} instance.</p>
105: */
106: public void testSpecificCatalog() {
107:
108: Catalog catalog = new CatalogBase();
109: factory.setCatalog(catalog);
110: catalog = new CatalogBase();
111: factory.addCatalog("foo", catalog);
112: assertTrue(catalog == factory.getCatalog("foo"));
113: assertEquals(1, getCatalogCount());
114: factory.addCatalog("foo", new CatalogBase());
115: assertEquals(1, getCatalogCount());
116: assertTrue(!(catalog == factory.getCatalog("foo")));
117: CatalogFactory.clear();
118: factory = CatalogFactory.getInstance();
119: assertEquals(0, getCatalogCount());
120:
121: }
122:
123: /**
124: * <p>Test <code>getCatalog()</code> method.</p>
125: */
126: public void testCatalogIdentifier() {
127:
128: Catalog defaultCatalog = new CatalogBase();
129: Command defaultFoo = new NonDelegatingCommand();
130: defaultCatalog.addCommand("foo", defaultFoo);
131: Command fallback = new NonDelegatingCommand();
132: defaultCatalog.addCommand("noSuchCatalog:fallback", fallback);
133:
134: factory.setCatalog(defaultCatalog);
135:
136: Catalog specificCatalog = new CatalogBase();
137: Command specificFoo = new NonDelegatingCommand();
138: specificCatalog.addCommand("foo", specificFoo);
139: factory.addCatalog("specific", specificCatalog);
140:
141: Command command = factory.getCommand("foo");
142: assertSame(defaultFoo, command);
143:
144: command = factory.getCommand("specific:foo");
145: assertSame(specificFoo, command);
146:
147: command = factory.getCommand("void");
148: assertNull(command);
149:
150: command = factory.getCommand("foo:void");
151: assertNull(command);
152:
153: command = factory.getCommand("specific:void");
154: assertNull(command);
155:
156: command = factory.getCommand("noSuchCatalog:fallback");
157: assertNull(command);
158:
159: try {
160: command = factory
161: .getCommand("multiple:delimiters:reserved");
162: fail("A command ID with more than one delimiter should throw an IllegalArgumentException");
163: } catch (IllegalArgumentException ex) {
164: // expected behavior
165: }
166:
167: }
168:
169: // ------------------------------------------------------- Support Methods
170:
171: /**
172: * <p>Return the number of {@link Catalog}s defined in our
173: * {@link CatalogFactory}.</p>
174: */
175: private int getCatalogCount() {
176:
177: Iterator names = factory.getNames();
178: assertNotNull(names);
179: int n = 0;
180: while (names.hasNext()) {
181: names.next();
182: n++;
183: }
184: return n;
185:
186: }
187:
188: }
|