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.commons.net.ftp.FTPClient;
19import org.mockftpserver.test.AbstractTest;
20import org.mockftpserver.test.PortTestUtil;
21
22/**
23 * Abstract superclass for tests of AbstractFtpServer subclasses that require the server thread to be started.
24 *
25 * @author Chris Mair
26 * @version $Revision$ - $Date$
27 */
28public abstract class AbstractFtpServer_StartTest extends AbstractTest {
29
30    private static final String SERVER = "localhost";
31
32    private AbstractFtpServer ftpServer;
33
34    /**
35     * Test the start() and stop() methods. Start the server and then stop it immediately.
36     */
37    public void testStartAndStop() throws Exception {
38        ftpServer.setServerControlPort(PortTestUtil.getFtpServerControlPort());
39        assertEquals("started - before", false, ftpServer.isStarted());
40
41        ftpServer.start();
42        Thread.sleep(200L);     // give it some time to get started
43        assertEquals("started - after start()", true, ftpServer.isStarted());
44        assertEquals("shutdown - after start()", false, ftpServer.isShutdown());
45
46        ftpServer.stop();
47
48        assertEquals("shutdown - after stop()", true, ftpServer.isShutdown());
49    }
50
51    /**
52     * Test setting a non-default port number for the StubFtpServer control connection socket.
53     */
54    public void testCustomServerControlPort() throws Exception {
55        final int SERVER_CONTROL_PORT = 9187;
56
57        ftpServer.setServerControlPort(SERVER_CONTROL_PORT);
58        ftpServer.start();
59
60        try {
61            FTPClient ftpClient = new FTPClient();
62            ftpClient.connect(SERVER, SERVER_CONTROL_PORT);
63        }
64        finally {
65            ftpServer.stop();
66        }
67    }
68
69    //-------------------------------------------------------------------------
70    // Test setup
71    //-------------------------------------------------------------------------
72
73    /**
74     * @see org.mockftpserver.test.AbstractTest#setUp()
75     */
76    protected void setUp() throws Exception {
77        super.setUp();
78        ftpServer = createFtpServer();
79    }
80
81    //-------------------------------------------------------------------------
82    // Abstract method declarations
83    //-------------------------------------------------------------------------
84
85    protected abstract AbstractFtpServer createFtpServer();
86
87}