RetrCommandHandlerTest.groovy revision 0b92a4802c313851d15dad79c9914fd11fb2bdac
1/*
2 * Copyright 2008 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.fake.command
17
18import org.mockftpserver.core.command.Command
19import org.mockftpserver.core.command.CommandHandler
20import org.mockftpserver.core.command.CommandNames
21import org.mockftpserver.core.command.ReplyCodes
22import org.mockftpserver.core.session.SessionKeys
23import org.mockftpserver.fake.filesystem.FileEntry
24
25/**
26 * Tests for RetrCommandHandler
27 *
28 * @version $Revision$ - $Date$
29 *
30 * @author Chris Mair
31 */
32class RetrCommandHandlerTest extends AbstractLoginRequiredCommandHandlerTest {
33
34    def DIR = "/"
35    def FILENAME = "file.txt"
36    def FILE = p(DIR, FILENAME)
37    def CONTENTS = "abc\ndef\nghi"
38    def CONTENTS_ASCII = "abc\r\ndef\r\nghi"
39
40    void testHandleCommand_MissingPathParameter() {
41        testHandleCommand_MissingRequiredParameter([])
42    }
43
44    void testHandleCommand_AbsolutePath() {
45        handleCommandAndVerifySendDataReplies([FILE])
46        assertSessionData(CONTENTS_ASCII)
47    }
48
49    void testHandleCommand_AbsolutePath_NonAsciiMode() {
50        session.setAttribute(SessionKeys.ASCII_TYPE, false)
51        handleCommandAndVerifySendDataReplies([FILE])
52        assertSessionData(CONTENTS)
53    }
54
55    void testHandleCommand_RelativePath() {
56        setCurrentDirectory(DIR)
57        handleCommandAndVerifySendDataReplies([FILENAME])
58        assertSessionData(CONTENTS_ASCII)
59    }
60
61    void testHandleCommand_PathSpecifiesAnExistingDirectory() {
62        commandHandler.handleCommand(createCommand([DIR]), session)
63        assertSessionReply(ReplyCodes.EXISTING_FILE_ERROR, ['filesystem.isNotAFile', DIR])
64    }
65
66    void testHandleCommand_CreateInputStreamThrowsException() {
67        overrideMethodToThrowFileSystemException("createInputStream")
68        handleCommand([FILE])
69        assertSessionReply(0, ReplyCodes.TRANSFER_DATA_INITIAL_OK)
70        assertSessionReply(1, ReplyCodes.EXISTING_FILE_ERROR, ERROR_MESSAGE_KEY)
71    }
72
73    void testConvertLfToCrLf() {
74        // LF='\n' and CRLF='\r\n'
75        assert commandHandler.convertLfToCrLf('abc'.bytes) == 'abc'.bytes
76        assert commandHandler.convertLfToCrLf('abc\r\ndef'.bytes) == 'abc\r\ndef'.bytes
77        assert commandHandler.convertLfToCrLf('abc\ndef'.bytes) == 'abc\r\ndef'.bytes
78        assert commandHandler.convertLfToCrLf('abc\ndef\nghi'.bytes) == 'abc\r\ndef\r\nghi'.bytes
79        assert commandHandler.convertLfToCrLf('\n'.bytes) == '\r\n'.bytes
80        assert commandHandler.convertLfToCrLf('\r\nabc\n'.bytes) == '\r\nabc\r\n'.bytes
81    }
82
83    //-------------------------------------------------------------------------
84    // Helper Methods
85    //-------------------------------------------------------------------------
86
87    CommandHandler createCommandHandler() {
88        new RetrCommandHandler()
89    }
90
91    Command createValidCommand() {
92        return new Command(CommandNames.RETR, [FILE])
93    }
94
95    void setUp() {
96        super.setUp()
97        assert fileSystem.createDirectory(DIR)
98        fileSystem.addEntry(new FileEntry(path: FILE, contents: CONTENTS))
99    }
100
101}