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