StorCommandHandlerTest.groovy revision 7d4a3a2990f1a386eaf9d50cf05a9d45fab32de6
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        // Override createOutputStream() method to throw exception
75        def emc = new ExpandoMetaClass(fileSystem.class, false)
76        emc.createOutputStream = { String path, boolean append ->
77            println "Calling createOutputStream() - throwing exception"
78            throw new FileSystemException("bad")
79        }
80        emc.initialize()
81        fileSystem.metaClass = emc
82
83        handleCommand([FILE])
84        assertSessionReply(0, ReplyCodes.SEND_DATA_INITIAL_OK)
85        assertSessionReply(1, ReplyCodes.NEW_FILE_ERROR)
86	}
87
88    //-------------------------------------------------------------------------
89    // Helper Methods
90    //-------------------------------------------------------------------------
91
92	CommandHandler createCommandHandler() {
93	    new StorCommandHandler()
94	}
95
96    Command createValidCommand() {
97        return new Command(CommandNames.STOR, [FILE])
98    }
99
100    void setUp() {
101        super.setUp()
102        assert fileSystem.createDirectory(DIR)
103    }
104
105}