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: package org.apache.commons.chain.generic;
017:
018: import junit.framework.Test;
019: import junit.framework.TestCase;
020: import junit.framework.TestSuite;
021: import org.apache.commons.chain.Context;
022: import org.apache.commons.chain.Catalog;
023: import org.apache.commons.chain.impl.CatalogBase;
024: import org.apache.commons.chain.impl.ContextBase;
025: import org.apache.commons.chain.impl.ChainBase;
026: import org.apache.commons.chain.impl.CatalogFactoryBase;
027: import org.apache.commons.chain.impl.DelegatingCommand;
028: import org.apache.commons.chain.impl.NonDelegatingCommand;
029:
030: /**
031: * <p>Test case for the <code>LookupCommand</code> class.</p>
032: *
033: * @author Sean Schofield
034: * @version $Revision: 155403 $
035: */
036:
037: public class LookupCommandTestCase extends TestCase {
038:
039: // ---------------------------------------------------- Instance Variables
040:
041: /**
042: * The instance of {@link Catalog} to use when looking up commands
043: */
044: protected Catalog catalog;
045:
046: /**
047: * The {@link LookupCommand} instance under test.
048: */
049: protected LookupCommand command;
050:
051: /**
052: * The {@link Context} instance on which to execute the chain.
053: */
054: protected Context context = null;
055:
056: // ---------------------------------------------------------- Constructors
057:
058: /**
059: * Construct a new instance of this test case.
060: *
061: * @param name Name of the test case
062: */
063: public LookupCommandTestCase(String name) {
064: super (name);
065: }
066:
067: // -------------------------------------------------- Overall Test Methods
068:
069: /**
070: * Set up instance variables required by this test case.
071: */
072: public void setUp() {
073: catalog = new CatalogBase();
074: CatalogFactoryBase.getInstance().setCatalog(catalog);
075: command = new LookupCommand();
076: context = new ContextBase();
077: }
078:
079: /**
080: * Return the tests included in this test suite.
081: */
082: public static Test suite() {
083: return (new TestSuite(LookupCommandTestCase.class));
084: }
085:
086: /**
087: * Tear down instance variables required by this test case.
088: */
089: public void tearDown() {
090: catalog = null;
091: CatalogFactoryBase.getInstance().clear();
092: command = null;
093: context = null;
094: }
095:
096: // ------------------------------------------------ Individual Test Methods
097:
098: // Test ability to lookup and execute single non-delegating command
099: public void testExecuteMethodLookup_1a() {
100:
101: // use default catalog
102: catalog.addCommand("foo", new NonDelegatingCommand("1a"));
103: command.setName("foo");
104:
105: try {
106: assertTrue("Command should return true", command
107: .execute(context));
108: } catch (Exception e) {
109: fail("Threw exception: " + e);
110: }
111: checkExecuteLog("1a");
112: }
113:
114: // Test ability to lookup and execute a chain
115: public void testExecuteMethodLookup_1b() {
116:
117: ChainBase chain = new ChainBase();
118: chain.addCommand(new DelegatingCommand("1b1"));
119: chain.addCommand(new DelegatingCommand("1b2"));
120: chain.addCommand(new NonDelegatingCommand("1b3"));
121:
122: // use default catalog
123: catalog.addCommand("foo", chain);
124: command.setName("foo");
125:
126: try {
127: assertTrue("Command should return true", command
128: .execute(context));
129: } catch (Exception e) {
130: fail("Threw exception: " + e);
131: }
132: checkExecuteLog("1b1/1b2/1b3");
133: }
134:
135: // Test ability to lookup and execute single non-delegating command
136: // using the context
137: public void testExecuteMethodLookup_2a() {
138:
139: // use default catalog
140: catalog.addCommand("foo", new NonDelegatingCommand("2a"));
141: command.setNameKey("nameKey");
142: context.put("nameKey", "foo");
143:
144: try {
145: assertTrue("Command should return true", command
146: .execute(context));
147: } catch (Exception e) {
148: fail("Threw exception: " + e);
149: }
150: checkExecuteLog("2a");
151: }
152:
153: // Test ability to lookup and execute a chain using the context
154: public void testExecuteMethodLookup_2b() {
155:
156: ChainBase chain = new ChainBase();
157: chain.addCommand(new DelegatingCommand("2b1"));
158: chain.addCommand(new DelegatingCommand("2b2"));
159: chain.addCommand(new NonDelegatingCommand("2b3"));
160:
161: // use default catalog
162: catalog.addCommand("foo", chain);
163: command.setNameKey("nameKey");
164: context.put("nameKey", "foo");
165:
166: try {
167: assertTrue("Command should return true", command
168: .execute(context));
169: } catch (Exception e) {
170: fail("Threw exception: " + e);
171: }
172: checkExecuteLog("2b1/2b2/2b3");
173: }
174:
175: // Test ability to lookup and execute single non-delegating command, ignoring its result
176: public void testExecuteMethodLookup_3a() {
177:
178: // use default catalog
179: catalog.addCommand("foo", new NonDelegatingCommand("3a"));
180: command.setIgnoreExecuteResult(true);
181: command.setName("foo");
182:
183: try {
184: assertFalse("Command should return false", command
185: .execute(context));
186: } catch (Exception e) {
187: fail("Threw exception: " + e);
188: }
189: checkExecuteLog("3a");
190: }
191:
192: // -------------------------------------------------------- Support Methods
193:
194: // Verify the contents of the execution log
195: protected void checkExecuteLog(String expected) {
196: StringBuffer log = (StringBuffer) context.get("log");
197: assertNotNull("Context failed to return log", log);
198: assertEquals("Context returned correct log", expected, log
199: .toString());
200: }
201:
202: }
|