001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.chain.generic;
018    
019    import junit.framework.TestCase;
020    import org.apache.commons.chain.Context;
021    import org.apache.commons.chain.impl.ContextBase;
022    
023    /* JUnitTest case for class: org.apache.commons.chain.generic.DispatchCommand */
024    public class DispatchCommandTestCase extends TestCase {
025    
026        public DispatchCommandTestCase(String _name) {
027            super(_name);
028        }
029    
030        /* setUp method for test case */
031        protected void setUp() {
032        }
033    
034        /* tearDown method for test case */
035        protected void tearDown() {
036        }
037    
038        /* Executes the test case */
039        public static void main(String[] argv) {
040            String[] testCaseList = {DispatchCommandTestCase.class.getName()};
041            junit.textui.TestRunner.main(testCaseList);
042        }
043    
044        public void testMethodDispatch() throws Exception {
045            TestCommand test = new TestCommand();
046    
047            test.setMethod("testMethod");
048            Context context = new ContextBase();
049            assertNull(context.get("foo"));
050            boolean result = test.execute(context);
051            assertTrue(result);
052            assertNotNull(context.get("foo"));
053            assertEquals("foo", context.get("foo"));
054    
055    
056        }
057    
058    
059        public void testMethodKeyDispatch() throws Exception {
060            TestCommand test = new TestCommand();
061    
062            test.setMethodKey("foo");
063            Context context = new ContextBase();
064            context.put("foo", "testMethodKey");
065            assertNull(context.get("bar"));
066            boolean result = test.execute(context);
067            assertFalse(result);
068            assertNotNull(context.get("bar"));
069            assertEquals("bar", context.get("bar"));
070    
071    
072        }
073    
074        public void testAlternateContext() throws Exception {
075            TestAlternateContextCommand test = new TestAlternateContextCommand();
076    
077            test.setMethod("foo");
078            Context context = new ContextBase();
079            assertNull(context.get("elephant"));
080            boolean result = test.execute(context);
081            assertTrue(result);
082            assertNotNull(context.get("elephant"));
083            assertEquals("elephant", context.get("elephant"));
084    
085    
086        }
087    
088        
089        class TestCommand extends DispatchCommand {
090            
091    
092            public boolean testMethod(Context context) {
093                context.put("foo", "foo");
094                return true;
095            }
096    
097            public boolean testMethodKey(Context context) {
098                
099                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    }