001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.chain.config;
018:
019: import junit.framework.Test;
020: import junit.framework.TestCase;
021: import junit.framework.TestSuite;
022: import org.apache.commons.chain.Catalog;
023: import org.apache.commons.chain.Command;
024: import org.apache.commons.chain.Context;
025: import org.apache.commons.chain.impl.*;
026: import org.apache.commons.digester.Digester;
027:
028: import java.util.Iterator;
029:
030: /**
031: * <p>Test Case for <code>org.apache.commons.chain.config.ConfigParser</code>.</p>
032: */
033:
034: public class ConfigParserTestCase extends TestCase {
035:
036: private static final String DEFAULT_XML = "/org/apache/commons/chain/config/test-config.xml";
037:
038: // ------------------------------------------------------------ Constructors
039:
040: /**
041: * Construct a new instance of this test case.
042: *
043: * @param name Name of the test case
044: */
045: public ConfigParserTestCase(String name) {
046: super (name);
047: }
048:
049: // ------------------------------------------------------ Instance Variables
050:
051: /**
052: * <p>The <code>Catalog</code> to contain our configured commands.</p>
053: */
054: protected Catalog catalog = null;
055:
056: /**
057: * <p>The <code>Context</code> to use for execution tests.</p>
058: */
059: protected Context context = null;
060:
061: /**
062: * <p>The <code>ConfigParser</code> instance under test.</p>
063: */
064: protected ConfigParser parser = null;
065:
066: // ---------------------------------------------------- Overall Test Methods
067:
068: /**
069: * Set up instance variables required by this test case.
070: */
071: public void setUp() {
072: catalog = new CatalogBase();
073: context = new ContextBase();
074: parser = new ConfigParser();
075: }
076:
077: /**
078: * Return the tests included in this test suite.
079: */
080: public static Test suite() {
081: return (new TestSuite(ConfigParserTestCase.class));
082: }
083:
084: /**
085: * Tear down instance variables required by this test case.
086: */
087: public void tearDown() {
088: parser = null;
089: context = null;
090: catalog = null;
091: }
092:
093: // ------------------------------------------------ Individual Test Methods
094:
095: // Load the default test-config.xml file and examine the results
096: public void testDefaut() throws Exception {
097:
098: // Check overall command count
099: load(DEFAULT_XML);
100: checkCommandCount(17);
101:
102: // Check individual single command instances
103: Command command = null;
104:
105: command = catalog.getCommand("AddingCommand");
106: assertNotNull(command);
107: assertTrue(command instanceof AddingCommand);
108:
109: command = catalog.getCommand("DelegatingCommand");
110: assertNotNull(command);
111: assertTrue(command instanceof DelegatingCommand);
112:
113: command = catalog.getCommand("DelegatingFilter");
114: assertNotNull(command);
115: assertTrue(command instanceof DelegatingFilter);
116:
117: command = catalog.getCommand("ExceptionCommand");
118: assertNotNull(command);
119: assertTrue(command instanceof ExceptionCommand);
120:
121: command = catalog.getCommand("ExceptionFilter");
122: assertNotNull(command);
123: assertTrue(command instanceof ExceptionFilter);
124:
125: command = catalog.getCommand("NonDelegatingCommand");
126: assertNotNull(command);
127: assertTrue(command instanceof NonDelegatingCommand);
128:
129: command = catalog.getCommand("NonDelegatingFilter");
130: assertNotNull(command);
131: assertTrue(command instanceof NonDelegatingFilter);
132:
133: command = catalog.getCommand("ChainBase");
134: assertNotNull(command);
135: assertTrue(command instanceof ChainBase);
136: assertTrue(command instanceof TestChain);
137:
138: // Check configurable properties instance
139: TestCommand tcommand = (TestCommand) catalog
140: .getCommand("Configurable");
141: assertNotNull(tcommand);
142: assertEquals("Foo Value", tcommand.getFoo());
143: assertEquals("Bar Value", tcommand.getBar());
144:
145: }
146:
147: // Test execution of chain "Execute2a"
148: public void testExecute2a() throws Exception {
149:
150: load(DEFAULT_XML);
151: assertTrue("Chain returned true", catalog.getCommand(
152: "Execute2a").execute(context));
153: checkExecuteLog("1/2/3");
154:
155: }
156:
157: // Test execution of chain "Execute2b"
158: public void testExecute2b() throws Exception {
159:
160: load(DEFAULT_XML);
161: assertTrue("Chain returned false", !catalog.getCommand(
162: "Execute2b").execute(context));
163: checkExecuteLog("1/2/3");
164:
165: }
166:
167: // Test execution of chain "Execute2c"
168: public void testExecute2c() throws Exception {
169:
170: load(DEFAULT_XML);
171: try {
172: catalog.getCommand("Execute2c").execute(context);
173: } catch (ArithmeticException e) {
174: assertEquals("Correct exception id", "3", e.getMessage());
175: }
176: checkExecuteLog("1/2/3");
177:
178: }
179:
180: // Test execution of chain "Execute2d"
181: public void testExecute2d() throws Exception {
182:
183: load(DEFAULT_XML);
184: try {
185: catalog.getCommand("Execute2d").execute(context);
186: } catch (ArithmeticException e) {
187: assertEquals("Correct exception id", "2", e.getMessage());
188: }
189: checkExecuteLog("1/2");
190:
191: }
192:
193: // Test execution of chain "Execute4a"
194: public void testExecute4a() throws Exception {
195:
196: load(DEFAULT_XML);
197: assertTrue("Chain returned true", catalog.getCommand(
198: "Execute4a").execute(context));
199: checkExecuteLog("1/2/3/c/a");
200:
201: }
202:
203: // Test execution of chain "Execute2b"
204: public void testExecute4b() throws Exception {
205:
206: load(DEFAULT_XML);
207: assertTrue("Chain returned false", !catalog.getCommand(
208: "Execute4b").execute(context));
209: checkExecuteLog("1/2/3/b");
210:
211: }
212:
213: // Test execution of chain "Execute4c"
214: public void testExecute4c() throws Exception {
215:
216: load(DEFAULT_XML);
217: try {
218: catalog.getCommand("Execute4c").execute(context);
219: } catch (ArithmeticException e) {
220: assertEquals("Correct exception id", "3", e.getMessage());
221: }
222: checkExecuteLog("1/2/3/c/b/a");
223:
224: }
225:
226: // Test execution of chain "Execute4d"
227: public void testExecute4d() throws Exception {
228:
229: load(DEFAULT_XML);
230: try {
231: catalog.getCommand("Execute4d").execute(context);
232: } catch (ArithmeticException e) {
233: assertEquals("Correct exception id", "2", e.getMessage());
234: }
235: checkExecuteLog("1/2/b/a");
236:
237: }
238:
239: // Test a pristine ConfigParser instance
240: public void testPristine() {
241:
242: // Validate the "digester" property
243: Digester digester = parser.getDigester();
244: assertNotNull("Returned a Digester instance", digester);
245: assertTrue("Default namespaceAware", !digester
246: .getNamespaceAware());
247: assertTrue("Default useContextClassLoader", digester
248: .getUseContextClassLoader());
249: assertTrue("Default validating", !digester.getValidating());
250:
251: // Validate the "ruleSet" property
252: ConfigRuleSet ruleSet = (ConfigRuleSet) parser.getRuleSet();
253: assertNotNull("Returned a RuleSet instance", ruleSet);
254: assertEquals("Default chainElement", "chain", ruleSet
255: .getChainElement());
256: assertEquals("Default classAttribute", "className", ruleSet
257: .getClassAttribute());
258: assertEquals("Default commandElement", "command", ruleSet
259: .getCommandElement());
260: assertEquals("Default nameAttribute", "name", ruleSet
261: .getNameAttribute());
262: assertNull("Default namespaceURI", ruleSet.getNamespaceURI());
263:
264: // Validate the "useContextClassLoader" property
265: assertTrue("Defaults to use context class loader", parser
266: .getUseContextClassLoader());
267:
268: // Ensure that there are no preconfigured commands in the catalog
269: checkCommandCount(0);
270:
271: }
272:
273: // --------------------------------------------------------- Private Methods
274:
275: // Verify the number of configured commands
276: protected void checkCommandCount(int expected) {
277: int n = 0;
278: Iterator names = catalog.getNames();
279: while (names.hasNext()) {
280: String name = (String) names.next();
281: n++;
282: assertNotNull(name + " exists", catalog.getCommand(name));
283: }
284: assertEquals("Correct command count", expected, n);
285: }
286:
287: // Verify the contents of the execution log
288: protected void checkExecuteLog(String expected) {
289: StringBuffer log = (StringBuffer) context.get("log");
290: assertNotNull("Context returned log");
291: assertEquals("Context returned correct log", expected, log
292: .toString());
293: }
294:
295: // Load the specified catalog from the specified resource path
296: protected void load(String path) throws Exception {
297: parser.parse(this.getClass().getResource(path));
298: catalog = CatalogFactoryBase.getInstance().getCatalog();
299: }
300:
301: }
|