1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.chain.impl;
17
18 import junit.framework.Test;
19 import junit.framework.TestCase;
20 import junit.framework.TestSuite;
21 import org.apache.commons.chain.Catalog;
22 import org.apache.commons.chain.CatalogFactory;
23 import org.apache.commons.chain.Command;
24 import org.apache.commons.chain.impl.CatalogBase;
25 import java.util.Iterator;
26
27 /***
28 * <p>Test case for the <code>CatalogFactoryBase</code> class.</p>
29 *
30 * @author Craig R. McClanahan
31 * @version $Revision: 155403 $ $Date: 2005-02-26 12:52:46 +0000 (Sat, 26 Feb 2005) $
32 */
33
34 public class CatalogFactoryBaseTestCase extends TestCase {
35
36
37
38
39
40 /***
41 * <p>The {@link CatalogFactory} instance under test.</p>
42 */
43 protected CatalogFactory factory = null;
44
45
46
47
48
49 /***
50 * Construct a new instance of this test case.
51 *
52 * @param name Name of the test case
53 */
54 public CatalogFactoryBaseTestCase(String name) {
55 super(name);
56 }
57
58
59
60
61
62 /***
63 * <p>Set up instance variables required by this test case.</p>
64 */
65 public void setUp() {
66 factory = CatalogFactory.getInstance();
67 }
68
69
70 /***
71 * <p>Return the tests included in this test suite.</p>
72 */
73 public static Test suite() {
74 return (new TestSuite(CatalogFactoryBaseTestCase.class));
75 }
76
77 /***
78 * <p>Tear down instance variables required by this test case.</p>
79 */
80 public void tearDown() {
81 factory = null;
82 }
83
84
85
86
87
88 /***
89 * <p>Test a pristine instance of {@link CatalogFactory}.</p>
90 */
91 public void testPristine() {
92
93 assertNotNull(factory);
94 assertNull(factory.getCatalog());
95 assertNull(factory.getCatalog("foo"));
96 assertEquals(0, getCatalogCount());
97
98 }
99
100
101 /***
102 * <p>Test the default {@link Catalog} instance.</p>
103 */
104 public void testDefaultCatalog() {
105
106 Catalog catalog = new CatalogBase();
107 factory.setCatalog(catalog);
108 assertTrue(catalog == factory.getCatalog());
109 assertEquals(0, getCatalogCount());
110
111 }
112
113
114 /***
115 * <p>Test adding a specifically named {@link Catalog} instance.</p>
116 */
117 public void testSpecificCatalog() {
118
119 Catalog catalog = new CatalogBase();
120 factory.setCatalog(catalog);
121 catalog = new CatalogBase();
122 factory.addCatalog("foo", catalog);
123 assertTrue(catalog == factory.getCatalog("foo"));
124 assertEquals(1, getCatalogCount());
125 factory.addCatalog("foo", new CatalogBase());
126 assertEquals(1, getCatalogCount());
127 assertTrue(!(catalog == factory.getCatalog("foo")));
128 CatalogFactory.clear();
129 factory = CatalogFactory.getInstance();
130 assertEquals(0, getCatalogCount());
131
132 }
133
134
135 /***
136 * <p>Test <code>getCatalog()</code> method.</p>
137 */
138 public void testCatalogIdentifier() {
139
140 Catalog defaultCatalog = new CatalogBase();
141 Command defaultFoo = new NonDelegatingCommand();
142 defaultCatalog.addCommand("foo", defaultFoo);
143 Command fallback = new NonDelegatingCommand();
144 defaultCatalog.addCommand("noSuchCatalog:fallback", fallback);
145
146 factory.setCatalog(defaultCatalog);
147
148 Catalog specificCatalog = new CatalogBase();
149 Command specificFoo = new NonDelegatingCommand();
150 specificCatalog.addCommand("foo", specificFoo);
151 factory.addCatalog("specific", specificCatalog);
152
153 Command command = factory.getCommand("foo");
154 assertSame(defaultFoo, command);
155
156 command = factory.getCommand("specific:foo");
157 assertSame(specificFoo, command);
158
159 command = factory.getCommand("void");
160 assertNull(command);
161
162 command = factory.getCommand("foo:void");
163 assertNull(command);
164
165 command = factory.getCommand("specific:void");
166 assertNull(command);
167
168 command = factory.getCommand("noSuchCatalog:fallback");
169 assertNull(command);
170
171 try {
172 command = factory.getCommand("multiple:delimiters:reserved");
173 fail("A command ID with more than one delimiter should throw an IllegalArgumentException");
174 }
175 catch (IllegalArgumentException ex) {
176
177 }
178
179 }
180
181
182
183
184
185 /***
186 * <p>Return the number of {@link Catalog}s defined in our
187 * {@link CatalogFactory}.</p>
188 */
189 private int getCatalogCount() {
190
191 Iterator names = factory.getNames();
192 assertNotNull(names);
193 int n = 0;
194 while (names.hasNext()) {
195 names.next();
196 n++;
197 }
198 return n;
199
200 }
201
202
203 }