FakeFtpServerSpringConfigurationTest.groovy revision 101796da6d6bdb5aa447885b507bbe844d89f805
1package org.mockftpserver.fake.example
2
3import org.apache.commons.net.ftp.FTPClient
4import org.apache.commons.net.ftp.FTPFile
5import org.mockftpserver.fake.server.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() {
36        fakeFtpServer.start()
37
38        ftpClient.connect(SERVER, PORT)
39
40        // USER and PASS
41        assert ftpClient.login(USERNAME, PASSWORD)
42
43        // PWD
44        String dir = ftpClient.printWorkingDirectory()
45        assert dir == 'c:\\'
46
47        // LIST
48        FTPFile[] files = ftpClient.listFiles()
49        LOG.info("FTPFile[0]=" + files[0])
50        assert files.length == 1
51
52        // RETR
53        ByteArrayOutputStream outputStream = new ByteArrayOutputStream()
54        assert ftpClient.retrieveFile("File.txt", outputStream)
55        LOG.info("File contents=[" + outputStream.toString() + "]")
56    }
57
58    void setUp() throws Exception {
59        super.setUp()
60
61        ApplicationContext context = new ClassPathXmlApplicationContext("fakeftpserver-beans.xml")
62        fakeFtpServer = (FakeFtpServer) context.getBean("fakeFtpServer")
63
64        ftpClient = new FTPClient()
65    }
66
67    void tearDown() throws Exception {
68        super.tearDown()
69        fakeFtpServer.stop()
70    }
71
72}