StouCommandHandlerTest.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 StouCommandHandler
26 *
27 * @version $Revision$ - $Date$
28 *
29 * @author Chris Mair
30 */
31class StouCommandHandlerTest 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_SpecifyBaseFilename() {
39        handleCommand([FILENAME])
40        testHandleCommand(FILENAME)
41    }
42
43    void testHandleCommand_UseDefaultBaseFilename() {
44        handleCommand([])
45        testHandleCommand('Temp')
46    }
47
48    void testHandleCommand_CreateOutputStreamThrowsException() {
49        def newMethod = {String path, boolean append ->
50            println "Calling createOutputStream() - throwing exception"
51            throw new FileSystemException("bad")
52        }
53        overrideMethod(fileSystem, "createOutputStream", newMethod)
54
55        handleCommand([])
56        assertSessionReplies([ReplyCodes.TRANSFER_DATA_INITIAL_OK, ReplyCodes.NEW_FILE_ERROR])
57    }
58
59    //-------------------------------------------------------------------------
60    // Helper Methods
61    //-------------------------------------------------------------------------
62
63    CommandHandler createCommandHandler() {
64        new StouCommandHandler()
65    }
66
67    Command createValidCommand() {
68        return new Command(CommandNames.STOU, [])
69    }
70
71    void setUp() {
72        super.setUp()
73        assert fileSystem.createDirectory(DIR)
74        setCurrentDirectory(DIR)
75        session.dataToRead = CONTENTS.bytes
76        serverConfiguration.setTextForKey('stou', 'STOU {0}')
77    }
78
79    private void testHandleCommand(String expectedBaseName) {
80        assertSessionReply(0, ReplyCodes.TRANSFER_DATA_INITIAL_OK)
81        assertSessionReply(1, ReplyCodes.TRANSFER_DATA_FINAL_OK, 'STOU')
82
83        def names = fileSystem.listNames(DIR)
84        def filename = names.find {name -> name.startsWith(expectedBaseName) }
85        assert filename
86
87        assert session.getReplyMessage(1).contains(filename)
88
89        def absPath = p(DIR, filename)
90        assert fileSystem.exists(absPath)
91        def contents = fileSystem.createInputStream(absPath).text
92        assert contents == CONTENTS
93    }
94
95}