1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.chain.generic;
17
18
19 import junit.framework.Test;
20 import junit.framework.TestCase;
21 import junit.framework.TestSuite;
22 import org.apache.commons.chain.Context;
23 import org.apache.commons.chain.Catalog;
24 import org.apache.commons.chain.impl.CatalogBase;
25 import org.apache.commons.chain.impl.ContextBase;
26 import org.apache.commons.chain.impl.CatalogFactoryBase;
27 import org.apache.commons.chain.impl.NonDelegatingCommand;
28
29 /***
30 * <p>Test case for the <code>DispatchLookupCommand</code> class.</p>
31 *
32 * @author Sean Schofield
33 * @version $Revision: 155403 $
34 */
35
36 public class DispatchLookupCommandTestCase extends TestCase {
37
38
39
40
41 /***
42 * The instance of {@link Catalog} to use when looking up commands
43 */
44 protected Catalog catalog;
45
46 /***
47 * The {@link DispatchLookupCommand} instance under test.
48 */
49 protected DispatchLookupCommand command;
50
51 /***
52 * The {@link Context} instance on which to execute the chain.
53 */
54 protected Context context = null;
55
56
57
58
59 /***
60 * Construct a new instance of this test case.
61 *
62 * @param name Name of the test case
63 */
64 public DispatchLookupCommandTestCase(String name) {
65 super(name);
66 }
67
68
69
70
71
72 /***
73 * Set up instance variables required by this test case.
74 */
75 public void setUp() {
76 catalog = new CatalogBase();
77 CatalogFactoryBase.getInstance().setCatalog(catalog);
78 command = new DispatchLookupCommand();
79 context = new ContextBase();
80 }
81
82
83 /***
84 * Return the tests included in this test suite.
85 *
86 * @return The suite of tests to run
87 */
88 public static Test suite() {
89 return (new TestSuite(DispatchLookupCommandTestCase.class));
90 }
91
92 /***
93 * Tear down instance variables required by this test case.
94 */
95 public void tearDown() {
96 catalog = null;
97 CatalogFactoryBase.getInstance().clear();
98 command = null;
99 context = null;
100 }
101
102
103
104
105
106
107
108 public void testExecuteDispatchLookup_1a() {
109
110
111 catalog.addCommand("fooCommand", new TestCommand("1"));
112
113
114 command.setName("fooCommand");
115 command.setMethod("fooMethod");
116
117 try {
118 assertTrue("Command should return true",
119 command.execute(context));
120 } catch (Exception e) {
121
122 fail("Threw exception: " + e);
123 }
124
125
126 command.setMethod("barMethod");
127
128 try {
129 assertTrue("Command should return true",
130 command.execute(context));
131 } catch (Exception e) {
132 fail("Threw exception: " + e);
133 }
134
135 checkExecuteLog("1/1");
136
137 }
138
139
140 public void testExecuteDispatchLookup_2() {
141
142
143 catalog.addCommand("barCommand", new TestCommand("2"));
144
145
146 command.setName("fooCommand");
147 command.setMethod("fooMethod");
148
149 try {
150 command.execute(context);
151 } catch (IllegalArgumentException e) {
152
153 return;
154 } catch (Exception e) {
155
156 }
157
158 fail("Expected IllegalArgumentException");
159 }
160
161
162
163 public void testExecuteDispatchLookup_3() {
164
165
166 catalog.addCommand("fooCommand", new TestCommand("3"));
167
168
169 command.setName("fooCommand");
170 command.setMethodKey("methodKey");
171 context.put("methodKey", "fooMethod");
172
173 try {
174 assertTrue("Command should return true",
175 command.execute(context));
176 } catch (Exception e) {
177 fail("Threw exception: " + e);
178 }
179
180
181 command.setMethodKey("methodKey");
182 context.put("methodKey", "barMethod");
183
184
185 try {
186 assertTrue("Command should return true",
187 command.execute(context));
188 } catch (Exception e) {
189 fail("Threw exception: " + e);
190 }
191
192 checkExecuteLog("3/3");
193
194 }
195
196
197
198
199
200
201 protected void checkExecuteLog(String expected) {
202 StringBuffer log = (StringBuffer) context.get("log");
203 assertNotNull("Context failed to return log", log);
204 assertEquals("Context returned correct log",
205 expected, log.toString());
206 }
207
208
209
210
211 class TestCommand extends NonDelegatingCommand {
212
213 public TestCommand(String id)
214 {
215 super(id);
216 }
217
218 public boolean fooMethod(Context context) {
219 log(context, id);
220 return true;
221 }
222
223 public boolean barMethod(Context context) {
224 log(context, id);
225 return true;
226 }
227
228 }
229
230 }