1 // DirectoryInfoTest.cs - NUnit Test Cases for System.IO.DirectoryInfo class
4 // Ville Palo (vi64pa@koti.soon.fi)
5 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
12 using System.Collections;
14 using System.Runtime.Serialization;
15 using System.Runtime.Serialization.Formatters.Binary;
17 using NUnit.Framework;
19 namespace MonoTests.System.IO
22 public class DirectoryInfoTest
24 string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
26 static readonly char DSC = Path.DirectorySeparatorChar;
30 protected void SetUp ()
32 current = Directory.GetCurrentDirectory ();
33 if (Directory.Exists (TempFolder))
34 Directory.Delete (TempFolder, true);
35 Directory.CreateDirectory (TempFolder);
39 protected void TearDown ()
41 if (Directory.Exists (TempFolder))
42 Directory.Delete (TempFolder, true);
43 Directory.SetCurrentDirectory (current);
46 [Test] // ctor (String)
47 public void Constructor1 ()
49 string path = TempFolder + DSC + "DIT.Ctr.Test";
52 DirectoryInfo info = new DirectoryInfo (path);
53 Assert.AreEqual ("DIT.Ctr.Test", info.Name, "#1");
54 Assert.IsFalse (info.Exists, "#2");
55 Assert.AreEqual (".Test", info.Extension, "#3");
56 Assert.AreEqual ("DIT.Ctr.Test", info.Name, "#4");
59 [Test] // ctor (String)
60 public void Constructor1_Path_Null ()
63 new DirectoryInfo (null);
65 } catch (ArgumentNullException ex) {
66 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
67 Assert.IsNull (ex.InnerException, "#3");
68 Assert.IsNotNull (ex.Message, "#4");
69 Assert.AreEqual ("path", ex.ParamName, "#5");
73 [Test] // ctor (String)
74 public void Constructor1_Path_Empty ()
77 new DirectoryInfo (string.Empty);
79 } catch (ArgumentException ex) {
80 // Empty file name is not legal
81 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
82 Assert.IsNull (ex.InnerException, "#3");
83 Assert.IsNotNull (ex.Message, "#4");
84 Assert.IsNull (ex.ParamName, "#5");
88 [Test] // ctor (String)
89 public void Constructor1_Path_Whitespace ()
92 new DirectoryInfo (" ");
94 } catch (ArgumentException ex) {
95 // The path is not of a legal form
96 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
97 Assert.IsNull (ex.InnerException, "#3");
98 Assert.IsNotNull (ex.Message, "#4");
99 Assert.IsNull (ex.ParamName, "#5");
103 [Test] // ctor (String)
104 public void Constructor1_Path_InvalidPathChars ()
106 string path = string.Empty;
107 foreach (char c in Path.InvalidPathChars)
110 new DirectoryInfo (path);
112 } catch (ArgumentException ex) {
113 // The path contains illegal characters
114 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
115 Assert.IsNull (ex.InnerException, "#3");
116 Assert.IsNotNull (ex.Message, "#4");
117 Assert.IsNull (ex.ParamName, "#5");
122 public void Exists ()
124 string path = TempFolder + DSC + "DIT.Exists.Test";
128 DirectoryInfo info = new DirectoryInfo (path);
129 Assert.IsFalse (info.Exists, "#1");
131 Directory.CreateDirectory (path);
132 Assert.IsFalse (info.Exists, "#2");
133 info = new DirectoryInfo (path);
134 Assert.IsTrue (info.Exists, "#3");
143 string path = TempFolder + DSC + "DIT.Name.Test";
147 DirectoryInfo info = new DirectoryInfo (path);
148 Assert.AreEqual ("DIT.Name.Test", info.Name, "#1");
150 info = Directory.CreateDirectory (path);
151 Assert.AreEqual ("DIT.Name.Test", info.Name, "#2");
153 info = Directory.CreateDirectory ("whatever");
154 Assert.AreEqual ("whatever", info.Name, "#3");
157 info = new DirectoryInfo ("/");
158 Assert.AreEqual ("/", info.Name, "#4");
160 info = new DirectoryInfo ("test/");
161 Assert.AreEqual ("test", info.Name, "#5");
163 info = new DirectoryInfo ("/test");
164 Assert.AreEqual ("test", info.Name, "#4");
166 info = new DirectoryInfo ("/test/");
167 Assert.AreEqual ("test", info.Name, "#4");
169 info = new DirectoryInfo (@"c:");
170 Assert.AreEqual (@"C:\", info.Name, "#4");
172 info = new DirectoryInfo (@"c:\");
173 Assert.AreEqual (@"c:\", info.Name, "#5");
175 info = new DirectoryInfo (@"c:\test");
176 Assert.AreEqual ("test", info.Name, "#6");
178 info = new DirectoryInfo (@"c:\test\");
179 Assert.AreEqual ("test", info.Name, "#7");
187 public void Parent ()
189 string path = TempFolder + DSC + "DIT.Parent.Test";
193 DirectoryInfo info = new DirectoryInfo (path);
194 Assert.AreEqual ("MonoTests.System.IO.Tests", info.Parent.Name, "#1");
196 info = Directory.CreateDirectory (path);
197 Assert.AreEqual ("MonoTests.System.IO.Tests", info.Parent.Name, "#2");
199 info = new DirectoryInfo ("test");
200 Assert.AreEqual (Directory.GetCurrentDirectory (), info.Parent.FullName, "#3");
203 info = new DirectoryInfo ("/");
204 Assert.IsNull (info.Parent, "#4");
206 info = new DirectoryInfo ("test/");
207 Assert.IsNotNull (info.Parent, "#5a");
208 Assert.AreEqual (Directory.GetCurrentDirectory (), info.Parent.FullName, "#5b");
210 info = new DirectoryInfo ("/test");
211 Assert.IsNotNull (info.Parent, "#6a");
212 Assert.AreEqual ("/", info.Parent.FullName, "#6b");
214 info = new DirectoryInfo ("/test/");
215 Assert.IsNotNull (info.Parent, "#7a");
216 Assert.AreEqual ("/", info.Parent.FullName, "#7b");
218 info = new DirectoryInfo (@"c:");
219 Assert.IsNull (info.Parent, "#4");
221 info = new DirectoryInfo (@"c:\");
222 Assert.IsNull (info.Parent, "#5");
224 info = new DirectoryInfo (@"c:\test");
225 Assert.IsNotNull (info.Parent, "#6a");
226 Assert.AreEqual (@"c:\", info.Parent.FullName, "#6b");
228 info = new DirectoryInfo (@"c:\test\");
229 Assert.IsNotNull (info.Parent, "#7a");
230 Assert.AreEqual (@"c:\", info.Parent.FullName, "#7b");
238 public void Create ()
240 string path = TempFolder + DSC + "DIT.Create.Test";
244 DirectoryInfo info = new DirectoryInfo (path);
245 Assert.IsFalse (info.Exists, "#1");
247 Assert.IsFalse (info.Exists, "#2");
248 info = new DirectoryInfo (path);
249 Assert.IsTrue (info.Exists, "#3");
256 public void CreateSubdirectory ()
258 string sub_path = Path.Combine ("test01", "test02");
260 DirectoryInfo info = new DirectoryInfo (TempFolder);
261 DirectoryInfo sub = info.CreateSubdirectory (sub_path);
262 Assert.IsNotNull (sub, "#1");
263 Assert.AreEqual (Path.Combine (TempFolder, sub_path), sub.FullName, "#2");
264 Assert.IsTrue (Directory.Exists (sub.FullName), "#3");
266 DeleteDir (Path.Combine (TempFolder, sub_path));
271 public void CreateSubdirectory_Path_Null ()
273 DirectoryInfo info = new DirectoryInfo (TempFolder);
275 info.CreateSubdirectory (null);
277 } catch (ArgumentNullException ex) {
278 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
279 Assert.IsNull (ex.InnerException, "#3");
280 Assert.IsNotNull (ex.Message, "#4");
281 Assert.AreEqual ("path", ex.ParamName, "#5");
286 public void CreateSubdirectory_Path_Empty ()
288 DirectoryInfo info = new DirectoryInfo (TempFolder);
290 info.CreateSubdirectory (string.Empty);
292 } catch (ArgumentException ex) {
293 // Empty file name is not legal
294 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
295 Assert.IsNull (ex.InnerException, "#3");
296 Assert.IsNotNull (ex.Message, "#4");
301 public void Delete1 ()
303 string path = TempFolder + DSC + "DIT.Delete1.Test";
307 Directory.CreateDirectory (path);
308 DirectoryInfo info = new DirectoryInfo (path);
309 Assert.IsTrue (info.Exists, "#1");
312 Assert.IsTrue (info.Exists, "#2");
314 info = new DirectoryInfo (path);
315 Assert.IsFalse (info.Exists, "#3");
322 public void Delete1_DirectoryNotEmpty ()
324 string path = TempFolder + DSC + "DIT.DeleteIOException1.Test";
328 Directory.CreateDirectory (path);
329 File.Create (path + DSC + "test").Close ();
330 DirectoryInfo info = new DirectoryInfo (path);
334 } catch (IOException ex) {
335 // The directory is not empty
336 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
337 Assert.IsNull (ex.InnerException, "#3");
338 Assert.IsNotNull (ex.Message, "#4");
345 [Test] // Delete (Boolean)
346 public void Delete2 ()
348 string path = TempFolder + DSC + "DIT.Delete2.Test";
352 Directory.CreateDirectory (path);
353 File.Create (path + DSC + "test").Close ();
354 DirectoryInfo info = new DirectoryInfo (path);
355 Assert.IsTrue (info.Exists, "#1");
358 Assert.IsTrue (info.Exists, "#2");
360 info = new DirectoryInfo (path);
361 Assert.IsFalse (info.Exists, "#3");
367 [Test] // Delete (Boolean)
368 public void Delete2_DirectoryNotEmpty ()
370 string path = TempFolder + DSC + "DIT.DeleteIOException2.Test";
374 Directory.CreateDirectory (path);
375 File.Create (path + DSC + "test").Close ();
376 DirectoryInfo info = new DirectoryInfo (path);
380 } catch (IOException ex) {
381 // The directory is not empty
382 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
383 Assert.IsNull (ex.InnerException, "#3");
384 Assert.IsNotNull (ex.Message, "#4");
392 public void FullName ()
394 DirectoryInfo di = new DirectoryInfo ("something");
395 Assert.IsFalse (di.Exists, "#A1");
396 Assert.AreEqual (Path.Combine (Directory.GetCurrentDirectory (), "something"), di.FullName, "#A2");
398 di = new DirectoryInfo ("something" + Path.DirectorySeparatorChar);
399 Assert.IsFalse (di.Exists, "#B1");
400 Assert.AreEqual (Path.DirectorySeparatorChar, di.FullName [di.FullName.Length - 1], "#B2");
402 di = new DirectoryInfo ("something" + Path.AltDirectorySeparatorChar);
403 Assert.IsFalse (di.Exists, "#C1");
404 Assert.AreEqual (Path.DirectorySeparatorChar, di.FullName [di.FullName.Length - 1], "#C2");
407 di = new DirectoryInfo ("/");
408 Assert.AreEqual ("/", di.FullName, "#D1");
410 di = new DirectoryInfo ("test/");
411 Assert.AreEqual (Path.Combine (Directory.GetCurrentDirectory (), "test/"), di.FullName, "#D2");
413 di = new DirectoryInfo ("/test");
414 Assert.AreEqual ("/test", di.FullName, "#D3");
416 di = new DirectoryInfo ("/test/");
417 Assert.AreEqual ("/test/", di.FullName, "#D4");
419 di = new DirectoryInfo (@"c:");
420 Assert.AreEqual (@"C:\", di.FullName, "#D1");
422 di = new DirectoryInfo (@"c:\");
423 Assert.AreEqual (@"c:\", di.FullName, "#D2");
425 di = new DirectoryInfo (@"c:\test");
426 Assert.AreEqual (@"c:\test", di.FullName, "#D3");
428 di = new DirectoryInfo (@"c:\test\");
429 Assert.AreEqual (@"c:\test\", di.FullName, "#D4");
434 public void FullName_RootDirectory ()
436 DirectoryInfo di = new DirectoryInfo (String.Empty + Path.DirectorySeparatorChar);
438 // can't be sure of the root drive under windows
439 Assert.AreEqual ("/", di.FullName, "#1");
441 Assert.IsNull (di.Parent, "#2");
443 di = new DirectoryInfo (String.Empty + Path.AltDirectorySeparatorChar);
445 // can't be sure of the root drive under windows
446 Assert.AreEqual ("/", di.FullName, "#3");
448 Assert.IsNull (di.Parent, "#4");
451 [Test] // GetDirectories ()
452 public void GetDirectories1 ()
454 string path = TempFolder + DSC + "DIT.GetDirectories1.Test";
457 DirectoryInfo info = Directory.CreateDirectory (path);
458 Assert.AreEqual (0, info.GetDirectories ().Length, "#1");
460 Directory.CreateDirectory (path + DSC + "1");
461 Directory.CreateDirectory (path + DSC + "2");
462 File.Create (path + DSC + "filetest").Close ();
463 Assert.AreEqual (2, info.GetDirectories ().Length, "#2");
465 Directory.Delete (path + DSC + 2);
466 Assert.AreEqual (1, info.GetDirectories ().Length, "#3");
472 [Test] // GetDirectories ()
473 public void GetDirectories1_DirectoryDoesNotExist ()
475 string path = TempFolder + DSC + "DIT.GetDirectoriesDirectoryNotFoundException1.Test";
479 DirectoryInfo info = new DirectoryInfo (path);
480 info.GetDirectories ();
482 } catch (DirectoryNotFoundException ex) {
483 // Could not find a part of '...'
484 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
485 Assert.IsNull (ex.InnerException, "#3");
486 Assert.IsNotNull (ex.Message, "#4");
487 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
493 [Test] // GetDirectories (String)
494 public void GetDirectories2 ()
496 string path = TempFolder + DSC + "DIT.GetDirectories2.Test";
499 DirectoryInfo info = Directory.CreateDirectory (path);
500 Assert.AreEqual (0, info.GetDirectories ("*").Length, "#1");
502 Directory.CreateDirectory (path + DSC + "test120");
503 Directory.CreateDirectory (path + DSC + "test210");
504 Directory.CreateDirectory (path + DSC + "atest330");
505 Directory.CreateDirectory (path + DSC + "test220");
506 Directory.CreateDirectory (path + DSC + "rest");
507 Directory.CreateDirectory (path + DSC + "rest" + DSC + "subdir");
508 File.Create (path + DSC + "filetest").Close ();
510 Assert.AreEqual (5, info.GetDirectories ("*").Length, "#2");
511 Assert.AreEqual (3, info.GetDirectories ("test*").Length, "#3");
512 Assert.AreEqual (2, info.GetDirectories ("test?20").Length, "#4");
513 Assert.AreEqual (0, info.GetDirectories ("test?").Length, "#5");
514 Assert.AreEqual (0, info.GetDirectories ("test[12]*").Length, "#6");
515 Assert.AreEqual (2, info.GetDirectories ("test2*0").Length, "#7");
516 Assert.AreEqual (4, info.GetDirectories ("*test*").Length, "#8");
518 Assert.AreEqual (6, info.GetDirectories ("*", SearchOption.AllDirectories).Length, "#9");
525 [Test] // GetDirectories (String)
526 public void GetDirectories2_DirectoryDoesNotExist ()
528 string path = TempFolder + DSC + "DIT.GetDirectoriesDirectoryNotFoundException2.Test";
532 DirectoryInfo info = new DirectoryInfo (path);
533 info.GetDirectories ("*");
535 } catch (DirectoryNotFoundException ex) {
536 // Could not find a part of '...'
537 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
538 Assert.IsNull (ex.InnerException, "#3");
539 Assert.IsNotNull (ex.Message, "#4");
540 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
546 [Test] // GetDirectories (String)
547 public void GetDirectories2_SearchPattern_Null ()
549 DirectoryInfo info = new DirectoryInfo (TempFolder);
551 info.GetDirectories (null);
553 } catch (ArgumentNullException ex) {
554 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
555 Assert.IsNull (ex.InnerException, "#3");
556 Assert.IsNotNull (ex.Message, "#4");
557 Assert.AreEqual ("searchPattern", ex.ParamName, "#5");
561 [Test] //GetDirectories (String, SearchOptions)
562 public void GetDirectiories_SearchOptionAllDirectories ()
564 string directoryToBeLookedFor = "lookforme";
565 DirectoryInfo baseDir = Directory.CreateDirectory(Path.Combine(TempFolder, "GetDirectiories_SearchOptionAllDirectories"));
566 DirectoryInfo subdir = baseDir.CreateSubdirectory("subdir");
567 DirectoryInfo subsubdir = subdir.CreateSubdirectory(directoryToBeLookedFor);
568 DirectoryInfo[] directoriesFound = baseDir.GetDirectories(directoryToBeLookedFor, SearchOption.AllDirectories);
569 Assert.AreEqual(1, directoriesFound.Length, "There should be exactly one directory with the specified name.");
570 Assert.AreEqual(directoryToBeLookedFor, directoriesFound[0].Name, "The name of the directory found should match the expected one.");
574 [Test] // GetDirectories (String, SearchOption)
575 public void GetDirectories3_SearchPattern_Null ()
577 DirectoryInfo info = new DirectoryInfo (TempFolder);
579 info.GetDirectories (null, SearchOption.AllDirectories);
581 } catch (ArgumentNullException ex) {
582 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
583 Assert.IsNull (ex.InnerException, "#3");
584 Assert.IsNotNull (ex.Message, "#4");
585 Assert.AreEqual ("searchPattern", ex.ParamName, "#5");
590 [Test] // GetFiles ()
591 public void GetFiles1 ()
593 string path = TempFolder + DSC + "DIT.GetFiles1.Test";
597 DirectoryInfo info = Directory.CreateDirectory (path);
598 Assert.AreEqual (0, info.GetFiles ().Length, "#1");
599 File.Create (path + DSC + "file1").Close ();
600 File.Create (path + DSC + "file2").Close ();
601 Directory.CreateDirectory (path + DSC + "directory1");
602 Assert.AreEqual (2, info.GetFiles ().Length, "#2");
608 [Test] // GetFiles ()
609 public void GetFiles1_DirectoryDoesNotExist ()
611 string path = TempFolder + DSC + "DIT.GetFilesDirectoryNotFoundException1.Test";
615 DirectoryInfo info = new DirectoryInfo (path);
618 } catch (DirectoryNotFoundException ex) {
619 // Could not find a part of '...'
620 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
621 Assert.IsNull (ex.InnerException, "#3");
622 Assert.IsNotNull (ex.Message, "#4");
623 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
629 [Test] // GetFiles (String)
630 public void GetFiles2 ()
632 string path = TempFolder + DSC + "DIT.GetFiles2.Test";
636 DirectoryInfo info = Directory.CreateDirectory (path);
637 Assert.AreEqual (0, info.GetFiles ("*").Length, "#1");
638 File.Create (path + DSC + "file120file").Close ();
639 File.Create (path + DSC + "file220file").Close ();
640 File.Create (path + DSC + "afile330file").Close ();
641 File.Create (path + DSC + "test.abc").Close ();
642 File.Create (path + DSC + "test.abcd").Close ();
643 File.Create (path + DSC + "test.abcdef").Close ();
644 Directory.CreateDirectory (path + DSC + "dir");
646 Assert.AreEqual (6, info.GetFiles ("*").Length, "#2");
647 Assert.AreEqual (2, info.GetFiles ("file*file").Length, "#3");
648 Assert.AreEqual (3, info.GetFiles ("*file*").Length, "#4");
649 Assert.AreEqual (2, info.GetFiles ("file?20file").Length, "#5");
650 Assert.AreEqual (1, info.GetFiles ("*.abcd").Length, "#6");
651 Assert.AreEqual (2, info.GetFiles ("*.abcd*").Length, "#7");
657 [Test] // GetFiles (String)
658 public void GetFiles2_DirectoryDoesNotExist ()
660 string path = TempFolder + DSC + "DIT.GetFilesDirectoryNotFoundException2.Test";
664 DirectoryInfo info = new DirectoryInfo (path);
667 } catch (DirectoryNotFoundException ex) {
668 // Could not find a part of '...'
669 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
670 Assert.IsNull (ex.InnerException, "#3");
671 Assert.IsNotNull (ex.Message, "#4");
672 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
678 [Test] // GetFiles (String)
679 public void GetFiles2_SearchPattern_Null ()
681 DirectoryInfo info = new DirectoryInfo (TempFolder);
683 info.GetFiles (null);
685 } catch (ArgumentNullException ex) {
686 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
687 Assert.IsNull (ex.InnerException, "#3");
688 Assert.IsNotNull (ex.Message, "#4");
689 Assert.AreEqual ("searchPattern", ex.ParamName, "#5");
694 [Test] // GetFiles (String, SearchOption)
695 public void GetFiles3_SearchPattern_Null ()
697 DirectoryInfo info = new DirectoryInfo (TempFolder);
699 info.GetFiles (null, SearchOption.TopDirectoryOnly);
701 } catch (ArgumentNullException ex) {
702 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
703 Assert.IsNull (ex.InnerException, "#3");
704 Assert.IsNotNull (ex.Message, "#4");
705 Assert.AreEqual ("searchPattern", ex.ParamName, "#5");
711 public void GetFileSystemInfos2_SearchPattern_Null ()
713 DirectoryInfo info = new DirectoryInfo (TempFolder);
715 info.GetFileSystemInfos (null);
717 } catch (ArgumentNullException ex) {
718 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
719 Assert.IsNull (ex.InnerException, "#3");
720 Assert.IsNotNull (ex.Message, "#4");
721 Assert.AreEqual ("searchPattern", ex.ParamName, "#5");
726 public void MoveTo ()
728 string path1 = TempFolder + DSC + "DIT.MoveTo.Soucre.Test";
729 string path2 = TempFolder + DSC + "DIT.MoveTo.Dest.Test";
734 DirectoryInfo info1 = Directory.CreateDirectory (path1);
735 DirectoryInfo info2 = new DirectoryInfo (path2);
737 Assert.IsTrue (info1.Exists, "#A1");
738 Assert.IsFalse (info2.Exists, "#A2");
740 info1.MoveTo (path2);
741 Assert.IsTrue (info1.Exists, "#B1");
742 Assert.IsFalse (info2.Exists, "#B2");
744 info1 = new DirectoryInfo (path1);
745 info2 = new DirectoryInfo (path2);
746 Assert.IsFalse (info1.Exists, "#C1");
747 Assert.IsTrue (info2.Exists, "#C2");
755 public void MoveTo_DestDirName_Empty ()
757 string path = TempFolder + DSC + "DIT.MoveToArgumentException1.Test";
761 DirectoryInfo info = Directory.CreateDirectory (path);
763 info.MoveTo (string.Empty);
765 } catch (ArgumentException ex) {
766 // Empty file name is not legal
767 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
768 Assert.IsNull (ex.InnerException, "#3");
769 Assert.IsNotNull (ex.Message, "#4");
770 Assert.AreEqual ("destDirName", ex.ParamName, "#5");
778 public void MoveTo_DestDirName_Null ()
780 string path = TempFolder + DSC + "DIT.MoveToArgumentNullException.Test";
784 DirectoryInfo info = Directory.CreateDirectory (path);
788 } catch (ArgumentNullException ex) {
789 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
790 Assert.IsNull (ex.InnerException, "#3");
791 Assert.IsNotNull (ex.Message, "#4");
792 Assert.AreEqual ("destDirName", ex.ParamName, "#5");
800 public void MoveTo_DestDirName_InvalidPathChars ()
802 string path = TempFolder + DSC + "DIT.MoveToArgumentException3.Test";
806 DirectoryInfo info = Directory.CreateDirectory (path);
808 info.MoveTo (Path.InvalidPathChars [0].ToString ());
810 } catch (ArgumentException ex) {
811 // The path contains illegal characters
812 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
813 Assert.IsNull (ex.InnerException, "#3");
814 Assert.IsNotNull (ex.Message, "#4");
815 Assert.IsNull (ex.ParamName, "#5");
823 public void MoveTo_DestDirName_Whitespace ()
825 string path = TempFolder + DSC + "DIT.MoveToArgumentException2.Test";
829 DirectoryInfo info = Directory.CreateDirectory (path);
833 } catch (ArgumentException ex) {
834 // The path is not of a legal form
835 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
836 Assert.IsNull (ex.InnerException, "#3");
837 Assert.IsNotNull (ex.Message, "#4");
838 Assert.IsNull (ex.ParamName, "#5");
846 public void MoveTo_SourceDest_NotDifferent ()
848 string path = TempFolder + DSC + "DIT.MoveToIOException1.Test";
852 DirectoryInfo info = Directory.CreateDirectory (path);
856 } catch (IOException ex) {
857 // Source and destination path must be different
858 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
859 Assert.IsNull (ex.InnerException, "#A3");
860 Assert.IsNotNull (ex.Message, "#A4");
867 DirectoryInfo info = new DirectoryInfo (path);
871 } catch (IOException ex) {
872 // Source and destination path must be different
873 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
874 Assert.IsNull (ex.InnerException, "#B3");
875 Assert.IsNotNull (ex.Message, "#B4");
883 public void MoveTo_UpdateProperties ()
885 string path = TempFolder + DSC + "DIT.MoveToUpdateProperties.Test";
886 string path2 = TempFolder + DSC + "DIT.MoveToUpdateProperties2.Test";
887 string path3 = path2 + DSC + "DIT.MoveToUpdateProperties3.Test";
889 Directory.CreateDirectory (path);
890 Directory.CreateDirectory (path2);
892 DirectoryInfo info = new DirectoryInfo(path);
894 Assert.IsTrue (Directory.Exists(info.FullName));
895 Assert.AreEqual (path, info.FullName);
896 Assert.AreEqual ("DIT.MoveToUpdateProperties.Test", info.Name);
897 Assert.AreEqual (TempFolder, info.Parent.FullName);
898 Assert.AreEqual (path, info.ToString ());
901 Assert.IsTrue (Directory.Exists(info.FullName));
902 Assert.AreEqual (path3, info.FullName);
903 Assert.AreEqual ("DIT.MoveToUpdateProperties3.Test", info.Name);
904 Assert.AreEqual (path2, info.Parent.FullName);
905 Assert.AreEqual (path3, info.ToString ());
909 public void DirectoryNameWithSpace ()
911 DeleteDir ("this has a space at the end ");
912 string path = Path.Combine (TempFolder, "this has a space at the end ");
913 Directory.CreateDirectory (path);
914 DirectoryInfo i = new DirectoryInfo (path);
916 foreach (FileInfo f in i.GetFiles ())
921 public void LastWriteTime ()
923 DirectoryInfo info = new DirectoryInfo (TempFolder);
924 info.LastWriteTime = new DateTime (2003, 6, 4, 6, 4, 0);
926 DateTime time = Directory.GetLastWriteTime (TempFolder);
927 Assert.AreEqual (2003, time.Year, "#A1");
928 Assert.AreEqual (6, time.Month, "#A2");
929 Assert.AreEqual (4, time.Day, "#A3");
930 Assert.AreEqual (6, time.Hour, "#A4");
931 Assert.AreEqual (4, time.Minute, "#A5");
932 Assert.AreEqual (0, time.Second, "#A6");
934 time = TimeZone.CurrentTimeZone.ToLocalTime (
935 Directory.GetLastWriteTimeUtc (TempFolder));
936 Assert.AreEqual (2003, time.Year, "#B1");
937 Assert.AreEqual (6, time.Month, "#B2");
938 Assert.AreEqual (4, time.Day, "#B3");
939 Assert.AreEqual (6, time.Hour, "#B4");
940 Assert.AreEqual (4, time.Minute, "#B5");
941 Assert.AreEqual (0, time.Second, "#B6");
945 public void LastWriteTimeUtc ()
947 DirectoryInfo info = new DirectoryInfo (TempFolder);
948 info.LastWriteTimeUtc = new DateTime (2003, 6, 4, 6, 4, 0);
950 DateTime time = TimeZone.CurrentTimeZone.ToUniversalTime (
951 Directory.GetLastWriteTime (TempFolder));
952 Assert.AreEqual (2003, time.Year, "#A1");
953 Assert.AreEqual (6, time.Month, "#A2");
954 Assert.AreEqual (4, time.Day, "#A3");
955 Assert.AreEqual (6, time.Hour, "#A4");
956 Assert.AreEqual (4, time.Minute, "#A5");
957 Assert.AreEqual (0, time.Second, "#A6");
959 time = Directory.GetLastWriteTimeUtc (TempFolder);
960 Assert.AreEqual (2003, time.Year, "#B1");
961 Assert.AreEqual (6, time.Month, "#B2");
962 Assert.AreEqual (4, time.Day, "#B3");
963 Assert.AreEqual (6, time.Hour, "#B4");
964 Assert.AreEqual (4, time.Minute, "#B5");
965 Assert.AreEqual (0, time.Second, "#B6");
969 [Category("TargetJvmNotSupported")] // LastAccessTime not supported for TARGET_JVM
970 public void LastAccessTime ()
972 DirectoryInfo info = new DirectoryInfo (TempFolder);
973 info.LastAccessTime = DateTime.Now;
977 [Category("TargetJvmNotSupported")] // LastAccessTime not supported for TARGET_JVM
978 public void LastAccessTimeUtc ()
980 DirectoryInfo info = new DirectoryInfo (TempFolder);
981 info.LastAccessTimeUtc = DateTime.Now;
985 [Category("TargetJvmNotSupported")] // CreationTime not supported for TARGET_JVM
986 public void CreationTime ()
988 DirectoryInfo info = new DirectoryInfo (TempFolder);
989 info.CreationTime = DateTime.Now;
993 [Category("TargetJvmNotSupported")] // CreationTime not supported for TARGET_JVM
994 public void CreationTimeUtc ()
996 DirectoryInfo info = new DirectoryInfo (TempFolder);
997 info.CreationTimeUtc = DateTime.Now;
1001 public void Name_Bug76903 ()
1003 CheckName ("/usr/share");
1004 CheckName ("/usr/share/");
1005 CheckName ("/usr/share/.");
1006 CheckName ("/usr/share/./");
1007 CheckName ("/usr/share/blabla/../");
1008 CheckName ("/usr/lib/../share/.");
1012 public void Hang_76191 ()
1014 // from bug #76191 (hangs on Windows)
1015 DirectoryInfo di = new DirectoryInfo (Environment.CurrentDirectory);
1016 Stack s = new Stack ();
1018 while (di.Parent != null) {
1022 while (s.Count > 0) {
1023 di = (DirectoryInfo) s.Pop ();
1024 Assert.IsTrue (di.Exists, di.Name);
1029 public void WindowsSystem32_76191 ()
1034 Directory.SetCurrentDirectory (@"C:\WINDOWS\system32");
1035 WindowsParentFullName ("C:", "C:\\WINDOWS");
1036 WindowsParentFullName ("C:\\", null);
1037 WindowsParentFullName ("C:\\dir", "C:\\");
1038 WindowsParentFullName ("C:\\dir\\", "C:\\");
1039 WindowsParentFullName ("C:\\dir\\dir", "C:\\dir");
1040 WindowsParentFullName ("C:\\dir\\dir\\", "C:\\dir");
1044 public void Parent_Bug77090 ()
1046 DirectoryInfo di = new DirectoryInfo ("/home");
1047 if (Path.DirectorySeparatorChar == '\\') {
1048 Assert.IsTrue (di.Parent.Name.EndsWith (":\\"), "#1");
1050 Assert.AreEqual ("/", di.Parent.Name, "#1");
1051 Assert.IsNull (di.Parent.Parent, "#2");
1055 public void ToStringTest ()
1059 info = new DirectoryInfo ("Test");
1060 Assert.AreEqual ("Test", info.ToString (), "#1");
1061 info = new DirectoryInfo (TempFolder + DSC + "ToString.Test");
1062 Assert.AreEqual (TempFolder + DSC + "ToString.Test", info.ToString ());
1066 public void Serialization ()
1069 SerializationInfo si;
1071 info = new DirectoryInfo ("Test");
1072 si = new SerializationInfo (typeof (DirectoryInfo), new FormatterConverter ());
1073 info.GetObjectData (si, new StreamingContext ());
1075 Assert.AreEqual (2, si.MemberCount, "#A1");
1076 Assert.AreEqual ("Test", si.GetString ("OriginalPath"), "#A2");
1077 Assert.AreEqual (Path.Combine (Directory.GetCurrentDirectory (), "Test"), si.GetString ("FullPath"), "#A3");
1079 info = new DirectoryInfo (TempFolder);
1080 si = new SerializationInfo (typeof (DirectoryInfo), new FormatterConverter ());
1081 info.GetObjectData (si, new StreamingContext ());
1083 Assert.AreEqual (2, si.MemberCount, "#B1");
1084 Assert.AreEqual (TempFolder, si.GetString ("OriginalPath"), "#B2");
1085 Assert.AreEqual (TempFolder, si.GetString ("FullPath"), "#B3");
1089 public void Deserialization ()
1091 DirectoryInfo info = new DirectoryInfo ("Test");
1093 MemoryStream ms = new MemoryStream ();
1094 BinaryFormatter bf = new BinaryFormatter ();
1095 bf.Serialize (ms, info);
1098 DirectoryInfo clone = (DirectoryInfo) bf.Deserialize (ms);
1099 Assert.AreEqual (info.Name, clone.Name, "#1");
1100 Assert.AreEqual (info.FullName, clone.FullName, "#2");
1103 // Needed so that UnixSymbolicLinkInfo doesn't have to
1104 // be JITted on windows
1105 private void Symlink_helper ()
1107 string path = TempFolder + DSC + "DIT.Symlink";
1108 string dir = path + DSC + "dir";
1109 string link = path + DSC + "link";
1114 Directory.CreateDirectory (path);
1115 Directory.CreateDirectory (dir);
1116 Mono.Unix.UnixSymbolicLinkInfo li = new Mono.Unix.UnixSymbolicLinkInfo (link);
1117 li.CreateSymbolicLinkTo (dir);
1119 DirectoryInfo info = new DirectoryInfo (path);
1120 DirectoryInfo[] dirs = info.GetDirectories ();
1121 Assert.AreEqual (2, dirs.Length, "#1");
1128 [Category ("NotDotNet")]
1129 public void Symlink ()
1131 // This test only applies to Linux and
1132 // Linux-like platforms but mono-on-windows
1133 // doesn't set the NotDotNet category
1134 if (!RunningOnUnix) {
1141 static bool RunningOnUnix {
1143 int p = (int) Environment.OSVersion.Platform;
1144 return ((p == 4) || (p == 128) || (p == 6));
1148 void WindowsParentFullName (string name, string expected)
1150 DirectoryInfo di = new DirectoryInfo (name);
1151 if (di.Parent == null)
1152 Assert.IsNull (expected, name);
1154 Assert.AreEqual (expected, di.Parent.FullName, name);
1157 void CheckName (string name)
1159 DirectoryInfo di = new DirectoryInfo (name);
1160 Assert.AreEqual ("share", di.Name, name + ".Name");
1161 Assert.AreEqual ("usr", di.Parent.Name, name + ".Parent.Name");
1164 void DeleteDir (string path)
1166 if (Directory.Exists (path))
1167 Directory.Delete (path, true);