1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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.CatalogFactoryBase;
28  import org.apache.commons.chain.impl.NonDelegatingCommand;
29  
30  /**
31   * <p>Test case for the <code>DispatchLookupCommand</code> class.</p>
32   *
33   * @author Sean Schofield
34   * @version $Revision: 480477 $
35   */
36  
37  public class DispatchLookupCommandTestCase extends TestCase {
38  
39  
40      // ---------------------------------------------------- Instance Variables
41  
42      /**
43       * The instance of {@link Catalog} to use when looking up commands
44       */
45      protected Catalog catalog;
46  
47      /**
48       * The {@link DispatchLookupCommand} instance under test.
49       */
50      protected DispatchLookupCommand command;
51  
52      /**
53       * The {@link Context} instance on which to execute the chain.
54       */
55      protected Context context = null;
56  
57  
58      // ---------------------------------------------------------- Constructors
59  
60      /**
61       * Construct a new instance of this test case.
62       *
63       * @param name Name of the test case
64       */
65      public DispatchLookupCommandTestCase(String name) {
66          super(name);
67      }
68  
69  
70      // -------------------------------------------------- Overall Test Methods
71  
72  
73      /**
74       * Set up instance variables required by this test case.
75       */
76      public void setUp() {
77          catalog = new CatalogBase();
78          CatalogFactoryBase.getInstance().setCatalog(catalog);
79          command = new DispatchLookupCommand();        
80          context = new ContextBase();
81      }
82  
83  
84      /**
85       * Return the tests included in this test suite.
86       * 
87       * @return The suite of tests to run
88       */
89      public static Test suite() {
90          return (new TestSuite(DispatchLookupCommandTestCase.class));
91      }
92  
93      /**
94       * Tear down instance variables required by this test case.
95       */
96      public void tearDown() {
97          catalog = null;
98          CatalogFactoryBase.getInstance().clear();
99          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 }