001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.net.ftp;
018    import java.io.IOException;
019    import java.lang.reflect.Method;
020    import java.util.Arrays;
021    import java.util.Iterator;
022    import java.util.List;
023    
024    import junit.framework.Test;
025    import junit.framework.TestCase;
026    import junit.framework.TestSuite;
027    
028    /**
029     * A functional test suite for checking that site listings work.
030     * @author <a href="mailto:brekke@apache.org">Jeffrey D. Brekke</a>
031     * @version $Id: ListingFunctionalTest.java 636854 2008-03-13 19:55:01Z sebb $
032     */
033    public class ListingFunctionalTest extends TestCase
034    {
035        static final int HOSTNAME = 0;
036        static final int INVALID_PARSERKEY = 2;
037        static final int INVALID_PATH = 3;
038        static final int VALID_FILENAME = 4;
039        static final int VALID_PARSERKEY = 1;
040        static final int VALID_PATH = 5;
041    
042        public static final Test suite()
043        {
044            String[][] testData =
045                {
046                    {
047                        "ftp.ibiblio.org", "unix", "vms",
048                        "HA!", "javaio.jar",
049                        "pub/languages/java/javafaq"
050                    },
051                    {
052                        "ftp.wacom.com", "windows", "VMS", "HA!",
053                        "wacom97.zip", "pub\\drivers"
054                    },
055                    {
056                        "h71000.www7.hp.com", "vms", "windows",
057                        "[.HA!]", "ACLOCAL.M4;1",
058    
059                        "[.FREEWARE50.XTERM]"
060                    }
061                };
062            Class<?> clasz = ListingFunctionalTest.class;
063            Method[] methods = clasz.getDeclaredMethods();
064            TestSuite allSuites = new TestSuite("FTP Listing Functional Test Suite");
065    
066            for (int i = 0; i < testData.length; i++)
067            {
068                TestSuite suite = new TestSuite(testData[i][VALID_PARSERKEY]);
069    
070                for (int j = 0; j < methods.length; j++)
071                {
072                    Method method = methods[j];
073    
074                    if (method.getName().startsWith("test"))
075                    {
076                        suite.addTest(new ListingFunctionalTest(
077                                                                method.getName(),
078                                                                testData[i]));
079                    }
080                }
081    
082                allSuites.addTest(suite);
083            }
084    
085            return allSuites;
086        }
087    
088        private FTPClient client;
089        private String hostName;
090        private String invalidParserKey;
091        private String invalidPath;
092        private String validFilename;
093        private String validParserKey;
094        private String validPath;
095    
096        /**
097         * Constructor for FTPClientTest.
098         *
099         * @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    }