001: package org.apache.commons.chain.generic;
002:
003: import junit.framework.TestCase;
004: import org.apache.commons.chain.Context;
005: import org.apache.commons.chain.impl.ContextBase;
006:
007: /* JUnitTest case for class: org.apache.commons.chain.generic.DispatchCommand */
008: public class DispatchCommandTestCase extends TestCase {
009:
010: public DispatchCommandTestCase(String _name) {
011: super (_name);
012: }
013:
014: /* setUp method for test case */
015: protected void setUp() {
016: }
017:
018: /* tearDown method for test case */
019: protected void tearDown() {
020: }
021:
022: /* Executes the test case */
023: public static void main(String[] argv) {
024: String[] testCaseList = { DispatchCommandTestCase.class
025: .getName() };
026: junit.textui.TestRunner.main(testCaseList);
027: }
028:
029: public void testMethodDispatch() throws Exception {
030: TestCommand test = new TestCommand();
031:
032: test.setMethod("testMethod");
033: Context context = new ContextBase();
034: assertNull(context.get("foo"));
035: boolean result = test.execute(context);
036: assertTrue(result);
037: assertNotNull(context.get("foo"));
038: assertEquals("foo", context.get("foo"));
039:
040: }
041:
042: public void testMethodKeyDispatch() throws Exception {
043: TestCommand test = new TestCommand();
044:
045: test.setMethodKey("foo");
046: Context context = new ContextBase();
047: context.put("foo", "testMethodKey");
048: assertNull(context.get("bar"));
049: boolean result = test.execute(context);
050: assertFalse(result);
051: assertNotNull(context.get("bar"));
052: assertEquals("bar", context.get("bar"));
053:
054: }
055:
056: public void testAlternateContext() throws Exception {
057: TestAlternateContextCommand test = new TestAlternateContextCommand();
058:
059: test.setMethod("foo");
060: Context context = new ContextBase();
061: assertNull(context.get("elephant"));
062: boolean result = test.execute(context);
063: assertTrue(result);
064: assertNotNull(context.get("elephant"));
065: assertEquals("elephant", context.get("elephant"));
066:
067: }
068:
069: class TestCommand extends DispatchCommand {
070:
071: public boolean testMethod(Context context) {
072: context.put("foo", "foo");
073: return true;
074: }
075:
076: public boolean testMethodKey(Context context) {
077:
078: context.put("bar", "bar");
079: return false;
080: }
081:
082: }
083:
084: /**
085: * Command which uses alternate method signature.
086: * <p>Title: Commons Chain</p>
087: * <p>Description: An implmentation of the GoF Chain of Responsibility pattern</p>
088: * <p>Copyright: Copyright (c) 2003-2004 The Apache Software Foundation - All Rights Reserved.</p>
089: * <p>Company: The Apache Software Foundation</p>
090: * @author germuska
091: * @version 0.2-dev
092: */
093: class TestAlternateContextCommand extends DispatchCommand {
094:
095: protected Class[] getSignature() {
096: return new Class[] { TestAlternateContext.class };
097: }
098:
099: protected Object[] getArguments(Context context) {
100: return new Object[] { new TestAlternateContext(context) };
101: }
102:
103: public boolean foo(TestAlternateContext context) {
104: context.put("elephant", "elephant");
105: return true;
106: }
107:
108: }
109:
110: class TestAlternateContext extends java.util.HashMap implements
111: Context {
112: Context wrappedContext = null;
113:
114: TestAlternateContext(Context context) {
115: this .wrappedContext = context;
116: }
117:
118: public Object get(Object o) {
119: return this .wrappedContext.get(o);
120: }
121:
122: public Object put(Object key, Object value) {
123: return this.wrappedContext.put(key, value);
124: }
125:
126: }
127: }
|