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.ChainBase;
27 import org.apache.commons.chain.impl.CatalogFactoryBase;
28 import org.apache.commons.chain.impl.DelegatingCommand;
29 import org.apache.commons.chain.impl.NonDelegatingCommand;
30
31 /***
32 * <p>Test case for the <code>LookupCommand</code> class.</p>
33 *
34 * @author Sean Schofield
35 * @version $Revision: 155403 $
36 */
37
38 public class LookupCommandTestCase extends TestCase {
39
40
41
42
43 /***
44 * The instance of {@link Catalog} to use when looking up commands
45 */
46 protected Catalog catalog;
47
48 /***
49 * The {@link LookupCommand} instance under test.
50 */
51 protected LookupCommand command;
52
53 /***
54 * The {@link Context} instance on which to execute the chain.
55 */
56 protected Context context = null;
57
58
59
60
61 /***
62 * Construct a new instance of this test case.
63 *
64 * @param name Name of the test case
65 */
66 public LookupCommandTestCase(String name) {
67 super(name);
68 }
69
70
71
72
73
74 /***
75 * Set up instance variables required by this test case.
76 */
77 public void setUp() {
78 catalog = new CatalogBase();
79 CatalogFactoryBase.getInstance().setCatalog(catalog);
80 command = new LookupCommand();
81 context = new ContextBase();
82 }
83
84
85 /***
86 * Return the tests included in this test suite.
87 */
88 public static Test suite() {
89 return (new TestSuite(LookupCommandTestCase.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 public void testExecuteMethodLookup_1a() {
108
109
110 catalog.addCommand("foo", new NonDelegatingCommand("1a"));
111 command.setName("foo");
112
113 try {
114 assertTrue("Command should return true",
115 command.execute(context));
116 } catch (Exception e) {
117 fail("Threw exception: " + e);
118 }
119 checkExecuteLog("1a");
120 }
121
122
123 public void testExecuteMethodLookup_1b() {
124
125 ChainBase chain = new ChainBase();
126 chain.addCommand(new DelegatingCommand("1b1"));
127 chain.addCommand(new DelegatingCommand("1b2"));
128 chain.addCommand(new NonDelegatingCommand("1b3"));
129
130
131 catalog.addCommand("foo", chain);
132 command.setName("foo");
133
134 try {
135 assertTrue("Command should return true",
136 command.execute(context));
137 } catch (Exception e) {
138 fail("Threw exception: " + e);
139 }
140 checkExecuteLog("1b1/1b2/1b3");
141 }
142
143
144
145 public void testExecuteMethodLookup_2a() {
146
147
148 catalog.addCommand("foo", new NonDelegatingCommand("2a"));
149 command.setNameKey("nameKey");
150 context.put("nameKey", "foo");
151
152 try {
153 assertTrue("Command should return true",
154 command.execute(context));
155 } catch (Exception e) {
156 fail("Threw exception: " + e);
157 }
158 checkExecuteLog("2a");
159 }
160
161
162 public void testExecuteMethodLookup_2b() {
163
164 ChainBase chain = new ChainBase();
165 chain.addCommand(new DelegatingCommand("2b1"));
166 chain.addCommand(new DelegatingCommand("2b2"));
167 chain.addCommand(new NonDelegatingCommand("2b3"));
168
169
170 catalog.addCommand("foo", chain);
171 command.setNameKey("nameKey");
172 context.put("nameKey", "foo");
173
174 try {
175 assertTrue("Command should return true",
176 command.execute(context));
177 } catch (Exception e) {
178 fail("Threw exception: " + e);
179 }
180 checkExecuteLog("2b1/2b2/2b3");
181 }
182
183
184 public void testExecuteMethodLookup_3a() {
185
186
187 catalog.addCommand("foo", new NonDelegatingCommand("3a"));
188 command.setIgnoreExecuteResult(true);
189 command.setName("foo");
190
191 try {
192 assertFalse("Command should return false",
193 command.execute(context));
194 } catch (Exception e) {
195 fail("Threw exception: " + e);
196 }
197 checkExecuteLog("3a");
198 }
199
200
201
202
203
204
205 protected void checkExecuteLog(String expected) {
206 StringBuffer log = (StringBuffer) context.get("log");
207 assertNotNull("Context failed to return log", log);
208 assertEquals("Context returned correct log",
209 expected, log.toString());
210 }
211
212
213 }