|
Commons Net example source code file (ListingFunctionalTest.java)
The Commons Net ListingFunctionalTest.java source code/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.commons.net.ftp; import java.io.IOException; import java.lang.reflect.Method; import java.util.Arrays; import java.util.Iterator; import java.util.List; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; /** * A functional test suite for checking that site listings work. * @author <a href="mailto:brekke@apache.org">Jeffrey D. Brekke * @version $Id: ListingFunctionalTest.java 1081478 2011-03-14 17:12:48Z sebb $ */ public class ListingFunctionalTest extends TestCase { // Offsets within testData below static final int HOSTNAME = 0; static final int VALID_PARSERKEY = 1; static final int INVALID_PARSERKEY = 2; static final int INVALID_PATH = 3; static final int VALID_FILENAME = 4; static final int VALID_PATH = 5; public static final Test suite() { String[][] testData = { { "ftp.ibiblio.org", "unix", "vms", "HA!", "javaio.jar", "pub/languages/java/javafaq" }, { "ftp.wacom.com", "windows", "VMS", "HA!", "wacom97.zip", "pub\\drivers" }, { "ftp.decuslib.com", "vms", "windows", // VMS OpenVMS V8.3 "[.HA!]", "FREEWARE_SUBMISSION_INSTRUCTIONS.TXT;1", "[.FREEWAREV80.FREEWARE]" }, // { // VMS TCPware V5.7-2 does not return (RWED) permissions // "ftp.process.com", "vms", "windows", // "[.HA!]", "MESSAGE.;1", // "[.VMS-FREEWARE.FREE-VMS]" // // }, }; Class<?> clasz = ListingFunctionalTest.class; Method[] methods = clasz.getDeclaredMethods(); TestSuite allSuites = new TestSuite("FTP Listing Functional Test Suite"); for (int i = 0; i < testData.length; i++) { TestSuite suite = new TestSuite(testData[i][VALID_PARSERKEY]+ " @ " +testData[i][HOSTNAME]); for (int j = 0; j < methods.length; j++) { Method method = methods[j]; if (method.getName().startsWith("test")) { suite.addTest(new ListingFunctionalTest( method.getName(), testData[i])); } } allSuites.addTest(suite); } return allSuites; } private FTPClient client; private String hostName; private String invalidParserKey; private String invalidPath; private String validFilename; private String validParserKey; private String validPath; /** * Constructor for FTPClientTest. * * @param arg0 */ public ListingFunctionalTest(String arg0, String[] settings) { super(arg0); invalidParserKey = settings[INVALID_PARSERKEY]; validParserKey = settings[VALID_PARSERKEY]; invalidPath = settings[INVALID_PATH]; validFilename = settings[VALID_FILENAME]; validPath = settings[VALID_PATH]; hostName = settings[HOSTNAME]; } /** * @param fileList * @param string * * @return */ private boolean findByName(List<?> fileList, String string) { boolean found = false; Iterator<?> iter = fileList.iterator(); while (iter.hasNext() && !found) { Object element = iter.next(); if (element instanceof FTPFile) { FTPFile file = (FTPFile) element; found = file.getName().equals(string); } else { String filename = (String) element; found = filename.endsWith(string); } } return found; } /* * @see TestCase#setUp() */ @Override protected void setUp() throws Exception { super.setUp(); client = new FTPClient(); client.connect(hostName); client.login("anonymous", "anonymous"); client.enterLocalPassiveMode(); } /* * @see TestCase#tearDown() */ @Override protected void tearDown() throws Exception { try { client.logout(); } catch (IOException e) { e.printStackTrace(); } if (client.isConnected()) { client.disconnect(); } client = null; super.tearDown(); } /* * Test for FTPListParseEngine initiateListParsing() */ public void testInitiateListParsing() throws IOException { client.changeWorkingDirectory(validPath); FTPListParseEngine engine = client.initiateListParsing(); List<FTPFile> files = Arrays.asList(engine.getNext(25)); assertTrue(files.toString(), findByName(files, validFilename)); } /* * Test for FTPListParseEngine initiateListParsing(String, String) */ public void testInitiateListParsingWithPath() throws IOException { FTPListParseEngine engine = client.initiateListParsing(validParserKey, validPath); List<FTPFile> files = Arrays.asList(engine.getNext(25)); assertTrue(files.toString(), findByName(files, validFilename)); } /* * Test for FTPListParseEngine initiateListParsing(String) */ public void testInitiateListParsingWithPathAndAutodetection() throws IOException { FTPListParseEngine engine = client.initiateListParsing(validPath); List<FTPFile> files = Arrays.asList(engine.getNext(25)); assertTrue(files.toString(), findByName(files, validFilename)); } /* * Test for FTPListParseEngine initiateListParsing(String) */ public void testInitiateListParsingWithPathAndAutodetectionButEmpty() throws IOException { FTPListParseEngine engine = client.initiateListParsing(invalidPath); assertFalse(engine.hasNext()); } /* * Test for FTPListParseEngine initiateListParsing(String, String) */ public void testInitiateListParsingWithPathAndIncorrectParser() throws IOException { FTPListParseEngine engine = client.initiateListParsing(invalidParserKey, invalidPath); assertFalse(engine.hasNext()); } /* * Test for FTPFile[] listFiles(String, String) */ public void testListFiles() throws IOException { FTPClientConfig config = new FTPClientConfig(validParserKey); client.configure(config); List<FTPFile> files = Arrays.asList(client.listFiles(validPath)); assertTrue(files.toString(), findByName(files, validFilename)); } public void testListFilesWithAutodection() throws IOException { client.changeWorkingDirectory(validPath); List<FTPFile> files = Arrays.asList(client.listFiles()); assertTrue(files.toString(), findByName(files, validFilename)); } /* * Test for FTPFile[] listFiles(String, String) */ public void testListFilesWithIncorrectParser() throws IOException { FTPClientConfig config = new FTPClientConfig(invalidParserKey); client.configure(config); FTPFile[] files = client.listFiles(validPath); assertEquals(0, files.length); } /* * Test for FTPFile[] listFiles(String) */ public void testListFilesWithPathAndAutodectionButEmpty() throws IOException { FTPFile[] files = client.listFiles(invalidPath); assertEquals(0, files.length); } /* * Test for FTPFile[] listFiles(String) */ public void testListFilesWithPathAndAutodetection() throws IOException { List<FTPFile> files = Arrays.asList(client.listFiles(validPath)); assertTrue(files.toString(), findByName(files, validFilename)); } /* * Test for String[] listNames() */ public void testListNames() throws IOException { client.changeWorkingDirectory(validPath); String[] names = client.listNames(); assertNotNull(names); List<String> lnames = Arrays.asList(names); assertTrue(lnames.toString(), lnames.contains(validFilename)); } /* * Test for String[] listNames(String) */ public void testListNamesWithPath() throws IOException { String[] listNames = client.listNames(validPath); assertNotNull("listNames not null", listNames); List<String> names = Arrays.asList(listNames); assertTrue(names.toString(), findByName(names, validFilename)); } public void testListNamesWithPathButEmpty() throws IOException { String[] names = client.listNames(invalidPath); assertNull(names); } } Other Commons Net examples (source code examples)Here is a short list of links related to this Commons Net ListingFunctionalTest.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.