1   /*
2    * Copyright 1999-2004 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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.CatalogFactoryBase;
27  import org.apache.commons.chain.impl.NonDelegatingCommand;
28  
29  /***
30   * <p>Test case for the <code>DispatchLookupCommand</code> class.</p>
31   *
32   * @author Sean Schofield
33   * @version $Revision: 155403 $
34   */
35  
36  public class DispatchLookupCommandTestCase extends TestCase {
37  
38  
39      // ---------------------------------------------------- Instance Variables
40  
41      /***
42       * The instance of {@link Catalog} to use when looking up commands
43       */
44      protected Catalog catalog;
45  
46      /***
47       * The {@link DispatchLookupCommand} instance under test.
48       */
49      protected DispatchLookupCommand command;
50  
51      /***
52       * The {@link Context} instance on which to execute the chain.
53       */
54      protected Context context = null;
55  
56  
57      // ---------------------------------------------------------- Constructors
58  
59      /***
60       * Construct a new instance of this test case.
61       *
62       * @param name Name of the test case
63       */
64      public DispatchLookupCommandTestCase(String name) {
65          super(name);
66      }
67  
68  
69      // -------------------------------------------------- Overall Test Methods
70  
71  
72      /***
73       * Set up instance variables required by this test case.
74       */
75      public void setUp() {
76          catalog = new CatalogBase();
77          CatalogFactoryBase.getInstance().setCatalog(catalog);
78          command = new DispatchLookupCommand();        
79          context = new ContextBase();
80      }
81  
82  
83      /***
84       * Return the tests included in this test suite.
85       * 
86       * @return The suite of tests to run
87       */
88      public static Test suite() {
89          return (new TestSuite(DispatchLookupCommandTestCase.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     // ------------------------------------------------ Individual Test Methods
104 
105 
106     // Test ability to lookup and execute a dispatch method on a single 
107     // non-delegating command
108     public void testExecuteDispatchLookup_1a() {
109 
110         // use default catalog
111         catalog.addCommand("fooCommand", new TestCommand("1"));
112         
113         // command should lookup the fooCommand and execute the fooMethod
114         command.setName("fooCommand");
115         command.setMethod("fooMethod");
116         
117         try {
118             assertTrue("Command should return true",
119                        command.execute(context));
120         } catch (Exception e) {
121 
122             fail("Threw exception: " + e);
123         }
124         
125         // command should lookup the fooCommand and execute the barMethod
126         command.setMethod("barMethod");
127 
128         try {
129             assertTrue("Command should return true",
130                        command.execute(context));
131         } catch (Exception e) {
132             fail("Threw exception: " + e);
133         }
134         
135         checkExecuteLog("1/1");
136         
137     }
138     
139     // Test IllegalArgumentException when incorrect command name specified
140     public void testExecuteDispatchLookup_2() {
141 
142         // use default catalog
143         catalog.addCommand("barCommand", new TestCommand("2"));
144 
145         // command should lookup the fooCommand and execute the fooMethod
146         command.setName("fooCommand");
147         command.setMethod("fooMethod");
148 
149         try {
150             command.execute(context);
151         } catch (IllegalArgumentException e) {
152             // test passed
153             return;
154         } catch (Exception e) {
155             // this is a failure
156         }
157       
158         fail("Expected IllegalArgumentException");
159     }
160 
161     // Test ability to lookup and execute a dispatch method on a single 
162     // non-delegating command (using context to specify method name)
163     public void testExecuteDispatchLookup_3() {
164 
165         // use default catalog
166         catalog.addCommand("fooCommand", new TestCommand("3"));
167 
168         // command should lookup the fooCommand and execute the fooMethod
169         command.setName("fooCommand");
170         command.setMethodKey("methodKey");
171         context.put("methodKey", "fooMethod");
172 
173         try {
174             assertTrue("Command should return true",
175                        command.execute(context));
176         } catch (Exception e) {
177             fail("Threw exception: " + e);
178         }
179 
180         // command should lookup the fooCommand and execute the barMethod
181         command.setMethodKey("methodKey");
182         context.put("methodKey", "barMethod");
183 
184 
185         try {
186             assertTrue("Command should return true",
187                        command.execute(context));
188         } catch (Exception e) {
189             fail("Threw exception: " + e);
190         }
191 
192         checkExecuteLog("3/3");
193 
194     }
195 
196 
197     // -------------------------------------------------------- Support Methods
198 
199 
200     // Verify the contents of the execution log
201     protected void checkExecuteLog(String expected) {
202         StringBuffer log = (StringBuffer) context.get("log");
203         assertNotNull("Context failed to return log", log);
204         assertEquals("Context returned correct log",
205                      expected, log.toString());
206     }
207 
208     // ---------------------------------------------------------- Inner Classes
209 
210 
211     class TestCommand extends NonDelegatingCommand {
212 
213         public TestCommand(String id)
214         {
215             super(id);
216         }
217     
218         public boolean fooMethod(Context context) {
219             log(context, id);            
220             return true;
221         }
222         
223         public boolean barMethod(Context context) {
224             log(context, id);
225             return true;
226         }
227         
228     }
229 
230 }