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