KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > net > ftp > ListingFunctionalTest


1 /*
2  * Copyright 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.net.ftp;
17 import java.io.IOException JavaDoc;
18 import java.lang.reflect.Method JavaDoc;
19 import java.util.Arrays JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import junit.framework.Test;
24 import junit.framework.TestCase;
25 import junit.framework.TestSuite;
26
27 /**
28  * A functional test suite for checking that site listings work.
29  * @author <a HREF="mailto:brekke@apache.org">Jeffrey D. Brekke</a>
30  * @version $Id: ListingFunctionalTest.java 167953 2005-05-03 18:29:42Z dfs $
31  */

32 public class ListingFunctionalTest extends TestCase
33 {
34     static final int HOSTNAME = 0;
35     static final int INVALID_PARSERKEY = 2;
36     static final int INVALID_PATH = 3;
37     static final int VALID_FILENAME = 4;
38     static final int VALID_PARSERKEY = 1;
39     static final int VALID_PATH = 5;
40
41     public static final Test suite()
42     {
43         String JavaDoc[][] testData =
44             {
45                 {
46                     "ftp.ibiblio.org", "unix", "vms",
47                     "HA!", "javaio.jar",
48                     "pub/languages/java/javafaq"
49                 },
50                 {
51                     "ftp.wacom.com", "windows", "VMS", "HA!",
52                     "wacom97.zip", "pub\\ftp\\drivers"
53                 },
54                 {
55                     "h71000.www7.hp.com", "vms", "windows",
56                     "[.HA!]", "ACLOCAL.M4;1",
57
58                     "[.FREEWARE50.XTERM]"
59                 }
60             };
61         Class JavaDoc clasz = ListingFunctionalTest.class;
62         Method JavaDoc[] methods = clasz.getDeclaredMethods();
63         TestSuite allSuites = new TestSuite("FTP Listing Functional Test Suite");
64
65         for (int i = 0; i < testData.length; i++)
66         {
67             TestSuite suite = new TestSuite(testData[i][VALID_PARSERKEY]);
68
69             for (int j = 0; j < methods.length; j++)
70             {
71                 Method JavaDoc method = methods[j];
72
73                 if (method.getName().startsWith("test"))
74                 {
75                     suite.addTest(new ListingFunctionalTest(
76                                                             method.getName(),
77                                                             testData[i]));
78                 }
79             }
80
81             allSuites.addTest(suite);
82         }
83
84         return allSuites;
85     }
86
87     private FTPClient client;
88     private String JavaDoc hostName;
89     private String JavaDoc invalidParserKey;
90     private String JavaDoc invalidPath;
91     private String JavaDoc validFilename;
92     private String JavaDoc validParserKey;
93     private String JavaDoc validPath;
94
95     /**
96      * Constructor for FTPClientTest.
97      *
98      * @param arg0
99      */

100     public ListingFunctionalTest(String JavaDoc arg0,
101                                  String JavaDoc[] settings)
102     {
103         super(arg0);
104         invalidParserKey = settings[INVALID_PARSERKEY];
105         validParserKey = settings[VALID_PARSERKEY];
106         invalidPath = settings[INVALID_PATH];
107         validFilename = settings[VALID_FILENAME];
108         validPath = settings[VALID_PATH];
109         hostName = settings[HOSTNAME];
110     }
111
112     /**
113      * @param fileList
114      * @param string
115      *
116      * @return
117      */

118     private boolean findByName(List JavaDoc fileList,
119                                String JavaDoc string)
120     {
121         boolean found = false;
122         Iterator JavaDoc iter = fileList.iterator();
123
124         while (iter.hasNext() && !found)
125         {
126             Object JavaDoc element = iter.next();
127
128             if (element instanceof FTPFile)
129             {
130                 FTPFile file = (FTPFile) element;
131
132                 found = file.getName().equals(string);
133             }
134             else
135             {
136                 String JavaDoc filename = (String JavaDoc) element;
137
138                 found = filename.endsWith(string);
139             }
140         }
141
142         return found;
143     }
144
145     /*
146      * @see TestCase#setUp()
147      */

148     protected void setUp() throws Exception JavaDoc
149     {
150         super.setUp();
151         client = new FTPClient();
152         client.connect(hostName);
153         client.login("anonymous", "anonymous");
154         client.enterLocalPassiveMode();
155     }
156
157     /*
158      * @see TestCase#tearDown()
159      */

160     protected void tearDown()
161         throws Exception JavaDoc
162     {
163         try
164         {
165             client.logout();
166         }
167         catch (IOException JavaDoc e)
168         {
169             e.printStackTrace();
170         }
171
172         if (client.isConnected())
173         {
174             client.disconnect();
175         }
176
177         client = null;
178         super.tearDown();
179     }
180
181     /*
182      * Test for FTPListParseEngine initiateListParsing()
183      */

184     public void testInitiateListParsing()
185         throws IOException JavaDoc
186     {
187         client.changeWorkingDirectory(validPath);
188
189         FTPListParseEngine engine = client.initiateListParsing();
190         List JavaDoc files = Arrays.asList(engine.getNext(25));
191
192         assertTrue(files.toString(),
193                    findByName(files, validFilename));
194     }
195
196     /*
197      * Test for FTPListParseEngine initiateListParsing(String, String)
198      */

199     public void testInitiateListParsingWithPath()
200         throws IOException JavaDoc
201     {
202         FTPListParseEngine engine = client.initiateListParsing(validParserKey,
203                                                                validPath);
204         List JavaDoc files = Arrays.asList(engine.getNext(25));
205
206         assertTrue(files.toString(),
207                    findByName(files, validFilename));
208     }
209
210     /*
211      * Test for FTPListParseEngine initiateListParsing(String)
212      */

213     public void testInitiateListParsingWithPathAndAutodetection()
214         throws IOException JavaDoc
215     {
216         FTPListParseEngine engine = client.initiateListParsing(validPath);
217         List JavaDoc files = Arrays.asList(engine.getNext(25));
218
219         assertTrue(files.toString(),
220                    findByName(files, validFilename));
221     }
222
223     /*
224      * Test for FTPListParseEngine initiateListParsing(String)
225      */

226     public void testInitiateListParsingWithPathAndAutodetectionButEmpty()
227         throws IOException JavaDoc
228     {
229         FTPListParseEngine engine = client.initiateListParsing(invalidPath);
230
231         assertFalse(engine.hasNext());
232     }
233
234     /*
235      * Test for FTPListParseEngine initiateListParsing(String, String)
236      */

237     public void testInitiateListParsingWithPathAndIncorrectParser()
238         throws IOException JavaDoc
239     {
240         FTPListParseEngine engine = client.initiateListParsing(invalidParserKey,
241                                                                invalidPath);
242
243         assertFalse(engine.hasNext());
244     }
245
246     /*
247      * Test for FTPFile[] listFiles(String, String)
248      */

249     public void testListFiles()
250         throws IOException JavaDoc
251     {
252         FTPClientConfig config = new FTPClientConfig(validParserKey);
253         client.configure(config);
254         List JavaDoc files = Arrays.asList(client.listFiles(validPath));
255
256         assertTrue(files.toString(),
257                    findByName(files, validFilename));
258     }
259
260     public void testListFilesWithAutodection()
261         throws IOException JavaDoc
262     {
263         client.changeWorkingDirectory(validPath);
264
265         List JavaDoc files = Arrays.asList(client.listFiles());
266
267         assertTrue(files.toString(),
268                    findByName(files, validFilename));
269     }
270
271     /*
272      * Test for FTPFile[] listFiles(String, String)
273      */

274     public void testListFilesWithIncorrectParser()
275         throws IOException JavaDoc
276     {
277         FTPClientConfig config = new FTPClientConfig(invalidParserKey);
278         client.configure(config);
279
280         FTPFile[] files = client.listFiles(validPath);
281
282         assertEquals(0, files.length);
283     }
284
285     /*
286      * Test for FTPFile[] listFiles(String)
287      */

288     public void testListFilesWithPathAndAutodectionButEmpty()
289         throws IOException JavaDoc
290     {
291         FTPFile[] files = client.listFiles(invalidPath);
292
293         assertEquals(0, files.length);
294     }
295
296     /*
297      * Test for FTPFile[] listFiles(String)
298      */

299     public void testListFilesWithPathAndAutodetection()
300         throws IOException JavaDoc
301     {
302         List JavaDoc files = Arrays.asList(client.listFiles(validPath));
303
304         assertTrue(files.toString(),
305                    findByName(files, validFilename));
306     }
307
308     /*
309      * Test for String[] listNames()
310      */

311     public void testListNames()
312         throws IOException JavaDoc
313     {
314         client.changeWorkingDirectory(validPath);
315
316         String JavaDoc[] names = client.listNames();
317
318         assertNotNull(names);
319
320         List JavaDoc lnames = Arrays.asList(names);
321
322         assertTrue(lnames.toString(),
323                    lnames.contains(validFilename));
324     }
325
326     /*
327      * Test for String[] listNames(String)
328      */

329     public void testListNamesWithPath()
330         throws IOException JavaDoc
331     {
332         List JavaDoc names = Arrays.asList(client.listNames(validPath));
333
334         assertTrue(names.toString(),
335                    findByName(names, validFilename));
336     }
337
338     public void testListNamesWithPathButEmpty()
339         throws IOException JavaDoc
340     {
341         String JavaDoc[] names = client.listNames(invalidPath);
342
343         assertNull(names);
344     }
345 }
346
Popular Tags