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