1/*
2 * Copyright 2007 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.core.server;
17
18import org.apache.log4j.Logger;
19import org.mockftpserver.core.command.CommandHandler;
20import org.mockftpserver.core.command.CommandNames;
21import org.mockftpserver.core.session.DefaultSession;
22import org.mockftpserver.core.util.AssertFailedException;
23import org.mockftpserver.test.AbstractTest;
24
25import java.net.Socket;
26import java.util.HashMap;
27import java.util.Map;
28
29/**
30 * Abstract superclass for tests of AbstractFtpServer subclasses.
31 *
32 * @author Chris Mair
33 * @version $Revision$ - $Date$
34 */
35public abstract class AbstractFtpServerTest extends AbstractTest {
36
37    protected Logger LOG = Logger.getLogger(getClass());
38
39    protected AbstractFtpServer ftpServer;
40    private CommandHandler commandHandler;
41    private CommandHandler commandHandler2;
42
43    /**
44     * Test the setCommandHandlers() method
45     */
46    public void testSetCommandHandlers() {
47        Map mapping = new HashMap();
48        mapping.put("AAA", commandHandler);
49        mapping.put("BBB", commandHandler2);
50
51        ftpServer.setCommandHandlers(mapping);
52        assertSame("commandHandler1", commandHandler, ftpServer.getCommandHandler("AAA"));
53        assertSame("commandHandler2", commandHandler2, ftpServer.getCommandHandler("BBB"));
54
55        verifyCommandHandlerInitialized(commandHandler);
56        verifyCommandHandlerInitialized(commandHandler2);
57
58        // Make sure default CommandHandlers are still set
59        assertTrue("ConnectCommandHandler", ftpServer.getCommandHandler(CommandNames.CONNECT) != null);
60    }
61
62    /**
63     * Test the setCommandHandlers() method, when the Map is null
64     */
65    public void testSetCommandHandlers_Null() {
66        try {
67            ftpServer.setCommandHandlers(null);
68            fail("Expected AssertFailedException");
69        }
70        catch (AssertFailedException expected) {
71            LOG.info("Expected: " + expected);
72        }
73    }
74
75    /**
76     * Test the setCommandHandler() method
77     */
78    public void testSetCommandHandler() {
79        ftpServer.setCommandHandler("ZZZ", commandHandler2);
80        assertSame("commandHandler", commandHandler2, ftpServer.getCommandHandler("ZZZ"));
81        verifyCommandHandlerInitialized(commandHandler2);
82    }
83
84    /**
85     * Test the setCommandHandler() method, when the commandName is null
86     */
87    public void testSetCommandHandler_NullCommandName() {
88        CommandHandler commandHandler = (CommandHandler) createMock(CommandHandler.class);
89        try {
90            ftpServer.setCommandHandler(null, commandHandler);
91            fail("Expected AssertFailedException");
92        }
93        catch (AssertFailedException expected) {
94            LOG.info("Expected: " + expected);
95        }
96    }
97
98    /**
99     * Test the setCommandHandler() method, when the commandHandler is null
100     */
101    public void testSetCommandHandler_NullCommandHandler() {
102        try {
103            ftpServer.setCommandHandler("ZZZ", null);
104            fail("Expected AssertFailedException");
105        }
106        catch (AssertFailedException expected) {
107            LOG.info("Expected: " + expected);
108        }
109    }
110
111    /**
112     * Test the setCommandHandler() and getCommandHandler() methods for commands in lower case or mixed case
113     */
114    public void testLowerCaseOrMixedCaseCommandNames() {
115        ftpServer.setCommandHandler("XXX", commandHandler);
116        assertSame("ZZZ", commandHandler, ftpServer.getCommandHandler("XXX"));
117        assertSame("Zzz", commandHandler, ftpServer.getCommandHandler("Xxx"));
118        assertSame("zzz", commandHandler, ftpServer.getCommandHandler("xxx"));
119
120        ftpServer.setCommandHandler("YyY", commandHandler);
121        assertSame("ZZZ", commandHandler, ftpServer.getCommandHandler("YYY"));
122        assertSame("Zzz", commandHandler, ftpServer.getCommandHandler("Yyy"));
123        assertSame("zzz", commandHandler, ftpServer.getCommandHandler("yyy"));
124
125        ftpServer.setCommandHandler("zzz", commandHandler);
126        assertSame("ZZZ", commandHandler, ftpServer.getCommandHandler("ZZZ"));
127        assertSame("Zzz", commandHandler, ftpServer.getCommandHandler("zzZ"));
128        assertSame("zzz", commandHandler, ftpServer.getCommandHandler("zzz"));
129    }
130
131    /**
132     * Test calling stop() for a server that was never started.
133     */
134    public void testStopWithoutStart() {
135        ftpServer.stop();
136    }
137
138    public void testCreateSession() {
139        assertEquals(ftpServer.createSession(new Socket()).getClass(), DefaultSession.class);
140    }
141
142    //-------------------------------------------------------------------------
143    // Test setup
144    //-------------------------------------------------------------------------
145
146    /**
147     * @see org.mockftpserver.test.AbstractTest#setUp()
148     */
149    protected void setUp() throws Exception {
150        super.setUp();
151
152        ftpServer = createFtpServer();
153
154        commandHandler = createCommandHandler();
155        commandHandler2 = createCommandHandler();
156    }
157
158    //-------------------------------------------------------------------------
159    // Abstract method declarations
160    //-------------------------------------------------------------------------
161
162    protected abstract AbstractFtpServer createFtpServer();
163
164    protected abstract CommandHandler createCommandHandler();
165
166    protected abstract void verifyCommandHandlerInitialized(CommandHandler commandHandler);
167
168}