1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.ftp;
12
13 import org.mule.tck.FunctionalTestCase;
14 import org.mule.transport.ftp.server.FTPTestClient;
15 import org.mule.transport.ftp.server.MuleFtplet;
16 import org.mule.transport.ftp.server.Server;
17 import org.mule.util.FileUtils;
18
19 import java.io.File;
20 import java.io.FileWriter;
21 import java.io.IOException;
22 import java.io.Writer;
23
24 import org.apache.ftpserver.ftplet.Ftplet;
25
26
27
28
29
30 public abstract class AbstractFtpServerTestCase extends FunctionalTestCase
31 implements MuleFtplet.Callback
32 {
33 public static final String TEST_MESSAGE = "Test FTP message";
34
35 private static final String DEFAULT_FTP_HOST = "localhost";
36 private static int DEFAULT_TIMEOUT = 10000;
37 public static final String FTP_SERVER_BASE_DIR = "target/ftpserver";
38
39
40
41
42
43
44 private String ftpHost;
45
46 private int ftpPort;
47 private String ftpUser = "anonymous";
48 private String ftpPassword = "password";
49 private int timeout;
50 private Server server = null;
51 private FTPTestClient ftpClient = null;
52
53
54
55
56 protected Ftplet ftplet = new MuleFtplet(this);
57
58 public AbstractFtpServerTestCase(String ftpHost, int port, int timeout)
59 {
60 this.ftpHost = ftpHost;
61 this.ftpPort = port;
62 this.timeout = timeout;
63 ftpClient = new FTPTestClient(this.ftpHost, this.ftpPort, this.ftpUser, this.ftpPassword);
64 }
65
66 public AbstractFtpServerTestCase(int port, int timeout)
67 {
68 this(DEFAULT_FTP_HOST, port, timeout);
69 }
70
71 public AbstractFtpServerTestCase(int port)
72 {
73 this(port, DEFAULT_TIMEOUT);
74 }
75
76 protected void startServer() throws Exception
77 {
78 server = new Server(ftpPort, ftplet);
79
80
81 synchronized(this)
82 {
83 wait(500);
84 }
85 }
86
87 protected void stopServer() throws Exception
88 {
89
90 if (null != server)
91 {
92 server.stop();
93 }
94 }
95
96 @Override
97 protected void doSetUp() throws Exception
98 {
99 super.doSetUp();
100
101
102 createFtpServerBaseDir();
103
104 startServer();
105 if (!ftpClient.testConnection())
106 {
107 throw new IOException("could not connect to ftp server");
108 }
109 }
110
111 @Override
112 protected void doTearDown() throws Exception
113 {
114 ftpClient.disconnect();
115 stopServer();
116
117 deleteFtpServerBaseDir();
118
119 super.doTearDown();
120 }
121
122 private void createFtpServerBaseDir()
123 {
124 deleteFtpServerBaseDir();
125 File ftpBaseDir = new File(FTP_SERVER_BASE_DIR);
126 ftpBaseDir.mkdirs();
127 }
128
129 private void deleteFtpServerBaseDir()
130 {
131 File ftpServerBase = new File(FTP_SERVER_BASE_DIR);
132 FileUtils.deleteTree(ftpServerBase);
133 }
134
135 protected int getTimeout()
136 {
137 return timeout;
138 }
139
140 public FTPTestClient getFtpClient()
141 {
142 return ftpClient;
143 }
144
145
146
147
148 public String getMuleFtpEndpoint()
149 {
150 return "ftp://" + ftpUser + ":" + ftpPassword + "@" + ftpHost + ":" + ftpPort;
151 }
152
153 protected void createFileOnFtpServer(String fileName) throws IOException
154 {
155 File outFile = new File(FTP_SERVER_BASE_DIR, fileName);
156 assertFalse(outFile.exists());
157
158 Writer outWriter = new FileWriter(outFile);
159 outWriter.write(TEST_MESSAGE);
160 outWriter.close();
161 }
162
163
164
165
166 public void fileUploadCompleted()
167 {
168
169 }
170
171 public void fileMoveCompleted()
172 {
173
174 }
175 }