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 testSystemStatus() {
91        assert ftpServer.systemStatus == "Connected"
92        ftpServer.systemStatus = "abc"
93        assert ftpServer.systemStatus == "abc"
94    }
95
96    void testReplyText() {
97        ftpServer.replyTextBaseName = "SampleReplyText"
98
99        ResourceBundle resourceBundle = ftpServer.replyTextBundle
100        assert resourceBundle.getString("110") == "Testing123"
101    }
102
103    //-------------------------------------------------------------------------
104    // Test set up
105    //-------------------------------------------------------------------------
106
107    void setUp() {
108        super.setUp();
109        commandHandler = new TestCommandHandler()
110        commandHandler_NotServerConfigurationAware = new TestCommandHandlerNotServerConfigurationAware()
111    }
112
113    //-------------------------------------------------------------------------
114    // Abstract method implementations
115    //-------------------------------------------------------------------------
116
117    protected AbstractFtpServer createFtpServer() {
118        return new FakeFtpServer();
119    }
120
121    protected CommandHandler createCommandHandler() {
122        return new TestCommandHandler();
123    }
124
125    protected void verifyCommandHandlerInitialized(CommandHandler commandHandler) {
126        //To change body of implemented methods use File | Settings | File Templates.
127    }
128
129}
130class TestCommandHandlerReplyTextBundleAware implements CommandHandler, ReplyTextBundleAware {
131    ResourceBundle replyTextBundle
132
133    public void handleCommand(Command command, Session session) {
134    }
135
136}