FakeFtpServerTest.groovy revision 0da0f84ad9831bea470896e79b5ec207c83f258e
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
17
18import org.mockftpserver.core.command.Command
19import org.mockftpserver.core.command.CommandHandler
20import org.mockftpserver.core.command.ReplyTextBundleAware
21import org.mockftpserver.core.server.AbstractFtpServer
22import org.mockftpserver.core.server.AbstractFtpServerTest
23import org.mockftpserver.core.session.Session
24import org.mockftpserver.fake.FakeFtpServer
25import org.mockftpserver.fake.TestCommandHandler
26import org.mockftpserver.fake.TestCommandHandlerNotServerConfigurationAware
27import org.mockftpserver.fake.UserAccount
28
29/**
30 * Tests for FakeFtpServer.
31 *
32 * @version $Revision$ - $Date$
33 *
34 * @author Chris Mair
35 */
36class FakeFtpServerTest extends AbstractFtpServerTest {
37
38    def commandHandler
39    def commandHandler_NotServerConfigurationAware
40
41    //-------------------------------------------------------------------------
42    // Extra tests  (Standard tests defined in superclass)
43    //-------------------------------------------------------------------------
44
45    void testSetCommandHandler_NotServerConfigurationAware() {
46        ftpServer.setCommandHandler("ZZZ", commandHandler_NotServerConfigurationAware)
47        assert ftpServer.getCommandHandler("ZZZ") == commandHandler_NotServerConfigurationAware
48    }
49
50    void testSetCommandHandler_ServerConfigurationAware() {
51        ftpServer.setCommandHandler("ZZZ", commandHandler)
52        assert ftpServer.getCommandHandler("ZZZ") == commandHandler
53        assert ftpServer == commandHandler.serverConfiguration
54    }
55
56    void testSetCommandHandler_ReplyTextBundleAware() {
57        def cmdHandler = new TestCommandHandlerReplyTextBundleAware()
58        ftpServer.setCommandHandler("ZZZ", cmdHandler)
59        assert ftpServer.getCommandHandler("ZZZ") == cmdHandler
60        assert ftpServer.replyTextBundle == cmdHandler.replyTextBundle
61    }
62
63    void testUserAccounts() {
64        def userAccount = new UserAccount(username: 'abc')
65
66        // addUserAccount()
67        ftpServer.addUserAccount(userAccount)
68        assert ftpServer.getUserAccount("abc") == userAccount
69
70        // setUserAccounts
71        def userAccounts = [userAccount]
72        ftpServer.userAccounts = userAccounts
73        assert ftpServer.getUserAccount("abc") == userAccount
74    }
75
76    void testHelpText() {
77        ftpServer.helpText = [a: 'aaaaa', b: 'bbbbb', '': 'default']
78        assert ftpServer.getHelpText('a') == 'aaaaa'
79        assert ftpServer.getHelpText('b') == 'bbbbb'
80        assert ftpServer.getHelpText('') == 'default'
81        assert ftpServer.getHelpText('unrecognized') == null
82    }
83
84    void testSystemName() {
85        assert ftpServer.systemName == "WINDOWS"
86        ftpServer.systemName = "abc"
87        assert ftpServer.systemName == "abc"
88    }
89
90    void testReplyText() {
91        ftpServer.replyTextBaseName = "SampleReplyText"
92
93        ResourceBundle resourceBundle = ftpServer.replyTextBundle
94        assert resourceBundle.getString("110") == "Testing123"
95    }
96
97    //-------------------------------------------------------------------------
98    // Test set up
99    //-------------------------------------------------------------------------
100
101    void setUp() {
102        super.setUp();
103        commandHandler = new TestCommandHandler()
104        commandHandler_NotServerConfigurationAware = new TestCommandHandlerNotServerConfigurationAware()
105    }
106
107    //-------------------------------------------------------------------------
108    // Abstract method implementations
109    //-------------------------------------------------------------------------
110
111    protected AbstractFtpServer createFtpServer() {
112        return new FakeFtpServer();
113    }
114
115    protected CommandHandler createCommandHandler() {
116        return new TestCommandHandler();
117    }
118
119    protected void verifyCommandHandlerInitialized(CommandHandler commandHandler) {
120        //To change body of implemented methods use File | Settings | File Templates.
121    }
122
123}
124class TestCommandHandlerReplyTextBundleAware implements CommandHandler, ReplyTextBundleAware {
125    ResourceBundle replyTextBundle
126
127    public void handleCommand(Command command, Session session) {
128    }
129
130}