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