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