001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.chain.impl;
017:
018: import junit.framework.Test;
019: import junit.framework.TestCase;
020: import junit.framework.TestSuite;
021: import org.apache.commons.chain.Chain;
022: import org.apache.commons.chain.Command;
023: import org.apache.commons.chain.Context;
024:
025: /**
026: * <p>Test case for the <code>ChainBase</code> class.</p>
027: *
028: * @author Craig R. McClanahan
029: * @version $Revision: 155403 $ $Date: 2005-02-26 12:52:46 +0000 (Sat, 26 Feb 2005) $
030: */
031:
032: public class ChainBaseTestCase extends TestCase {
033:
034: // ---------------------------------------------------- Instance Variables
035:
036: /**
037: * The {@link Chain} instance under test.
038: */
039: protected Chain chain = null;
040:
041: /**
042: * The {@link Context} instance on which to execute the chain.
043: */
044: protected Context context = null;
045:
046: // ---------------------------------------------------------- Constructors
047:
048: /**
049: * Construct a new instance of this test case.
050: *
051: * @param name Name of the test case
052: */
053: public ChainBaseTestCase(String name) {
054: super (name);
055: }
056:
057: // -------------------------------------------------- Overall Test Methods
058:
059: /**
060: * Set up instance variables required by this test case.
061: */
062: public void setUp() {
063: chain = new ChainBase();
064: context = new ContextBase();
065: }
066:
067: /**
068: * Return the tests included in this test suite.
069: */
070: public static Test suite() {
071: return (new TestSuite(ChainBaseTestCase.class));
072: }
073:
074: /**
075: * Tear down instance variables required by this test case.
076: */
077: public void tearDown() {
078: chain = null;
079: context = null;
080: }
081:
082: // ------------------------------------------------ Individual Test Methods
083:
084: // Test the ability to add commands
085: public void testCommands() {
086:
087: checkCommandCount(0);
088:
089: Command command1 = new NonDelegatingCommand("1");
090: chain.addCommand(command1);
091: checkCommandCount(1);
092:
093: Command command2 = new DelegatingCommand("2");
094: chain.addCommand(command2);
095: checkCommandCount(2);
096:
097: Command command3 = new ExceptionCommand("3");
098: chain.addCommand(command3);
099: checkCommandCount(3);
100:
101: }
102:
103: // Test execution of a single non-delegating command
104: public void testExecute1a() {
105: chain.addCommand(new NonDelegatingCommand("1"));
106: try {
107: assertTrue("Chain returned true", chain.execute(context));
108: } catch (Exception e) {
109: fail("Threw exception: " + e);
110: }
111: checkExecuteLog("1");
112: }
113:
114: // Test execution of a single delegating command
115: public void testExecute1b() {
116: chain.addCommand(new DelegatingCommand("1"));
117: try {
118: assertTrue("Chain returned false", !chain.execute(context));
119: } catch (Exception e) {
120: fail("Threw exception: " + e);
121: }
122: checkExecuteLog("1");
123: }
124:
125: // Test execution of a single exception-throwing command
126: public void testExecute1c() {
127: chain.addCommand(new ExceptionCommand("1"));
128: try {
129: chain.execute(context);
130: } catch (ArithmeticException e) {
131: assertEquals("Correct exception id", "1", e.getMessage());
132: } catch (Exception e) {
133: fail("Threw exception: " + e);
134: }
135: checkExecuteLog("1");
136: }
137:
138: // Test execution of an attempt to add a new Command while executing
139: public void testExecute1d() {
140: chain.addCommand(new AddingCommand("1", chain));
141: try {
142: chain.execute(context);
143: } catch (IllegalStateException e) {
144: ; // Expected result
145: } catch (Exception e) {
146: fail("Threw exception: " + e);
147: }
148: checkExecuteLog("1");
149: }
150:
151: // Test execution of a chain that should return true
152: public void testExecute2a() {
153: chain.addCommand(new DelegatingCommand("1"));
154: chain.addCommand(new DelegatingCommand("2"));
155: chain.addCommand(new NonDelegatingCommand("3"));
156: try {
157: assertTrue("Chain returned true", chain.execute(context));
158: } catch (Exception e) {
159: fail("Threw exception: " + e);
160: }
161: checkExecuteLog("1/2/3");
162: }
163:
164: // Test execution of a chain that should return false
165: public void testExecute2b() {
166: chain.addCommand(new DelegatingCommand("1"));
167: chain.addCommand(new DelegatingCommand("2"));
168: chain.addCommand(new DelegatingCommand("3"));
169: try {
170: assertTrue("Chain returned false", !chain.execute(context));
171: } catch (Exception e) {
172: fail("Threw exception: " + e);
173: }
174: checkExecuteLog("1/2/3");
175: }
176:
177: // Test execution of a chain that should throw an exception
178: public void testExecute2c() {
179: chain.addCommand(new DelegatingCommand("1"));
180: chain.addCommand(new DelegatingCommand("2"));
181: chain.addCommand(new ExceptionCommand("3"));
182: try {
183: chain.execute(context);
184: } catch (ArithmeticException e) {
185: assertEquals("Correct exception id", "3", e.getMessage());
186: } catch (Exception e) {
187: fail("Threw exception: " + e);
188: }
189: checkExecuteLog("1/2/3");
190: }
191:
192: // Test execution of a chain that should throw an exception in the middle
193: public void testExecute2d() {
194: chain.addCommand(new DelegatingCommand("1"));
195: chain.addCommand(new ExceptionCommand("2"));
196: chain.addCommand(new NonDelegatingCommand("3"));
197: try {
198: chain.execute(context);
199: } catch (ArithmeticException e) {
200: assertEquals("Correct exception id", "2", e.getMessage());
201: } catch (Exception e) {
202: fail("Threw exception: " + e);
203: }
204: checkExecuteLog("1/2");
205: }
206:
207: // Test execution of a single non-delegating filter
208: public void testExecute3a() {
209: chain.addCommand(new NonDelegatingFilter("1", "a"));
210: try {
211: assertTrue("Chain returned true", chain.execute(context));
212: } catch (Exception e) {
213: fail("Threw exception: " + e);
214: }
215: checkExecuteLog("1/a");
216: }
217:
218: // Test execution of a single delegating filter
219: public void testExecute3b() {
220: chain.addCommand(new DelegatingFilter("1", "a"));
221: try {
222: assertTrue("Chain returned false", !chain.execute(context));
223: } catch (Exception e) {
224: fail("Threw exception: " + e);
225: }
226: checkExecuteLog("1/a");
227: }
228:
229: // Test execution of a single exception-throwing filter
230: public void testExecute3c() {
231: chain.addCommand(new ExceptionFilter("1", "a"));
232: try {
233: chain.execute(context);
234: } catch (ArithmeticException e) {
235: assertEquals("Correct exception id", "1", e.getMessage());
236: } catch (Exception e) {
237: fail("Threw exception: " + e);
238: }
239: checkExecuteLog("1/a");
240: }
241:
242: // Test execution of a chain that should return true
243: public void testExecute4a() {
244: chain.addCommand(new DelegatingFilter("1", "a"));
245: chain.addCommand(new DelegatingCommand("2"));
246: chain.addCommand(new NonDelegatingFilter("3", "c"));
247: try {
248: assertTrue("Chain returned true", chain.execute(context));
249: } catch (Exception e) {
250: fail("Threw exception: " + e);
251: }
252: checkExecuteLog("1/2/3/c/a");
253: }
254:
255: // Test execution of a chain that should return false
256: public void testExecute4b() {
257: chain.addCommand(new DelegatingCommand("1"));
258: chain.addCommand(new DelegatingFilter("2", "b"));
259: chain.addCommand(new DelegatingCommand("3"));
260: try {
261: assertTrue("Chain returned false", !chain.execute(context));
262: } catch (Exception e) {
263: fail("Threw exception: " + e);
264: }
265: checkExecuteLog("1/2/3/b");
266: }
267:
268: // Test execution of a chain that should throw an exception
269: public void testExecute4c() {
270: chain.addCommand(new DelegatingFilter("1", "a"));
271: chain.addCommand(new DelegatingFilter("2", "b"));
272: chain.addCommand(new ExceptionFilter("3", "c"));
273: try {
274: chain.execute(context);
275: } catch (ArithmeticException e) {
276: assertEquals("Correct exception id", "3", e.getMessage());
277: } catch (Exception e) {
278: fail("Threw exception: " + e);
279: }
280: checkExecuteLog("1/2/3/c/b/a");
281: }
282:
283: // Test execution of a chain that should throw an exception in the middle
284: public void testExecute4d() {
285: chain.addCommand(new DelegatingFilter("1", "a"));
286: chain.addCommand(new ExceptionFilter("2", "b"));
287: chain.addCommand(new NonDelegatingFilter("3", "c"));
288: try {
289: chain.execute(context);
290: } catch (ArithmeticException e) {
291: assertEquals("Correct exception id", "2", e.getMessage());
292: } catch (Exception e) {
293: fail("Threw exception: " + e);
294: }
295: checkExecuteLog("1/2/b/a");
296: }
297:
298: // Test state of newly created instance
299: public void testNewInstance() {
300: checkCommandCount(0);
301: }
302:
303: // -------------------------------------------------------- Support Methods
304:
305: // Verify the number of configured commands
306: protected void checkCommandCount(int expected) {
307: if (chain instanceof ChainBase) {
308: Command commands[] = ((ChainBase) chain).getCommands();
309: assertNotNull("getCommands() returned a non-null array",
310: commands);
311: assertEquals("Correct command count", expected,
312: commands.length);
313: }
314: }
315:
316: // Verify the contents of the execution log
317: protected void checkExecuteLog(String expected) {
318: StringBuffer log = (StringBuffer) context.get("log");
319: assertNotNull("Context failed to return log", log);
320: assertEquals("Context returned correct log", expected, log
321: .toString());
322: }
323:
324: }
|