1bda3441225e0607b5ced8b538123fd7c7a417910chrismair/*
2bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Copyright 2011 the original author or authors.
3bda3441225e0607b5ced8b538123fd7c7a417910chrismair *
4bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Licensed under the Apache License, Version 2.0 (the "License");
5bda3441225e0607b5ced8b538123fd7c7a417910chrismair * you may not use this file except in compliance with the License.
6bda3441225e0607b5ced8b538123fd7c7a417910chrismair * You may obtain a copy of the License at
7bda3441225e0607b5ced8b538123fd7c7a417910chrismair *
8bda3441225e0607b5ced8b538123fd7c7a417910chrismair *      http://www.apache.org/licenses/LICENSE-2.0
9bda3441225e0607b5ced8b538123fd7c7a417910chrismair *
10bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Unless required by applicable law or agreed to in writing, software
11bda3441225e0607b5ced8b538123fd7c7a417910chrismair * distributed under the License is distributed on an "AS IS" BASIS,
12bda3441225e0607b5ced8b538123fd7c7a417910chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bda3441225e0607b5ced8b538123fd7c7a417910chrismair * See the License for the specific language governing permissions and
14bda3441225e0607b5ced8b538123fd7c7a417910chrismair * limitations under the License.
15bda3441225e0607b5ced8b538123fd7c7a417910chrismair */
16bda3441225e0607b5ced8b538123fd7c7a417910chrismairpackage org.mockftpserver.core.server
17bda3441225e0607b5ced8b538123fd7c7a417910chrismair
18bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.mockftpserver.fake.FakeFtpServer
19bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.mockftpserver.test.AbstractGroovyTestCase
20bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.mockftpserver.test.PortTestUtil
21bda3441225e0607b5ced8b538123fd7c7a417910chrismair
22bda3441225e0607b5ced8b538123fd7c7a417910chrismair/**
23bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Test starting and stopping Abstract(Fake)FtpServer multiple times.
24bda3441225e0607b5ced8b538123fd7c7a417910chrismair *
25bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @version $Revision: 242 $ - $Date: 2010-03-21 07:41:01 -0400 (Sun, 21 Mar 2010) $
26bda3441225e0607b5ced8b538123fd7c7a417910chrismair *
27bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @author Chris Mair
28bda3441225e0607b5ced8b538123fd7c7a417910chrismair */
29bda3441225e0607b5ced8b538123fd7c7a417910chrismairclass AbstractFtpServer_MultipleStartAndStopTest extends AbstractGroovyTestCase {
30bda3441225e0607b5ced8b538123fd7c7a417910chrismair
31bda3441225e0607b5ced8b538123fd7c7a417910chrismair    private FakeFtpServer ftpServer = new FakeFtpServer()
32bda3441225e0607b5ced8b538123fd7c7a417910chrismair
33bda3441225e0607b5ced8b538123fd7c7a417910chrismair    // Takes ~ 500ms per start/stop
34bda3441225e0607b5ced8b538123fd7c7a417910chrismair
35bda3441225e0607b5ced8b538123fd7c7a417910chrismair    void testStartAndStop() {
36bda3441225e0607b5ced8b538123fd7c7a417910chrismair        10.times {
37bda3441225e0607b5ced8b538123fd7c7a417910chrismair            final def port = PortTestUtil.getFtpServerControlPort()
38bda3441225e0607b5ced8b538123fd7c7a417910chrismair            ftpServer.setServerControlPort(port);
39bda3441225e0607b5ced8b538123fd7c7a417910chrismair
40bda3441225e0607b5ced8b538123fd7c7a417910chrismair            ftpServer.start();
41bda3441225e0607b5ced8b538123fd7c7a417910chrismair            assert ftpServer.getServerControlPort() == port
42bda3441225e0607b5ced8b538123fd7c7a417910chrismair            Thread.sleep(100L);     // give it some time to get started
43bda3441225e0607b5ced8b538123fd7c7a417910chrismair            assertEquals("started - after start()", true, ftpServer.isStarted());
44bda3441225e0607b5ced8b538123fd7c7a417910chrismair            assertEquals("shutdown - after start()", false, ftpServer.isShutdown());
45bda3441225e0607b5ced8b538123fd7c7a417910chrismair
46bda3441225e0607b5ced8b538123fd7c7a417910chrismair            ftpServer.stop();
47bda3441225e0607b5ced8b538123fd7c7a417910chrismair
48bda3441225e0607b5ced8b538123fd7c7a417910chrismair            assertEquals("shutdown - after stop()", true, ftpServer.isShutdown());
49bda3441225e0607b5ced8b538123fd7c7a417910chrismair        }
50bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
51bda3441225e0607b5ced8b538123fd7c7a417910chrismair
52bda3441225e0607b5ced8b538123fd7c7a417910chrismair    void testStartAndStop_UseDynamicFreePort() {
53bda3441225e0607b5ced8b538123fd7c7a417910chrismair        5.times {
54bda3441225e0607b5ced8b538123fd7c7a417910chrismair            ftpServer.setServerControlPort(0);
55bda3441225e0607b5ced8b538123fd7c7a417910chrismair            assert ftpServer.getServerControlPort() == 0
56bda3441225e0607b5ced8b538123fd7c7a417910chrismair
57bda3441225e0607b5ced8b538123fd7c7a417910chrismair            ftpServer.start();
58bda3441225e0607b5ced8b538123fd7c7a417910chrismair            log("Using port ${ftpServer.getServerControlPort()}")
59bda3441225e0607b5ced8b538123fd7c7a417910chrismair            assert ftpServer.getServerControlPort() != 0
60bda3441225e0607b5ced8b538123fd7c7a417910chrismair
61bda3441225e0607b5ced8b538123fd7c7a417910chrismair            ftpServer.stop();
62bda3441225e0607b5ced8b538123fd7c7a417910chrismair        }
63bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
64bda3441225e0607b5ced8b538123fd7c7a417910chrismair
65bda3441225e0607b5ced8b538123fd7c7a417910chrismair    void tearDown() {
66bda3441225e0607b5ced8b538123fd7c7a417910chrismair        super.tearDown()
67bda3441225e0607b5ced8b538123fd7c7a417910chrismair        ftpServer.stop();   // just to be sure
68bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
69bda3441225e0607b5ced8b538123fd7c7a417910chrismair}
70