SimpleCompositeCommandHandlerTest.java revision 848932d9e7c6953b3c345c9aa6b0b6c3cfe20d79
1/*
2 * Copyright 2007 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.mockftpserver.core.command;
17
18import org.apache.log4j.Logger;
19import org.mockftpserver.core.session.Session;
20import org.mockftpserver.core.util.AssertFailedException;
21import org.mockftpserver.test.AbstractTest;
22
23import java.util.ArrayList;
24import java.util.List;
25import java.util.ListResourceBundle;
26import java.util.ResourceBundle;
27
28/**
29 * Tests for SimpleCompositeCommandHandler
30 *
31 * @version $Revision$ - $Date$
32 *
33 * @author Chris Mair
34 */
35public class SimpleCompositeCommandHandlerTest extends AbstractTest {
36
37    private static final Logger LOG = Logger.getLogger(SimpleCompositeCommandHandlerTest.class);
38
39    private SimpleCompositeCommandHandler simpleCompositeCommandHandler;
40    private Session session;
41    private Command command;
42    private CommandHandler commandHandler1;
43    private CommandHandler commandHandler2;
44    private CommandHandler commandHandler3;
45
46    /**
47     * Test the handleCommand() method
48     */
49    public void testHandleCommand_OneHandler_OneInvocation() throws Exception {
50        simpleCompositeCommandHandler.addCommandHandler(commandHandler1);
51
52        commandHandler1.handleCommand(command, session);
53        replay(commandHandler1);
54
55        simpleCompositeCommandHandler.handleCommand(command, session);
56        verify(commandHandler1);
57    }
58
59    /**
60     * Test the handleCommand() method, with two CommandHandler defined, but with multiple invocation
61     */
62    public void testHandleCommand_TwoHandlers() throws Exception {
63        simpleCompositeCommandHandler.addCommandHandler(commandHandler1);
64        simpleCompositeCommandHandler.addCommandHandler(commandHandler2);
65
66        commandHandler1.handleCommand(command, session);
67        commandHandler2.handleCommand(command, session);
68        replayAll();
69
70        simpleCompositeCommandHandler.handleCommand(command, session);
71        simpleCompositeCommandHandler.handleCommand(command, session);
72        verifyAll();
73    }
74
75    /**
76     * Test the handleCommand() method, with three CommandHandler defined, and multiple invocation
77     */
78    public void testHandleCommand_ThreeHandlers() throws Exception {
79
80        List list = new ArrayList();
81        list.add(commandHandler1);
82        list.add(commandHandler2);
83        list.add(commandHandler3);
84        simpleCompositeCommandHandler.setCommandHandlers(list);
85
86        commandHandler1.handleCommand(command, session);
87        commandHandler2.handleCommand(command, session);
88        commandHandler3.handleCommand(command, session);
89        replayAll();
90
91        simpleCompositeCommandHandler.handleCommand(command, session);
92        simpleCompositeCommandHandler.handleCommand(command, session);
93        simpleCompositeCommandHandler.handleCommand(command, session);
94        verifyAll();
95    }
96
97    /**
98     * Test the handleCommand() method, with a single CommandHandler defined, but too many invocations
99     */
100    public void testHandleCommand_OneHandler_TooManyInvocations() throws Exception {
101        simpleCompositeCommandHandler.addCommandHandler(commandHandler1);
102
103        commandHandler1.handleCommand(command, session);
104        replay(commandHandler1);
105
106        simpleCompositeCommandHandler.handleCommand(command, session);
107
108        // Second invocation throws an exception
109        try {
110            simpleCompositeCommandHandler.handleCommand(command, session);
111            fail("Expected AssertFailedException");
112        }
113        catch (AssertFailedException expected) {
114            LOG.info("Expected: " + expected);
115        }
116    }
117
118    /**
119     * Test the handleCommand_NoHandlersDefined() method
120     */
121    public void testHandleCommand_NoHandlersDefined() throws Exception {
122        try {
123            simpleCompositeCommandHandler.handleCommand(command, session);
124            fail("Expected AssertFailedException");
125        }
126        catch(AssertFailedException expected) {
127            LOG.info("Expected: " + expected);
128        }
129    }
130
131    /**
132     * Test the handleCommand(Command,Session) method, passing in a null Command
133     */
134    public void testHandleCommand_NullCommand() throws Exception {
135        try {
136            simpleCompositeCommandHandler.handleCommand(null, session);
137            fail("Expected AssertFailedException");
138        }
139        catch (AssertFailedException expected) {
140            LOG.info("Expected: " + expected);
141        }
142    }
143
144    /**
145     * Test the handleCommand(Command,Session) method, passing in a null Session
146     */
147    public void testHandleCommand_NullSession() throws Exception {
148        try {
149            simpleCompositeCommandHandler.handleCommand(command, null);
150            fail("Expected AssertFailedException");
151        }
152        catch (AssertFailedException expected) {
153            LOG.info("Expected: " + expected);
154        }
155    }
156
157    /**
158     * Test the addCommandHandler(CommandHandler) method, passing in a null CommandHandler
159     */
160    public void testAddCommandHandler_NullCommandHandler() throws Exception {
161        try {
162            simpleCompositeCommandHandler.addCommandHandler(null);
163            fail("Expected AssertFailedException");
164        }
165        catch (AssertFailedException expected) {
166            LOG.info("Expected: " + expected);
167        }
168    }
169
170    /**
171     * Test the setCommandHandlers(List) method, passing in a null
172     */
173    public void testSetCommandHandlers_Null() throws Exception {
174        try {
175            simpleCompositeCommandHandler.setCommandHandlers(null);
176            fail("Expected AssertFailedException");
177        }
178        catch (AssertFailedException expected) {
179            LOG.info("Expected: " + expected);
180        }
181    }
182
183    /**
184     * Test the getCommandHandler(int) method, passing in an index for which no CommandHandler is defined
185     */
186    public void testGetCommandHandler_UndefinedIndex() throws Exception {
187        simpleCompositeCommandHandler.addCommandHandler(commandHandler1);
188        try {
189            simpleCompositeCommandHandler.getCommandHandler(1);
190            fail("Expected AssertFailedException");
191        }
192        catch (AssertFailedException expected) {
193            LOG.info("Expected: " + expected);
194        }
195    }
196
197    /**
198     * Test the getCommandHandler(int) method
199     */
200    public void testGetCommandHandler() throws Exception {
201        simpleCompositeCommandHandler.addCommandHandler(commandHandler1);
202        simpleCompositeCommandHandler.addCommandHandler(commandHandler2);
203        assertSame("index 0", commandHandler1, simpleCompositeCommandHandler.getCommandHandler(0));
204        assertSame("index 1", commandHandler2, simpleCompositeCommandHandler.getCommandHandler(1));
205    }
206
207    /**
208     * Test the getCommandHandler(int) method, passing in a negative index
209     */
210    public void testGetCommandHandler_NegativeIndex() throws Exception {
211        simpleCompositeCommandHandler.addCommandHandler(commandHandler1);
212        try {
213            simpleCompositeCommandHandler.getCommandHandler(-1);
214            fail("Expected AssertFailedException");
215        }
216        catch (AssertFailedException expected) {
217            LOG.info("Expected: " + expected);
218        }
219    }
220
221    /**
222     * Test the getReplyTextBundle() method
223     */
224    public void testGetReplyTextBundle() {
225        assertNull(simpleCompositeCommandHandler.getReplyTextBundle());
226    }
227
228    /**
229     * Test the setReplyTextBundle() method
230     */
231    public void testSetReplyTextBundle() {
232
233        AbstractTrackingCommandHandler replyTextBundleAwareCommandHandler1 = new StaticReplyCommandHandler();
234        AbstractTrackingCommandHandler replyTextBundleAwareCommandHandler2 = new StaticReplyCommandHandler();
235        simpleCompositeCommandHandler.addCommandHandler(replyTextBundleAwareCommandHandler1);
236        simpleCompositeCommandHandler.addCommandHandler(commandHandler1);
237        simpleCompositeCommandHandler.addCommandHandler(replyTextBundleAwareCommandHandler2);
238
239        ResourceBundle resourceBundle = new ListResourceBundle() {
240            protected Object[][] getContents() {
241                return null;
242            }
243        };
244
245        simpleCompositeCommandHandler.setReplyTextBundle(resourceBundle);
246        assertSame("1", resourceBundle, replyTextBundleAwareCommandHandler1.getReplyTextBundle());
247        assertSame("2", resourceBundle, replyTextBundleAwareCommandHandler1.getReplyTextBundle());
248    }
249
250    //-------------------------------------------------------------------------
251    // Test setup
252    //-------------------------------------------------------------------------
253
254    /**
255     * Perform initialization before each test
256     * @see org.mockftpserver.test.AbstractTest#setUp()
257     */
258    protected void setUp() throws Exception {
259        super.setUp();
260        simpleCompositeCommandHandler = new SimpleCompositeCommandHandler();
261        session = (Session) createMock(Session.class);
262        command = new Command("cmd", EMPTY);
263        commandHandler1 = (CommandHandler) createMock(CommandHandler.class);
264        commandHandler2 = (CommandHandler) createMock(CommandHandler.class);
265        commandHandler3 = (CommandHandler) createMock(CommandHandler.class);
266    }
267
268}
269