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.stub.command;
17
18import org.apache.log4j.Logger;
19import org.mockftpserver.core.command.Command;
20import org.mockftpserver.core.command.InvocationRecord;
21import org.mockftpserver.core.session.Session;
22import org.mockftpserver.core.util.AssertFailedException;
23import org.mockftpserver.test.AbstractTest;
24
25import java.util.ListResourceBundle;
26import java.util.ResourceBundle;
27
28/**
29 * Tests for AbstractStubDataCommandHandler. The class name is prefixed with an underscore
30 * so that it is not filtered out by Maven's Surefire test plugin.
31 *
32 * @version $Revision$ - $Date$
33 *
34 * @author Chris Mair
35 */
36public final class _AbstractStubDataCommandHandlerTest extends AbstractTest {
37
38    private static final Logger LOG = Logger.getLogger(_AbstractStubDataCommandHandlerTest.class);
39    private static final Command COMMAND = new Command("command", EMPTY);
40    private static final InvocationRecord INVOCATION_RECORD = new InvocationRecord(COMMAND, DEFAULT_HOST);
41
42    private static final String REPLY_TEXT150 = "reply 150 ... abcdef";
43    private static final String REPLY_TEXT226 = "reply 226 ... abcdef";
44    private static final String REPLY_TEXT222 = "reply 222 ... abcdef";
45    private static final String REPLY_TEXT333 = "reply 333 ... abcdef";
46    private static final String REPLY_TEXT444 = "reply 444 ... abcdef";
47
48    private Session session;
49    private ResourceBundle replyTextBundle;
50    private AbstractStubDataCommandHandler commandHandler;
51
52    /**
53     * Test the handleCommand() method
54     */
55    public void testHandleCommand() throws Exception {
56
57        session.sendReply(150, REPLY_TEXT150);
58        session.openDataConnection();
59        session.sendReply(222, REPLY_TEXT222);
60        session.sendReply(333, REPLY_TEXT333);
61        session.sendReply(444, REPLY_TEXT444);
62        session.closeDataConnection();
63        session.sendReply(226, REPLY_TEXT226);
64        replay(session);
65
66        // Define CommandHandler test subclass
67        commandHandler = new AbstractStubDataCommandHandler() {
68            protected void beforeProcessData(Command c, Session s, InvocationRecord ir) {
69                verifyParameters(c, s, ir);
70                // Send unique reply code so that we can verify proper method invocation and ordering
71                session.sendReply(222, REPLY_TEXT222);
72            }
73
74            protected void processData(Command c, Session s, InvocationRecord ir) {
75                verifyParameters(c, s, ir);
76                // Send unique reply code so that we can verify proper method invocation and ordering
77                session.sendReply(333, REPLY_TEXT333);
78            }
79
80            protected void afterProcessData(Command c, Session s, InvocationRecord ir) {
81                verifyParameters(c, s, ir);
82                // Send unique reply code so that we can verify proper method invocation and ordering
83                session.sendReply(444, REPLY_TEXT444);
84            }
85
86            private void verifyParameters(Command c, Session s, InvocationRecord ir) {
87                assertSame("command", COMMAND, c);
88                assertSame("session", session, s);
89                assertSame("invocationRecord", INVOCATION_RECORD, ir);
90            }
91        };
92
93        commandHandler.setReplyTextBundle(replyTextBundle);
94        commandHandler.handleCommand(COMMAND, session, INVOCATION_RECORD);
95
96        verify(session);
97    }
98
99    /**
100     * Test the handleCommand() method, overriding the initial reply code and text
101     */
102    public void testHandleCommand_OverrideInitialReplyCodeAndText() throws Exception {
103
104        final int OVERRIDE_REPLY_CODE = 333;
105        final String OVERRIDE_REPLY_TEXT = "reply text";
106
107        session.sendReply(OVERRIDE_REPLY_CODE, OVERRIDE_REPLY_TEXT);
108        session.openDataConnection();
109        session.closeDataConnection();
110        session.sendReply(226, REPLY_TEXT226);
111        replay(session);
112
113        commandHandler.setPreliminaryReplyCode(OVERRIDE_REPLY_CODE);
114        commandHandler.setPreliminaryReplyText(OVERRIDE_REPLY_TEXT);
115        commandHandler.setReplyTextBundle(replyTextBundle);
116        commandHandler.handleCommand(COMMAND, session, INVOCATION_RECORD);
117
118        verify(session);
119    }
120
121    /**
122     * Test the setPreliminaryReplyCode() method, passing in an invalid value
123     */
124    public void testSetPreliminaryReplyCode_Invalid() {
125        try {
126            commandHandler.setPreliminaryReplyCode(0);
127            fail("Expected AssertFailedException");
128        }
129        catch (AssertFailedException expected) {
130            LOG.info("Expected: " + expected);
131        }
132    }
133
134    /**
135     * Test the setFinalReplyCode() method, passing in an invalid value
136     */
137    public void testSetFinalReplyCode_Invalid() {
138        try {
139            commandHandler.setFinalReplyCode(0);
140            fail("Expected AssertFailedException");
141        }
142        catch (AssertFailedException expected) {
143            LOG.info("Expected: " + expected);
144        }
145    }
146
147    //-------------------------------------------------------------------------
148    // Test setup
149    //-------------------------------------------------------------------------
150
151    /**
152     * Perform initialization before each test
153     *
154     * @see org.mockftpserver.test.AbstractTest#setUp()
155     */
156    protected void setUp() throws Exception {
157        super.setUp();
158        session = (Session) createMock(Session.class);
159        replyTextBundle = new ListResourceBundle() {
160            protected Object[][] getContents() {
161                return new Object[][] {
162                        { Integer.toString(150), REPLY_TEXT150 },
163                        { Integer.toString(222), REPLY_TEXT222 },
164                        { Integer.toString(226), REPLY_TEXT226 },
165                        { Integer.toString(333), REPLY_TEXT333 },
166                        { Integer.toString(444), REPLY_TEXT444 },
167                };
168            }
169        };
170        commandHandler = new AbstractStubDataCommandHandler() {
171            protected void processData(Command c, Session s, InvocationRecord ir) {
172            }
173        };
174    }
175
176}
177