1 16 package org.apache.commons.net.ftp; 17 import java.io.IOException ; 18 import java.lang.reflect.Method ; 19 import java.util.Arrays ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import junit.framework.Test; 24 import junit.framework.TestCase; 25 import junit.framework.TestSuite; 26 27 32 public class ListingFunctionalTest extends TestCase 33 { 34 static final int HOSTNAME = 0; 35 static final int INVALID_PARSERKEY = 2; 36 static final int INVALID_PATH = 3; 37 static final int VALID_FILENAME = 4; 38 static final int VALID_PARSERKEY = 1; 39 static final int VALID_PATH = 5; 40 41 public static final Test suite() 42 { 43 String [][] testData = 44 { 45 { 46 "ftp.ibiblio.org", "unix", "vms", 47 "HA!", "javaio.jar", 48 "pub/languages/java/javafaq" 49 }, 50 { 51 "ftp.wacom.com", "windows", "VMS", "HA!", 52 "wacom97.zip", "pub\\ftp\\drivers" 53 }, 54 { 55 "h71000.www7.hp.com", "vms", "windows", 56 "[.HA!]", "ACLOCAL.M4;1", 57 58 "[.FREEWARE50.XTERM]" 59 } 60 }; 61 Class clasz = ListingFunctionalTest.class; 62 Method [] methods = clasz.getDeclaredMethods(); 63 TestSuite allSuites = new TestSuite("FTP Listing Functional Test Suite"); 64 65 for (int i = 0; i < testData.length; i++) 66 { 67 TestSuite suite = new TestSuite(testData[i][VALID_PARSERKEY]); 68 69 for (int j = 0; j < methods.length; j++) 70 { 71 Method method = methods[j]; 72 73 if (method.getName().startsWith("test")) 74 { 75 suite.addTest(new ListingFunctionalTest( 76 method.getName(), 77 testData[i])); 78 } 79 } 80 81 allSuites.addTest(suite); 82 } 83 84 return allSuites; 85 } 86 87 private FTPClient client; 88 private String hostName; 89 private String invalidParserKey; 90 private String invalidPath; 91 private String validFilename; 92 private String validParserKey; 93 private String validPath; 94 95 100 public ListingFunctionalTest(String arg0, 101 String [] settings) 102 { 103 super(arg0); 104 invalidParserKey = settings[INVALID_PARSERKEY]; 105 validParserKey = settings[VALID_PARSERKEY]; 106 invalidPath = settings[INVALID_PATH]; 107 validFilename = settings[VALID_FILENAME]; 108 validPath = settings[VALID_PATH]; 109 hostName = settings[HOSTNAME]; 110 } 111 112 118 private boolean findByName(List fileList, 119 String string) 120 { 121 boolean found = false; 122 Iterator iter = fileList.iterator(); 123 124 while (iter.hasNext() && !found) 125 { 126 Object element = iter.next(); 127 128 if (element instanceof FTPFile) 129 { 130 FTPFile file = (FTPFile) element; 131 132 found = file.getName().equals(string); 133 } 134 else 135 { 136 String filename = (String ) element; 137 138 found = filename.endsWith(string); 139 } 140 } 141 142 return found; 143 } 144 145 148 protected void setUp() throws Exception 149 { 150 super.setUp(); 151 client = new FTPClient(); 152 client.connect(hostName); 153 client.login("anonymous", "anonymous"); 154 client.enterLocalPassiveMode(); 155 } 156 157 160 protected void tearDown() 161 throws Exception 162 { 163 try 164 { 165 client.logout(); 166 } 167 catch (IOException e) 168 { 169 e.printStackTrace(); 170 } 171 172 if (client.isConnected()) 173 { 174 client.disconnect(); 175 } 176 177 client = null; 178 super.tearDown(); 179 } 180 181 184 public void testInitiateListParsing() 185 throws IOException 186 { 187 client.changeWorkingDirectory(validPath); 188 189 FTPListParseEngine engine = client.initiateListParsing(); 190 List files = Arrays.asList(engine.getNext(25)); 191 192 assertTrue(files.toString(), 193 findByName(files, validFilename)); 194 } 195 196 199 public void testInitiateListParsingWithPath() 200 throws IOException 201 { 202 FTPListParseEngine engine = client.initiateListParsing(validParserKey, 203 validPath); 204 List files = Arrays.asList(engine.getNext(25)); 205 206 assertTrue(files.toString(), 207 findByName(files, validFilename)); 208 } 209 210 213 public void testInitiateListParsingWithPathAndAutodetection() 214 throws IOException 215 { 216 FTPListParseEngine engine = client.initiateListParsing(validPath); 217 List files = Arrays.asList(engine.getNext(25)); 218 219 assertTrue(files.toString(), 220 findByName(files, validFilename)); 221 } 222 223 226 public void testInitiateListParsingWithPathAndAutodetectionButEmpty() 227 throws IOException 228 { 229 FTPListParseEngine engine = client.initiateListParsing(invalidPath); 230 231 assertFalse(engine.hasNext()); 232 } 233 234 237 public void testInitiateListParsingWithPathAndIncorrectParser() 238 throws IOException 239 { 240 FTPListParseEngine engine = client.initiateListParsing(invalidParserKey, 241 invalidPath); 242 243 assertFalse(engine.hasNext()); 244 } 245 246 249 public void testListFiles() 250 throws IOException 251 { 252 FTPClientConfig config = new FTPClientConfig(validParserKey); 253 client.configure(config); 254 List files = Arrays.asList(client.listFiles(validPath)); 255 256 assertTrue(files.toString(), 257 findByName(files, validFilename)); 258 } 259 260 public void testListFilesWithAutodection() 261 throws IOException 262 { 263 client.changeWorkingDirectory(validPath); 264 265 List files = Arrays.asList(client.listFiles()); 266 267 assertTrue(files.toString(), 268 findByName(files, validFilename)); 269 } 270 271 274 public void testListFilesWithIncorrectParser() 275 throws IOException 276 { 277 FTPClientConfig config = new FTPClientConfig(invalidParserKey); 278 client.configure(config); 279 280 FTPFile[] files = client.listFiles(validPath); 281 282 assertEquals(0, files.length); 283 } 284 285 288 public void testListFilesWithPathAndAutodectionButEmpty() 289 throws IOException 290 { 291 FTPFile[] files = client.listFiles(invalidPath); 292 293 assertEquals(0, files.length); 294 } 295 296 299 public void testListFilesWithPathAndAutodetection() 300 throws IOException 301 { 302 List files = Arrays.asList(client.listFiles(validPath)); 303 304 assertTrue(files.toString(), 305 findByName(files, validFilename)); 306 } 307 308 311 public void testListNames() 312 throws IOException 313 { 314 client.changeWorkingDirectory(validPath); 315 316 String [] names = client.listNames(); 317 318 assertNotNull(names); 319 320 List lnames = Arrays.asList(names); 321 322 assertTrue(lnames.toString(), 323 lnames.contains(validFilename)); 324 } 325 326 329 public void testListNamesWithPath() 330 throws IOException 331 { 332 List names = Arrays.asList(client.listNames(validPath)); 333 334 assertTrue(names.toString(), 335 findByName(names, validFilename)); 336 } 337 338 public void testListNamesWithPathButEmpty() 339 throws IOException 340 { 341 String [] names = client.listNames(invalidPath); 342 343 assertNull(names); 344 } 345 } 346 | Popular Tags |