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