Home | History | Annotate | Download | only in server
      1 /*
      2  * Copyright 2011 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  */
     16 package org.mockftpserver.core.server
     17 
     18 import org.mockftpserver.fake.FakeFtpServer
     19 import org.mockftpserver.test.AbstractGroovyTestCase
     20 import org.mockftpserver.test.PortTestUtil
     21 
     22 /**
     23  * Test starting and stopping Abstract(Fake)FtpServer multiple times.
     24  *
     25  * @version $Revision: 242 $ - $Date: 2010-03-21 07:41:01 -0400 (Sun, 21 Mar 2010) $
     26  *
     27  * @author Chris Mair
     28  */
     29 class AbstractFtpServer_MultipleStartAndStopTest extends AbstractGroovyTestCase {
     30 
     31     private FakeFtpServer ftpServer = new FakeFtpServer()
     32 
     33     // Takes ~ 500ms per start/stop
     34 
     35     void testStartAndStop() {
     36         10.times {
     37             final def port = PortTestUtil.getFtpServerControlPort()
     38             ftpServer.setServerControlPort(port);
     39 
     40             ftpServer.start();
     41             assert ftpServer.getServerControlPort() == port
     42             Thread.sleep(100L);     // 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     void testStartAndStop_UseDynamicFreePort() {
     53         5.times {
     54             ftpServer.setServerControlPort(0);
     55             assert ftpServer.getServerControlPort() == 0
     56 
     57             ftpServer.start();
     58             log("Using port ${ftpServer.getServerControlPort()}")
     59             assert ftpServer.getServerControlPort() != 0
     60 
     61             ftpServer.stop();
     62         }
     63     }
     64 
     65     void tearDown() {
     66         super.tearDown()
     67         ftpServer.stop();   // just to be sure
     68     }
     69 }
     70