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  import junit.framework.TestCase;
20  import org.apache.commons.chain.Context;
21  import org.apache.commons.chain.impl.ContextBase;
22  
23  /* JUnitTest case for class: org.apache.commons.chain.generic.DispatchCommand */
24  public class DispatchCommandTestCase extends TestCase {
25  
26      public DispatchCommandTestCase(String _name) {
27          super(_name);
28      }
29  
30      /* setUp method for test case */
31      protected void setUp() {
32      }
33  
34      /* tearDown method for test case */
35      protected void tearDown() {
36      }
37  
38      /* Executes the test case */
39      public static void main(String[] argv) {
40          String[] testCaseList = {DispatchCommandTestCase.class.getName()};
41          junit.textui.TestRunner.main(testCaseList);
42      }
43  
44      public void testMethodDispatch() throws Exception {
45          TestCommand test = new TestCommand();
46  
47          test.setMethod("testMethod");
48          Context context = new ContextBase();
49          assertNull(context.get("foo"));
50          boolean result = test.execute(context);
51          assertTrue(result);
52          assertNotNull(context.get("foo"));
53          assertEquals("foo", context.get("foo"));
54  
55  
56      }
57  
58  
59      public void testMethodKeyDispatch() throws Exception {
60          TestCommand test = new TestCommand();
61  
62          test.setMethodKey("foo");
63          Context context = new ContextBase();
64          context.put("foo", "testMethodKey");
65          assertNull(context.get("bar"));
66          boolean result = test.execute(context);
67          assertFalse(result);
68          assertNotNull(context.get("bar"));
69          assertEquals("bar", context.get("bar"));
70  
71  
72      }
73  
74      public void testAlternateContext() throws Exception {
75          TestAlternateContextCommand test = new TestAlternateContextCommand();
76  
77          test.setMethod("foo");
78          Context context = new ContextBase();
79          assertNull(context.get("elephant"));
80          boolean result = test.execute(context);
81          assertTrue(result);
82          assertNotNull(context.get("elephant"));
83          assertEquals("elephant", context.get("elephant"));
84  
85  
86      }
87  
88      
89      class TestCommand extends DispatchCommand {
90          
91  
92          public boolean testMethod(Context context) {
93              context.put("foo", "foo");
94              return true;
95          }
96  
97          public boolean testMethodKey(Context context) {
98              
99              context.put("bar", "bar");
100             return false;
101         }
102 
103     }
104 
105     /**
106      * Command which uses alternate method signature.
107      * @author germuska
108      * @version 0.2-dev
109      */
110     class TestAlternateContextCommand extends DispatchCommand {
111 
112 
113         protected Class[] getSignature() {
114             return new Class[] { TestAlternateContext.class };
115         }
116 
117         protected Object[] getArguments(Context context) {
118             return new Object[] { new TestAlternateContext(context) };
119         }
120 
121         public boolean foo(TestAlternateContext context) {
122             context.put("elephant", "elephant");
123             return true;
124         }
125         
126     }
127 
128 
129     class TestAlternateContext extends java.util.HashMap implements Context {
130         Context wrappedContext = null;
131          TestAlternateContext(Context context) {
132             this.wrappedContext = context;
133         }
134 
135         public Object get(Object o) {
136             return this.wrappedContext.get(o);
137         }
138 
139         public Object put(Object key, Object value) {
140             return this.wrappedContext.put(key, value);
141         }
142 
143     }
144 }