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