StorCommandHandlerTest.groovy revision 5f38fd8ca104fe456a2cf7fcedc56e9b0ce777d3
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.fake.filesystem.FileSystemException
23
24/**
25 * Tests for StorCommandHandler
26 *
27 * @version $Revision$ - $Date$
28 *
29 * @author Chris Mair
30 */
31class StorCommandHandlerTest extends AbstractLoginRequiredCommandHandlerTest {
32
33    def DIR = "/"
34    def FILENAME = "file.txt"
35    def FILE = p(DIR, FILENAME)
36    def CONTENTS = "abc"
37
38    void testHandleCommand_MissingPathParameter() {
39        testHandleCommand_MissingRequiredParameter([])
40    }
41
42    void testHandleCommand_AbsolutePath() {
43        session.dataToRead = CONTENTS.bytes
44        handleCommandAndVerifySendDataReplies([FILE])
45        assert fileSystem.isFile(FILE)
46
47        def contents = fileSystem.createInputStream(FILE).text
48        assert contents == CONTENTS
49    }
50
51    void testHandleCommand_RelativePath() {
52        setCurrentDirectory(DIR)
53        session.dataToRead = CONTENTS.bytes
54        handleCommandAndVerifySendDataReplies([FILENAME])
55        assert fileSystem.isFile(FILE)
56
57        def contents = fileSystem.createInputStream(FILE).text
58        assert contents == CONTENTS
59    }
60
61    void testHandleCommand_PathSpecifiesAnExistingDirectory() {
62        assert fileSystem.createDirectory(FILE)
63        commandHandler.handleCommand(createCommand([FILE]), session)
64        assertSessionReply(ReplyCodes.FILENAME_NOT_VALID, FILE)
65    }
66
67    void testHandleCommand_ParentDirectoryDoesNotExist() {
68        def NO_SUCH_DIR = "/path/DoesNotExist"
69        handleCommand([p(NO_SUCH_DIR, FILENAME)])
70        assertSessionReply(ReplyCodes.FILENAME_NOT_VALID, NO_SUCH_DIR)
71    }
72
73    void testHandleCommand_CreateOutputStreamThrowsException() {
74        def newMethod = {String path, boolean append ->
75            println "Calling createOutputStream() - throwing exception"
76            throw new FileSystemException("bad")
77        }
78        overrideMethod(fileSystem, "createOutputStream", newMethod)
79
80        handleCommand([FILE])
81        assertSessionReplies([ReplyCodes.TRANSFER_DATA_INITIAL_OK, ReplyCodes.NEW_FILE_ERROR])
82    }
83
84    //-------------------------------------------------------------------------
85    // Helper Methods
86    //-------------------------------------------------------------------------
87
88    CommandHandler createCommandHandler() {
89        new StorCommandHandler()
90    }
91
92    Command createValidCommand() {
93        return new Command(CommandNames.STOR, [FILE])
94    }
95
96    void setUp() {
97        super.setUp()
98        assert fileSystem.createDirectory(DIR)
99    }
100
101}