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