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