1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.chain.config;
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.Command;
24 import org.apache.commons.chain.Context;
25 import org.apache.commons.chain.impl.*;
26 import org.apache.commons.digester.Digester;
27
28 import java.util.Iterator;
29
30
31 /***
32 * <p>Test Case for <code>org.apache.commons.chain.config.ConfigParser</code>.</p>
33 */
34
35 public class ConfigParserTestCase extends TestCase {
36
37
38 private static final String DEFAULT_XML =
39 "/org/apache/commons/chain/config/test-config.xml";
40
41
42
43
44
45 /***
46 * Construct a new instance of this test case.
47 *
48 * @param name Name of the test case
49 */
50 public ConfigParserTestCase(String name) {
51 super(name);
52 }
53
54
55
56
57
58 /***
59 * <p>The <code>Catalog</code> to contain our configured commands.</p>
60 */
61 protected Catalog catalog = null;
62
63
64 /***
65 * <p>The <code>Context</code> to use for execution tests.</p>
66 */
67 protected Context context = null;
68
69
70 /***
71 * <p>The <code>ConfigParser</code> instance under test.</p>
72 */
73 protected ConfigParser parser = null;
74
75
76
77
78
79 /***
80 * Set up instance variables required by this test case.
81 */
82 public void setUp() {
83 catalog = new CatalogBase();
84 context = new ContextBase();
85 parser = new ConfigParser();
86 }
87
88
89 /***
90 * Return the tests included in this test suite.
91 */
92 public static Test suite() {
93 return (new TestSuite(ConfigParserTestCase.class));
94 }
95
96
97 /***
98 * Tear down instance variables required by this test case.
99 */
100 public void tearDown() {
101 parser = null;
102 context = null;
103 catalog = null;
104 }
105
106
107
108
109
110
111 public void testDefaut() throws Exception {
112
113
114 load(DEFAULT_XML);
115 checkCommandCount(17);
116
117
118 Command command = null;
119
120 command = catalog.getCommand("AddingCommand");
121 assertNotNull(command);
122 assertTrue(command instanceof AddingCommand);
123
124 command = catalog.getCommand("DelegatingCommand");
125 assertNotNull(command);
126 assertTrue(command instanceof DelegatingCommand);
127
128 command = catalog.getCommand("DelegatingFilter");
129 assertNotNull(command);
130 assertTrue(command instanceof DelegatingFilter);
131
132 command = catalog.getCommand("ExceptionCommand");
133 assertNotNull(command);
134 assertTrue(command instanceof ExceptionCommand);
135
136 command = catalog.getCommand("ExceptionFilter");
137 assertNotNull(command);
138 assertTrue(command instanceof ExceptionFilter);
139
140 command = catalog.getCommand("NonDelegatingCommand");
141 assertNotNull(command);
142 assertTrue(command instanceof NonDelegatingCommand);
143
144 command = catalog.getCommand("NonDelegatingFilter");
145 assertNotNull(command);
146 assertTrue(command instanceof NonDelegatingFilter);
147
148 command = catalog.getCommand("ChainBase");
149 assertNotNull(command);
150 assertTrue(command instanceof ChainBase);
151 assertTrue(command instanceof TestChain);
152
153
154 TestCommand tcommand = (TestCommand) catalog.getCommand("Configurable");
155 assertNotNull(tcommand);
156 assertEquals("Foo Value", tcommand.getFoo());
157 assertEquals("Bar Value", tcommand.getBar());
158
159 }
160
161
162
163 public void testExecute2a() throws Exception {
164
165 load(DEFAULT_XML);
166 assertTrue("Chain returned true",
167 catalog.getCommand("Execute2a").execute(context));
168 checkExecuteLog("1/2/3");
169
170 }
171
172
173
174 public void testExecute2b() throws Exception {
175
176 load(DEFAULT_XML);
177 assertTrue("Chain returned false",
178 !catalog.getCommand("Execute2b").execute(context));
179 checkExecuteLog("1/2/3");
180
181 }
182
183
184
185 public void testExecute2c() throws Exception {
186
187 load(DEFAULT_XML);
188 try {
189 catalog.getCommand("Execute2c").execute(context);
190 } catch (ArithmeticException e) {
191 assertEquals("Correct exception id",
192 "3", e.getMessage());
193 }
194 checkExecuteLog("1/2/3");
195
196 }
197
198
199
200 public void testExecute2d() throws Exception {
201
202 load(DEFAULT_XML);
203 try {
204 catalog.getCommand("Execute2d").execute(context);
205 } catch (ArithmeticException e) {
206 assertEquals("Correct exception id",
207 "2", e.getMessage());
208 }
209 checkExecuteLog("1/2");
210
211 }
212
213
214
215 public void testExecute4a() throws Exception {
216
217 load(DEFAULT_XML);
218 assertTrue("Chain returned true",
219 catalog.getCommand("Execute4a").execute(context));
220 checkExecuteLog("1/2/3/c/a");
221
222 }
223
224
225
226 public void testExecute4b() throws Exception {
227
228 load(DEFAULT_XML);
229 assertTrue("Chain returned false",
230 !catalog.getCommand("Execute4b").execute(context));
231 checkExecuteLog("1/2/3/b");
232
233 }
234
235
236
237 public void testExecute4c() throws Exception {
238
239 load(DEFAULT_XML);
240 try {
241 catalog.getCommand("Execute4c").execute(context);
242 } catch (ArithmeticException e) {
243 assertEquals("Correct exception id",
244 "3", e.getMessage());
245 }
246 checkExecuteLog("1/2/3/c/b/a");
247
248 }
249
250
251
252 public void testExecute4d() throws Exception {
253
254 load(DEFAULT_XML);
255 try {
256 catalog.getCommand("Execute4d").execute(context);
257 } catch (ArithmeticException e) {
258 assertEquals("Correct exception id",
259 "2", e.getMessage());
260 }
261 checkExecuteLog("1/2/b/a");
262
263 }
264
265
266
267 public void testPristine() {
268
269
270 Digester digester = parser.getDigester();
271 assertNotNull("Returned a Digester instance", digester);
272 assertTrue("Default namespaceAware",
273 !digester.getNamespaceAware());
274 assertTrue("Default useContextClassLoader",
275 digester.getUseContextClassLoader());
276 assertTrue("Default validating",
277 !digester.getValidating());
278
279
280 ConfigRuleSet ruleSet = (ConfigRuleSet) parser.getRuleSet();
281 assertNotNull("Returned a RuleSet instance", ruleSet);
282 assertEquals("Default chainElement",
283 "chain", ruleSet.getChainElement());
284 assertEquals("Default classAttribute",
285 "className", ruleSet.getClassAttribute());
286 assertEquals("Default commandElement",
287 "command", ruleSet.getCommandElement());
288 assertEquals("Default nameAttribute",
289 "name", ruleSet.getNameAttribute());
290 assertNull("Default namespaceURI",
291 ruleSet.getNamespaceURI());
292
293
294 assertTrue("Defaults to use context class loader",
295 parser.getUseContextClassLoader());
296
297
298 checkCommandCount(0);
299
300 }
301
302
303
304
305
306
307 protected void checkCommandCount(int expected) {
308 int n = 0;
309 Iterator names = catalog.getNames();
310 while (names.hasNext()) {
311 String name = (String) names.next();
312 n++;
313 assertNotNull(name + " exists", catalog.getCommand(name));
314 }
315 assertEquals("Correct command count", expected, n);
316 }
317
318
319
320 protected void checkExecuteLog(String expected) {
321 StringBuffer log = (StringBuffer) context.get("log");
322 assertNotNull("Context returned log");
323 assertEquals("Context returned correct log",
324 expected, log.toString());
325 }
326
327
328
329 protected void load(String path) throws Exception {
330 parser.parse(this.getClass().getResource(path));
331 catalog = CatalogFactoryBase.getInstance().getCatalog();
332 }
333
334
335 }