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