KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > chain > config > ConfigParserTestCase


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
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 JavaDoc;
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 JavaDoc DEFAULT_XML =
39         "/org/apache/commons/chain/config/test-config.xml";
40
41
42     // ------------------------------------------------------------ Constructors
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 JavaDoc name) {
51         super(name);
52     }
53
54
55     // ------------------------------------------------------ Instance Variables
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     // ---------------------------------------------------- Overall Test Methods
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     // ------------------------------------------------ Individual Test Methods
108

109
110     // Load the default test-config.xml file and examine the results
111
public void testDefaut() throws Exception JavaDoc {
112
113         // Check overall command count
114
load(DEFAULT_XML);
115         checkCommandCount(17);
116
117         // Check individual single command instances
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         // Check configurable properties instance
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     // Test execution of chain "Execute2a"
163
public void testExecute2a() throws Exception JavaDoc {
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     // Test execution of chain "Execute2b"
174
public void testExecute2b() throws Exception JavaDoc {
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     // Test execution of chain "Execute2c"
185
public void testExecute2c() throws Exception JavaDoc {
186
187         load(DEFAULT_XML);
188         try {
189             catalog.getCommand("Execute2c").execute(context);
190         } catch (ArithmeticException JavaDoc e) {
191             assertEquals("Correct exception id",
192                          "3", e.getMessage());
193         }
194         checkExecuteLog("1/2/3");
195
196     }
197
198
199     // Test execution of chain "Execute2d"
200
public void testExecute2d() throws Exception JavaDoc {
201
202         load(DEFAULT_XML);
203         try {
204             catalog.getCommand("Execute2d").execute(context);
205         } catch (ArithmeticException JavaDoc e) {
206             assertEquals("Correct exception id",
207                          "2", e.getMessage());
208         }
209         checkExecuteLog("1/2");
210
211     }
212
213
214     // Test execution of chain "Execute4a"
215
public void testExecute4a() throws Exception JavaDoc {
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     // Test execution of chain "Execute2b"
226
public void testExecute4b() throws Exception JavaDoc {
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     // Test execution of chain "Execute4c"
237
public void testExecute4c() throws Exception JavaDoc {
238
239         load(DEFAULT_XML);
240         try {
241             catalog.getCommand("Execute4c").execute(context);
242         } catch (ArithmeticException JavaDoc e) {
243             assertEquals("Correct exception id",
244                          "3", e.getMessage());
245         }
246         checkExecuteLog("1/2/3/c/b/a");
247
248     }
249
250
251     // Test execution of chain "Execute4d"
252
public void testExecute4d() throws Exception JavaDoc {
253
254         load(DEFAULT_XML);
255         try {
256             catalog.getCommand("Execute4d").execute(context);
257         } catch (ArithmeticException JavaDoc e) {
258             assertEquals("Correct exception id",
259                          "2", e.getMessage());
260         }
261         checkExecuteLog("1/2/b/a");
262
263     }
264
265
266     // Test a pristine ConfigParser instance
267
public void testPristine() {
268
269         // Validate the "digester" property
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         // Validate the "ruleSet" property
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         // Validate the "useContextClassLoader" property
294
assertTrue("Defaults to use context class loader",
295                    parser.getUseContextClassLoader());
296
297         // Ensure that there are no preconfigured commands in the catalog
298
checkCommandCount(0);
299
300     }
301
302
303     // --------------------------------------------------------- Private Methods
304

305
306     // Verify the number of configured commands
307
protected void checkCommandCount(int expected) {
308         int n = 0;
309         Iterator JavaDoc names = catalog.getNames();
310         while (names.hasNext()) {
311             String JavaDoc name = (String JavaDoc) names.next();
312             n++;
313             assertNotNull(name + " exists", catalog.getCommand(name));
314         }
315         assertEquals("Correct command count", expected, n);
316     }
317
318
319     // Verify the contents of the execution log
320
protected void checkExecuteLog(String JavaDoc expected) {
321         StringBuffer JavaDoc log = (StringBuffer JavaDoc) context.get("log");
322         assertNotNull("Context returned log");
323         assertEquals("Context returned correct log",
324                      expected, log.toString());
325     }
326
327
328     // Load the specified catalog from the specified resource path
329
protected void load(String JavaDoc path) throws Exception JavaDoc {
330         parser.parse(this.getClass().getResource(path));
331         catalog = CatalogFactoryBase.getInstance().getCatalog();
332     }
333
334
335 }
336
Popular Tags