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