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    private static final int PORT = 9981;
37
38    private StubFtpServer stubFtpServer;
39    private FTPClient ftpClient;
40
41    /**
42     * Test starting the StubFtpServer configured within the example Spring configuration file
43     */
44    public void testStubFtpServer() throws Exception {
45        stubFtpServer.start();
46
47        ftpClient.connect(SERVER, PORT);
48
49        // PWD
50        String dir = ftpClient.printWorkingDirectory();
51        assertEquals("PWD", "foo/bar", dir);
52
53        // LIST
54        FTPFile[] files = ftpClient.listFiles();
55        LOG.info("FTPFile[0]=" + files[0]);
56        LOG.info("FTPFile[1]=" + files[1]);
57        assertEquals("number of files from LIST", 2, files.length);
58
59        // DELE
60        assertFalse("DELE", ftpClient.deleteFile("AnyFile.txt"));
61
62        // RETR
63        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
64        assertTrue(ftpClient.retrieveFile("SomeFile.txt", outputStream));
65        LOG.info("File contents=[" + outputStream.toString() + "]");
66    }
67
68    /**
69     * @see org.mockftpserver.test.AbstractTest#setUp()
70     */
71    protected void setUp() throws Exception {
72        super.setUp();
73
74        ApplicationContext context = new ClassPathXmlApplicationContext("stubftpserver-beans.xml");
75        stubFtpServer = (StubFtpServer) context.getBean("stubFtpServer");
76
77        ftpClient = new FTPClient();
78    }
79
80    /**
81     * @see org.mockftpserver.test.AbstractTest#tearDown()
82     */
83    protected void tearDown() throws Exception {
84        super.tearDown();
85        stubFtpServer.stop();
86    }
87
88}
89