_AbstractFakeCommandHandlerTest.groovy revision 9b31560d2bb49af587fe31dbc4f272d726eec298
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.CommandSyntaxException
19import org.mockftpserver.core.IllegalStateException
20import org.mockftpserver.core.NotLoggedInException
21import org.mockftpserver.core.command.Command
22import org.mockftpserver.core.command.ReplyCodes
23import org.mockftpserver.core.session.Session
24import org.mockftpserver.core.session.SessionKeys
25import org.mockftpserver.core.session.StubSession
26import org.mockftpserver.fake.filesystem.ExistingFileOperationException
27import org.mockftpserver.fake.filesystem.FakeUnixFileSystem
28import org.mockftpserver.fake.filesystem.FileSystemException
29import org.mockftpserver.fake.filesystem.InvalidFilenameException
30import org.mockftpserver.fake.filesystem.NewFileOperationException
31import org.mockftpserver.fake.server.StubServerConfiguration
32import org.mockftpserver.test.AbstractGroovyTest
33
34/**
35 * Tests for AbstractFakeCommandHandler
36 *
37 * @version $Revision$ - $Date$
38 *
39 * @author Chris Mair
40 */
41class AbstractFakeCommandHandlerClassTest extends AbstractGroovyTest {
42
43    static PATH = "some/path"
44    static REPLY_CODE = 99
45    static MESSAGE_KEY = "99.WithFilename"
46    static ARG = "ABC"
47    static MSG = "text {0}"
48    static MSG_WITH_ARG = "text ABC"
49    static MSG_FOR_KEY = "some other message"
50    private AbstractFakeCommandHandler commandHandler
51    private session
52    private serverConfiguration
53    private fileSystem
54
55    //-------------------------------------------------------------------------
56    // Tests
57    //-------------------------------------------------------------------------
58
59    void testHandleCommand() {
60        def command = new Command("C1", ["abc"])
61        commandHandler.handleCommand(command, session)
62        assert commandHandler.handled
63
64        assertHandleCommandReplyCode(new CommandSyntaxException(""), ReplyCodes.COMMAND_SYNTAX_ERROR)
65        assertHandleCommandReplyCode(new IllegalStateException(""), ReplyCodes.ILLEGAL_STATE)
66        assertHandleCommandReplyCode(new NotLoggedInException(""), ReplyCodes.NOT_LOGGED_IN)
67        assertHandleCommandReplyCode(new ExistingFileOperationException(""), ReplyCodes.EXISTING_FILE_ERROR)
68        assertHandleCommandReplyCode(new NewFileOperationException(""), ReplyCodes.NEW_FILE_ERROR)
69        assertHandleCommandReplyCode(new InvalidFilenameException(""), ReplyCodes.FILENAME_NOT_VALID)
70
71        shouldFail { commandHandler.handleCommand(null, session) }
72        shouldFail { commandHandler.handleCommand(command, null) }
73    }
74
75    void testHandleCommand_FileSystemException() {
76        assertHandleCommandReplyCode(new FileSystemException(PATH), ReplyCodes.EXISTING_FILE_ERROR, PATH)
77        commandHandler.replyCodeForFileSystemException = ReplyCodes.NEW_FILE_ERROR
78        assertHandleCommandReplyCode(new FileSystemException(PATH), ReplyCodes.NEW_FILE_ERROR, PATH)
79    }
80
81    void testSendReply() {
82        commandHandler.sendReply(session, REPLY_CODE)
83        assert session.sentReplies[0] == [REPLY_CODE, MSG], session.sentReplies[0]
84
85        commandHandler.sendReply(session, REPLY_CODE, [ARG])
86        assert session.sentReplies[1] == [REPLY_CODE, MSG_WITH_ARG], session.sentReplies[0]
87
88        shouldFail { commandHandler.sendReply(null, REPLY_CODE) }
89        shouldFail { commandHandler.sendReply(session, 0) }
90    }
91
92    void testSendReply_MessageKey() {
93        commandHandler.sendReply(session, REPLY_CODE, MESSAGE_KEY)
94        assert session.sentReplies[0] == [REPLY_CODE, MSG_FOR_KEY], session.sentReplies[0]
95
96        shouldFail { commandHandler.sendReply(null, REPLY_CODE, MESSAGE_KEY) }
97        shouldFail { commandHandler.sendReply(session, 0, MESSAGE_KEY) }
98    }
99
100    void testAssertValidReplyCode() {
101        commandHandler.assertValidReplyCode(1)        // no exception expected
102        shouldFail { commandHandler.assertValidReplyCode(0) }
103    }
104
105    void testGetRequiredParameter() {
106        def command = new Command("C1", ["abc"])
107        assert commandHandler.getRequiredParameter(command) == "abc"
108        assert commandHandler.getRequiredParameter(command, 0) == "abc"
109        shouldFail(CommandSyntaxException) { commandHandler.getRequiredParameter(command, 1) }
110
111        command = new Command("C1", [])
112        shouldFail(CommandSyntaxException) { commandHandler.getRequiredParameter(command) }
113    }
114
115    void testGetRequiredSessionAttribute() {
116        shouldFail(IllegalStateException) { commandHandler.getRequiredSessionAttribute(session, "undefined") }
117
118        session.setAttribute("abc", "not empty")
119        commandHandler.getRequiredSessionAttribute(session, "abc") // no exception
120
121        session.setAttribute("abc", "")
122        commandHandler.getRequiredSessionAttribute(session, "abc") // no exception
123    }
124
125    void testVerifyLoggedIn() {
126        shouldFail(NotLoggedInException) { commandHandler.verifyLoggedIn(session) }
127        session.setAttribute(SessionKeys.USER_ACCOUNT, "OK")
128        commandHandler.verifyLoggedIn(session)        // no exception expected
129    }
130
131    void testVerifyFileSystemCondition() {
132        commandHandler.verifyFileSystemCondition(true, PATH)    // no exception expected
133        shouldFail(FileSystemException) { commandHandler.verifyFileSystemCondition(false, PATH) }
134        shouldFail(FileSystemException) { commandHandler.verifyFileSystemCondition([], PATH) }
135    }
136
137    void testGetRealPath() {
138        assert commandHandler.getRealPath(session, "/xxx") == "/xxx"
139
140        session.setAttribute(SessionKeys.CURRENT_DIRECTORY, "/usr/me")
141        assert commandHandler.getRealPath(session, null) == "/usr/me"
142        assert commandHandler.getRealPath(session, "/xxx") == "/xxx"
143        assert commandHandler.getRealPath(session, "xxx") == "/usr/me/xxx"
144    }
145
146    //-------------------------------------------------------------------------
147    // Test Setup
148    //-------------------------------------------------------------------------
149
150    void setUp() {
151        super.setUp()
152        commandHandler = new TestFakeCommandHandler()
153        session = new StubSession()
154        serverConfiguration = new StubServerConfiguration()
155        fileSystem = new FakeUnixFileSystem()
156        serverConfiguration.setFileSystem(fileSystem)
157
158        serverConfiguration.setTextForKey(REPLY_CODE, MSG)
159        serverConfiguration.setTextForKey(MESSAGE_KEY, MSG_FOR_KEY)
160
161        commandHandler.serverConfiguration = serverConfiguration
162    }
163
164    //-------------------------------------------------------------------------
165    // Helper Methods
166    //-------------------------------------------------------------------------
167
168    /**
169     * Assert that when the CommandHandler handleCommand() method throws the
170     * specified exception, that the expected reply is sent through the session.
171     */
172    private void assertHandleCommandReplyCode(Throwable exception, int expected, text = null) {
173        commandHandler.exception = exception
174        def command = new Command("C1", ["abc"])
175        session.sentReplies.clear()
176        commandHandler.handleCommand(command, session)
177        def sentReply = session.sentReplies[0][0]
178        assert sentReply == expected
179        if (text) {
180            def sentMessage = session.sentReplies[0][1]
181            assert sentMessage.contains(text), "sentMessage=[$sentMessage] text=[$text]"
182        }
183    }
184
185}
186
187/**
188 * Concrete subclass of AbstractFakeCommandHandler for testing
189 */
190private class TestFakeCommandHandler extends AbstractFakeCommandHandler {
191    boolean handled = false
192    def exception
193
194    protected void handle(Command command, Session session) {
195        if (exception) {
196            throw exception
197        }
198        this.handled = true
199    }
200}