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 */
017package org.apache.commons.chain.generic;
018
019import static org.junit.jupiter.api.Assertions.assertEquals;
020import static org.junit.jupiter.api.Assertions.assertFalse;
021import static org.junit.jupiter.api.Assertions.assertNotNull;
022import static org.junit.jupiter.api.Assertions.assertNull;
023import static org.junit.jupiter.api.Assertions.assertTrue;
024
025import java.util.HashMap;
026
027import org.apache.commons.chain.Context;
028import org.apache.commons.chain.impl.ContextBase;
029import org.junit.jupiter.api.AfterEach;
030import org.junit.jupiter.api.Test;
031
032/**
033 * JUnitTest case for class: org.apache.commons.chain.generic.DispatchCommand
034 * */
035public class DispatchCommandTestCase {
036
037    // ---------------------------------------------------------- Constructors
038
039    /**
040     * The Default-Constructor for this class.
041     */
042    public DispatchCommandTestCase() {
043    }
044
045    // -------------------------------------------------- Overall Test Methods
046
047    /**
048     * tearDown method for test case
049     */
050    @AfterEach
051    protected void tearDown() {
052    }
053
054    // ------------------------------------------------ Individual Test Methods
055
056    @Test
057    public void testMethodDispatch() throws Exception {
058        TestCommand test = new TestCommand();
059
060        test.setMethod("testMethod");
061        Context context = new ContextBase();
062        assertNull(context.get("foo"));
063        boolean result = test.execute(context);
064        assertTrue(result);
065        assertNotNull(context.get("foo"));
066        assertEquals("foo", context.get("foo"));
067    }
068
069    @Test
070    public void testMethodKeyDispatch() throws Exception {
071        TestCommand test = new TestCommand();
072
073        test.setMethodKey("foo");
074        Context context = new ContextBase();
075        context.put("foo", "testMethodKey");
076        assertNull(context.get("bar"));
077        boolean result = test.execute(context);
078        assertFalse(result);
079        assertNotNull(context.get("bar"));
080        assertEquals("bar", context.get("bar"));
081    }
082
083    @Test
084    public void testAlternateContext() throws Exception {
085        TestAlternateContextCommand test = new TestAlternateContextCommand();
086
087        test.setMethod("foo");
088        Context context = new ContextBase();
089        assertNull(context.get("elephant"));
090        boolean result = test.execute(context);
091        assertTrue(result);
092        assertNotNull(context.get("elephant"));
093        assertEquals("elephant", context.get("elephant"));
094    }
095
096    public class TestCommand extends DispatchCommand<Context> {
097        public boolean testMethod(Context context) {
098            context.put("foo", "foo");
099            return true;
100        }
101
102        public boolean testMethodKey(Context context) {
103            context.put("bar", "bar");
104            return false;
105        }
106    }
107
108    /**
109     * Command which uses alternate method signature.
110     *
111     * @author germuska
112     * @version 0.2-dev
113     */
114    public class TestAlternateContextCommand extends DispatchCommand<Context> {
115        @Override
116        protected Class<?>[] getSignature() {
117            return new Class<?>[] { TestAlternateContext.class };
118        }
119
120        @Override
121        protected Object[] getArguments(Context context) {
122            return new Object[] { new TestAlternateContext(context) };
123        }
124
125        public boolean foo(TestAlternateContext context) {
126            context.put("elephant", "elephant");
127            return true;
128        }
129    }
130
131    public class TestAlternateContext extends HashMap<String, Object> implements Context {
132        private static final long serialVersionUID = -367573884920704101L;
133
134        transient Context wrappedContext = null;
135        TestAlternateContext(Context context) {
136            this.wrappedContext = context;
137        }
138
139        @Override
140        public Object get(Object o) {
141            return this.wrappedContext.get(o);
142        }
143
144        @Override
145        public Object put(String key, Object value) {
146            return this.wrappedContext.put(key, value);
147        }
148    }
149}