1 // DirectoryInfoTest.cs - NUnit Test Cases for System.IO.DirectoryInfo class 2 // 3 // Authors 4 // Ville Palo (vi64pa@koti.soon.fi) 5 // Sebastien Pouliot <sebastien@ximian.com> 6 // 7 // (C) 2003 Ville Palo 8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com) 9 // 10 11 using System; 12 using System.Collections; 13 using System.Collections.Generic; 14 using System.IO; 15 using System.Runtime.Serialization; 16 using System.Runtime.Serialization.Formatters.Binary; 17 18 using NUnit.Framework; 19 20 namespace MonoTests.System.IO 21 { 22 [TestFixture] 23 public class DirectoryInfoTest 24 { 25 string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests"); 26 27 static readonly char DSC = Path.DirectorySeparatorChar; 28 string current; 29 30 [SetUp] SetUp()31 protected void SetUp () 32 { 33 current = Directory.GetCurrentDirectory (); 34 if (Directory.Exists (TempFolder)) 35 Directory.Delete (TempFolder, true); 36 Directory.CreateDirectory (TempFolder); 37 } 38 39 [TearDown] TearDown()40 protected void TearDown () 41 { 42 if (Directory.Exists (TempFolder)) 43 Directory.Delete (TempFolder, true); 44 Directory.SetCurrentDirectory (current); 45 } 46 47 [Test] // ctor (String) Constructor1()48 public void Constructor1 () 49 { 50 string path = TempFolder + DSC + "DIT.Ctr.Test"; 51 DeleteDir (path); 52 53 DirectoryInfo info = new DirectoryInfo (path); 54 Assert.AreEqual ("DIT.Ctr.Test", info.Name, "#1"); 55 Assert.IsFalse (info.Exists, "#2"); 56 Assert.AreEqual (".Test", info.Extension, "#3"); 57 Assert.AreEqual ("DIT.Ctr.Test", info.Name, "#4"); 58 } 59 60 [Test] // ctor (String) Constructor1_Path_Null()61 public void Constructor1_Path_Null () 62 { 63 try { 64 new DirectoryInfo (null); 65 Assert.Fail ("#1"); 66 } catch (ArgumentNullException ex) { 67 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); 68 Assert.IsNull (ex.InnerException, "#3"); 69 Assert.IsNotNull (ex.Message, "#4"); 70 Assert.AreEqual ("path", ex.ParamName, "#5"); 71 } 72 } 73 74 [Test] // ctor (String) Constructor1_Path_Empty()75 public void Constructor1_Path_Empty () 76 { 77 try { 78 new DirectoryInfo (string.Empty); 79 Assert.Fail ("#1"); 80 } catch (ArgumentException ex) { 81 // Empty file name is not legal 82 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2"); 83 Assert.IsNull (ex.InnerException, "#3"); 84 Assert.IsNotNull (ex.Message, "#4"); 85 Assert.IsNull (ex.ParamName, "#5"); 86 } 87 } 88 89 [Test] // ctor (String) Constructor1_Path_Whitespace()90 public void Constructor1_Path_Whitespace () 91 { 92 try { 93 new DirectoryInfo (" "); 94 Assert.Fail ("#1"); 95 } catch (ArgumentException ex) { 96 // The path is not of a legal form 97 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2"); 98 Assert.IsNull (ex.InnerException, "#3"); 99 Assert.IsNotNull (ex.Message, "#4"); 100 Assert.IsNull (ex.ParamName, "#5"); 101 } 102 } 103 104 [Test] // ctor (String) Constructor1_Path_InvalidPathChars()105 public void Constructor1_Path_InvalidPathChars () 106 { 107 string path = string.Empty; 108 foreach (char c in Path.InvalidPathChars) 109 path += c; 110 try { 111 new DirectoryInfo (path); 112 Assert.Fail ("#1"); 113 } catch (ArgumentException ex) { 114 // The path contains illegal characters 115 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2"); 116 Assert.IsNull (ex.InnerException, "#3"); 117 Assert.IsNotNull (ex.Message, "#4"); 118 Assert.IsNull (ex.ParamName, "#5"); 119 } 120 } 121 122 [Test] Exists()123 public void Exists () 124 { 125 string path = TempFolder + DSC + "DIT.Exists.Test"; 126 DeleteDir (path); 127 128 try { 129 DirectoryInfo info = new DirectoryInfo (path); 130 Assert.IsFalse (info.Exists, "#1"); 131 132 Directory.CreateDirectory (path); 133 Assert.IsFalse (info.Exists, "#2"); 134 info = new DirectoryInfo (path); 135 Assert.IsTrue (info.Exists, "#3"); 136 } finally { 137 DeleteDir (path); 138 } 139 } 140 141 [Test] 142 [Category ("MobileNotWorking")] Name()143 public void Name () 144 { 145 string path = TempFolder + DSC + "DIT.Name.Test"; 146 DeleteDir (path); 147 148 try { 149 DirectoryInfo info = new DirectoryInfo (path); 150 Assert.AreEqual ("DIT.Name.Test", info.Name, "#1"); 151 152 info = Directory.CreateDirectory (path); 153 Assert.AreEqual ("DIT.Name.Test", info.Name, "#2"); 154 155 info = Directory.CreateDirectory ("whatever"); 156 Assert.AreEqual ("whatever", info.Name, "#3"); 157 158 if (RunningOnUnix) { 159 info = new DirectoryInfo ("/"); 160 Assert.AreEqual ("/", info.Name, "#4"); 161 162 info = new DirectoryInfo ("test/"); 163 Assert.AreEqual ("test", info.Name, "#5"); 164 165 info = new DirectoryInfo ("/test"); 166 Assert.AreEqual ("test", info.Name, "#4"); 167 168 info = new DirectoryInfo ("/test/"); 169 Assert.AreEqual ("test", info.Name, "#4"); 170 } else { 171 Directory.SetCurrentDirectory (@"C:\"); 172 173 info = new DirectoryInfo (@"c:"); 174 Assert.AreEqual (@"C:\", info.Name, "#4"); 175 176 info = new DirectoryInfo (@"c:\"); 177 Assert.AreEqual (@"c:\", info.Name, "#5"); 178 179 info = new DirectoryInfo (@"c:\test"); 180 Assert.AreEqual ("test", info.Name, "#6"); 181 182 info = new DirectoryInfo (@"c:\test\"); 183 Assert.AreEqual ("test", info.Name, "#7"); 184 } 185 } finally { 186 DeleteDir (path); 187 } 188 } 189 190 [Test] Parent()191 public void Parent () 192 { 193 string path = TempFolder + DSC + "DIT.Parent.Test"; 194 DeleteDir (path); 195 196 try { 197 DirectoryInfo info = new DirectoryInfo (path); 198 Assert.AreEqual ("MonoTests.System.IO.Tests", info.Parent.Name, "#1"); 199 200 info = Directory.CreateDirectory (path); 201 Assert.AreEqual ("MonoTests.System.IO.Tests", info.Parent.Name, "#2"); 202 203 info = new DirectoryInfo ("test"); 204 Assert.AreEqual (Directory.GetCurrentDirectory (), info.Parent.FullName, "#3"); 205 206 if (RunningOnUnix) { 207 info = new DirectoryInfo ("/"); 208 Assert.IsNull (info.Parent, "#4"); 209 210 info = new DirectoryInfo ("test/"); 211 Assert.IsNotNull (info.Parent, "#5a"); 212 Assert.AreEqual (Directory.GetCurrentDirectory (), info.Parent.FullName, "#5b"); 213 214 info = new DirectoryInfo ("/test"); 215 Assert.IsNotNull (info.Parent, "#6a"); 216 Assert.AreEqual ("/", info.Parent.FullName, "#6b"); 217 218 info = new DirectoryInfo ("/test/"); 219 Assert.IsNotNull (info.Parent, "#7a"); 220 Assert.AreEqual ("/", info.Parent.FullName, "#7b"); 221 } else { 222 Directory.SetCurrentDirectory (@"C:\"); 223 224 info = new DirectoryInfo (@"c:"); 225 Assert.IsNull (info.Parent, "#4"); 226 227 info = new DirectoryInfo (@"c:\"); 228 Assert.IsNull (info.Parent, "#5"); 229 230 info = new DirectoryInfo (@"c:\test"); 231 Assert.IsNotNull (info.Parent, "#6a"); 232 Assert.AreEqual (@"c:\", info.Parent.FullName, "#6b"); 233 234 info = new DirectoryInfo (@"c:\test\"); 235 Assert.IsNotNull (info.Parent, "#7a"); 236 Assert.AreEqual (@"c:\", info.Parent.FullName, "#7b"); 237 } 238 } finally { 239 DeleteDir (path); 240 } 241 } 242 243 [Test] Create()244 public void Create () 245 { 246 string path = TempFolder + DSC + "DIT.Create.Test"; 247 DeleteDir (path); 248 249 try { 250 DirectoryInfo info = new DirectoryInfo (path); 251 Assert.IsFalse (info.Exists, "#1"); 252 info.Create (); 253 Assert.IsFalse (info.Exists, "#2"); 254 info = new DirectoryInfo (path); 255 Assert.IsTrue (info.Exists, "#3"); 256 } finally { 257 DeleteDir (path); 258 } 259 } 260 261 [Test] CreateSubdirectory()262 public void CreateSubdirectory () 263 { 264 string sub_path = Path.Combine ("test01", "test02"); 265 try { 266 DirectoryInfo info = new DirectoryInfo (TempFolder); 267 DirectoryInfo sub = info.CreateSubdirectory (sub_path); 268 Assert.IsNotNull (sub, "#1"); 269 Assert.AreEqual (Path.Combine (TempFolder, sub_path), sub.FullName, "#2"); 270 Assert.IsTrue (Directory.Exists (sub.FullName), "#3"); 271 } finally { 272 DeleteDir (Path.Combine (TempFolder, sub_path)); 273 } 274 } 275 276 [Test] CreateSubdirectory_Path_Null()277 public void CreateSubdirectory_Path_Null () 278 { 279 DirectoryInfo info = new DirectoryInfo (TempFolder); 280 try { 281 info.CreateSubdirectory (null); 282 Assert.Fail ("#1"); 283 } catch (ArgumentNullException ex) { 284 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); 285 Assert.IsNull (ex.InnerException, "#3"); 286 Assert.IsNotNull (ex.Message, "#4"); 287 Assert.AreEqual ("path", ex.ParamName, "#5"); 288 } 289 } 290 291 [Test] CreateSubdirectory_Path_Empty()292 public void CreateSubdirectory_Path_Empty () 293 { 294 DirectoryInfo info = new DirectoryInfo (TempFolder); 295 try { 296 info.CreateSubdirectory (string.Empty); 297 Assert.Fail ("#1"); 298 } catch (ArgumentException ex) { 299 // Empty file name is not legal 300 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2"); 301 Assert.IsNull (ex.InnerException, "#3"); 302 Assert.IsNotNull (ex.Message, "#4"); 303 } 304 } 305 306 [Test] // Delete () Delete1()307 public void Delete1 () 308 { 309 string path = TempFolder + DSC + "DIT.Delete1.Test"; 310 DeleteDir (path); 311 312 try { 313 Directory.CreateDirectory (path); 314 DirectoryInfo info = new DirectoryInfo (path); 315 Assert.IsTrue (info.Exists, "#1"); 316 317 info.Delete (); 318 Assert.IsTrue (info.Exists, "#2"); 319 320 info = new DirectoryInfo (path); 321 Assert.IsFalse (info.Exists, "#3"); 322 } finally { 323 DeleteDir (path); 324 } 325 } 326 327 [Test] // Delete () Delete1_DirectoryNotEmpty()328 public void Delete1_DirectoryNotEmpty () 329 { 330 string path = TempFolder + DSC + "DIT.DeleteIOException1.Test"; 331 DeleteDir (path); 332 333 try { 334 Directory.CreateDirectory (path); 335 File.Create (path + DSC + "test").Close (); 336 DirectoryInfo info = new DirectoryInfo (path); 337 try { 338 info.Delete (); 339 Assert.Fail ("#1"); 340 } catch (IOException ex) { 341 // The directory is not empty 342 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2"); 343 Assert.IsNull (ex.InnerException, "#3"); 344 Assert.IsNotNull (ex.Message, "#4"); 345 } 346 } finally { 347 DeleteDir (path); 348 } 349 } 350 351 [Test] // Delete (Boolean) Delete2()352 public void Delete2 () 353 { 354 string path = TempFolder + DSC + "DIT.Delete2.Test"; 355 DeleteDir (path); 356 357 try { 358 Directory.CreateDirectory (path); 359 File.Create (path + DSC + "test").Close (); 360 DirectoryInfo info = new DirectoryInfo (path); 361 Assert.IsTrue (info.Exists, "#1"); 362 363 info.Delete (true); 364 Assert.IsTrue (info.Exists, "#2"); 365 366 info = new DirectoryInfo (path); 367 Assert.IsFalse (info.Exists, "#3"); 368 } finally { 369 DeleteDir (path); 370 } 371 } 372 373 [Test] // Delete (Boolean) Delete2_DirectoryNotEmpty()374 public void Delete2_DirectoryNotEmpty () 375 { 376 string path = TempFolder + DSC + "DIT.DeleteIOException2.Test"; 377 DeleteDir (path); 378 379 try { 380 Directory.CreateDirectory (path); 381 File.Create (path + DSC + "test").Close (); 382 DirectoryInfo info = new DirectoryInfo (path); 383 try { 384 info.Delete (false); 385 Assert.Fail ("#1"); 386 } catch (IOException ex) { 387 // The directory is not empty 388 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2"); 389 Assert.IsNull (ex.InnerException, "#3"); 390 Assert.IsNotNull (ex.Message, "#4"); 391 } 392 } finally { 393 DeleteDir (path); 394 } 395 } 396 397 [Test] // bug #75443 FullName()398 public void FullName () 399 { 400 DirectoryInfo di = new DirectoryInfo ("something"); 401 Assert.IsFalse (di.Exists, "#A1"); 402 Assert.AreEqual (Path.Combine (Directory.GetCurrentDirectory (), "something"), di.FullName, "#A2"); 403 404 di = new DirectoryInfo ("something" + Path.DirectorySeparatorChar); 405 Assert.IsFalse (di.Exists, "#B1"); 406 Assert.AreEqual (Path.DirectorySeparatorChar, di.FullName [di.FullName.Length - 1], "#B2"); 407 408 di = new DirectoryInfo ("something" + Path.AltDirectorySeparatorChar); 409 Assert.IsFalse (di.Exists, "#C1"); 410 Assert.AreEqual (Path.DirectorySeparatorChar, di.FullName [di.FullName.Length - 1], "#C2"); 411 412 if (RunningOnUnix) { 413 di = new DirectoryInfo ("/"); 414 Assert.AreEqual ("/", di.FullName, "#D1"); 415 416 di = new DirectoryInfo ("test/"); 417 Assert.AreEqual (Path.Combine (Directory.GetCurrentDirectory (), "test/"), di.FullName, "#D2"); 418 419 di = new DirectoryInfo ("/test"); 420 Assert.AreEqual ("/test", di.FullName, "#D3"); 421 422 di = new DirectoryInfo ("/test/"); 423 Assert.AreEqual ("/test/", di.FullName, "#D4"); 424 } else { 425 Directory.SetCurrentDirectory (@"C:\"); 426 427 di = new DirectoryInfo (@"c:"); 428 Assert.AreEqual (@"C:\", di.FullName, "#D1"); 429 430 di = new DirectoryInfo (@"c:\"); 431 Assert.AreEqual (@"c:\", di.FullName, "#D2"); 432 433 di = new DirectoryInfo (@"c:\test"); 434 Assert.AreEqual (@"c:\test", di.FullName, "#D3"); 435 436 di = new DirectoryInfo (@"c:\test\"); 437 Assert.AreEqual (@"c:\test\", di.FullName, "#D4"); 438 } 439 } 440 441 [Test] FullName_RootDirectory()442 public void FullName_RootDirectory () 443 { 444 DirectoryInfo di = new DirectoryInfo (String.Empty + Path.DirectorySeparatorChar); 445 if (RunningOnUnix) { 446 // can't be sure of the root drive under windows 447 Assert.AreEqual ("/", di.FullName, "#1"); 448 } 449 Assert.IsNull (di.Parent, "#2"); 450 451 di = new DirectoryInfo (String.Empty + Path.AltDirectorySeparatorChar); 452 if (RunningOnUnix) { 453 // can't be sure of the root drive under windows 454 Assert.AreEqual ("/", di.FullName, "#3"); 455 } 456 Assert.IsNull (di.Parent, "#4"); 457 } 458 459 [Test] // GetDirectories () GetDirectories1()460 public void GetDirectories1 () 461 { 462 string path = TempFolder + DSC + "DIT.GetDirectories1.Test"; 463 464 try { 465 DirectoryInfo info = Directory.CreateDirectory (path); 466 Assert.AreEqual (0, info.GetDirectories ().Length, "#1"); 467 468 Directory.CreateDirectory (path + DSC + "1"); 469 Directory.CreateDirectory (path + DSC + "2"); 470 File.Create (path + DSC + "filetest").Close (); 471 Assert.AreEqual (2, info.GetDirectories ().Length, "#2"); 472 473 Directory.Delete (path + DSC + 2); 474 Assert.AreEqual (1, info.GetDirectories ().Length, "#3"); 475 } finally { 476 DeleteDir (path); 477 } 478 } 479 480 [Test] // GetDirectories () GetDirectories1_DirectoryDoesNotExist()481 public void GetDirectories1_DirectoryDoesNotExist () 482 { 483 string path = TempFolder + DSC + "DIT.GetDirectoriesDirectoryNotFoundException1.Test"; 484 DeleteDir (path); 485 486 try { 487 DirectoryInfo info = new DirectoryInfo (path); 488 info.GetDirectories (); 489 Assert.Fail ("#1"); 490 } catch (DirectoryNotFoundException ex) { 491 // Could not find a part of '...' 492 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2"); 493 Assert.IsNull (ex.InnerException, "#3"); 494 Assert.IsNotNull (ex.Message, "#4"); 495 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5"); 496 } finally { 497 DeleteDir (path); 498 } 499 } 500 501 [Test] // GetDirectories (String) GetDirectories2()502 public void GetDirectories2 () 503 { 504 string path = TempFolder + DSC + "DIT.GetDirectories2.Test"; 505 506 try { 507 DirectoryInfo info = Directory.CreateDirectory (path); 508 Assert.AreEqual (0, info.GetDirectories ("*").Length, "#1"); 509 510 Directory.CreateDirectory (path + DSC + "test120"); 511 Directory.CreateDirectory (path + DSC + "test210"); 512 Directory.CreateDirectory (path + DSC + "atest330"); 513 Directory.CreateDirectory (path + DSC + "test220"); 514 Directory.CreateDirectory (path + DSC + "rest"); 515 Directory.CreateDirectory (path + DSC + "rest" + DSC + "subdir"); 516 File.Create (path + DSC + "filetest").Close (); 517 518 Assert.AreEqual (5, info.GetDirectories ("*").Length, "#2"); 519 Assert.AreEqual (3, info.GetDirectories ("test*").Length, "#3"); 520 Assert.AreEqual (2, info.GetDirectories ("test?20").Length, "#4"); 521 Assert.AreEqual (0, info.GetDirectories ("test?").Length, "#5"); 522 Assert.AreEqual (0, info.GetDirectories ("test[12]*").Length, "#6"); 523 Assert.AreEqual (2, info.GetDirectories ("test2*0").Length, "#7"); 524 Assert.AreEqual (4, info.GetDirectories ("*test*").Length, "#8"); 525 Assert.AreEqual (6, info.GetDirectories ("*", SearchOption.AllDirectories).Length, "#9"); 526 } finally { 527 DeleteDir (path); 528 } 529 } 530 531 [Test] // GetDirectories (String) GetDirectories2_DirectoryDoesNotExist()532 public void GetDirectories2_DirectoryDoesNotExist () 533 { 534 string path = TempFolder + DSC + "DIT.GetDirectoriesDirectoryNotFoundException2.Test"; 535 DeleteDir (path); 536 537 try { 538 DirectoryInfo info = new DirectoryInfo (path); 539 info.GetDirectories ("*"); 540 Assert.Fail ("#1"); 541 } catch (DirectoryNotFoundException ex) { 542 // Could not find a part of '...' 543 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2"); 544 Assert.IsNull (ex.InnerException, "#3"); 545 Assert.IsNotNull (ex.Message, "#4"); 546 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5"); 547 } finally { 548 DeleteDir (path); 549 } 550 } 551 552 [Test] // GetDirectories (String) GetDirectories2_SearchPattern_Null()553 public void GetDirectories2_SearchPattern_Null () 554 { 555 DirectoryInfo info = new DirectoryInfo (TempFolder); 556 try { 557 info.GetDirectories (null); 558 Assert.Fail ("#1"); 559 } catch (ArgumentNullException ex) { 560 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); 561 Assert.IsNull (ex.InnerException, "#3"); 562 Assert.IsNotNull (ex.Message, "#4"); 563 Assert.AreEqual ("searchPattern", ex.ParamName, "#5"); 564 } 565 } 566 567 [Test] //GetDirectories (String, SearchOptions) GetDirectiories_SearchOptionAllDirectories()568 public void GetDirectiories_SearchOptionAllDirectories () 569 { 570 string directoryToBeLookedFor = "lookforme"; 571 DirectoryInfo baseDir = Directory.CreateDirectory(Path.Combine(TempFolder, "GetDirectiories_SearchOptionAllDirectories")); 572 DirectoryInfo subdir = baseDir.CreateSubdirectory("subdir"); 573 DirectoryInfo subsubdir = subdir.CreateSubdirectory(directoryToBeLookedFor); 574 DirectoryInfo[] directoriesFound = baseDir.GetDirectories(directoryToBeLookedFor, SearchOption.AllDirectories); 575 Assert.AreEqual(1, directoriesFound.Length, "There should be exactly one directory with the specified name."); 576 Assert.AreEqual(directoryToBeLookedFor, directoriesFound[0].Name, "The name of the directory found should match the expected one."); 577 } 578 579 [Test] // GetDirectories (String, SearchOption) GetDirectories3_SearchPattern_Null()580 public void GetDirectories3_SearchPattern_Null () 581 { 582 DirectoryInfo info = new DirectoryInfo (TempFolder); 583 try { 584 info.GetDirectories (null, SearchOption.AllDirectories); 585 Assert.Fail ("#1"); 586 } catch (ArgumentNullException ex) { 587 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); 588 Assert.IsNull (ex.InnerException, "#3"); 589 Assert.IsNotNull (ex.Message, "#4"); 590 Assert.AreEqual ("searchPattern", ex.ParamName, "#5"); 591 } 592 } 593 594 [Test] // GetFiles () GetFiles1()595 public void GetFiles1 () 596 { 597 string path = TempFolder + DSC + "DIT.GetFiles1.Test"; 598 DeleteDir (path); 599 600 try { 601 DirectoryInfo info = Directory.CreateDirectory (path); 602 Assert.AreEqual (0, info.GetFiles ().Length, "#1"); 603 File.Create (path + DSC + "file1").Close (); 604 File.Create (path + DSC + "file2").Close (); 605 Directory.CreateDirectory (path + DSC + "directory1"); 606 Assert.AreEqual (2, info.GetFiles ().Length, "#2"); 607 } finally { 608 DeleteDir (path); 609 } 610 } 611 612 [Test] // GetFiles () GetFiles1_DirectoryDoesNotExist()613 public void GetFiles1_DirectoryDoesNotExist () 614 { 615 string path = TempFolder + DSC + "DIT.GetFilesDirectoryNotFoundException1.Test"; 616 DeleteDir (path); 617 618 try { 619 DirectoryInfo info = new DirectoryInfo (path); 620 info.GetFiles (); 621 Assert.Fail ("#1"); 622 } catch (DirectoryNotFoundException ex) { 623 // Could not find a part of '...' 624 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2"); 625 Assert.IsNull (ex.InnerException, "#3"); 626 Assert.IsNotNull (ex.Message, "#4"); 627 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5"); 628 } finally { 629 DeleteDir (path); 630 } 631 } 632 633 [Test] // GetFiles (String) GetFiles2()634 public void GetFiles2 () 635 { 636 string path = TempFolder + DSC + "DIT.GetFiles2.Test"; 637 DeleteDir (path); 638 639 try { 640 DirectoryInfo info = Directory.CreateDirectory (path); 641 Assert.AreEqual (0, info.GetFiles ("*").Length, "#1"); 642 File.Create (path + DSC + "file120file").Close (); 643 File.Create (path + DSC + "file220file").Close (); 644 File.Create (path + DSC + "afile330file").Close (); 645 File.Create (path + DSC + "test.abc").Close (); 646 File.Create (path + DSC + "test.abcd").Close (); 647 File.Create (path + DSC + "test.abcdef").Close (); 648 Directory.CreateDirectory (path + DSC + "dir"); 649 650 Assert.AreEqual (6, info.GetFiles ("*").Length, "#2"); 651 Assert.AreEqual (2, info.GetFiles ("file*file").Length, "#3"); 652 Assert.AreEqual (3, info.GetFiles ("*file*").Length, "#4"); 653 Assert.AreEqual (2, info.GetFiles ("file?20file").Length, "#5"); 654 Assert.AreEqual (1, info.GetFiles ("*.abcd").Length, "#6"); 655 Assert.AreEqual (2, info.GetFiles ("*.abcd*").Length, "#7"); 656 } finally { 657 DeleteDir (path); 658 } 659 } 660 661 [Test] // GetFiles (String) GetFiles2_DirectoryDoesNotExist()662 public void GetFiles2_DirectoryDoesNotExist () 663 { 664 string path = TempFolder + DSC + "DIT.GetFilesDirectoryNotFoundException2.Test"; 665 DeleteDir (path); 666 667 try { 668 DirectoryInfo info = new DirectoryInfo (path); 669 info.GetFiles ("*"); 670 Assert.Fail ("#1"); 671 } catch (DirectoryNotFoundException ex) { 672 // Could not find a part of '...' 673 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2"); 674 Assert.IsNull (ex.InnerException, "#3"); 675 Assert.IsNotNull (ex.Message, "#4"); 676 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5"); 677 } finally { 678 DeleteDir (path); 679 } 680 } 681 682 [Test] // GetFiles (String) GetFiles2_SearchPattern_Null()683 public void GetFiles2_SearchPattern_Null () 684 { 685 DirectoryInfo info = new DirectoryInfo (TempFolder); 686 try { 687 info.GetFiles (null); 688 Assert.Fail ("#1"); 689 } catch (ArgumentNullException ex) { 690 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); 691 Assert.IsNull (ex.InnerException, "#3"); 692 Assert.IsNotNull (ex.Message, "#4"); 693 Assert.AreEqual ("searchPattern", ex.ParamName, "#5"); 694 } 695 } 696 697 [Test] // GetFiles (String, SearchOption) GetFiles3_SearchPattern_Null()698 public void GetFiles3_SearchPattern_Null () 699 { 700 DirectoryInfo info = new DirectoryInfo (TempFolder); 701 try { 702 info.GetFiles (null, SearchOption.TopDirectoryOnly); 703 Assert.Fail ("#1"); 704 } catch (ArgumentNullException ex) { 705 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); 706 Assert.IsNull (ex.InnerException, "#3"); 707 Assert.IsNotNull (ex.Message, "#4"); 708 Assert.AreEqual ("searchPattern", ex.ParamName, "#5"); 709 } 710 } 711 712 [Test] GetFileSystemInfos2_SearchPattern_Null()713 public void GetFileSystemInfos2_SearchPattern_Null () 714 { 715 DirectoryInfo info = new DirectoryInfo (TempFolder); 716 try { 717 info.GetFileSystemInfos (null); 718 Assert.Fail ("#1"); 719 } catch (ArgumentNullException ex) { 720 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); 721 Assert.IsNull (ex.InnerException, "#3"); 722 Assert.IsNotNull (ex.Message, "#4"); 723 Assert.AreEqual ("searchPattern", ex.ParamName, "#5"); 724 } 725 } 726 727 [Test] MoveTo()728 public void MoveTo () 729 { 730 string path1 = TempFolder + DSC + "DIT.MoveTo.Soucre.Test"; 731 string path2 = TempFolder + DSC + "DIT.MoveTo.Dest.Test"; 732 DeleteDir (path1); 733 DeleteDir (path2); 734 735 try { 736 DirectoryInfo info1 = Directory.CreateDirectory (path1); 737 DirectoryInfo info2 = new DirectoryInfo (path2); 738 739 Assert.IsTrue (info1.Exists, "#A1"); 740 Assert.IsFalse (info2.Exists, "#A2"); 741 742 info1.MoveTo (path2); 743 Assert.IsTrue (info1.Exists, "#B1"); 744 Assert.IsFalse (info2.Exists, "#B2"); 745 746 info1 = new DirectoryInfo (path1); 747 info2 = new DirectoryInfo (path2); 748 Assert.IsFalse (info1.Exists, "#C1"); 749 Assert.IsTrue (info2.Exists, "#C2"); 750 } finally { 751 DeleteDir (path1); 752 DeleteDir (path2); 753 } 754 } 755 756 [Test] MoveTo_DestDirName_Empty()757 public void MoveTo_DestDirName_Empty () 758 { 759 string path = TempFolder + DSC + "DIT.MoveToArgumentException1.Test"; 760 DeleteDir (path); 761 762 try { 763 DirectoryInfo info = Directory.CreateDirectory (path); 764 try { 765 info.MoveTo (string.Empty); 766 Assert.Fail ("#1"); 767 } catch (ArgumentException ex) { 768 // Empty file name is not legal 769 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2"); 770 Assert.IsNull (ex.InnerException, "#3"); 771 Assert.IsNotNull (ex.Message, "#4"); 772 Assert.AreEqual ("destDirName", ex.ParamName, "#5"); 773 } 774 } finally { 775 DeleteDir (path); 776 } 777 } 778 779 [Test] MoveTo_DestDirName_Null()780 public void MoveTo_DestDirName_Null () 781 { 782 string path = TempFolder + DSC + "DIT.MoveToArgumentNullException.Test"; 783 DeleteDir (path); 784 785 try { 786 DirectoryInfo info = Directory.CreateDirectory (path); 787 try { 788 info.MoveTo (null); 789 Assert.Fail ("#1"); 790 } catch (ArgumentNullException ex) { 791 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2"); 792 Assert.IsNull (ex.InnerException, "#3"); 793 Assert.IsNotNull (ex.Message, "#4"); 794 Assert.AreEqual ("destDirName", ex.ParamName, "#5"); 795 } 796 } finally { 797 DeleteDir (path); 798 } 799 } 800 801 [Test] MoveTo_DestDirName_InvalidPathChars()802 public void MoveTo_DestDirName_InvalidPathChars () 803 { 804 string path = TempFolder + DSC + "DIT.MoveToArgumentException3.Test"; 805 DeleteDir (path); 806 807 try { 808 DirectoryInfo info = Directory.CreateDirectory (path); 809 try { 810 info.MoveTo (Path.InvalidPathChars [0].ToString ()); 811 Assert.Fail ("#1"); 812 } catch (ArgumentException ex) { 813 // The path contains illegal characters 814 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2"); 815 Assert.IsNull (ex.InnerException, "#3"); 816 Assert.IsNotNull (ex.Message, "#4"); 817 Assert.IsNull (ex.ParamName, "#5"); 818 } 819 } finally { 820 DeleteDir (path); 821 } 822 } 823 824 [Test] MoveTo_DestDirName_Whitespace()825 public void MoveTo_DestDirName_Whitespace () 826 { 827 string path = TempFolder + DSC + "DIT.MoveToArgumentException2.Test"; 828 DeleteDir (path); 829 830 try { 831 DirectoryInfo info = Directory.CreateDirectory (path); 832 try { 833 info.MoveTo (" "); 834 Assert.Fail ("#1"); 835 } catch (ArgumentException ex) { 836 // The path is not of a legal form 837 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2"); 838 Assert.IsNull (ex.InnerException, "#3"); 839 Assert.IsNotNull (ex.Message, "#4"); 840 Assert.IsNull (ex.ParamName, "#5"); 841 } 842 } finally { 843 DeleteDir (path); 844 } 845 } 846 847 [Test] MoveTo_SourceDest_NotDifferent()848 public void MoveTo_SourceDest_NotDifferent () 849 { 850 string path = TempFolder + DSC + "DIT.MoveToIOException1.Test"; 851 DeleteDir (path); 852 853 try { 854 DirectoryInfo info = Directory.CreateDirectory (path); 855 try { 856 info.MoveTo (path); 857 Assert.Fail ("#A1"); 858 } catch (IOException ex) { 859 // Source and destination path must be different 860 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2"); 861 Assert.IsNull (ex.InnerException, "#A3"); 862 Assert.IsNotNull (ex.Message, "#A4"); 863 } 864 } finally { 865 DeleteDir (path); 866 } 867 868 try { 869 DirectoryInfo info = new DirectoryInfo (path); 870 try { 871 info.MoveTo (path); 872 Assert.Fail ("#B1"); 873 } catch (IOException ex) { 874 // Source and destination path must be different 875 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2"); 876 Assert.IsNull (ex.InnerException, "#B3"); 877 Assert.IsNotNull (ex.Message, "#B4"); 878 } 879 } finally { 880 DeleteDir (path); 881 } 882 } 883 884 [Test] MoveTo_UpdateProperties()885 public void MoveTo_UpdateProperties () 886 { 887 string path = TempFolder + DSC + "DIT.MoveToUpdateProperties.Test"; 888 string path2 = TempFolder + DSC + "DIT.MoveToUpdateProperties2.Test"; 889 string path3 = path2 + DSC + "DIT.MoveToUpdateProperties3.Test"; 890 DeleteDir (path); 891 Directory.CreateDirectory (path); 892 Directory.CreateDirectory (path2); 893 894 DirectoryInfo info = new DirectoryInfo(path); 895 896 Assert.IsTrue (Directory.Exists(info.FullName)); 897 Assert.AreEqual (path, info.FullName); 898 Assert.AreEqual ("DIT.MoveToUpdateProperties.Test", info.Name); 899 Assert.AreEqual (TempFolder, info.Parent.FullName); 900 Assert.AreEqual (path, info.ToString ()); 901 902 info.MoveTo (path3); 903 Assert.IsTrue (Directory.Exists(info.FullName)); 904 Assert.AreEqual (path3, info.FullName); 905 Assert.AreEqual ("DIT.MoveToUpdateProperties3.Test", info.Name); 906 Assert.AreEqual (path2, info.Parent.FullName); 907 Assert.AreEqual (path3, info.ToString ()); 908 } 909 910 [Test] DirectoryNameWithSpace()911 public void DirectoryNameWithSpace () 912 { 913 DeleteDir ("this has a space at the end "); 914 string path = Path.Combine (TempFolder, "this has a space at the end "); 915 Directory.CreateDirectory (path); 916 DirectoryInfo i = new DirectoryInfo (path); 917 string dummy = null; 918 foreach (FileInfo f in i.GetFiles ()) 919 dummy = f.Name; 920 } 921 922 [Test] LastWriteTime()923 public void LastWriteTime () 924 { 925 DirectoryInfo info = new DirectoryInfo (TempFolder); 926 info.LastWriteTime = new DateTime (2003, 6, 4, 6, 4, 0); 927 928 DateTime time = Directory.GetLastWriteTime (TempFolder); 929 Assert.AreEqual (2003, time.Year, "#A1"); 930 Assert.AreEqual (6, time.Month, "#A2"); 931 Assert.AreEqual (4, time.Day, "#A3"); 932 Assert.AreEqual (6, time.Hour, "#A4"); 933 Assert.AreEqual (4, time.Minute, "#A5"); 934 Assert.AreEqual (0, time.Second, "#A6"); 935 936 time = TimeZone.CurrentTimeZone.ToLocalTime ( 937 Directory.GetLastWriteTimeUtc (TempFolder)); 938 Assert.AreEqual (2003, time.Year, "#B1"); 939 Assert.AreEqual (6, time.Month, "#B2"); 940 Assert.AreEqual (4, time.Day, "#B3"); 941 Assert.AreEqual (6, time.Hour, "#B4"); 942 Assert.AreEqual (4, time.Minute, "#B5"); 943 Assert.AreEqual (0, time.Second, "#B6"); 944 } 945 946 [Test] LastWriteTimeUtc()947 public void LastWriteTimeUtc () 948 { 949 DirectoryInfo info = new DirectoryInfo (TempFolder); 950 info.LastWriteTimeUtc = new DateTime (2003, 6, 4, 6, 4, 0); 951 952 DateTime time = TimeZone.CurrentTimeZone.ToUniversalTime ( 953 Directory.GetLastWriteTime (TempFolder)); 954 Assert.AreEqual (2003, time.Year, "#A1"); 955 Assert.AreEqual (6, time.Month, "#A2"); 956 Assert.AreEqual (4, time.Day, "#A3"); 957 Assert.AreEqual (6, time.Hour, "#A4"); 958 Assert.AreEqual (4, time.Minute, "#A5"); 959 Assert.AreEqual (0, time.Second, "#A6"); 960 961 time = Directory.GetLastWriteTimeUtc (TempFolder); 962 Assert.AreEqual (2003, time.Year, "#B1"); 963 Assert.AreEqual (6, time.Month, "#B2"); 964 Assert.AreEqual (4, time.Day, "#B3"); 965 Assert.AreEqual (6, time.Hour, "#B4"); 966 Assert.AreEqual (4, time.Minute, "#B5"); 967 Assert.AreEqual (0, time.Second, "#B6"); 968 } 969 970 [Test] LastAccessTime()971 public void LastAccessTime () 972 { 973 DirectoryInfo info = new DirectoryInfo (TempFolder); 974 info.LastAccessTime = DateTime.Now; 975 } 976 977 [Test] LastAccessTimeUtc()978 public void LastAccessTimeUtc () 979 { 980 DirectoryInfo info = new DirectoryInfo (TempFolder); 981 info.LastAccessTimeUtc = DateTime.UtcNow; 982 } 983 984 [Test] CreationTime()985 public void CreationTime () 986 { 987 DirectoryInfo info = new DirectoryInfo (TempFolder); 988 info.CreationTime = DateTime.Now; 989 } 990 991 [Test] CreationTimeUtc()992 public void CreationTimeUtc () 993 { 994 DirectoryInfo info = new DirectoryInfo (TempFolder); 995 info.CreationTimeUtc = DateTime.UtcNow; 996 } 997 998 [Test] Name_Bug76903()999 public void Name_Bug76903 () 1000 { 1001 CheckName ("/usr/share"); 1002 CheckName ("/usr/share/"); 1003 CheckName ("/usr/share/."); 1004 CheckName ("/usr/share/./"); 1005 CheckName ("/usr/share/blabla/../"); 1006 CheckName ("/usr/lib/../share/."); 1007 } 1008 1009 [Test] Hang_76191()1010 public void Hang_76191 () 1011 { 1012 // from bug #76191 (hangs on Windows) 1013 DirectoryInfo di = new DirectoryInfo (Environment.CurrentDirectory); 1014 Stack s = new Stack (); 1015 s.Push (di); 1016 while (di.Parent != null) { 1017 di = di.Parent; 1018 s.Push (di); 1019 } 1020 while (s.Count > 0) { 1021 di = (DirectoryInfo) s.Pop (); 1022 Assert.IsTrue (di.Exists, di.Name); 1023 } 1024 } 1025 1026 [Test] WindowsSystem32_76191()1027 public void WindowsSystem32_76191 () 1028 { 1029 if (RunningOnUnix) 1030 Assert.Ignore ("Running on Unix."); 1031 1032 Directory.SetCurrentDirectory (@"C:\WINDOWS\system32"); 1033 WindowsParentFullName ("C:", "C:\\WINDOWS"); 1034 WindowsParentFullName ("C:\\", null); 1035 WindowsParentFullName ("C:\\dir", "C:\\"); 1036 WindowsParentFullName ("C:\\dir\\", "C:\\"); 1037 WindowsParentFullName ("C:\\dir\\dir", "C:\\dir"); 1038 WindowsParentFullName ("C:\\dir\\dir\\", "C:\\dir"); 1039 } 1040 1041 [Test] Parent_Bug77090()1042 public void Parent_Bug77090 () 1043 { 1044 DirectoryInfo di = new DirectoryInfo ("/home"); 1045 if (Path.DirectorySeparatorChar == '\\') { 1046 Assert.IsTrue (di.Parent.Name.EndsWith (":\\"), "#1"); 1047 } else 1048 Assert.AreEqual ("/", di.Parent.Name, "#1"); 1049 Assert.IsNull (di.Parent.Parent, "#2"); 1050 } 1051 1052 [Test] ToStringTest()1053 public void ToStringTest () 1054 { 1055 DirectoryInfo info; 1056 1057 info = new DirectoryInfo ("Test"); 1058 Assert.AreEqual ("Test", info.ToString (), "#1"); 1059 info = new DirectoryInfo (TempFolder + DSC + "ToString.Test"); 1060 Assert.AreEqual (TempFolder + DSC + "ToString.Test", info.ToString ()); 1061 } 1062 1063 [Test] EnumerateFileSystemInfosTest()1064 public void EnumerateFileSystemInfosTest () 1065 { 1066 var dirInfo = new DirectoryInfo (TempFolder); 1067 dirInfo.CreateSubdirectory ("1").CreateSubdirectory ("a"); 1068 dirInfo.CreateSubdirectory ("2").CreateSubdirectory ("b"); 1069 1070 var l = new List<string> (); 1071 foreach (var info in dirInfo.EnumerateFileSystemInfos ("*", SearchOption.AllDirectories)) 1072 l.Add (info.Name); 1073 1074 l.Sort (); 1075 Assert.AreEqual ("1,2,a,b", string.Join (",", l), "#1"); 1076 } 1077 1078 #if !MOBILE 1079 [Test] Serialization()1080 public void Serialization () 1081 { 1082 DirectoryInfo info; 1083 SerializationInfo si; 1084 1085 info = new DirectoryInfo ("Test"); 1086 si = new SerializationInfo (typeof (DirectoryInfo), new FormatterConverter ()); 1087 info.GetObjectData (si, new StreamingContext ()); 1088 1089 Assert.AreEqual (2, si.MemberCount, "#A1"); 1090 Assert.AreEqual ("Test", si.GetString ("OriginalPath"), "#A2"); 1091 Assert.AreEqual (Path.Combine (Directory.GetCurrentDirectory (), "Test"), si.GetString ("FullPath"), "#A3"); 1092 1093 info = new DirectoryInfo (TempFolder); 1094 si = new SerializationInfo (typeof (DirectoryInfo), new FormatterConverter ()); 1095 info.GetObjectData (si, new StreamingContext ()); 1096 1097 Assert.AreEqual (2, si.MemberCount, "#B1"); 1098 Assert.AreEqual (TempFolder, si.GetString ("OriginalPath"), "#B2"); 1099 Assert.AreEqual (TempFolder, si.GetString ("FullPath"), "#B3"); 1100 } 1101 1102 [Test] Deserialization()1103 public void Deserialization () 1104 { 1105 DirectoryInfo info = new DirectoryInfo ("Test"); 1106 1107 MemoryStream ms = new MemoryStream (); 1108 BinaryFormatter bf = new BinaryFormatter (); 1109 bf.Serialize (ms, info); 1110 ms.Position = 0; 1111 1112 DirectoryInfo clone = (DirectoryInfo) bf.Deserialize (ms); 1113 Assert.AreEqual (info.Name, clone.Name, "#1"); 1114 Assert.AreEqual (info.FullName, clone.FullName, "#2"); 1115 } 1116 1117 // Needed so that UnixSymbolicLinkInfo doesn't have to 1118 // be JITted on windows Symlink_helper()1119 private void Symlink_helper () 1120 { 1121 string path = TempFolder + DSC + "DIT.Symlink"; 1122 string dir = path + DSC + "dir"; 1123 string link = path + DSC + "link"; 1124 1125 DeleteDir (path); 1126 1127 try { 1128 Directory.CreateDirectory (path); 1129 Directory.CreateDirectory (dir); 1130 global::Mono.Unix.UnixSymbolicLinkInfo li = new global::Mono.Unix.UnixSymbolicLinkInfo (link); 1131 li.CreateSymbolicLinkTo (dir); 1132 1133 DirectoryInfo info = new DirectoryInfo (path); 1134 DirectoryInfo[] dirs = info.GetDirectories (); 1135 Assert.AreEqual (2, dirs.Length, "#1"); 1136 } finally { 1137 DeleteDir (path); 1138 } 1139 } 1140 1141 [Test] 1142 [Category ("NotDotNet")] Symlink()1143 public void Symlink () 1144 { 1145 // This test only applies to Linux and 1146 // Linux-like platforms but mono-on-windows 1147 // doesn't set the NotDotNet category 1148 if (!RunningOnUnix) { 1149 Assert.Ignore ("Not running on Unix."); 1150 } 1151 1152 Symlink_helper (); 1153 } 1154 #endif 1155 static bool RunningOnUnix { 1156 get { 1157 int p = (int) Environment.OSVersion.Platform; 1158 return ((p == 4) || (p == 128) || (p == 6)); 1159 } 1160 } 1161 WindowsParentFullName(string name, string expected)1162 void WindowsParentFullName (string name, string expected) 1163 { 1164 DirectoryInfo di = new DirectoryInfo (name); 1165 if (di.Parent == null) 1166 Assert.IsNull (expected, name); 1167 else 1168 Assert.AreEqual (expected, di.Parent.FullName, name); 1169 } 1170 CheckName(string name)1171 void CheckName (string name) 1172 { 1173 DirectoryInfo di = new DirectoryInfo (name); 1174 Assert.AreEqual ("share", di.Name, name + ".Name"); 1175 Assert.AreEqual ("usr", di.Parent.Name, name + ".Parent.Name"); 1176 } 1177 DeleteDir(string path)1178 void DeleteDir (string path) 1179 { 1180 if (Directory.Exists (path)) 1181 Directory.Delete (path, true); 1182 } 1183 } 1184 } 1185