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