1package org.mockftpserver.fake.example 2 3import org.apache.commons.net.ftp.FTPClient 4import org.apache.commons.net.ftp.FTPFile 5import org.mockftpserver.fake.FakeFtpServer 6import org.mockftpserver.test.AbstractGroovyTest 7import org.springframework.context.ApplicationContext 8import org.springframework.context.support.ClassPathXmlApplicationContext 9 10/* 11* Copyright 2008 the original author or authors. 12* 13* Licensed under the Apache License, Version 2.0 (the "License"); 14* you may not use this file except in compliance with the License. 15* You may obtain a copy of the License at 16* 17* http://www.apache.org/licenses/LICENSE-2.0 18* 19* Unless required by applicable law or agreed to in writing, software 20* distributed under the License is distributed on an "AS IS" BASIS, 21* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 22* See the License for the specific language governing permissions and 23* limitations under the License. 24*/ 25class FakeFtpServerSpringConfigurationTest extends AbstractGroovyTest { 26 27 static final SERVER = "localhost" 28 static final PORT = 9981 29 static final USERNAME = 'joe' // Must match Spring config 30 static final PASSWORD = 'password' // Must match Spring config 31 32 private FakeFtpServer fakeFtpServer 33 private FTPClient ftpClient 34 35 void testFakeFtpServer_Unix() { 36 startFtpServer('fakeftpserver-beans.xml') 37 connectAndLogin() 38 39 // PWD 40 String dir = ftpClient.printWorkingDirectory() 41 assert dir == '/' 42 43 // LIST 44 FTPFile[] files = ftpClient.listFiles() 45 LOG.info("FTPFile[0]=" + files[0]) 46 assert files.length == 1 47 48 // RETR 49 ByteArrayOutputStream outputStream = new ByteArrayOutputStream() 50 assert ftpClient.retrieveFile("File.txt", outputStream) 51 LOG.info("File contents=[" + outputStream.toString() + "]") 52 } 53 54 void testFakeFtpServer_Windows_WithPermissions() { 55 startFtpServer('fakeftpserver-permissions-beans.xml') 56 connectAndLogin() 57 58 // PWD 59 String dir = ftpClient.printWorkingDirectory() 60 assert dir == 'c:\\' 61 62 // LIST 63 FTPFile[] files = ftpClient.listFiles() 64 assert files.length == 2 65 LOG.info("FTPFile[0]=" + files[0]) 66 LOG.info("FTPFile[1]=" + files[1]) 67 68 // RETR - File1.txt; we have required permissions 69 ByteArrayOutputStream outputStream = new ByteArrayOutputStream() 70 assert ftpClient.retrieveFile("File1.txt", outputStream) 71 LOG.info("File contents=[" + outputStream.toString() + "]") 72 73 // RETR - File2.txt; we DO NOT have required permissions 74 outputStream = new ByteArrayOutputStream() 75 assert !ftpClient.retrieveFile("File2.txt", outputStream) 76 assert ftpClient.replyCode == 550 77 } 78 79 void setUp() { 80 super.setUp() 81 ftpClient = new FTPClient() 82 } 83 84 void tearDown() { 85 super.tearDown() 86 fakeFtpServer?.stop() 87 } 88 89 private void startFtpServer(String springConfigFile) { 90 ApplicationContext context = new ClassPathXmlApplicationContext(springConfigFile) 91 fakeFtpServer = (FakeFtpServer) context.getBean("fakeFtpServer") 92 fakeFtpServer.start() 93 } 94 95 private void connectAndLogin() { 96 ftpClient.connect(SERVER, PORT) 97 assert ftpClient.login(USERNAME, PASSWORD) 98 } 99 100}