RetrCommandHandlerTest.java revision bda3441225e0607b5ced8b538123fd7c7a417910
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.easymock.MockControl;
20import org.mockftpserver.core.command.AbstractCommandHandlerTestCase;
21import org.mockftpserver.core.command.Command;
22import org.mockftpserver.core.command.CommandNames;
23import org.mockftpserver.core.command.ReplyCodes;
24import org.mockftpserver.core.util.AssertFailedException;
25
26/**
27 * Tests for the RetrCommandHandler class
28 *
29 * @author Chris Mair
30 * @version $Revision$ - $Date$
31 */
32public final class RetrCommandHandlerTest extends AbstractCommandHandlerTestCase {
33
34    private static final Logger LOG = Logger.getLogger(RetrCommandHandlerTest.class);
35
36    private RetrCommandHandler commandHandler;
37
38    /**
39     * Test the constructor that takes a String, passing in a null
40     */
41    public void testConstructor_String_Null() {
42        try {
43            new RetrCommandHandler((String) null);
44            fail("Expected AssertFailedException");
45        }
46        catch (AssertFailedException expected) {
47            LOG.info("Expected: " + expected);
48        }
49    }
50
51    /**
52     * Test the constructor that takes a byte[], passing in a null
53     */
54    public void testConstructor_ByteArray_Null() {
55        try {
56            new RetrCommandHandler((byte[]) null);
57            fail("Expected AssertFailedException");
58        }
59        catch (AssertFailedException expected) {
60            LOG.info("Expected: " + expected);
61        }
62    }
63
64    /**
65     * Test the setFileContents(String) method, passing in a null
66     */
67    public void testSetFileContents_String_Null() {
68        try {
69            commandHandler.setFileContents((String) null);
70            fail("Expected AssertFailedException");
71        }
72        catch (AssertFailedException expected) {
73            LOG.info("Expected: " + expected);
74        }
75    }
76
77    /**
78     * Test the setFileContents(byte[]) method, passing in a null
79     */
80    public void testSetFileContents_ByteArray_Null() {
81        try {
82            commandHandler.setFileContents((byte[]) null);
83            fail("Expected AssertFailedException");
84        }
85        catch (AssertFailedException expected) {
86            LOG.info("Expected: " + expected);
87        }
88    }
89
90    /**
91     * Test the handleCommand() method
92     *
93     * @throws Exception
94     */
95    public void testHandleCommand() throws Exception {
96        final String FILE_CONTENTS = "abc_123 456";
97        commandHandler.setFileContents(FILE_CONTENTS);
98
99        session.sendReply(ReplyCodes.TRANSFER_DATA_INITIAL_OK, replyTextFor(ReplyCodes.TRANSFER_DATA_INITIAL_OK));
100        session.openDataConnection();
101        session.sendData(FILE_CONTENTS.getBytes(), FILE_CONTENTS.length());
102        control(session).setMatcher(MockControl.ARRAY_MATCHER);
103        session.closeDataConnection();
104        session.sendReply(ReplyCodes.TRANSFER_DATA_FINAL_OK, replyTextFor(ReplyCodes.TRANSFER_DATA_FINAL_OK));
105        replay(session);
106
107        Command command = new Command(CommandNames.RETR, array(FILENAME1));
108        commandHandler.handleCommand(command, session);
109        verify(session);
110
111        verifyNumberOfInvocations(commandHandler, 1);
112        verifyOneDataElement(commandHandler.getInvocation(0), RetrCommandHandler.PATHNAME_KEY, FILENAME1);
113    }
114
115    /**
116     * Test the handleCommand() method, when no pathname parameter has been specified
117     */
118    public void testHandleCommand_MissingPathnameParameter() throws Exception {
119        testHandleCommand_InvalidParameters(commandHandler, CommandNames.RETR, EMPTY);
120    }
121
122    /**
123     * Perform initialization before each test
124     *
125     * @see org.mockftpserver.core.command.AbstractCommandHandlerTestCase#setUp()
126     */
127    protected void setUp() throws Exception {
128        super.setUp();
129        commandHandler = new RetrCommandHandler();
130        commandHandler.setReplyTextBundle(replyTextBundle);
131    }
132
133}
134