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