RntoCommandHandlerTest.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.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
28
29/**
30 * Tests for RntoCommandHandler
31 *
32 * @version $Revision$ - $Date$
33 *
34 * @author Chris Mair
35 */
36class RntoCommandHandlerTest extends AbstractLoginRequiredCommandHandlerTest {
37
38    def FROM_FILE = "/from.txt"
39    def TO_FILE = "/file.txt"
40
41    void testHandleCommand() {
42        assert fileSystem.createFile(FROM_FILE)
43        commandHandler.handleCommand(createCommand([TO_FILE]), session)
44        assertSessionReply(ReplyCodes.RNTO_OK)
45        assert !fileSystem.exists(FROM_FILE), FROM_FILE
46        assert fileSystem.exists(TO_FILE), TO_FILE
47        assert session.getAttribute(SessionKeys.RENAME_FROM) == null
48	}
49
50    void testHandleCommand_PathIsRelative() {
51        assert fileSystem.createFile(FROM_FILE)
52        session.setAttribute(SessionKeys.CURRENT_DIRECTORY, "/")
53        commandHandler.handleCommand(createCommand(["file.txt"]), session)
54        assertSessionReply(ReplyCodes.RNTO_OK)
55        assert !fileSystem.exists(FROM_FILE), FROM_FILE
56        assert fileSystem.exists(TO_FILE), TO_FILE
57        assert session.getAttribute(SessionKeys.RENAME_FROM) == null
58	}
59
60    void testHandleCommand_FromFileNotSetInSession() {
61        session.removeAttribute(SessionKeys.RENAME_FROM)
62        testHandleCommand_MissingRequiredSessionAttribute()
63	}
64
65    void testHandleCommand_ToFilenameNotValid() {
66        assert fileSystem.createFile(FROM_FILE)
67        commandHandler.handleCommand(createCommand(["///"]), session)
68        assertSessionReply(ReplyCodes.FILENAME_NOT_VALID, "///")
69        assert session.getAttribute(SessionKeys.RENAME_FROM) == FROM_FILE
70	}
71
72    void testHandleCommand_ToFilenameSpecifiesADirectory() {
73        assert fileSystem.createDirectory(TO_FILE)
74        commandHandler.handleCommand(createCommand([TO_FILE]), session)
75        assertSessionReply(ReplyCodes.NEW_FILE_ERROR, TO_FILE)
76        assert session.getAttribute(SessionKeys.RENAME_FROM) == FROM_FILE
77	}
78
79    void testHandleCommand_RenameFails() {
80        commandHandler.handleCommand(createCommand([TO_FILE]), session)
81        assertSessionReply(ReplyCodes.FILENAME_NOT_VALID, TO_FILE)
82        assert session.getAttribute(SessionKeys.RENAME_FROM) == FROM_FILE
83	}
84
85    void testHandleCommand_MissingPathParameter() {
86        testHandleCommand_MissingRequiredParameter([])
87    }
88
89    //-------------------------------------------------------------------------
90    // Helper Methods
91    //-------------------------------------------------------------------------
92
93	CommandHandler createCommandHandler() {
94	    new RntoCommandHandler()
95	}
96
97    Command createValidCommand() {
98        return new Command(CommandNames.RNTO, [TO_FILE])
99    }
100
101    void setUp() {
102        super.setUp()
103        session.setAttribute(SessionKeys.RENAME_FROM, FROM_FILE)
104    }
105
106}