1   /*
2    * 
3    * $Revision: 1.3 $
4    * $Date: 2004/07/09 18:03:42 $
5    *
6    * ====================================================================
7    * struts-it
8    * Copyright (C) 2004 - strutsit community
9    * 
10   * Licensed under the Apache License, Version 2.0 (the "License");
11   * you may not use this file except in compliance with the License.
12   * You may obtain a copy of the License at
13   * 
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   * 
16   * Unless required by applicable law or agreed to in writing, software
17   * distributed under the License is distributed on an "AS IS" BASIS,
18   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   * See the License for the specific language governing permissions and
20   * limitations under the License.
21   *
22   * created: 2004-06-05  Manfred Wolff
23   */
24  package org.strutsit.chain;
25  
26  import junit.framework.Test;
27  import junit.framework.TestCase;
28  import junit.framework.TestSuite;
29  
30  import org.apache.commons.chain.Command;
31  import org.apache.commons.chain.Context;
32  import org.apache.commons.chain.impl.ContextBase;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.strutsit.chain.configuration.ChainXMLConfiguration;
36  import org.strutsit.chain.interfaces.ChainConfiguration;
37  
38  /***
39   * Interface for a chain configuration.
40   * 
41   * @author <a href="mailto:wolff@struts-it.de">Manfred Wolff </a>
42   * @since JDK 1.4
43   * @version $Revision: 1.3 $
44   */
45  public class TestCommands extends TestCase {
46  
47      /***
48       * <p>
49       * The logger for this class (commons-logging).
50       * </p>
51       */
52      private static Log log = LogFactory.getLog(TestCommands.class);
53  
54      //------------------------------------- methods that belongs to the
55      // TestCase
56      /***
57       * Defines the testcase name for JUnit.
58       * 
59       * @param theName the testcase's name.
60       */
61      public TestCommands(String theName) {
62          super(theName);
63          if (log.isTraceEnabled()) {
64              log.trace("TestCommands -> START");
65          }
66          if (log.isTraceEnabled()) {
67              log.trace("TestCommands -> END");
68          }
69      }
70  
71      /***
72       * Start the tests.
73       * 
74       * @param theArgs the arguments. Not used
75       */
76      public static void main(String[] theArgs) {
77          if (log.isTraceEnabled()) {
78              log.trace("main -> START");
79          }
80          junit.awtui.TestRunner
81                  .main(new String[] { TestCommands.class.getName() });
82          if (log.isTraceEnabled()) {
83              log.trace("main -> END");
84          }
85      }
86  
87      /***
88       * @return a test suite (<code>TestSuite</code>) that includes all
89       *         methods starting with "test"
90       */
91      public static Test suite() {
92          // All methods starting with "test" will be executed in the test suite.
93          if (log.isTraceEnabled()) {
94              log.trace("suite -> START");
95          }
96          if (log.isTraceEnabled()) {
97              log.trace("suite -> END");
98          }
99          return new TestSuite(TestCommands.class);
100     }
101 
102     /***
103      * Nothing to be done.
104      */
105     public void setUp() {
106         if (log.isTraceEnabled()) {
107             log.trace("setUp -> START");
108         }
109         if (log.isTraceEnabled()) {
110             log.trace("setUp -> END");
111         }
112     }
113 
114     /***
115      * Nothing to be done.
116      */
117     public void tearDown() {
118         if (log.isTraceEnabled()) {
119             log.trace("tearDown -> START");
120         }
121         if (log.isTraceEnabled()) {
122             log.trace("tearDown -> END");
123         }
124     }
125 
126     //------------------------------------------------------------- test
127     // methods
128 
129     /***
130      * Tests a command that returns true.
131      */
132     public void testsimpleCommandTrue() {
133 
134         if (log.isTraceEnabled()) {
135             log.trace("testsimpleCommandTrue -> START");
136         }
137 
138         // creating context
139         Context context = createContext();
140 
141         // initializing configuration
142         ChainConfiguration config = null;
143         try {
144             config = initConfiguration();
145         } catch (Exception e) {
146             fail("Error while reading configuration.");
147         }
148 
149         // gets the right command for the test
150         Command testCommand = config.getCatalog().getCommand("Test-Command");
151         boolean fail = true;
152 
153         // executing and testing the response from the command
154         try {
155             fail = testCommand.execute(context);
156             assertTrue(fail);
157             String erg = (String) context.get("TestCommand");
158             if (log.isInfoEnabled()) {
159                 log.info(erg);
160             }
161             assertEquals(
162                     "execute CommandForTestTrue            ok".equals(erg),
163                     true);
164         } catch (Exception e) {
165             fail("Error while executing command.");
166         }
167 
168         if (log.isTraceEnabled()) {
169             log.trace("testsimpleCommandTrue -> END");
170         }
171     }
172 
173     /***
174      * Tests a command that returns false.
175      */
176     public void testsimpleCommandFalse() {
177 
178         if (log.isTraceEnabled()) {
179             log.trace("testsimpleCommandFalse -> START");
180         }
181 
182         // creating context
183         Context context = createContext();
184 
185         // initializing configuration
186         ChainConfiguration config = null;
187         try {
188             config = initConfiguration();
189         } catch (Exception e) {
190             fail("Error while reading configuration.");
191         }
192 
193         // gets the right command for the test
194         Command testCommand = config.getCatalog().getCommand("Test-Command2");
195 
196         assertNotNull("Testcommand is null", testCommand);
197 
198         boolean fail = true;
199 
200         // executing and testing the response from the command
201         try {
202             fail = testCommand.execute(context);
203             assertFalse(fail);
204             String erg = (String) context.get("TestCommand");
205             if (log.isInfoEnabled()) {
206                 log.info(erg);
207             }
208             assertEquals(
209                     "execute CommandForTestFalse           ok".equals(erg),
210                     true);
211         } catch (Exception e) {
212             fail("Error while executing command.");
213         }
214 
215         if (log.isTraceEnabled()) {
216             log.trace("testsimpleCommandFalse -> END");
217         }
218     }
219 
220     /***
221      * @return
222      */
223     private Context createContext() {
224         Context context = new ContextBase();
225         StringBuffer buffer = new StringBuffer();
226         context.put("chainerg", buffer);
227         return context;
228     }
229 
230     /***
231      * Tests a chain containing two commands. Both commands must executed.
232      */
233     public void testChainCommand1() {
234 
235         if (log.isTraceEnabled()) {
236             log.trace("testChainCommand1 -> START");
237         }
238 
239         // creating context
240         Context context = createContext();
241 
242         // initializing configuration
243         ChainConfiguration config = null;
244         try {
245             config = initConfiguration();
246         } catch (Exception e) {
247             fail("Error while reading configuration.");
248         }
249 
250         // gets the right command for the test
251         Command testCommand = config.getCatalog().getCommand("double");
252 
253         assertNotNull("Testcommand is null", testCommand);
254 
255         boolean fail = false;
256         // executing and testing the response from the command
257         try {
258             fail = testCommand.execute(context);
259             StringBuffer buffer = (StringBuffer) context.get("chainerg");
260             if (log.isInfoEnabled()) {
261                 log.info("fail = " + fail);
262                 log.info("erg  = " + buffer.toString());
263             }
264         } catch (Exception e) {
265             fail("Error while executing command.");
266         }
267         if (log.isTraceEnabled()) {
268             log.trace("testChainCommand1 -> END");
269         }
270     }
271 
272     /***
273      * Tests a chain containing two commands. Both commands must executed.
274      */
275     public void tChainCommandDependFails() {
276 
277         if (log.isTraceEnabled()) {
278             log.trace("testChainCommand1 -> START");
279         }
280 
281         // creating context
282         Context context = createContext();
283 
284         // initializing configuration
285         ChainConfiguration config = null;
286         try {
287             config = initConfiguration();
288         } catch (Exception e) {
289             fail("Error while reading configuration.");
290         }
291 
292         // gets the right command for the test
293         Command testCommand = config.getCatalog().getCommand("depend2");
294 
295         assertNotNull("Testcommand is null", testCommand);
296 
297         boolean fail = false;
298         // executing and testing the response from the command
299         try {
300             fail = testCommand.execute(context);
301             StringBuffer buffer = (StringBuffer) context.get("chainerg");
302             if (log.isInfoEnabled()) {
303                 log.info("fail = " + fail);
304                 log.info("erg  = " + buffer.toString());
305             }
306         } catch (Exception e) {
307             fail("Error while executing command.");
308         }
309         if (log.isTraceEnabled()) {
310             log.trace("testChainCommand1 -> END");
311         }
312     }
313 
314     /***
315      * Tests a chain containing two commands. Only one command has to executed.
316      */
317     public void testChainCommand2() {
318 
319         if (log.isTraceEnabled()) {
320             log.trace("testChainCommand2 -> START");
321         }
322 
323         // creating context
324         Context context = createContext();
325 
326         // initializing configuration
327         ChainConfiguration config = null;
328         try {
329             config = initConfiguration();
330         } catch (Exception e) {
331             fail("Error while reading configuration.");
332         }
333 
334         // gets the right command for the test
335         Command testCommand = config.getCatalog().getCommand("single");
336 
337         assertNotNull("Testcommand is null", testCommand);
338 
339         boolean fail = false;
340         // executing and testing the response from the command
341         try {
342             fail = testCommand.execute(context);
343             StringBuffer buffer = (StringBuffer) context.get("chainerg");
344             if (log.isInfoEnabled()) {
345                 log.info("fail = " + fail);
346                 log.info("erg  = " + buffer.toString());
347             }
348         } catch (Exception e) {
349             fail("Error while executing command.");
350         }
351 
352         if (log.isTraceEnabled()) {
353             log.trace("testChainCommand2 -> END");
354         }
355     }
356 
357     /***
358      * @return A new ChainConfiguration object.
359      * @throws ChainException if no configuration can be created.
360      */
361     private ChainConfiguration initConfiguration() throws ChainException {
362 
363         if (log.isTraceEnabled()) {
364             log.trace("initConfiguration -> START");
365         }
366         ChainConfiguration config = new ChainXMLConfiguration();
367         try {
368             config.init();
369             config.checkConfiguration();
370         } catch (Exception e) {
371             if (log.isInfoEnabled()) {
372                 log.info(e);
373             }
374             throw new ChainException("No configuration may be created.", e);
375         }
376         if (log.isTraceEnabled()) {
377             log.trace("initConfiguration -> END");
378         }
379         return config;
380     }
381 }