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.easymock.MockControl;
20import org.mockftpserver.core.session.Session;
21import org.mockftpserver.core.util.AssertFailedException;
22import org.mockftpserver.test.AbstractTest;
23
24import java.util.ListResourceBundle;
25import java.util.ResourceBundle;
26
27/**
28 * Tests for the AbstractTrackingCommandHandler class. The class name is prefixed with an
29 * underscore so that it is not filtered out by Maven's Surefire test plugin.
30 *
31 * @author Chris Mair
32 * @version $Revision$ - $Date$
33 */
34public final class _AbstractTrackingCommandHandlerTest extends AbstractTest {
35
36    private static final Logger LOG = Logger.getLogger(_AbstractTrackingCommandHandlerTest.class);
37    private static final String COMMAND_NAME = "abc";
38    private static final Object ARG = "123";
39    private static final Object[] ARGS = {ARG};
40    private static final Command COMMAND = new Command(COMMAND_NAME, EMPTY);
41    private static final Command COMMAND_WITH_ARGS = new Command(COMMAND_NAME, EMPTY);
42    private static final int REPLY_CODE1 = 777;
43    private static final int REPLY_CODE2 = 888;
44    private static final int REPLY_CODE3 = 999;
45    private static final String REPLY_TEXT1 = "reply1 ... abcdef";
46    private static final String REPLY_TEXT2 = "abc {0} def";
47    private static final String REPLY_TEXT2_FORMATTED = "abc 123 def";
48    private static final String OVERRIDE_REPLY_TEXT = "overridden reply ... abcdef";
49    private static final String MESSAGE_KEY = "key.123";
50    private static final String MESSAGE_TEXT = "message.123";
51
52    private AbstractTrackingCommandHandler commandHandler;
53    private Session session;
54
55    /**
56     * Test the handleCommand(Command,Session) method
57     */
58    public void testHandleCommand() throws Exception {
59        assertEquals("before", 0, commandHandler.numberOfInvocations());
60        commandHandler.handleCommand(COMMAND, session);
61        assertEquals("after", 1, commandHandler.numberOfInvocations());
62        assertTrue("locked", commandHandler.getInvocation(0).isLocked());
63    }
64
65    /**
66     * Test the handleCommand(Command,Session) method, passing in a null Command
67     */
68    public void testHandleCommand_NullCommand() throws Exception {
69        try {
70            commandHandler.handleCommand(null, session);
71            fail("Expected AssertFailedException");
72        }
73        catch (AssertFailedException expected) {
74            LOG.info("Expected: " + expected);
75        }
76    }
77
78    /**
79     * Test the handleCommand(Command,Session) method, passing in a null Session
80     */
81    public void testHandleCommand_NullSession() throws Exception {
82        try {
83            commandHandler.handleCommand(COMMAND, null);
84            fail("Expected AssertFailedException");
85        }
86        catch (AssertFailedException expected) {
87            LOG.info("Expected: " + expected);
88        }
89    }
90
91    /**
92     * Test the numberOfInvocations(), addInvocationRecord() and clearInvocationRecord() methods
93     */
94    public void testInvocationHistory() throws Exception {
95        control(session).expectAndDefaultReturn(session.getClientHost(), DEFAULT_HOST);
96        replay(session);
97
98        assertEquals("none", 0, commandHandler.numberOfInvocations());
99        commandHandler.handleCommand(COMMAND, session);
100        assertEquals("1", 1, commandHandler.numberOfInvocations());
101        commandHandler.handleCommand(COMMAND, session);
102        assertEquals("2", 2, commandHandler.numberOfInvocations());
103        commandHandler.clearInvocations();
104        assertEquals("cleared", 0, commandHandler.numberOfInvocations());
105    }
106
107    /**
108     * Test the getInvocation() method
109     *
110     * @throws Exception
111     */
112    public void testGetInvocation() throws Exception {
113        control(session).expectAndDefaultReturn(session.getClientHost(), DEFAULT_HOST);
114        replay(session);
115
116        commandHandler.handleCommand(COMMAND, session);
117        commandHandler.handleCommand(COMMAND_WITH_ARGS, session);
118        assertSame("1", COMMAND, commandHandler.getInvocation(0).getCommand());
119        assertSame("2", COMMAND_WITH_ARGS, commandHandler.getInvocation(1).getCommand());
120    }
121
122    /**
123     * Test the getInvocation() method, passing in an invalid index
124     */
125    public void testGetInvocation_IndexOutOfBounds() throws Exception {
126        commandHandler.handleCommand(COMMAND, session);
127        try {
128            commandHandler.getInvocation(2);
129            fail("Expected IndexOutOfBoundsException");
130        }
131        catch (IndexOutOfBoundsException expected) {
132            LOG.info("Expected: " + expected);
133        }
134    }
135
136    /**
137     * Test the sendReply() method, when no message arguments are specified
138     */
139    public void testSendReply() {
140        session.sendReply(REPLY_CODE1, REPLY_TEXT1);
141        session.sendReply(REPLY_CODE1, MESSAGE_TEXT);
142        session.sendReply(REPLY_CODE1, OVERRIDE_REPLY_TEXT);
143        session.sendReply(REPLY_CODE3, null);
144        replay(session);
145
146        commandHandler.sendReply(session, REPLY_CODE1, null, null, null);
147        commandHandler.sendReply(session, REPLY_CODE1, MESSAGE_KEY, null, null);
148        commandHandler.sendReply(session, REPLY_CODE1, MESSAGE_KEY, OVERRIDE_REPLY_TEXT, null);
149        commandHandler.sendReply(session, REPLY_CODE3, null, null, null);
150
151        verify(session);
152    }
153
154    /**
155     * Test the sendReply() method, passing in message arguments
156     */
157    public void testSendReply_WithMessageArguments() {
158        session.sendReply(REPLY_CODE1, REPLY_TEXT2_FORMATTED);
159        replay(session);
160
161        commandHandler.sendReply(session, REPLY_CODE1, null, REPLY_TEXT2, ARGS);
162
163        verify(session);
164    }
165
166    /**
167     * Test the sendReply() method, passing in a null Session
168     */
169    public void testSendReply_NullSession() {
170        try {
171            commandHandler.sendReply(null, REPLY_CODE1, REPLY_TEXT1, null, null);
172            fail("Expected AssertFailedException");
173        }
174        catch (AssertFailedException expected) {
175            LOG.info("Expected: " + expected);
176        }
177    }
178
179    /**
180     * Test the sendReply() method, passing in an invalid replyCode
181     */
182    public void testSendReply_InvalidReplyCode() {
183        try {
184            commandHandler.sendReply(session, 0, REPLY_TEXT1, null, null);
185            fail("Expected AssertFailedException");
186        }
187        catch (AssertFailedException expected) {
188            LOG.info("Expected: " + expected);
189        }
190    }
191
192    //-------------------------------------------------------------------------
193    // Test setup
194    //-------------------------------------------------------------------------
195
196    /**
197     * Perform initialization before each test
198     *
199     * @see org.mockftpserver.test.AbstractTest#setUp()
200     */
201    protected void setUp() throws Exception {
202        super.setUp();
203        session = (Session) createMock(Session.class);
204        control(session).setDefaultMatcher(MockControl.ARRAY_MATCHER);
205        commandHandler = new AbstractTrackingCommandHandler() {
206            public void handleCommand(Command command, Session session, InvocationRecord invocationRecord) throws Exception {
207            }
208        };
209        ResourceBundle replyTextBundle = new ListResourceBundle() {
210            protected Object[][] getContents() {
211                return new Object[][]{
212                        {Integer.toString(REPLY_CODE1), REPLY_TEXT1},
213                        {Integer.toString(REPLY_CODE2), REPLY_TEXT2},
214                        {MESSAGE_KEY, MESSAGE_TEXT}
215                };
216            }
217        };
218        commandHandler.setReplyTextBundle(replyTextBundle);
219    }
220
221}
222