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