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.CatalogFactoryBase;
026: import org.apache.commons.chain.impl.NonDelegatingCommand;
027:
028: /**
029: * <p>Test case for the <code>DispatchLookupCommand</code> class.</p>
030: *
031: * @author Sean Schofield
032: * @version $Revision: 155403 $
033: */
034:
035: public class DispatchLookupCommandTestCase extends TestCase {
036:
037: // ---------------------------------------------------- Instance Variables
038:
039: /**
040: * The instance of {@link Catalog} to use when looking up commands
041: */
042: protected Catalog catalog;
043:
044: /**
045: * The {@link DispatchLookupCommand} instance under test.
046: */
047: protected DispatchLookupCommand command;
048:
049: /**
050: * The {@link Context} instance on which to execute the chain.
051: */
052: protected Context context = null;
053:
054: // ---------------------------------------------------------- Constructors
055:
056: /**
057: * Construct a new instance of this test case.
058: *
059: * @param name Name of the test case
060: */
061: public DispatchLookupCommandTestCase(String name) {
062: super (name);
063: }
064:
065: // -------------------------------------------------- Overall Test Methods
066:
067: /**
068: * Set up instance variables required by this test case.
069: */
070: public void setUp() {
071: catalog = new CatalogBase();
072: CatalogFactoryBase.getInstance().setCatalog(catalog);
073: command = new DispatchLookupCommand();
074: context = new ContextBase();
075: }
076:
077: /**
078: * Return the tests included in this test suite.
079: *
080: * @return The suite of tests to run
081: */
082: public static Test suite() {
083: return (new TestSuite(DispatchLookupCommandTestCase.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 a dispatch method on a single
099: // non-delegating command
100: public void testExecuteDispatchLookup_1a() {
101:
102: // use default catalog
103: catalog.addCommand("fooCommand", new TestCommand("1"));
104:
105: // command should lookup the fooCommand and execute the fooMethod
106: command.setName("fooCommand");
107: command.setMethod("fooMethod");
108:
109: try {
110: assertTrue("Command should return true", command
111: .execute(context));
112: } catch (Exception e) {
113:
114: fail("Threw exception: " + e);
115: }
116:
117: // command should lookup the fooCommand and execute the barMethod
118: command.setMethod("barMethod");
119:
120: try {
121: assertTrue("Command should return true", command
122: .execute(context));
123: } catch (Exception e) {
124: fail("Threw exception: " + e);
125: }
126:
127: checkExecuteLog("1/1");
128:
129: }
130:
131: // Test IllegalArgumentException when incorrect command name specified
132: public void testExecuteDispatchLookup_2() {
133:
134: // use default catalog
135: catalog.addCommand("barCommand", new TestCommand("2"));
136:
137: // command should lookup the fooCommand and execute the fooMethod
138: command.setName("fooCommand");
139: command.setMethod("fooMethod");
140:
141: try {
142: command.execute(context);
143: } catch (IllegalArgumentException e) {
144: // test passed
145: return;
146: } catch (Exception e) {
147: // this is a failure
148: }
149:
150: fail("Expected IllegalArgumentException");
151: }
152:
153: // Test ability to lookup and execute a dispatch method on a single
154: // non-delegating command (using context to specify method name)
155: public void testExecuteDispatchLookup_3() {
156:
157: // use default catalog
158: catalog.addCommand("fooCommand", new TestCommand("3"));
159:
160: // command should lookup the fooCommand and execute the fooMethod
161: command.setName("fooCommand");
162: command.setMethodKey("methodKey");
163: context.put("methodKey", "fooMethod");
164:
165: try {
166: assertTrue("Command should return true", command
167: .execute(context));
168: } catch (Exception e) {
169: fail("Threw exception: " + e);
170: }
171:
172: // command should lookup the fooCommand and execute the barMethod
173: command.setMethodKey("methodKey");
174: context.put("methodKey", "barMethod");
175:
176: try {
177: assertTrue("Command should return true", command
178: .execute(context));
179: } catch (Exception e) {
180: fail("Threw exception: " + e);
181: }
182:
183: checkExecuteLog("3/3");
184:
185: }
186:
187: // -------------------------------------------------------- Support Methods
188:
189: // Verify the contents of the execution log
190: protected void checkExecuteLog(String expected) {
191: StringBuffer log = (StringBuffer) context.get("log");
192: assertNotNull("Context failed to return log", log);
193: assertEquals("Context returned correct log", expected, log
194: .toString());
195: }
196:
197: // ---------------------------------------------------------- Inner Classes
198:
199: class TestCommand extends NonDelegatingCommand {
200:
201: public TestCommand(String id) {
202: super (id);
203: }
204:
205: public boolean fooMethod(Context context) {
206: log(context, id);
207: return true;
208: }
209:
210: public boolean barMethod(Context context) {
211: log(context, id);
212: return true;
213: }
214:
215: }
216:
217: }
|