1 package org.apache.commons.chain.generic;
2
3 import junit.framework.TestCase;
4 import org.apache.commons.chain.Context;
5 import org.apache.commons.chain.impl.ContextBase;
6
7
8 public class DispatchCommandTestCase extends TestCase {
9
10 public DispatchCommandTestCase(String _name) {
11 super(_name);
12 }
13
14
15 protected void setUp() {
16 }
17
18
19 protected void tearDown() {
20 }
21
22
23 public static void main(String[] argv) {
24 String[] testCaseList = {DispatchCommandTestCase.class.getName()};
25 junit.textui.TestRunner.main(testCaseList);
26 }
27
28 public void testMethodDispatch() throws Exception {
29 TestCommand test = new TestCommand();
30
31 test.setMethod("testMethod");
32 Context context = new ContextBase();
33 assertNull(context.get("foo"));
34 boolean result = test.execute(context);
35 assertTrue(result);
36 assertNotNull(context.get("foo"));
37 assertEquals("foo", context.get("foo"));
38
39
40 }
41
42
43 public void testMethodKeyDispatch() throws Exception {
44 TestCommand test = new TestCommand();
45
46 test.setMethodKey("foo");
47 Context context = new ContextBase();
48 context.put("foo", "testMethodKey");
49 assertNull(context.get("bar"));
50 boolean result = test.execute(context);
51 assertFalse(result);
52 assertNotNull(context.get("bar"));
53 assertEquals("bar", context.get("bar"));
54
55
56 }
57
58 public void testAlternateContext() throws Exception {
59 TestAlternateContextCommand test = new TestAlternateContextCommand();
60
61 test.setMethod("foo");
62 Context context = new ContextBase();
63 assertNull(context.get("elephant"));
64 boolean result = test.execute(context);
65 assertTrue(result);
66 assertNotNull(context.get("elephant"));
67 assertEquals("elephant", context.get("elephant"));
68
69
70 }
71
72
73 class TestCommand extends DispatchCommand {
74
75
76 public boolean testMethod(Context context) {
77 context.put("foo", "foo");
78 return true;
79 }
80
81 public boolean testMethodKey(Context context) {
82
83 context.put("bar", "bar");
84 return false;
85 }
86
87 }
88
89 /***
90 * Command which uses alternate method signature.
91 * <p>Title: Commons Chain</p>
92 * <p>Description: An implmentation of the GoF Chain of Responsibility pattern</p>
93 * <p>Copyright: Copyright (c) 2003-2004 The Apache Software Foundation - All Rights Reserved.</p>
94 * <p>Company: The Apache Software Foundation</p>
95 * @author germuska
96 * @version 0.2-dev
97 */
98 class TestAlternateContextCommand extends DispatchCommand {
99
100
101 protected Class[] getSignature() {
102 return new Class[] { TestAlternateContext.class };
103 }
104
105 protected Object[] getArguments(Context context) {
106 return new Object[] { new TestAlternateContext(context) };
107 }
108
109 public boolean foo(TestAlternateContext context) {
110 context.put("elephant", "elephant");
111 return true;
112 }
113
114 }
115
116
117 class TestAlternateContext extends java.util.HashMap implements Context {
118 Context wrappedContext = null;
119 TestAlternateContext(Context context) {
120 this.wrappedContext = context;
121 }
122
123 public Object get(Object o) {
124 return this.wrappedContext.get(o);
125 }
126
127 public Object put(Object key, Object value) {
128 return this.wrappedContext.put(key, value);
129 }
130
131 }
132 }