RmdCommandHandlerTest.groovy revision 562a28c4b19670f3dcca8973ee68f166386dca26
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 RmdCommandHandler
32 *
33 * @version $Revision: $ - $Date: $
34 *
35 * @author Chris Mair
36 */
37class RmdCommandHandlerTest extends AbstractFakeCommandHandlerTest {
38
39    def DIR = "/usr"
40
41    void testHandleCommand() {
42        assert fileSystem.createDirectory(DIR)
43        commandHandler.handleCommand(createCommand([DIR]), session)
44        assertSessionReply(ReplyCodes.RMD_OK)
45        assert fileSystem.exists(DIR) == false
46	}
47
48    void testHandleCommand_PathDoesNotExistInFileSystem() {
49        commandHandler.handleCommand(createCommand([DIR]), session)
50        assertSessionReply(ReplyCodes.FILE_OPERATION_NOT_ALLOWED, DIR)
51	}
52
53    void testHandleCommand_PathSpecifiesAFile() {
54        assert fileSystem.createFile(DIR)
55        commandHandler.handleCommand(createCommand([DIR]), session)
56        assertSessionReply(ReplyCodes.FILE_OPERATION_NOT_ALLOWED, DIR)
57        assert fileSystem.exists(DIR)
58	}
59
60    void testHandleCommand_DirectoryIsNotEmpty() {
61        final FILE = DIR + "/file.txt"
62        assert fileSystem.createFile(FILE)
63        commandHandler.handleCommand(createCommand([DIR]), session)
64        assertSessionReply(ReplyCodes.FILE_OPERATION_NOT_ALLOWED, DIR)
65        assert fileSystem.exists(DIR)
66        assert fileSystem.exists(FILE)
67	}
68
69    void testHandleCommand_NotLoggedIn() {
70        session.removeAttribute(SessionKeys.USER_ACCOUNT)
71        testHandleCommand_MissingRequiredLogin()
72    }
73
74    void testHandleCommand_MissingPathParameter() {
75        testHandleCommand_MissingRequiredParameter([])
76    }
77
78    //-------------------------------------------------------------------------
79    // Helper Methods
80    //-------------------------------------------------------------------------
81
82	CommandHandler createCommandHandler() {
83	    new RmdCommandHandler()
84	}
85
86    Command createValidCommand() {
87        return new Command(CommandNames.RMD, [DIR])
88    }
89
90    void setUp() {
91        super.setUp()
92        session.setAttribute(SessionKeys.USER_ACCOUNT, userAccount)
93    }
94
95}