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.example; 17 18import java.io.ByteArrayOutputStream; 19 20import org.apache.commons.net.ftp.FTPClient; 21import org.apache.commons.net.ftp.FTPFile; 22import org.apache.log4j.Logger; 23import org.mockftpserver.stub.StubFtpServer; 24import org.mockftpserver.test.AbstractTest; 25import org.springframework.context.ApplicationContext; 26import org.springframework.context.support.ClassPathXmlApplicationContext; 27 28/** 29 * Example test for StubFtpServer, using the Spring Framework ({@link http://www.springframework.org/}) 30 * for configuration. 31 */ 32public class SpringConfigurationTest extends AbstractTest { 33 34 private static final Logger LOG = Logger.getLogger(SpringConfigurationTest.class); 35 private static final String SERVER = "localhost"; 36 37 private StubFtpServer stubFtpServer; 38 private FTPClient ftpClient; 39 40 /** 41 * Test starting the StubFtpServer configured within the example Spring configuration file 42 */ 43 public void testStubFtpServer() throws Exception { 44 stubFtpServer.start(); 45 46 ftpClient.connect(SERVER); 47 48 // PWD 49 String dir = ftpClient.printWorkingDirectory(); 50 assertEquals("PWD", "foo/bar", dir); 51 52 // LIST 53 FTPFile[] files = ftpClient.listFiles(); 54 LOG.info("FTPFile[0]=" + files[0]); 55 LOG.info("FTPFile[1]=" + files[1]); 56 assertEquals("number of files from LIST", 2, files.length); 57 58 // DELE 59 assertFalse("DELE", ftpClient.deleteFile("AnyFile.txt")); 60 61 // RETR 62 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 63 assertTrue(ftpClient.retrieveFile("SomeFile.txt", outputStream)); 64 LOG.info("File contents=[" + outputStream.toString() + "]"); 65 } 66 67 /** 68 * @see org.mockftpserver.test.AbstractTest#setUp() 69 */ 70 protected void setUp() throws Exception { 71 super.setUp(); 72 73 ApplicationContext context = new ClassPathXmlApplicationContext("stubftpserver-beans.xml"); 74 stubFtpServer = (StubFtpServer) context.getBean("stubFtpServer"); 75 76 ftpClient = new FTPClient(); 77 } 78 79 /** 80 * @see org.mockftpserver.test.AbstractTest#tearDown() 81 */ 82 protected void tearDown() throws Exception { 83 super.tearDown(); 84 stubFtpServer.stop(); 85 } 86 87} 88