1 17 package org.apache.tools.ant.taskdefs.optional.net; 18 19 import org.apache.tools.ant.BuildFileTest; 20 import org.apache.tools.ant.DirectoryScanner; 21 import org.apache.tools.ant.types.FileSet; 22 import org.apache.tools.ant.taskdefs.optional.net.FTP; 23 import org.apache.tools.ant.util.JavaEnvUtils; 24 import org.apache.tools.ant.taskdefs.condition.Os; 25 26 import java.io.File ; 27 import java.io.IOException ; 28 import java.util.Arrays ; 29 30 import org.apache.commons.net.ftp.FTPClient; 31 32 public class FTPTest extends BuildFileTest{ 33 private boolean supportsSymlinks = Os.isFamily("unix") 35 && !JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1); 36 37 private FTPClient ftp; 38 private boolean connectionSucceeded = true; 39 private boolean loginSuceeded = true; 40 private String tmpDir = null; 41 private String remoteTmpDir = null; 42 private String ftpFileSep = null; 43 private myFTP myFTPTask = new myFTP(); 44 45 public FTPTest(String name) { 46 super(name); 47 } 48 public void setUp() { 49 configureProject("src/etc/testcases/taskdefs/optional/net/ftp.xml"); 50 getProject().executeTarget("setup"); 51 tmpDir = getProject().getProperty("tmp.dir"); 52 ftp = new FTPClient(); 53 ftpFileSep = getProject().getProperty("ftp.filesep"); 54 myFTPTask.setSeparator(ftpFileSep); 55 myFTPTask.setProject(getProject()); 56 remoteTmpDir = myFTPTask.resolveFile(tmpDir); 57 String remoteHost = getProject().getProperty("ftp.host"); 58 int port = Integer.parseInt(getProject().getProperty("ftp.port")); 59 String remoteUser = getProject().getProperty("ftp.user"); 60 String password = getProject().getProperty("ftp.password"); 61 try { 62 ftp.connect(remoteHost, port); 63 } catch (Exception ex) { 64 connectionSucceeded = false; 65 loginSuceeded = false; 66 System.out.println("could not connect to host " + remoteHost + " on port " + port); 67 } 68 if (connectionSucceeded) { 69 try { 70 ftp.login(remoteUser, password); 71 } catch (IOException ioe) { 72 loginSuceeded = false; 73 System.out.println("could not log on to " + remoteHost + " as user " + remoteUser); 74 } 75 } 76 } 77 78 public void tearDown() { 79 try { 80 ftp.disconnect(); 81 } catch (IOException ioe) { 82 } 84 getProject().executeTarget("cleanup"); 85 } 86 private boolean changeRemoteDir(String remoteDir) { 87 boolean result = true; 88 try { 89 ftp.cwd(remoteDir); 90 } 91 catch (Exception ex) { 92 System.out.println("could not change directory to " + remoteTmpDir); 93 result = false; 94 } 95 return result; 96 } 97 public void test1() { 98 if (loginSuceeded) { 99 if (changeRemoteDir(remoteTmpDir)) { 100 FTP.FTPDirectoryScanner ds = myFTPTask.newScanner(ftp); 101 ds.setBasedir(new File (getProject().getBaseDir(), "tmp")); 102 ds.setIncludes(new String [] {"alpha"}); 103 ds.scan(); 104 compareFiles(ds, new String [] {} ,new String [] {"alpha"}); 105 } 106 } 107 } 108 109 public void test2() { 110 if (loginSuceeded) { 111 if (changeRemoteDir(remoteTmpDir)) { 112 FTP.FTPDirectoryScanner ds = myFTPTask.newScanner(ftp); 113 ds.setBasedir(new File (getProject().getBaseDir(), "tmp")); 114 ds.setIncludes(new String [] {"alpha/"}); 115 ds.scan(); 116 compareFiles(ds, new String [] {"alpha/beta/beta.xml", 117 "alpha/beta/gamma/gamma.xml"}, 118 new String [] {"alpha", "alpha/beta", "alpha/beta/gamma"}); 119 } 120 } 121 } 122 123 public void test3() { 124 if (loginSuceeded) { 125 if (changeRemoteDir(remoteTmpDir)) { 126 FTP.FTPDirectoryScanner ds = myFTPTask.newScanner(ftp); 127 ds.setBasedir(new File (getProject().getBaseDir(), "tmp")); 128 ds.scan(); 129 compareFiles(ds, new String [] {"alpha/beta/beta.xml", 130 "alpha/beta/gamma/gamma.xml"}, 131 new String [] {"alpha", "alpha/beta", 132 "alpha/beta/gamma"}); 133 } 134 } 135 } 136 137 public void testFullPathMatchesCaseSensitive() { 138 if (loginSuceeded) { 139 if (changeRemoteDir(remoteTmpDir)) { 140 FTP.FTPDirectoryScanner ds = myFTPTask.newScanner(ftp); 141 ds.setBasedir(new File (getProject().getBaseDir(), "tmp")); 142 ds.setIncludes(new String [] {"alpha/beta/gamma/GAMMA.XML"}); 143 ds.scan(); 144 compareFiles(ds, new String [] {}, new String [] {}); 145 } 146 } 147 } 148 149 public void testFullPathMatchesCaseInsensitive() { 150 if (loginSuceeded) { 151 if (changeRemoteDir(remoteTmpDir)) { 152 FTP.FTPDirectoryScanner ds = myFTPTask.newScanner(ftp); 153 ds.setCaseSensitive(false); 154 ds.setBasedir(new File (getProject().getBaseDir(), "tmp")); 155 ds.setIncludes(new String [] {"alpha/beta/gamma/GAMMA.XML"}); 156 ds.scan(); 157 compareFiles(ds, new String [] {"alpha/beta/gamma/gamma.xml"}, 158 new String [] {}); 159 } 160 } 161 } 162 163 public void test2ButCaseInsensitive() { 164 if (loginSuceeded) { 165 if (changeRemoteDir(remoteTmpDir)) { 166 FTP.FTPDirectoryScanner ds = myFTPTask.newScanner(ftp); 167 ds.setBasedir(new File (getProject().getBaseDir(), "tmp")); 168 ds.setIncludes(new String [] {"ALPHA/"}); 169 ds.setCaseSensitive(false); 170 ds.scan(); 171 compareFiles(ds, new String [] {"alpha/beta/beta.xml", 172 "alpha/beta/gamma/gamma.xml"}, 173 new String [] {"alpha", "alpha/beta", "alpha/beta/gamma"}); 174 } 175 } 176 } 177 public void test2bisButCaseInsensitive() { 178 if (loginSuceeded) { 179 if (changeRemoteDir(remoteTmpDir)) { 180 FTP.FTPDirectoryScanner ds = myFTPTask.newScanner(ftp); 181 ds.setBasedir(new File (getProject().getBaseDir(), "tmp")); 182 ds.setIncludes(new String [] {"alpha/BETA/gamma/"}); 183 ds.setCaseSensitive(false); 184 ds.scan(); 185 compareFiles(ds, new String [] {"alpha/beta/gamma/gamma.xml"}, 186 new String [] {"alpha/beta/gamma"}); 187 } 188 } 189 } 190 public void testGetWithSelector() { 191 expectLogContaining("ftp-get-with-selector", 192 "selectors are not supported in remote filesets"); 193 FileSet fsDestination = (FileSet) getProject().getReference("fileset-destination-without-selector"); 194 DirectoryScanner dsDestination = fsDestination.getDirectoryScanner(getProject()); 195 dsDestination.scan(); 196 String [] sortedDestinationDirectories = dsDestination.getIncludedDirectories(); 197 String [] sortedDestinationFiles = dsDestination.getIncludedFiles(); 198 for (int counter = 0; counter < sortedDestinationDirectories.length; counter++) { 199 sortedDestinationDirectories[counter] = 200 sortedDestinationDirectories[counter].replace(File.separatorChar, '/'); 201 } 202 for (int counter = 0; counter < sortedDestinationFiles.length; counter++) { 203 sortedDestinationFiles[counter] = 204 sortedDestinationFiles[counter].replace(File.separatorChar, '/'); 205 } 206 FileSet fsSource = (FileSet) getProject().getReference("fileset-source-without-selector"); 207 DirectoryScanner dsSource = fsSource.getDirectoryScanner(getProject()); 208 dsSource.scan(); 209 compareFiles(dsSource, sortedDestinationFiles, sortedDestinationDirectories); 210 } 211 public void testGetFollowSymlinksTrue() { 212 if (!supportsSymlinks) { 213 return; 214 } 215 if (!loginSuceeded) { 216 return; 217 } 218 if (!changeRemoteDir(remoteTmpDir)) { 219 return; 220 } 221 getProject().executeTarget("ftp-get-directory-symbolic-link"); 222 FileSet fsDestination = (FileSet) getProject().getReference("fileset-destination-without-selector"); 223 DirectoryScanner dsDestination = fsDestination.getDirectoryScanner(getProject()); 224 dsDestination.scan(); 225 compareFiles(dsDestination, new String [] {"alpha/beta/gamma/gamma.xml"}, 226 new String [] {"alpha", "alpha/beta", "alpha/beta/gamma"}); 227 } 228 public void testGetFollowSymlinksFalse() { 229 if (!supportsSymlinks) { 230 return; 231 } 232 if (!loginSuceeded) { 233 return; 234 } 235 if (!changeRemoteDir(remoteTmpDir)) { 236 return; 237 } 238 getProject().executeTarget("ftp-get-directory-no-symbolic-link"); 239 FileSet fsDestination = (FileSet) getProject().getReference("fileset-destination-without-selector"); 240 DirectoryScanner dsDestination = fsDestination.getDirectoryScanner(getProject()); 241 dsDestination.scan(); 242 compareFiles(dsDestination, new String [] {}, 243 new String [] {}); 244 } 245 public void testAllowSymlinks() { 246 if (!supportsSymlinks) { 247 return; 248 } 249 if (!loginSuceeded) { 250 return; 251 } 252 if (!changeRemoteDir(remoteTmpDir)) { 253 return; 254 } 255 getProject().executeTarget("symlink-setup"); 256 FTP.FTPDirectoryScanner ds = myFTPTask.newScanner(ftp); 257 ds.setBasedir(new File (getProject().getBaseDir(), "tmp")); 258 ds.setIncludes(new String [] {"alpha/beta/gamma/"}); 259 ds.setFollowSymlinks(true); 260 ds.scan(); 261 compareFiles(ds, new String [] {"alpha/beta/gamma/gamma.xml"}, 262 new String [] {"alpha/beta/gamma"}); 263 } 264 265 public void testProhibitSymlinks() { 266 if (!supportsSymlinks) { 267 return; 268 } 269 if (!loginSuceeded) { 270 return; 271 } 272 if (!changeRemoteDir(remoteTmpDir)) { 273 return; 274 } 275 getProject().executeTarget("symlink-setup"); 276 FTP.FTPDirectoryScanner ds = myFTPTask.newScanner(ftp); 277 ds.setBasedir(new File (getProject().getBaseDir(), "tmp")); 278 ds.setIncludes(new String [] {"alpha/beta/gamma/"}); 279 ds.setFollowSymlinks(false); 280 ds.scan(); 281 compareFiles(ds, new String [] {}, new String [] {}); 282 } 283 public void testFileSymlink() { 284 if (!supportsSymlinks) { 285 return; 286 } 287 if (!loginSuceeded) { 288 return; 289 } 290 if (!changeRemoteDir(remoteTmpDir)) { 291 return; 292 } 293 getProject().executeTarget("symlink-file-setup"); 294 FTP.FTPDirectoryScanner ds = myFTPTask.newScanner(ftp); 295 ds.setBasedir(new File (getProject().getBaseDir(), "tmp")); 296 ds.setIncludes(new String [] {"alpha/beta/gamma/"}); 297 ds.setFollowSymlinks(true); 298 ds.scan(); 299 compareFiles(ds, new String [] {"alpha/beta/gamma/gamma.xml"}, 300 new String [] {"alpha/beta/gamma"}); 301 } 302 public void testOrderOfIncludePatternsIrrelevant() { 304 if (!loginSuceeded) { 305 return; 306 } 307 if (!changeRemoteDir(remoteTmpDir)) { 308 return; 309 } 310 String [] expectedFiles = {"alpha/beta/beta.xml", 311 "alpha/beta/gamma/gamma.xml"}; 312 String [] expectedDirectories = {"alpha/beta", "alpha/beta/gamma" }; 313 FTP.FTPDirectoryScanner ds = myFTPTask.newScanner(ftp); 314 ds.setBasedir(new File (getProject().getBaseDir(), "tmp")); 315 ds.setIncludes(new String [] {"alpha/be?a/**", "alpha/beta/gamma/"}); 316 ds.scan(); 317 compareFiles(ds, expectedFiles, expectedDirectories); 318 ds = myFTPTask.newScanner(ftp); 320 ds.setBasedir(new File (getProject().getBaseDir(), "tmp")); 321 ds.setIncludes(new String [] {"alpha/beta/gamma/", "alpha/be?a/**"}); 322 ds.scan(); 323 compareFiles(ds, expectedFiles, expectedDirectories); 324 } 325 326 public void testPatternsDifferInCaseScanningSensitive() { 327 if (!loginSuceeded) { 328 return; 329 } 330 if (!changeRemoteDir(remoteTmpDir)) { 331 return; 332 } 333 FTP.FTPDirectoryScanner ds = myFTPTask.newScanner(ftp); 334 ds.setBasedir(new File (getProject().getBaseDir(), "tmp")); 335 ds.setIncludes(new String [] {"alpha/", "ALPHA/"}); 336 ds.scan(); 337 compareFiles(ds, new String [] {"alpha/beta/beta.xml", 338 "alpha/beta/gamma/gamma.xml"}, 339 new String [] {"alpha", "alpha/beta", "alpha/beta/gamma"}); 340 } 341 342 public void testPatternsDifferInCaseScanningInsensitive() { 343 if (!loginSuceeded) { 344 return; 345 } 346 if (!changeRemoteDir(remoteTmpDir)) { 347 return; 348 } 349 FTP.FTPDirectoryScanner ds = myFTPTask.newScanner(ftp); 350 ds.setBasedir(new File (getProject().getBaseDir(), "tmp")); 351 ds.setIncludes(new String [] {"alpha/", "ALPHA/"}); 352 ds.setCaseSensitive(false); 353 ds.scan(); 354 compareFiles(ds, new String [] {"alpha/beta/beta.xml", 355 "alpha/beta/gamma/gamma.xml"}, 356 new String [] {"alpha", "alpha/beta", "alpha/beta/gamma"}); 357 } 358 359 public void testFullpathDiffersInCaseScanningSensitive() { 360 if (!loginSuceeded) { 361 return; 362 } 363 if (!changeRemoteDir(remoteTmpDir)) { 364 return; 365 } 366 FTP.FTPDirectoryScanner ds = myFTPTask.newScanner(ftp); 367 ds.setBasedir(new File (getProject().getBaseDir(), "tmp")); 368 ds.setIncludes(new String [] { 369 "alpha/beta/gamma/gamma.xml", 370 "alpha/beta/gamma/GAMMA.XML" 371 }); 372 ds.scan(); 373 compareFiles(ds, new String [] {"alpha/beta/gamma/gamma.xml"}, 374 new String [] {}); 375 } 376 377 public void testFullpathDiffersInCaseScanningInsensitive() { 378 if (!loginSuceeded) { 379 return; 380 } 381 if (!changeRemoteDir(remoteTmpDir)) { 382 return; 383 } 384 FTP.FTPDirectoryScanner ds = myFTPTask.newScanner(ftp); 385 ds.setBasedir(new File (getProject().getBaseDir(), "tmp")); 386 ds.setIncludes(new String [] { 387 "alpha/beta/gamma/gamma.xml", 388 "alpha/beta/gamma/GAMMA.XML" 389 }); 390 ds.setCaseSensitive(false); 391 ds.scan(); 392 compareFiles(ds, new String [] {"alpha/beta/gamma/gamma.xml"}, 393 new String [] {}); 394 } 395 396 public void testParentDiffersInCaseScanningSensitive() { 397 if (!loginSuceeded) { 398 return; 399 } 400 if (!changeRemoteDir(remoteTmpDir)) { 401 return; 402 } 403 FTP.FTPDirectoryScanner ds = myFTPTask.newScanner(ftp); 404 ds.setBasedir(new File (getProject().getBaseDir(), "tmp")); 405 ds.setIncludes(new String [] {"alpha/", "ALPHA/beta/"}); 406 ds.scan(); 407 compareFiles(ds, new String [] {"alpha/beta/beta.xml", 408 "alpha/beta/gamma/gamma.xml"}, 409 new String [] {"alpha", "alpha/beta", "alpha/beta/gamma"}); 410 } 411 412 public void testParentDiffersInCaseScanningInsensitive() { 413 if (!loginSuceeded) { 414 return; 415 } 416 if (!changeRemoteDir(remoteTmpDir)) { 417 return; 418 } 419 FTP.FTPDirectoryScanner ds = myFTPTask.newScanner(ftp); 420 ds.setBasedir(new File (getProject().getBaseDir(), "tmp")); 421 ds.setIncludes(new String [] {"alpha/", "ALPHA/beta/"}); 422 ds.setCaseSensitive(false); 423 ds.scan(); 424 compareFiles(ds, new String [] {"alpha/beta/beta.xml", 425 "alpha/beta/gamma/gamma.xml"}, 426 new String [] {"alpha", "alpha/beta", "alpha/beta/gamma"}); 427 } 428 429 public void testExcludeOneFile() { 430 if (!loginSuceeded) { 431 return; 432 } 433 if (!changeRemoteDir(remoteTmpDir)) { 434 return; 435 } 436 FTP.FTPDirectoryScanner ds = myFTPTask.newScanner(ftp); 437 ds.setBasedir(new File (getProject().getBaseDir(), "tmp")); 438 ds.setIncludes(new String [] { 439 "**/*.xml" 440 }); 441 ds.setExcludes(new String [] { 442 "alpha/beta/b*xml" 443 }); 444 ds.scan(); 445 compareFiles(ds, new String [] {"alpha/beta/gamma/gamma.xml"}, 446 new String [] {}); 447 } 448 public void testExcludeHasPrecedence() { 449 if (!loginSuceeded) { 450 return; 451 } 452 if (!changeRemoteDir(remoteTmpDir)) { 453 return; 454 } 455 FTP.FTPDirectoryScanner ds = myFTPTask.newScanner(ftp); 456 ds.setBasedir(new File (getProject().getBaseDir(), "tmp")); 457 ds.setIncludes(new String [] { 458 "alpha/**" 459 }); 460 ds.setExcludes(new String [] { 461 "alpha/**" 462 }); 463 ds.scan(); 464 compareFiles(ds, new String [] {}, 465 new String [] {}); 466 467 } 468 public void testAlternateIncludeExclude() { 469 if (!loginSuceeded) { 470 return; 471 } 472 if (!changeRemoteDir(remoteTmpDir)) { 473 return; 474 } 475 FTP.FTPDirectoryScanner ds = myFTPTask.newScanner(ftp); 476 ds.setBasedir(new File (getProject().getBaseDir(), "tmp")); 477 ds.setIncludes(new String [] { 478 "alpha/**", 479 "alpha/beta/gamma/**" 480 }); 481 ds.setExcludes(new String [] { 482 "alpha/beta/**" 483 }); 484 ds.scan(); 485 compareFiles(ds, new String [] {}, 486 new String [] {"alpha"}); 487 488 } 489 public void testAlternateExcludeInclude() { 490 if (!loginSuceeded) { 491 return; 492 } 493 if (!changeRemoteDir(remoteTmpDir)) { 494 return; 495 } 496 FTP.FTPDirectoryScanner ds = myFTPTask.newScanner(ftp); 497 ds.setBasedir(new File (getProject().getBaseDir(), "tmp")); 498 ds.setExcludes(new String [] { 499 "alpha/**", 500 "alpha/beta/gamma/**" 501 }); 502 ds.setIncludes(new String [] { 503 "alpha/beta/**" 504 }); 505 ds.scan(); 506 compareFiles(ds, new String [] {}, 507 new String [] {}); 508 509 } 510 513 public void testChildrenOfExcludedDirectory() { 514 if (!loginSuceeded) { 515 return; 516 } 517 if (!changeRemoteDir(remoteTmpDir)) { 518 return; 519 } 520 getProject().executeTarget("children-of-excluded-dir-setup"); 521 FTP.FTPDirectoryScanner ds = myFTPTask.newScanner(ftp); 522 ds.setBasedir(new File (getProject().getBaseDir(), "tmp")); 523 ds.setExcludes(new String [] {"alpha/**"}); 524 ds.scan(); 525 compareFiles(ds, new String [] {"delta/delta.xml"}, 526 new String [] {"delta"}); 527 528 ds = myFTPTask.newScanner(ftp); 529 if (!changeRemoteDir(remoteTmpDir)) { 530 return; 531 } 532 ds.setBasedir(new File (getProject().getBaseDir(), "tmp")); 533 ds.setExcludes(new String [] {"alpha"}); 534 ds.scan(); 535 compareFiles(ds, new String [] {"alpha/beta/beta.xml", 536 "alpha/beta/gamma/gamma.xml", 537 "delta/delta.xml"}, 538 new String [] {"alpha/beta", "alpha/beta/gamma", "delta"}); 539 540 } 541 544 public void testFTPDelete() { 545 getProject().executeTarget("ftp-delete"); 546 } 547 private void compareFiles(DirectoryScanner ds, String [] expectedFiles, 548 String [] expectedDirectories) { 549 String includedFiles[] = ds.getIncludedFiles(); 550 String includedDirectories[] = ds.getIncludedDirectories(); 551 assertEquals("file present: ", expectedFiles.length, 552 includedFiles.length); 553 assertEquals("directories present: ", expectedDirectories.length, 554 includedDirectories.length); 555 556 for (int counter=0; counter < includedFiles.length; counter++) { 557 includedFiles[counter] = includedFiles[counter].replace(File.separatorChar, '/'); 558 } 559 Arrays.sort(includedFiles); 560 for (int counter=0; counter < includedDirectories.length; counter++) { 561 includedDirectories[counter] = includedDirectories[counter] 562 .replace(File.separatorChar, '/'); 563 } 564 Arrays.sort(includedDirectories); 565 for (int counter=0; counter < includedFiles.length; counter++) { 566 assertEquals(expectedFiles[counter], includedFiles[counter]); 567 } 568 for (int counter=0; counter < includedDirectories.length; counter++) { 569 assertEquals(expectedDirectories[counter], includedDirectories[counter]); 570 counter++; 571 } 572 } 573 private static class myFTP extends FTP { 574 public FTP.FTPDirectoryScanner newScanner(FTPClient client) { 575 return new FTP.FTPDirectoryScanner(client); 576 } 577 public String resolveFile(String file) { 579 return super.resolveFile(file); 580 } 581 582 } 583 } 584 | Popular Tags |