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.ChainBase;
28  import org.apache.commons.chain.impl.CatalogFactoryBase;
29  import org.apache.commons.chain.impl.DelegatingCommand;
30  import org.apache.commons.chain.impl.NonDelegatingCommand;
31  
32  /**
33   * <p>Test case for the <code>LookupCommand</code> class.</p>
34   *
35   * @author Sean Schofield
36   * @version $Revision: 480477 $
37   */
38  
39  public class LookupCommandTestCase extends TestCase {
40  
41  
42      // ---------------------------------------------------- Instance Variables
43  
44      /**
45       * The instance of {@link Catalog} to use when looking up commands
46       */
47      protected Catalog catalog;
48  
49      /**
50       * The {@link LookupCommand} instance under test.
51       */
52      protected LookupCommand command;
53  
54      /**
55       * The {@link Context} instance on which to execute the chain.
56       */
57      protected Context context = null;
58  
59  
60      // ---------------------------------------------------------- Constructors
61  
62      /**
63       * Construct a new instance of this test case.
64       *
65       * @param name Name of the test case
66       */
67      public LookupCommandTestCase(String name) {
68          super(name);
69      }
70  
71  
72      // -------------------------------------------------- Overall Test Methods
73  
74  
75      /**
76       * Set up instance variables required by this test case.
77       */
78      public void setUp() {
79          catalog = new CatalogBase();
80          CatalogFactoryBase.getInstance().setCatalog(catalog);
81          command = new LookupCommand();        
82          context = new ContextBase();
83      }
84  
85  
86      /**
87       * Return the tests included in this test suite.
88       */
89      public static Test suite() {
90          return (new TestSuite(LookupCommandTestCase.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 single non-delegating command
108     public void testExecuteMethodLookup_1a() {
109 
110         // use default catalog
111         catalog.addCommand("foo", new NonDelegatingCommand("1a"));
112         command.setName("foo");
113 
114         try {
115             assertTrue("Command should return true",
116                        command.execute(context));
117         } catch (Exception e) {
118             fail("Threw exception: " + e);
119         }
120         checkExecuteLog("1a");
121     }
122 
123     // Test ability to lookup and execute a chain
124     public void testExecuteMethodLookup_1b() {
125 
126         ChainBase chain = new ChainBase();
127         chain.addCommand(new DelegatingCommand("1b1"));
128         chain.addCommand(new DelegatingCommand("1b2"));
129         chain.addCommand(new NonDelegatingCommand("1b3"));
130         
131         // use default catalog
132         catalog.addCommand("foo", chain);
133         command.setName("foo");
134 
135         try {
136             assertTrue("Command should return true",
137                        command.execute(context));
138         } catch (Exception e) {
139             fail("Threw exception: " + e);
140         }
141         checkExecuteLog("1b1/1b2/1b3");
142     }
143 
144     // Test ability to lookup and execute single non-delegating command
145     // using the context
146     public void testExecuteMethodLookup_2a() {
147 
148         // use default catalog
149         catalog.addCommand("foo", new NonDelegatingCommand("2a"));
150         command.setNameKey("nameKey");
151         context.put("nameKey", "foo");
152 
153         try {
154             assertTrue("Command should return true",
155                        command.execute(context));
156         } catch (Exception e) {
157             fail("Threw exception: " + e);
158         }
159         checkExecuteLog("2a");
160     }
161 
162     // Test ability to lookup and execute a chain using the context 
163     public void testExecuteMethodLookup_2b() {
164 
165         ChainBase chain = new ChainBase();
166         chain.addCommand(new DelegatingCommand("2b1"));
167         chain.addCommand(new DelegatingCommand("2b2"));
168         chain.addCommand(new NonDelegatingCommand("2b3"));
169 
170         // use default catalog
171         catalog.addCommand("foo", chain);
172         command.setNameKey("nameKey");
173         context.put("nameKey", "foo");
174 
175         try {
176             assertTrue("Command should return true",
177                        command.execute(context));
178         } catch (Exception e) {
179             fail("Threw exception: " + e);
180         }
181         checkExecuteLog("2b1/2b2/2b3");
182     }
183 
184     // Test ability to lookup and execute single non-delegating command, ignoring its result
185     public void testExecuteMethodLookup_3a() {
186 
187         // use default catalog
188         catalog.addCommand("foo", new NonDelegatingCommand("3a"));
189         command.setIgnoreExecuteResult(true);
190         command.setName("foo");
191 
192         try {
193             assertFalse("Command should return false",
194                        command.execute(context));
195         } catch (Exception e) {
196             fail("Threw exception: " + e);
197         }
198         checkExecuteLog("3a");
199     }
200 
201 
202     // -------------------------------------------------------- Support Methods
203 
204 
205     // Verify the contents of the execution log
206     protected void checkExecuteLog(String expected) {
207         StringBuffer log = (StringBuffer) context.get("log");
208         assertNotNull("Context failed to return log", log);
209         assertEquals("Context returned correct log",
210                      expected, log.toString());
211     }
212 
213 
214 }