001: /*
002: * Copyright 2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.net.ftp;
017:
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 167953 2005-05-03 18:29:42Z dfs $
032: */
033: public class ListingFunctionalTest extends TestCase {
034: static final int HOSTNAME = 0;
035: static final int INVALID_PARSERKEY = 2;
036: static final int INVALID_PATH = 3;
037: static final int VALID_FILENAME = 4;
038: static final int VALID_PARSERKEY = 1;
039: static final int VALID_PATH = 5;
040:
041: public static final Test suite() {
042: String[][] testData = {
043: { "ftp.ibiblio.org", "unix", "vms", "HA!",
044: "javaio.jar", "pub/languages/java/javafaq" },
045: { "ftp.wacom.com", "windows", "VMS", "HA!",
046: "wacom97.zip", "pub\\ftp\\drivers" },
047: { "h71000.www7.hp.com", "vms", "windows", "[.HA!]",
048: "ACLOCAL.M4;1",
049:
050: "[.FREEWARE50.XTERM]" } };
051: Class clasz = ListingFunctionalTest.class;
052: Method[] methods = clasz.getDeclaredMethods();
053: TestSuite allSuites = new TestSuite(
054: "FTP Listing Functional Test Suite");
055:
056: for (int i = 0; i < testData.length; i++) {
057: TestSuite suite = new TestSuite(
058: testData[i][VALID_PARSERKEY]);
059:
060: for (int j = 0; j < methods.length; j++) {
061: Method method = methods[j];
062:
063: if (method.getName().startsWith("test")) {
064: suite.addTest(new ListingFunctionalTest(method
065: .getName(), testData[i]));
066: }
067: }
068:
069: allSuites.addTest(suite);
070: }
071:
072: return allSuites;
073: }
074:
075: private FTPClient client;
076: private String hostName;
077: private String invalidParserKey;
078: private String invalidPath;
079: private String validFilename;
080: private String validParserKey;
081: private String validPath;
082:
083: /**
084: * Constructor for FTPClientTest.
085: *
086: * @param arg0
087: */
088: public ListingFunctionalTest(String arg0, String[] settings) {
089: super (arg0);
090: invalidParserKey = settings[INVALID_PARSERKEY];
091: validParserKey = settings[VALID_PARSERKEY];
092: invalidPath = settings[INVALID_PATH];
093: validFilename = settings[VALID_FILENAME];
094: validPath = settings[VALID_PATH];
095: hostName = settings[HOSTNAME];
096: }
097:
098: /**
099: * @param fileList
100: * @param string
101: *
102: * @return
103: */
104: private boolean findByName(List fileList, String string) {
105: boolean found = false;
106: Iterator iter = fileList.iterator();
107:
108: while (iter.hasNext() && !found) {
109: Object element = iter.next();
110:
111: if (element instanceof FTPFile) {
112: FTPFile file = (FTPFile) element;
113:
114: found = file.getName().equals(string);
115: } else {
116: String filename = (String) element;
117:
118: found = filename.endsWith(string);
119: }
120: }
121:
122: return found;
123: }
124:
125: /*
126: * @see TestCase#setUp()
127: */
128: protected void setUp() throws Exception {
129: super .setUp();
130: client = new FTPClient();
131: client.connect(hostName);
132: client.login("anonymous", "anonymous");
133: client.enterLocalPassiveMode();
134: }
135:
136: /*
137: * @see TestCase#tearDown()
138: */
139: protected void tearDown() throws Exception {
140: try {
141: client.logout();
142: } catch (IOException e) {
143: e.printStackTrace();
144: }
145:
146: if (client.isConnected()) {
147: client.disconnect();
148: }
149:
150: client = null;
151: super .tearDown();
152: }
153:
154: /*
155: * Test for FTPListParseEngine initiateListParsing()
156: */
157: public void testInitiateListParsing() throws IOException {
158: client.changeWorkingDirectory(validPath);
159:
160: FTPListParseEngine engine = client.initiateListParsing();
161: List files = Arrays.asList(engine.getNext(25));
162:
163: assertTrue(files.toString(), findByName(files, validFilename));
164: }
165:
166: /*
167: * Test for FTPListParseEngine initiateListParsing(String, String)
168: */
169: public void testInitiateListParsingWithPath() throws IOException {
170: FTPListParseEngine engine = client.initiateListParsing(
171: validParserKey, validPath);
172: List files = Arrays.asList(engine.getNext(25));
173:
174: assertTrue(files.toString(), findByName(files, validFilename));
175: }
176:
177: /*
178: * Test for FTPListParseEngine initiateListParsing(String)
179: */
180: public void testInitiateListParsingWithPathAndAutodetection()
181: throws IOException {
182: FTPListParseEngine engine = client
183: .initiateListParsing(validPath);
184: List files = Arrays.asList(engine.getNext(25));
185:
186: assertTrue(files.toString(), findByName(files, validFilename));
187: }
188:
189: /*
190: * Test for FTPListParseEngine initiateListParsing(String)
191: */
192: public void testInitiateListParsingWithPathAndAutodetectionButEmpty()
193: throws IOException {
194: FTPListParseEngine engine = client
195: .initiateListParsing(invalidPath);
196:
197: assertFalse(engine.hasNext());
198: }
199:
200: /*
201: * Test for FTPListParseEngine initiateListParsing(String, String)
202: */
203: public void testInitiateListParsingWithPathAndIncorrectParser()
204: throws IOException {
205: FTPListParseEngine engine = client.initiateListParsing(
206: invalidParserKey, invalidPath);
207:
208: assertFalse(engine.hasNext());
209: }
210:
211: /*
212: * Test for FTPFile[] listFiles(String, String)
213: */
214: public void testListFiles() throws IOException {
215: FTPClientConfig config = new FTPClientConfig(validParserKey);
216: client.configure(config);
217: List files = Arrays.asList(client.listFiles(validPath));
218:
219: assertTrue(files.toString(), findByName(files, validFilename));
220: }
221:
222: public void testListFilesWithAutodection() throws IOException {
223: client.changeWorkingDirectory(validPath);
224:
225: List files = Arrays.asList(client.listFiles());
226:
227: assertTrue(files.toString(), findByName(files, validFilename));
228: }
229:
230: /*
231: * Test for FTPFile[] listFiles(String, String)
232: */
233: public void testListFilesWithIncorrectParser() throws IOException {
234: FTPClientConfig config = new FTPClientConfig(invalidParserKey);
235: client.configure(config);
236:
237: FTPFile[] files = client.listFiles(validPath);
238:
239: assertEquals(0, files.length);
240: }
241:
242: /*
243: * Test for FTPFile[] listFiles(String)
244: */
245: public void testListFilesWithPathAndAutodectionButEmpty()
246: throws IOException {
247: FTPFile[] files = client.listFiles(invalidPath);
248:
249: assertEquals(0, files.length);
250: }
251:
252: /*
253: * Test for FTPFile[] listFiles(String)
254: */
255: public void testListFilesWithPathAndAutodetection()
256: throws IOException {
257: List files = Arrays.asList(client.listFiles(validPath));
258:
259: assertTrue(files.toString(), findByName(files, validFilename));
260: }
261:
262: /*
263: * Test for String[] listNames()
264: */
265: public void testListNames() throws IOException {
266: client.changeWorkingDirectory(validPath);
267:
268: String[] names = client.listNames();
269:
270: assertNotNull(names);
271:
272: List lnames = Arrays.asList(names);
273:
274: assertTrue(lnames.toString(), lnames.contains(validFilename));
275: }
276:
277: /*
278: * Test for String[] listNames(String)
279: */
280: public void testListNamesWithPath() throws IOException {
281: List names = Arrays.asList(client.listNames(validPath));
282:
283: assertTrue(names.toString(), findByName(names, validFilename));
284: }
285:
286: public void testListNamesWithPathButEmpty() throws IOException {
287: String[] names = client.listNames(invalidPath);
288:
289: assertNull(names);
290: }
291: }
|