001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.chain.generic;
018
019
020 import junit.framework.Test;
021 import junit.framework.TestCase;
022 import junit.framework.TestSuite;
023 import org.apache.commons.chain.Context;
024 import org.apache.commons.chain.Catalog;
025 import org.apache.commons.chain.impl.CatalogBase;
026 import org.apache.commons.chain.impl.ContextBase;
027 import org.apache.commons.chain.impl.CatalogFactoryBase;
028 import org.apache.commons.chain.impl.NonDelegatingCommand;
029
030 /**
031 * <p>Test case for the <code>DispatchLookupCommand</code> class.</p>
032 *
033 * @author Sean Schofield
034 * @version $Revision: 480477 $
035 */
036
037 public class DispatchLookupCommandTestCase extends TestCase {
038
039
040 // ---------------------------------------------------- Instance Variables
041
042 /**
043 * The instance of {@link Catalog} to use when looking up commands
044 */
045 protected Catalog catalog;
046
047 /**
048 * The {@link DispatchLookupCommand} instance under test.
049 */
050 protected DispatchLookupCommand command;
051
052 /**
053 * The {@link Context} instance on which to execute the chain.
054 */
055 protected Context context = null;
056
057
058 // ---------------------------------------------------------- Constructors
059
060 /**
061 * Construct a new instance of this test case.
062 *
063 * @param name Name of the test case
064 */
065 public DispatchLookupCommandTestCase(String name) {
066 super(name);
067 }
068
069
070 // -------------------------------------------------- Overall Test Methods
071
072
073 /**
074 * Set up instance variables required by this test case.
075 */
076 public void setUp() {
077 catalog = new CatalogBase();
078 CatalogFactoryBase.getInstance().setCatalog(catalog);
079 command = new DispatchLookupCommand();
080 context = new ContextBase();
081 }
082
083
084 /**
085 * Return the tests included in this test suite.
086 *
087 * @return The suite of tests to run
088 */
089 public static Test suite() {
090 return (new TestSuite(DispatchLookupCommandTestCase.class));
091 }
092
093 /**
094 * Tear down instance variables required by this test case.
095 */
096 public void tearDown() {
097 catalog = null;
098 CatalogFactoryBase.getInstance().clear();
099 command = null;
100 context = null;
101 }
102
103
104 // ------------------------------------------------ Individual Test Methods
105
106
107 // Test ability to lookup and execute a dispatch method on a single
108 // non-delegating command
109 public void testExecuteDispatchLookup_1a() {
110
111 // use default catalog
112 catalog.addCommand("fooCommand", new TestCommand("1"));
113
114 // command should lookup the fooCommand and execute the fooMethod
115 command.setName("fooCommand");
116 command.setMethod("fooMethod");
117
118 try {
119 assertTrue("Command should return true",
120 command.execute(context));
121 } catch (Exception e) {
122
123 fail("Threw exception: " + e);
124 }
125
126 // command should lookup the fooCommand and execute the barMethod
127 command.setMethod("barMethod");
128
129 try {
130 assertTrue("Command should return true",
131 command.execute(context));
132 } catch (Exception e) {
133 fail("Threw exception: " + e);
134 }
135
136 checkExecuteLog("1/1");
137
138 }
139
140 // Test IllegalArgumentException when incorrect command name specified
141 public void testExecuteDispatchLookup_2() {
142
143 // use default catalog
144 catalog.addCommand("barCommand", new TestCommand("2"));
145
146 // command should lookup the fooCommand and execute the fooMethod
147 command.setName("fooCommand");
148 command.setMethod("fooMethod");
149
150 try {
151 command.execute(context);
152 } catch (IllegalArgumentException e) {
153 // test passed
154 return;
155 } catch (Exception e) {
156 // this is a failure
157 }
158
159 fail("Expected IllegalArgumentException");
160 }
161
162 // Test ability to lookup and execute a dispatch method on a single
163 // non-delegating command (using context to specify method name)
164 public void testExecuteDispatchLookup_3() {
165
166 // use default catalog
167 catalog.addCommand("fooCommand", new TestCommand("3"));
168
169 // command should lookup the fooCommand and execute the fooMethod
170 command.setName("fooCommand");
171 command.setMethodKey("methodKey");
172 context.put("methodKey", "fooMethod");
173
174 try {
175 assertTrue("Command should return true",
176 command.execute(context));
177 } catch (Exception e) {
178 fail("Threw exception: " + e);
179 }
180
181 // command should lookup the fooCommand and execute the barMethod
182 command.setMethodKey("methodKey");
183 context.put("methodKey", "barMethod");
184
185
186 try {
187 assertTrue("Command should return true",
188 command.execute(context));
189 } catch (Exception e) {
190 fail("Threw exception: " + e);
191 }
192
193 checkExecuteLog("3/3");
194
195 }
196
197
198 // -------------------------------------------------------- Support Methods
199
200
201 // Verify the contents of the execution log
202 protected void checkExecuteLog(String expected) {
203 StringBuffer log = (StringBuffer) context.get("log");
204 assertNotNull("Context failed to return log", log);
205 assertEquals("Context returned correct log",
206 expected, log.toString());
207 }
208
209 // ---------------------------------------------------------- Inner Classes
210
211
212 class TestCommand extends NonDelegatingCommand {
213
214 public TestCommand(String id)
215 {
216 super(id);
217 }
218
219 public boolean fooMethod(Context context) {
220 log(context, id);
221 return true;
222 }
223
224 public boolean barMethod(Context context) {
225 log(context, id);
226 return true;
227 }
228
229 }
230
231 }