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.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      // ---------------------------------------------------- Instance Variables
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      // ---------------------------------------------------------- Constructors
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      // -------------------------------------------------- Overall Test Methods
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     // ------------------------------------------------ Individual Test Methods
104 
105 
106     // Test ability to lookup and execute single non-delegating command
107     public void testExecuteMethodLookup_1a() {
108 
109         // use default catalog
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     // Test ability to lookup and execute a chain
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         // use default catalog
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     // Test ability to lookup and execute single non-delegating command
144     // using the context
145     public void testExecuteMethodLookup_2a() {
146 
147         // use default catalog
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     // Test ability to lookup and execute a chain using the context 
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         // use default catalog
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     // Test ability to lookup and execute single non-delegating command, ignoring its result
184     public void testExecuteMethodLookup_3a() {
185 
186         // use default catalog
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     // -------------------------------------------------------- Support Methods
202 
203 
204     // Verify the contents of the execution log
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 }