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
20 import junit.framework.Test;
21 import junit.framework.TestCase;
22 import junit.framework.TestSuite;
23 import org.apache.commons.chain.Context;
24 import org.apache.commons.chain.Catalog;
25 import org.apache.commons.chain.impl.CatalogBase;
26 import org.apache.commons.chain.impl.ContextBase;
27 import org.apache.commons.chain.impl.ChainBase;
28 import org.apache.commons.chain.impl.CatalogFactoryBase;
29 import org.apache.commons.chain.impl.DelegatingCommand;
30 import org.apache.commons.chain.impl.NonDelegatingCommand;
31
32
33
34
35
36
37
38
39 public class LookupCommandTestCase extends TestCase {
40
41
42
43
44
45
46
47 protected Catalog catalog;
48
49
50
51
52 protected LookupCommand command;
53
54
55
56
57 protected Context context = null;
58
59
60
61
62
63
64
65
66
67 public LookupCommandTestCase(String name) {
68 super(name);
69 }
70
71
72
73
74
75
76
77
78 public void setUp() {
79 catalog = new CatalogBase();
80 CatalogFactoryBase.getInstance().setCatalog(catalog);
81 command = new LookupCommand();
82 context = new ContextBase();
83 }
84
85
86
87
88
89 public static Test suite() {
90 return (new TestSuite(LookupCommandTestCase.class));
91 }
92
93
94
95
96 public void tearDown() {
97 catalog = null;
98 CatalogFactoryBase.getInstance().clear();
99 command = null;
100 context = null;
101 }
102
103
104
105
106
107
108 public void testExecuteMethodLookup_1a() {
109
110
111 catalog.addCommand("foo", new NonDelegatingCommand("1a"));
112 command.setName("foo");
113
114 try {
115 assertTrue("Command should return true",
116 command.execute(context));
117 } catch (Exception e) {
118 fail("Threw exception: " + e);
119 }
120 checkExecuteLog("1a");
121 }
122
123
124 public void testExecuteMethodLookup_1b() {
125
126 ChainBase chain = new ChainBase();
127 chain.addCommand(new DelegatingCommand("1b1"));
128 chain.addCommand(new DelegatingCommand("1b2"));
129 chain.addCommand(new NonDelegatingCommand("1b3"));
130
131
132 catalog.addCommand("foo", chain);
133 command.setName("foo");
134
135 try {
136 assertTrue("Command should return true",
137 command.execute(context));
138 } catch (Exception e) {
139 fail("Threw exception: " + e);
140 }
141 checkExecuteLog("1b1/1b2/1b3");
142 }
143
144
145
146 public void testExecuteMethodLookup_2a() {
147
148
149 catalog.addCommand("foo", new NonDelegatingCommand("2a"));
150 command.setNameKey("nameKey");
151 context.put("nameKey", "foo");
152
153 try {
154 assertTrue("Command should return true",
155 command.execute(context));
156 } catch (Exception e) {
157 fail("Threw exception: " + e);
158 }
159 checkExecuteLog("2a");
160 }
161
162
163 public void testExecuteMethodLookup_2b() {
164
165 ChainBase chain = new ChainBase();
166 chain.addCommand(new DelegatingCommand("2b1"));
167 chain.addCommand(new DelegatingCommand("2b2"));
168 chain.addCommand(new NonDelegatingCommand("2b3"));
169
170
171 catalog.addCommand("foo", chain);
172 command.setNameKey("nameKey");
173 context.put("nameKey", "foo");
174
175 try {
176 assertTrue("Command should return true",
177 command.execute(context));
178 } catch (Exception e) {
179 fail("Threw exception: " + e);
180 }
181 checkExecuteLog("2b1/2b2/2b3");
182 }
183
184
185 public void testExecuteMethodLookup_3a() {
186
187
188 catalog.addCommand("foo", new NonDelegatingCommand("3a"));
189 command.setIgnoreExecuteResult(true);
190 command.setName("foo");
191
192 try {
193 assertFalse("Command should return false",
194 command.execute(context));
195 } catch (Exception e) {
196 fail("Threw exception: " + e);
197 }
198 checkExecuteLog("3a");
199 }
200
201
202
203
204
205
206 protected void checkExecuteLog(String expected) {
207 StringBuffer log = (StringBuffer) context.get("log");
208 assertNotNull("Context failed to return log", log);
209 assertEquals("Context returned correct log",
210 expected, log.toString());
211 }
212
213
214 }