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  
19  import static org.junit.Assert.assertArrayEquals;
20  
21  import java.io.IOException;
22  import java.lang.reflect.Method;
23  import java.util.Arrays;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  import junit.framework.Test;
28  import junit.framework.TestCase;
29  import junit.framework.TestSuite;
30  
31  /**
32   * A functional test suite for checking that site listings work.
33   */
34  public class ListingFunctionalTest extends TestCase {
35      // Offsets within testData below
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; // response to PWD
43  
44      public static final Test suite() {
45          final String[][] testData = { { "ftp.ibiblio.org", "unix", "vms", "HA!", "javaio.jar", "pub/languages/java/javafaq", "/pub/languages/java/javafaq", },
46                  { "apache.cs.utah.edu", "unix", "vms", "HA!", "HEADER.html", "apache.org", "/apache.org", },
47  //                { // not available
48  //                    "ftp.wacom.com", "windows", "VMS", "HA!",
49  //                    "wacom97.zip", "pub\\drivers"
50  //                },
51                  { "ftp.decuslib.com", "vms", "windows", // VMS OpenVMS V8.3
52                          "[.HA!]", "FREEWARE_SUBMISSION_INSTRUCTIONS.TXT;1", "[.FREEWAREV80.FREEWARE]", "DECUSLIB:[DECUS.FREEWAREV80.FREEWARE]" },
53  //                {  // VMS TCPware V5.7-2 does not return (RWED) permissions
54  //                    "ftp.process.com", "vms", "windows",
55  //                    "[.HA!]", "MESSAGE.;1",
56  //                    "[.VMS-FREEWARE.FREE-VMS]" //
57  //                },
58          };
59          final Class<?> clasz = ListingFunctionalTest.class;
60          final Method[] methods = clasz.getDeclaredMethods();
61          final TestSuite allSuites = new TestSuite("FTP Listing Functional Test Suite");
62  
63          for (final String[] element : testData) {
64              final TestSuite suite = new TestSuite(element[VALID_PARSERKEY] + " @ " + element[HOSTNAME]);
65  
66              for (final Method method : methods) {
67                  if (method.getName().startsWith("test")) {
68                      suite.addTest(new ListingFunctionalTest(method.getName(), element));
69                  }
70              }
71  
72              allSuites.addTest(suite);
73          }
74  
75          return allSuites;
76      }
77  
78      private FTPClient client;
79      private final String hostName;
80      private final String invalidParserKey;
81      private final String invalidPath;
82      private final String validFilename;
83      private final String validParserKey;
84      private final String validPath;
85      private final String pwdPath;
86  
87      public ListingFunctionalTest(final String arg0, final String[] settings) {
88          super(arg0);
89          invalidParserKey = settings[INVALID_PARSERKEY];
90          validParserKey = settings[VALID_PARSERKEY];
91          invalidPath = settings[INVALID_PATH];
92          validFilename = settings[VALID_FILENAME];
93          validPath = settings[VALID_PATH];
94          pwdPath = settings[PATH_PWD];
95          hostName = settings[HOSTNAME];
96      }
97  
98      private boolean findByName(final List<?> fileList, final String string) {
99          boolean found = false;
100         final Iterator<?> iter = fileList.iterator();
101 
102         while (iter.hasNext() && !found) {
103             final Object element = iter.next();
104 
105             if (element instanceof FTPFile) {
106                 final FTPFile file = (FTPFile) element;
107 
108                 found = file.getName().equals(string);
109             } else {
110                 final String fileName = (String) element;
111 
112                 found = fileName.endsWith(string);
113             }
114         }
115 
116         return found;
117     }
118 
119     /*
120      * @see TestCase#setUp()
121      */
122     @Override
123     protected void setUp() throws Exception {
124         super.setUp();
125         client = new FTPClient();
126         client.connect(hostName);
127         client.login("anonymous", "anonymous");
128         client.enterLocalPassiveMode();
129 //        client.addProtocolCommandListener(new PrintCommandListener(System.out));
130     }
131 
132     /*
133      * @see TestCase#tearDown()
134      */
135     @Override
136     protected void tearDown() throws Exception {
137         try {
138             client.logout();
139         } catch (final IOException e) {
140             e.printStackTrace();
141         }
142 
143         if (client.isConnected()) {
144             client.disconnect();
145         }
146 
147         client = null;
148         super.tearDown();
149     }
150 
151     /*
152      * Test for FTPListParseEngine initiateListParsing()
153      */
154     public void testInitiateListParsing() throws IOException {
155         client.changeWorkingDirectory(validPath);
156 
157         final FTPListParseEngine engine = client.initiateListParsing();
158         final List<FTPFile> files = Arrays.asList(engine.getNext(25));
159 
160         assertTrue(files.toString(), findByName(files, validFilename));
161     }
162 
163     /*
164      * Test for FTPListParseEngine initiateListParsing(String, String)
165      */
166     public void testInitiateListParsingWithPath() throws IOException {
167         final FTPListParseEngine engine = client.initiateListParsing(validParserKey, validPath);
168         final List<FTPFile> files = Arrays.asList(engine.getNext(25));
169 
170         assertTrue(files.toString(), findByName(files, validFilename));
171     }
172 
173     /*
174      * Test for FTPListParseEngine initiateListParsing(String)
175      */
176     public void testInitiateListParsingWithPathAndAutodetection() throws IOException {
177         final FTPListParseEngine engine = client.initiateListParsing(validPath);
178         final List<FTPFile> files = Arrays.asList(engine.getNext(25));
179 
180         assertTrue(files.toString(), findByName(files, validFilename));
181     }
182 
183     /*
184      * Test for FTPListParseEngine initiateListParsing(String)
185      */
186     public void testInitiateListParsingWithPathAndAutodetectionButEmpty() throws IOException {
187         final FTPListParseEngine engine = client.initiateListParsing(invalidPath);
188 
189         assertFalse(engine.hasNext());
190     }
191 
192     /*
193      * Test for FTPListParseEngine initiateListParsing(String, String)
194      */
195     public void testInitiateListParsingWithPathAndIncorrectParser() throws IOException {
196         final FTPListParseEngine engine = client.initiateListParsing(invalidParserKey, invalidPath);
197 
198         assertFalse(engine.hasNext());
199     }
200 
201     /*
202      * Test for FTPFile[] listFiles(String, String)
203      */
204     public void testListFiles() throws IOException {
205         final FTPClientConfig config = new FTPClientConfig(validParserKey);
206         client.configure(config);
207         final List<FTPFile> files = Arrays.asList(client.listFiles(validPath));
208 
209         assertTrue(files.toString(), findByName(files, validFilename));
210     }
211 
212     public void testListFilesWithAutodection() throws IOException {
213         client.changeWorkingDirectory(validPath);
214 
215         final List<FTPFile> files = Arrays.asList(client.listFiles());
216 
217         assertTrue(files.toString(), findByName(files, validFilename));
218     }
219 
220     /*
221      * Test for FTPFile[] listFiles(String, String)
222      */
223     public void testListFilesWithIncorrectParser() throws IOException {
224         final FTPClientConfig config = new FTPClientConfig(invalidParserKey);
225         client.configure(config);
226 
227         final FTPFile[] files = client.listFiles(validPath);
228 
229         assertNotNull(files);
230 
231         // This may well fail, e.g. window parser for VMS listing
232         assertArrayEquals("Expected empty array: " + Arrays.toString(files), new FTPFile[] {}, files);
233     }
234 
235     /*
236      * Test for FTPFile[] listFiles(String)
237      */
238     public void testListFilesWithPathAndAutodectionButEmpty() throws IOException {
239         final FTPFile[] files = client.listFiles(invalidPath);
240 
241         assertEquals(0, files.length);
242     }
243 
244     /*
245      * Test for FTPFile[] listFiles(String)
246      */
247     public void testListFilesWithPathAndAutodetection() throws IOException {
248         final List<FTPFile> files = Arrays.asList(client.listFiles(validPath));
249 
250         assertTrue(files.toString(), findByName(files, validFilename));
251     }
252 
253     /*
254      * Test for String[] listNames()
255      */
256     public void testListNames() throws IOException {
257         client.changeWorkingDirectory(validPath);
258 
259         final String[] names = client.listNames();
260 
261         assertNotNull(names);
262 
263         final List<String> lnames = Arrays.asList(names);
264 
265         assertTrue(lnames.toString(), lnames.contains(validFilename));
266     }
267 
268     /*
269      * Test for String[] listNames(String)
270      */
271     public void testListNamesWithPath() throws IOException {
272         final String[] listNames = client.listNames(validPath);
273         assertNotNull("listNames not null", listNames);
274         final List<String> names = Arrays.asList(listNames);
275 
276         assertTrue(names.toString(), findByName(names, validFilename));
277     }
278 
279     public void testListNamesWithPathButEmpty() throws IOException {
280         final String[] names = client.listNames(invalidPath);
281 
282         assertNull(names);
283     }
284 
285     public void testPrintWorkingDirectory() throws IOException {
286         client.changeWorkingDirectory(validPath);
287         final String pwd = client.printWorkingDirectory();
288         assertEquals(pwdPath, pwd);
289     }
290 }