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.test.AbstractGroovyTest
19import org.mockftpserver.core.command.Command
20import org.mockftpserver.core.command.CommandHandler
21import org.mockftpserver.core.command.CommandNames
22import org.mockftpserver.core.session.StubSession
23import org.mockftpserver.core.session.SessionKeys
24import org.mockftpserver.fake.StubServerConfiguration
25import org.mockftpserver.fake.user.UserAccount
26import org.apache.log4j.Logger
27import org.mockftpserver.core.command.ReplyCodes
28import org.mockftpserver.fake.filesystem.FileSystem
29import org.mockftpserver.fake.filesystem.FileSystemException
30import org.mockftpserver.fake.filesystem.FakeUnixFileSystem
31import org.mockftpserver.fake.filesystem.FakeUnixFileSystem
32
33/**
34 * Tests for StorCommandHandler
35 *
36 * @version $Revision: $ - $Date: $
37 *
38 * @author Chris Mair
39 */
40class StorCommandHandlerTest extends AbstractLoginRequiredCommandHandlerTest {
41
42    def DIR = "/"
43    def FILENAME = "file.txt"
44    def FILE = p(DIR,FILENAME)
45    def CONTENTS = "abc"
46
47    void testHandleCommand_MissingPathParameter() {
48        testHandleCommand_MissingRequiredParameter([])
49    }
50
51    void testHandleCommand_AbsolutePath() {
52        session.dataToRead = CONTENTS.bytes
53        handleCommandAndVerifySendDataReplies([FILE])
54        assert fileSystem.isFile(FILE)
55
56        def contents = fileSystem.createInputStream(FILE).text
57        assert contents == CONTENTS
58	}
59
60    void testHandleCommand_RelativePath() {
61        setCurrentDirectory(DIR)
62        session.dataToRead = CONTENTS.bytes
63        handleCommandAndVerifySendDataReplies([FILENAME])
64        assert fileSystem.isFile(FILE)
65
66        def contents = fileSystem.createInputStream(FILE).text
67        assert contents == CONTENTS
68	}
69
70    void testHandleCommand_PathSpecifiesAnExistingDirectory() {
71        assert fileSystem.createDirectory(FILE)
72        commandHandler.handleCommand(createCommand([FILE]), session)
73        assertSessionReply(ReplyCodes.FILENAME_NOT_VALID, FILE)
74	}
75
76    void testHandleCommand_ParentDirectoryDoesNotExist() {
77        def NO_SUCH_DIR = "/path/DoesNotExist"
78        handleCommand([p(NO_SUCH_DIR,FILENAME)])
79        assertSessionReply(ReplyCodes.FILENAME_NOT_VALID, NO_SUCH_DIR)
80	}
81
82    void testHandleCommand_CreateOutputStreamThrowsException() {
83        // Override createOutputStream() method to throw exception
84        def emc = new ExpandoMetaClass(fileSystem.class, false)
85        emc.createOutputStream = { String path, boolean append ->
86            println "Calling createOutputStream() - throwing exception"
87            throw new FileSystemException("bad")
88        }
89        emc.initialize()
90        fileSystem.metaClass = emc
91
92        handleCommand([FILE])
93        assertSessionReply(0, ReplyCodes.SEND_DATA_INITIAL_OK)
94        assertSessionReply(1, ReplyCodes.NEW_FILE_ERROR)
95	}
96
97    //-------------------------------------------------------------------------
98    // Helper Methods
99    //-------------------------------------------------------------------------
100
101	CommandHandler createCommandHandler() {
102	    new StorCommandHandler()
103	}
104
105    Command createValidCommand() {
106        return new Command(CommandNames.STOR, [FILE])
107    }
108
109    void setUp() {
110        super.setUp()
111        assert fileSystem.createDirectory(DIR)
112    }
113
114}