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