View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * A functional test suite for checking that site listings work.
30   * @author <a href="mailto:brekke@apache.org">Jeffrey D. Brekke</a>
31   * @version $Id: ListingFunctionalTest.java 636854 2008-03-13 19:55:01Z sebb $
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       * Constructor for FTPClientTest.
98       *
99       * @param arg0
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      * @param fileList
115      * @param string
116      *
117      * @return
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      * @see TestCase#setUp()
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      * @see TestCase#tearDown()
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      * Test for FTPListParseEngine initiateListParsing()
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      * Test for FTPListParseEngine initiateListParsing(String, String)
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      * Test for FTPListParseEngine initiateListParsing(String)
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      * Test for FTPListParseEngine initiateListParsing(String)
228      */
229     public void testInitiateListParsingWithPathAndAutodetectionButEmpty()
230         throws IOException
231     {
232         FTPListParseEngine engine = client.initiateListParsing(invalidPath);
233 
234         assertFalse(engine.hasNext());
235     }
236 
237     /*
238      * Test for FTPListParseEngine initiateListParsing(String, String)
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      * Test for FTPFile[] listFiles(String, String)
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      * Test for FTPFile[] listFiles(String, String)
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      * Test for FTPFile[] listFiles(String)
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      * Test for FTPFile[] listFiles(String)
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      * Test for String[] listNames()
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      * Test for String[] listNames(String)
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 }