1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.net.ftp;
18 import java.io.IOException;
19 import java.lang.reflect.Method;
20 import java.util.Arrays;
21 import java.util.Iterator;
22 import java.util.List;
23
24 import junit.framework.Test;
25 import junit.framework.TestCase;
26 import junit.framework.TestSuite;
27
28
29
30
31
32
33 public class ListingFunctionalTest extends TestCase
34 {
35 static final int HOSTNAME = 0;
36 static final int INVALID_PARSERKEY = 2;
37 static final int INVALID_PATH = 3;
38 static final int VALID_FILENAME = 4;
39 static final int VALID_PARSERKEY = 1;
40 static final int VALID_PATH = 5;
41
42 public static final Test suite()
43 {
44 String[][] testData =
45 {
46 {
47 "ftp.ibiblio.org", "unix", "vms",
48 "HA!", "javaio.jar",
49 "pub/languages/java/javafaq"
50 },
51 {
52 "ftp.wacom.com", "windows", "VMS", "HA!",
53 "wacom97.zip", "pub\\drivers"
54 },
55 {
56 "h71000.www7.hp.com", "vms", "windows",
57 "[.HA!]", "ACLOCAL.M4;1",
58
59 "[.FREEWARE50.XTERM]"
60 }
61 };
62 Class<?> clasz = ListingFunctionalTest.class;
63 Method[] methods = clasz.getDeclaredMethods();
64 TestSuite allSuites = new TestSuite("FTP Listing Functional Test Suite");
65
66 for (int i = 0; i < testData.length; i++)
67 {
68 TestSuite suite = new TestSuite(testData[i][VALID_PARSERKEY]);
69
70 for (int j = 0; j < methods.length; j++)
71 {
72 Method method = methods[j];
73
74 if (method.getName().startsWith("test"))
75 {
76 suite.addTest(new ListingFunctionalTest(
77 method.getName(),
78 testData[i]));
79 }
80 }
81
82 allSuites.addTest(suite);
83 }
84
85 return allSuites;
86 }
87
88 private FTPClient client;
89 private String hostName;
90 private String invalidParserKey;
91 private String invalidPath;
92 private String validFilename;
93 private String validParserKey;
94 private String validPath;
95
96
97
98
99
100
101 public ListingFunctionalTest(String arg0,
102 String[] settings)
103 {
104 super(arg0);
105 invalidParserKey = settings[INVALID_PARSERKEY];
106 validParserKey = settings[VALID_PARSERKEY];
107 invalidPath = settings[INVALID_PATH];
108 validFilename = settings[VALID_FILENAME];
109 validPath = settings[VALID_PATH];
110 hostName = settings[HOSTNAME];
111 }
112
113
114
115
116
117
118
119 private boolean findByName(List<?> fileList,
120 String string)
121 {
122 boolean found = false;
123 Iterator<?> iter = fileList.iterator();
124
125 while (iter.hasNext() && !found)
126 {
127 Object element = iter.next();
128
129 if (element instanceof FTPFile)
130 {
131 FTPFile file = (FTPFile) element;
132
133 found = file.getName().equals(string);
134 }
135 else
136 {
137 String filename = (String) element;
138
139 found = filename.endsWith(string);
140 }
141 }
142
143 return found;
144 }
145
146
147
148
149 @Override
150 protected void setUp() throws Exception
151 {
152 super.setUp();
153 client = new FTPClient();
154 client.connect(hostName);
155 client.login("anonymous", "anonymous");
156 client.enterLocalPassiveMode();
157 }
158
159
160
161
162 @Override
163 protected void tearDown()
164 throws Exception
165 {
166 try
167 {
168 client.logout();
169 }
170 catch (IOException e)
171 {
172 e.printStackTrace();
173 }
174
175 if (client.isConnected())
176 {
177 client.disconnect();
178 }
179
180 client = null;
181 super.tearDown();
182 }
183
184
185
186
187 public void testInitiateListParsing()
188 throws IOException
189 {
190 client.changeWorkingDirectory(validPath);
191
192 FTPListParseEngine engine = client.initiateListParsing();
193 List<FTPFile> files = Arrays.asList(engine.getNext(25));
194
195 assertTrue(files.toString(),
196 findByName(files, validFilename));
197 }
198
199
200
201
202 public void testInitiateListParsingWithPath()
203 throws IOException
204 {
205 FTPListParseEngine engine = client.initiateListParsing(validParserKey,
206 validPath);
207 List<FTPFile> files = Arrays.asList(engine.getNext(25));
208
209 assertTrue(files.toString(),
210 findByName(files, validFilename));
211 }
212
213
214
215
216 public void testInitiateListParsingWithPathAndAutodetection()
217 throws IOException
218 {
219 FTPListParseEngine engine = client.initiateListParsing(validPath);
220 List<FTPFile> files = Arrays.asList(engine.getNext(25));
221
222 assertTrue(files.toString(),
223 findByName(files, validFilename));
224 }
225
226
227
228
229 public void testInitiateListParsingWithPathAndAutodetectionButEmpty()
230 throws IOException
231 {
232 FTPListParseEngine engine = client.initiateListParsing(invalidPath);
233
234 assertFalse(engine.hasNext());
235 }
236
237
238
239
240 public void testInitiateListParsingWithPathAndIncorrectParser()
241 throws IOException
242 {
243 FTPListParseEngine engine = client.initiateListParsing(invalidParserKey,
244 invalidPath);
245
246 assertFalse(engine.hasNext());
247 }
248
249
250
251
252 public void testListFiles()
253 throws IOException
254 {
255 FTPClientConfig config = new FTPClientConfig(validParserKey);
256 client.configure(config);
257 List<FTPFile> files = Arrays.asList(client.listFiles(validPath));
258
259 assertTrue(files.toString(),
260 findByName(files, validFilename));
261 }
262
263 public void testListFilesWithAutodection()
264 throws IOException
265 {
266 client.changeWorkingDirectory(validPath);
267
268 List<FTPFile> files = Arrays.asList(client.listFiles());
269
270 assertTrue(files.toString(),
271 findByName(files, validFilename));
272 }
273
274
275
276
277 public void testListFilesWithIncorrectParser()
278 throws IOException
279 {
280 FTPClientConfig config = new FTPClientConfig(invalidParserKey);
281 client.configure(config);
282
283 FTPFile[] files = client.listFiles(validPath);
284
285 assertEquals(0, files.length);
286 }
287
288
289
290
291 public void testListFilesWithPathAndAutodectionButEmpty()
292 throws IOException
293 {
294 FTPFile[] files = client.listFiles(invalidPath);
295
296 assertEquals(0, files.length);
297 }
298
299
300
301
302 public void testListFilesWithPathAndAutodetection()
303 throws IOException
304 {
305 List<FTPFile> files = Arrays.asList(client.listFiles(validPath));
306
307 assertTrue(files.toString(),
308 findByName(files, validFilename));
309 }
310
311
312
313
314 public void testListNames()
315 throws IOException
316 {
317 client.changeWorkingDirectory(validPath);
318
319 String[] names = client.listNames();
320
321 assertNotNull(names);
322
323 List<String> lnames = Arrays.asList(names);
324
325 assertTrue(lnames.toString(),
326 lnames.contains(validFilename));
327 }
328
329
330
331
332 public void testListNamesWithPath()
333 throws IOException
334 {
335 List<String> names = Arrays.asList(client.listNames(validPath));
336
337 assertTrue(names.toString(),
338 findByName(names, validFilename));
339 }
340
341 public void testListNamesWithPathButEmpty()
342 throws IOException
343 {
344 String[] names = client.listNames(invalidPath);
345
346 assertNull(names);
347 }
348 }