1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.chain.impl;
18
19
20 import junit.framework.Test;
21 import junit.framework.TestCase;
22 import junit.framework.TestSuite;
23 import org.apache.commons.chain.Chain;
24 import org.apache.commons.chain.Command;
25 import org.apache.commons.chain.Context;
26
27
28
29
30
31
32
33
34
35 public class ChainBaseTestCase extends TestCase {
36
37
38
39
40
41
42
43
44 protected Chain chain = null;
45
46
47
48
49
50 protected Context context = null;
51
52
53
54
55
56
57
58
59
60 public ChainBaseTestCase(String name) {
61 super(name);
62 }
63
64
65
66
67
68
69
70
71 public void setUp() {
72 chain = new ChainBase();
73 context = new ContextBase();
74 }
75
76
77
78
79
80 public static Test suite() {
81 return (new TestSuite(ChainBaseTestCase.class));
82 }
83
84
85
86
87 public void tearDown() {
88 chain = null;
89 context = null;
90 }
91
92
93
94
95
96
97 public void testCommands() {
98
99 checkCommandCount(0);
100
101 Command command1 = new NonDelegatingCommand("1");
102 chain.addCommand(command1);
103 checkCommandCount(1);
104
105 Command command2 = new DelegatingCommand("2");
106 chain.addCommand(command2);
107 checkCommandCount(2);
108
109 Command command3 = new ExceptionCommand("3");
110 chain.addCommand(command3);
111 checkCommandCount(3);
112
113 }
114
115
116
117 public void testExecute1a() {
118 chain.addCommand(new NonDelegatingCommand("1"));
119 try {
120 assertTrue("Chain returned true",
121 chain.execute(context));
122 } catch (Exception e) {
123 fail("Threw exception: " + e);
124 }
125 checkExecuteLog("1");
126 }
127
128
129
130 public void testExecute1b() {
131 chain.addCommand(new DelegatingCommand("1"));
132 try {
133 assertTrue("Chain returned false",
134 !chain.execute(context));
135 } catch (Exception e) {
136 fail("Threw exception: " + e);
137 }
138 checkExecuteLog("1");
139 }
140
141
142
143 public void testExecute1c() {
144 chain.addCommand(new ExceptionCommand("1"));
145 try {
146 chain.execute(context);
147 } catch (ArithmeticException e) {
148 assertEquals("Correct exception id", "1", e.getMessage());
149 } catch (Exception e) {
150 fail("Threw exception: " + e);
151 }
152 checkExecuteLog("1");
153 }
154
155
156
157 public void testExecute1d() {
158 chain.addCommand(new AddingCommand("1", chain));
159 try {
160 chain.execute(context);
161 } catch (IllegalStateException e) {
162 ;
163 } catch (Exception e) {
164 fail("Threw exception: " + e);
165 }
166 checkExecuteLog("1");
167 }
168
169
170
171 public void testExecute2a() {
172 chain.addCommand(new DelegatingCommand("1"));
173 chain.addCommand(new DelegatingCommand("2"));
174 chain.addCommand(new NonDelegatingCommand("3"));
175 try {
176 assertTrue("Chain returned true",
177 chain.execute(context));
178 } catch (Exception e) {
179 fail("Threw exception: " + e);
180 }
181 checkExecuteLog("1/2/3");
182 }
183
184
185
186 public void testExecute2b() {
187 chain.addCommand(new DelegatingCommand("1"));
188 chain.addCommand(new DelegatingCommand("2"));
189 chain.addCommand(new DelegatingCommand("3"));
190 try {
191 assertTrue("Chain returned false",
192 !chain.execute(context));
193 } catch (Exception e) {
194 fail("Threw exception: " + e);
195 }
196 checkExecuteLog("1/2/3");
197 }
198
199
200
201 public void testExecute2c() {
202 chain.addCommand(new DelegatingCommand("1"));
203 chain.addCommand(new DelegatingCommand("2"));
204 chain.addCommand(new ExceptionCommand("3"));
205 try {
206 chain.execute(context);
207 } catch (ArithmeticException e) {
208 assertEquals("Correct exception id", "3", e.getMessage());
209 } catch (Exception e) {
210 fail("Threw exception: " + e);
211 }
212 checkExecuteLog("1/2/3");
213 }
214
215
216
217 public void testExecute2d() {
218 chain.addCommand(new DelegatingCommand("1"));
219 chain.addCommand(new ExceptionCommand("2"));
220 chain.addCommand(new NonDelegatingCommand("3"));
221 try {
222 chain.execute(context);
223 } catch (ArithmeticException e) {
224 assertEquals("Correct exception id", "2", e.getMessage());
225 } catch (Exception e) {
226 fail("Threw exception: " + e);
227 }
228 checkExecuteLog("1/2");
229 }
230
231
232
233 public void testExecute3a() {
234 chain.addCommand(new NonDelegatingFilter("1", "a"));
235 try {
236 assertTrue("Chain returned true",
237 chain.execute(context));
238 } catch (Exception e) {
239 fail("Threw exception: " + e);
240 }
241 checkExecuteLog("1/a");
242 }
243
244
245
246 public void testExecute3b() {
247 chain.addCommand(new DelegatingFilter("1", "a"));
248 try {
249 assertTrue("Chain returned false",
250 !chain.execute(context));
251 } catch (Exception e) {
252 fail("Threw exception: " + e);
253 }
254 checkExecuteLog("1/a");
255 }
256
257
258
259 public void testExecute3c() {
260 chain.addCommand(new ExceptionFilter("1", "a"));
261 try {
262 chain.execute(context);
263 } catch (ArithmeticException e) {
264 assertEquals("Correct exception id", "1", e.getMessage());
265 } catch (Exception e) {
266 fail("Threw exception: " + e);
267 }
268 checkExecuteLog("1/a");
269 }
270
271
272
273 public void testExecute4a() {
274 chain.addCommand(new DelegatingFilter("1", "a"));
275 chain.addCommand(new DelegatingCommand("2"));
276 chain.addCommand(new NonDelegatingFilter("3", "c"));
277 try {
278 assertTrue("Chain returned true",
279 chain.execute(context));
280 } catch (Exception e) {
281 fail("Threw exception: " + e);
282 }
283 checkExecuteLog("1/2/3/c/a");
284 }
285
286
287
288 public void testExecute4b() {
289 chain.addCommand(new DelegatingCommand("1"));
290 chain.addCommand(new DelegatingFilter("2", "b"));
291 chain.addCommand(new DelegatingCommand("3"));
292 try {
293 assertTrue("Chain returned false",
294 !chain.execute(context));
295 } catch (Exception e) {
296 fail("Threw exception: " + e);
297 }
298 checkExecuteLog("1/2/3/b");
299 }
300
301
302
303 public void testExecute4c() {
304 chain.addCommand(new DelegatingFilter("1", "a"));
305 chain.addCommand(new DelegatingFilter("2", "b"));
306 chain.addCommand(new ExceptionFilter("3", "c"));
307 try {
308 chain.execute(context);
309 } catch (ArithmeticException e) {
310 assertEquals("Correct exception id", "3", e.getMessage());
311 } catch (Exception e) {
312 fail("Threw exception: " + e);
313 }
314 checkExecuteLog("1/2/3/c/b/a");
315 }
316
317
318
319 public void testExecute4d() {
320 chain.addCommand(new DelegatingFilter("1", "a"));
321 chain.addCommand(new ExceptionFilter("2", "b"));
322 chain.addCommand(new NonDelegatingFilter("3", "c"));
323 try {
324 chain.execute(context);
325 } catch (ArithmeticException e) {
326 assertEquals("Correct exception id", "2", e.getMessage());
327 } catch (Exception e) {
328 fail("Threw exception: " + e);
329 }
330 checkExecuteLog("1/2/b/a");
331 }
332
333
334
335 public void testNewInstance() {
336 checkCommandCount(0);
337 }
338
339
340
341
342
343
344 protected void checkCommandCount(int expected) {
345 if (chain instanceof ChainBase) {
346 Command commands[] = ((ChainBase) chain).getCommands();
347 assertNotNull("getCommands() returned a non-null array",
348 commands);
349 assertEquals("Correct command count", expected, commands.length);
350 }
351 }
352
353
354
355 protected void checkExecuteLog(String expected) {
356 StringBuffer log = (StringBuffer) context.get("log");
357 assertNotNull("Context failed to return log", log);
358 assertEquals("Context returned correct log",
359 expected, log.toString());
360 }
361
362
363 }