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.FileEntry
23import org.mockftpserver.fake.filesystem.Permissions
24
25/**
26 * Tests for AppeCommandHandler
27 *
28 * @version $Revision$ - $Date$
29 *
30 * @author Chris Mair
31 */
32class AppeCommandHandlerTest extends AbstractStoreFileCommandHandlerTest {
33
34    void testHandleCommand_MissingPathParameter() {
35        testHandleCommand_MissingRequiredParameter([])
36    }
37
38    void testHandleCommand_AbsolutePath() {
39        userAccount.defaultPermissionsForNewFile = Permissions.NONE
40        testHandleCommand([FILE], 'appe', CONTENTS)
41    }
42
43    void testHandleCommand_AbsolutePath_FileAlreadyExists() {
44        def ORIGINAL_CONTENTS = '123 456 789'
45        fileSystem.add(new FileEntry(path: FILE, contents: ORIGINAL_CONTENTS))
46        testHandleCommand([FILE], 'appe', ORIGINAL_CONTENTS + CONTENTS)
47    }
48
49    void testHandleCommand_RelativePath() {
50        setCurrentDirectory(DIR)
51        testHandleCommand([FILENAME], 'appe', CONTENTS)
52    }
53
54    void testHandleCommand_PathSpecifiesAnExistingDirectory() {
55        createDirectory(FILE)
56        handleCommand([FILE])
57        assertSessionReply(ReplyCodes.FILENAME_NOT_VALID, FILE)
58    }
59
60    void testHandleCommand_ParentDirectoryDoesNotExist() {
61        def NO_SUCH_DIR = "/path/DoesNotExist"
62        handleCommand([p(NO_SUCH_DIR, FILENAME)])
63        assertSessionReply(ReplyCodes.FILENAME_NOT_VALID, NO_SUCH_DIR)
64    }
65
66    //-------------------------------------------------------------------------
67    // Helper Methods
68    //-------------------------------------------------------------------------
69
70    CommandHandler createCommandHandler() {
71        new AppeCommandHandler()
72    }
73
74    Command createValidCommand() {
75        return new Command(CommandNames.APPE, [FILE])
76    }
77
78    void setUp() {
79        super.setUp()
80    }
81
82    protected String verifyOutputFile() {
83        assert fileSystem.isFile(FILE)
84        assert session.getReplyMessage(1).contains(FILENAME)
85        return FILE
86    }
87
88}