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