RetrCommandHandlerTest.groovy revision c51130cfee05b7294599cc93125db801972d0746
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
24import org.mockftpserver.fake.filesystem.FileSystemException
25
26
27/**
28 * Tests for RetrCommandHandler
29 *
30 * @version $Revision$ - $Date$
31 *
32 * @author Chris Mair
33 */
34class RetrCommandHandlerTest extends AbstractFakeCommandHandlerTest {
35
36    def DIR = "/"
37    def FILENAME = "file.txt"
38    def FILE = p(DIR, FILENAME)
39    def CONTENTS = "abc\ndef\nghi"
40    def CONTENTS_ASCII = "abc\r\ndef\r\nghi"
41
42    void testHandleCommand_MissingPathParameter() {
43        testHandleCommand_MissingRequiredParameter([])
44    }
45
46    void testHandleCommand_AbsolutePath() {
47        handleCommandAndVerifySendDataReplies([FILE])
48        assertSessionData(CONTENTS_ASCII)
49    }
50
51    void testHandleCommand_AbsolutePath_NonAsciiMode() {
52        session.setAttribute(SessionKeys.ASCII_TYPE, false)
53        handleCommandAndVerifySendDataReplies([FILE])
54        assertSessionData(CONTENTS)
55    }
56
57    void testHandleCommand_RelativePath() {
58        setCurrentDirectory(DIR)
59        handleCommandAndVerifySendDataReplies([FILENAME])
60        assertSessionData(CONTENTS_ASCII)
61    }
62
63    void testHandleCommand_PathSpecifiesAnExistingDirectory() {
64        commandHandler.handleCommand(createCommand([DIR]), session)
65        assertSessionReply(ReplyCodes.EXISTING_FILE_ERROR, ['filesystem.isNotAFile', DIR])
66    }
67
68    void testHandleCommand_PathDoesNotExist() {
69        def path = FILE + "XXX"
70        commandHandler.handleCommand(createCommand([path]), session)
71        assertSessionReply(ReplyCodes.EXISTING_FILE_ERROR, ['filesystem.pathDoesNotExist', path])
72    }
73
74    void testHandleCommand_ThrowsFileSystemException() {
75        fileSystem.delete(FILE)
76        def fileEntry = new BadFileEntry(FILE)
77        fileSystem.add(fileEntry)
78
79        handleCommand([FILE])
80        assertSessionReply(0, ReplyCodes.TRANSFER_DATA_INITIAL_OK)
81        assertSessionReply(1, ReplyCodes.EXISTING_FILE_ERROR, ERROR_MESSAGE_KEY)
82    }
83
84    void testConvertLfToCrLf() {
85        // LF='\n' and CRLF='\r\n'
86        assert commandHandler.convertLfToCrLf('abc'.bytes) == 'abc'.bytes
87        assert commandHandler.convertLfToCrLf('abc\r\ndef'.bytes) == 'abc\r\ndef'.bytes
88        assert commandHandler.convertLfToCrLf('abc\ndef'.bytes) == 'abc\r\ndef'.bytes
89        assert commandHandler.convertLfToCrLf('abc\ndef\nghi'.bytes) == 'abc\r\ndef\r\nghi'.bytes
90        assert commandHandler.convertLfToCrLf('\n'.bytes) == '\r\n'.bytes
91        assert commandHandler.convertLfToCrLf('\r\nabc\n'.bytes) == '\r\nabc\r\n'.bytes
92    }
93
94    //-------------------------------------------------------------------------
95    // Helper Methods
96    //-------------------------------------------------------------------------
97
98    CommandHandler createCommandHandler() {
99        new RetrCommandHandler()
100    }
101
102    Command createValidCommand() {
103        return new Command(CommandNames.RETR, [FILE])
104    }
105
106    void setUp() {
107        super.setUp()
108        createDirectory(DIR)
109        createFile(FILE, CONTENTS)
110    }
111
112}
113
114class BadFileEntry extends FileEntry {
115
116    BadFileEntry(String path) {
117        super(path)
118    }
119
120    InputStream createInputStream() {
121        throw new FileSystemException("BAD", AbstractFakeCommandHandlerTest.ERROR_MESSAGE_KEY)
122    }
123}