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");
562 [Test] // GetDirectories (String, SearchOption)
563 public void GetDirectories3_SearchPattern_Null ()
565 DirectoryInfo info = new DirectoryInfo (TempFolder);
567 info.GetDirectories (null, SearchOption.AllDirectories);
569 } catch (ArgumentNullException ex) {
570 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
571 Assert.IsNull (ex.InnerException, "#3");
572 Assert.IsNotNull (ex.Message, "#4");
573 Assert.AreEqual ("searchPattern", ex.ParamName, "#5");
578 [Test] // GetFiles ()
579 public void GetFiles1 ()
581 string path = TempFolder + DSC + "DIT.GetFiles1.Test";
585 DirectoryInfo info = Directory.CreateDirectory (path);
586 Assert.AreEqual (0, info.GetFiles ().Length, "#1");
587 File.Create (path + DSC + "file1").Close ();
588 File.Create (path + DSC + "file2").Close ();
589 Directory.CreateDirectory (path + DSC + "directory1");
590 Assert.AreEqual (2, info.GetFiles ().Length, "#2");
596 [Test] // GetFiles ()
597 public void GetFiles1_DirectoryDoesNotExist ()
599 string path = TempFolder + DSC + "DIT.GetFilesDirectoryNotFoundException1.Test";
603 DirectoryInfo info = new DirectoryInfo (path);
606 } catch (DirectoryNotFoundException ex) {
607 // Could not find a part of '...'
608 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
609 Assert.IsNull (ex.InnerException, "#3");
610 Assert.IsNotNull (ex.Message, "#4");
611 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
617 [Test] // GetFiles (String)
618 public void GetFiles2 ()
620 string path = TempFolder + DSC + "DIT.GetFiles2.Test";
624 DirectoryInfo info = Directory.CreateDirectory (path);
625 Assert.AreEqual (0, info.GetFiles ("*").Length, "#1");
626 File.Create (path + DSC + "file120file").Close ();
627 File.Create (path + DSC + "file220file").Close ();
628 File.Create (path + DSC + "afile330file").Close ();
629 File.Create (path + DSC + "test.abc").Close ();
630 File.Create (path + DSC + "test.abcd").Close ();
631 File.Create (path + DSC + "test.abcdef").Close ();
632 Directory.CreateDirectory (path + DSC + "dir");
634 Assert.AreEqual (6, info.GetFiles ("*").Length, "#2");
635 Assert.AreEqual (2, info.GetFiles ("file*file").Length, "#3");
636 Assert.AreEqual (3, info.GetFiles ("*file*").Length, "#4");
637 Assert.AreEqual (2, info.GetFiles ("file?20file").Length, "#5");
638 Assert.AreEqual (1, info.GetFiles ("*.abcd").Length, "#6");
639 Assert.AreEqual (2, info.GetFiles ("*.abcd*").Length, "#7");
645 [Test] // GetFiles (String)
646 public void GetFiles2_DirectoryDoesNotExist ()
648 string path = TempFolder + DSC + "DIT.GetFilesDirectoryNotFoundException2.Test";
652 DirectoryInfo info = new DirectoryInfo (path);
655 } catch (DirectoryNotFoundException ex) {
656 // Could not find a part of '...'
657 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
658 Assert.IsNull (ex.InnerException, "#3");
659 Assert.IsNotNull (ex.Message, "#4");
660 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
666 [Test] // GetFiles (String)
667 public void GetFiles2_SearchPattern_Null ()
669 DirectoryInfo info = new DirectoryInfo (TempFolder);
671 info.GetFiles (null);
673 } catch (ArgumentNullException ex) {
674 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
675 Assert.IsNull (ex.InnerException, "#3");
676 Assert.IsNotNull (ex.Message, "#4");
677 Assert.AreEqual ("searchPattern", ex.ParamName, "#5");
682 [Test] // GetFiles (String, SearchOption)
683 public void GetFiles3_SearchPattern_Null ()
685 DirectoryInfo info = new DirectoryInfo (TempFolder);
687 info.GetFiles (null, SearchOption.TopDirectoryOnly);
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");
699 public void GetFileSystemInfos2_SearchPattern_Null ()
701 DirectoryInfo info = new DirectoryInfo (TempFolder);
703 info.GetFileSystemInfos (null);
705 } catch (ArgumentNullException ex) {
706 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
707 Assert.IsNull (ex.InnerException, "#3");
708 Assert.IsNotNull (ex.Message, "#4");
709 Assert.AreEqual ("searchPattern", ex.ParamName, "#5");
714 public void MoveTo ()
716 string path1 = TempFolder + DSC + "DIT.MoveTo.Soucre.Test";
717 string path2 = TempFolder + DSC + "DIT.MoveTo.Dest.Test";
722 DirectoryInfo info1 = Directory.CreateDirectory (path1);
723 DirectoryInfo info2 = new DirectoryInfo (path2);
725 Assert.IsTrue (info1.Exists, "#A1");
726 Assert.IsFalse (info2.Exists, "#A2");
728 info1.MoveTo (path2);
729 Assert.IsTrue (info1.Exists, "#B1");
730 Assert.IsFalse (info2.Exists, "#B2");
732 info1 = new DirectoryInfo (path1);
733 info2 = new DirectoryInfo (path2);
734 Assert.IsFalse (info1.Exists, "#C1");
735 Assert.IsTrue (info2.Exists, "#C2");
743 public void MoveTo_DestDirName_Empty ()
745 string path = TempFolder + DSC + "DIT.MoveToArgumentException1.Test";
749 DirectoryInfo info = Directory.CreateDirectory (path);
751 info.MoveTo (string.Empty);
753 } catch (ArgumentException ex) {
754 // Empty file name is not legal
755 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
756 Assert.IsNull (ex.InnerException, "#3");
757 Assert.IsNotNull (ex.Message, "#4");
758 Assert.AreEqual ("destDirName", ex.ParamName, "#5");
766 public void MoveTo_DestDirName_Null ()
768 string path = TempFolder + DSC + "DIT.MoveToArgumentNullException.Test";
772 DirectoryInfo info = Directory.CreateDirectory (path);
776 } catch (ArgumentNullException ex) {
777 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
778 Assert.IsNull (ex.InnerException, "#3");
779 Assert.IsNotNull (ex.Message, "#4");
780 Assert.AreEqual ("destDirName", ex.ParamName, "#5");
788 public void MoveTo_DestDirName_InvalidPathChars ()
790 string path = TempFolder + DSC + "DIT.MoveToArgumentException3.Test";
794 DirectoryInfo info = Directory.CreateDirectory (path);
796 info.MoveTo (Path.InvalidPathChars [0].ToString ());
798 } catch (ArgumentException ex) {
799 // The path contains illegal characters
800 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
801 Assert.IsNull (ex.InnerException, "#3");
802 Assert.IsNotNull (ex.Message, "#4");
803 Assert.IsNull (ex.ParamName, "#5");
811 public void MoveTo_DestDirName_Whitespace ()
813 string path = TempFolder + DSC + "DIT.MoveToArgumentException2.Test";
817 DirectoryInfo info = Directory.CreateDirectory (path);
821 } catch (ArgumentException ex) {
822 // The path is not of a legal form
823 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
824 Assert.IsNull (ex.InnerException, "#3");
825 Assert.IsNotNull (ex.Message, "#4");
826 Assert.IsNull (ex.ParamName, "#5");
834 public void MoveTo_SourceDest_NotDifferent ()
836 string path = TempFolder + DSC + "DIT.MoveToIOException1.Test";
840 DirectoryInfo info = Directory.CreateDirectory (path);
844 } catch (IOException ex) {
845 // Source and destination path must be different
846 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
847 Assert.IsNull (ex.InnerException, "#A3");
848 Assert.IsNotNull (ex.Message, "#A4");
855 DirectoryInfo info = new DirectoryInfo (path);
859 } catch (IOException ex) {
860 // Source and destination path must be different
861 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
862 Assert.IsNull (ex.InnerException, "#B3");
863 Assert.IsNotNull (ex.Message, "#B4");
871 public void DirectoryNameWithSpace ()
873 DeleteDir ("this has a space at the end ");
874 string path = Path.Combine (TempFolder, "this has a space at the end ");
875 Directory.CreateDirectory (path);
876 DirectoryInfo i = new DirectoryInfo (path);
878 foreach (FileInfo f in i.GetFiles ())
883 public void LastWriteTime ()
885 DirectoryInfo info = new DirectoryInfo (TempFolder);
886 info.LastWriteTime = new DateTime (2003, 6, 4, 6, 4, 0);
888 DateTime time = Directory.GetLastWriteTime (TempFolder);
889 Assert.AreEqual (2003, time.Year, "#A1");
890 Assert.AreEqual (6, time.Month, "#A2");
891 Assert.AreEqual (4, time.Day, "#A3");
892 Assert.AreEqual (6, time.Hour, "#A4");
893 Assert.AreEqual (4, time.Minute, "#A5");
894 Assert.AreEqual (0, time.Second, "#A6");
896 time = TimeZone.CurrentTimeZone.ToLocalTime (
897 Directory.GetLastWriteTimeUtc (TempFolder));
898 Assert.AreEqual (2003, time.Year, "#B1");
899 Assert.AreEqual (6, time.Month, "#B2");
900 Assert.AreEqual (4, time.Day, "#B3");
901 Assert.AreEqual (6, time.Hour, "#B4");
902 Assert.AreEqual (4, time.Minute, "#B5");
903 Assert.AreEqual (0, time.Second, "#B6");
907 public void LastWriteTimeUtc ()
909 DirectoryInfo info = new DirectoryInfo (TempFolder);
910 info.LastWriteTimeUtc = new DateTime (2003, 6, 4, 6, 4, 0);
912 DateTime time = TimeZone.CurrentTimeZone.ToUniversalTime (
913 Directory.GetLastWriteTime (TempFolder));
914 Assert.AreEqual (2003, time.Year, "#A1");
915 Assert.AreEqual (6, time.Month, "#A2");
916 Assert.AreEqual (4, time.Day, "#A3");
917 Assert.AreEqual (6, time.Hour, "#A4");
918 Assert.AreEqual (4, time.Minute, "#A5");
919 Assert.AreEqual (0, time.Second, "#A6");
921 time = Directory.GetLastWriteTimeUtc (TempFolder);
922 Assert.AreEqual (2003, time.Year, "#B1");
923 Assert.AreEqual (6, time.Month, "#B2");
924 Assert.AreEqual (4, time.Day, "#B3");
925 Assert.AreEqual (6, time.Hour, "#B4");
926 Assert.AreEqual (4, time.Minute, "#B5");
927 Assert.AreEqual (0, time.Second, "#B6");
931 [Category("TargetJvmNotSupported")] // LastAccessTime not supported for TARGET_JVM
932 public void LastAccessTime ()
934 DirectoryInfo info = new DirectoryInfo (TempFolder);
935 info.LastAccessTime = DateTime.Now;
939 [Category("TargetJvmNotSupported")] // LastAccessTime not supported for TARGET_JVM
940 public void LastAccessTimeUtc ()
942 DirectoryInfo info = new DirectoryInfo (TempFolder);
943 info.LastAccessTimeUtc = DateTime.Now;
947 [Category("TargetJvmNotSupported")] // CreationTime not supported for TARGET_JVM
948 public void CreationTime ()
950 DirectoryInfo info = new DirectoryInfo (TempFolder);
951 info.CreationTime = DateTime.Now;
955 [Category("TargetJvmNotSupported")] // CreationTime not supported for TARGET_JVM
956 public void CreationTimeUtc ()
958 DirectoryInfo info = new DirectoryInfo (TempFolder);
959 info.CreationTimeUtc = DateTime.Now;
963 public void Name_Bug76903 ()
965 CheckName ("/usr/share");
966 CheckName ("/usr/share/");
967 CheckName ("/usr/share/.");
968 CheckName ("/usr/share/./");
969 CheckName ("/usr/share/blabla/../");
970 CheckName ("/usr/lib/../share/.");
974 public void Hang_76191 ()
976 // from bug #76191 (hangs on Windows)
977 DirectoryInfo di = new DirectoryInfo (Environment.CurrentDirectory);
978 Stack s = new Stack ();
980 while (di.Parent != null) {
984 while (s.Count > 0) {
985 di = (DirectoryInfo) s.Pop ();
986 Assert.IsTrue (di.Exists, di.Name);
991 public void WindowsSystem32_76191 ()
996 Directory.SetCurrentDirectory (@"C:\WINDOWS\system32");
997 WindowsParentFullName ("C:", "C:\\WINDOWS");
998 WindowsParentFullName ("C:\\", null);
999 WindowsParentFullName ("C:\\dir", "C:\\");
1000 WindowsParentFullName ("C:\\dir\\", "C:\\");
1001 WindowsParentFullName ("C:\\dir\\dir", "C:\\dir");
1002 WindowsParentFullName ("C:\\dir\\dir\\", "C:\\dir");
1006 public void Parent_Bug77090 ()
1008 DirectoryInfo di = new DirectoryInfo ("/home");
1009 if (Path.DirectorySeparatorChar == '\\') {
1010 Assert.IsTrue (di.Parent.Name.EndsWith (":\\"), "#1");
1012 Assert.AreEqual ("/", di.Parent.Name, "#1");
1013 Assert.IsNull (di.Parent.Parent, "#2");
1017 public void ToStringTest ()
1021 info = new DirectoryInfo ("Test");
1022 Assert.AreEqual ("Test", info.ToString (), "#1");
1023 info = new DirectoryInfo (TempFolder + DSC + "ToString.Test");
1024 Assert.AreEqual (TempFolder + DSC + "ToString.Test", info.ToString ());
1028 public void Serialization ()
1031 SerializationInfo si;
1033 info = new DirectoryInfo ("Test");
1034 si = new SerializationInfo (typeof (DirectoryInfo), new FormatterConverter ());
1035 info.GetObjectData (si, new StreamingContext ());
1037 Assert.AreEqual (2, si.MemberCount, "#A1");
1038 Assert.AreEqual ("Test", si.GetString ("OriginalPath"), "#A2");
1039 Assert.AreEqual (Path.Combine (Directory.GetCurrentDirectory (), "Test"), si.GetString ("FullPath"), "#A3");
1041 info = new DirectoryInfo (TempFolder);
1042 si = new SerializationInfo (typeof (DirectoryInfo), new FormatterConverter ());
1043 info.GetObjectData (si, new StreamingContext ());
1045 Assert.AreEqual (2, si.MemberCount, "#B1");
1046 Assert.AreEqual (TempFolder, si.GetString ("OriginalPath"), "#B2");
1047 Assert.AreEqual (TempFolder, si.GetString ("FullPath"), "#B3");
1051 public void Deserialization ()
1053 DirectoryInfo info = new DirectoryInfo ("Test");
1055 MemoryStream ms = new MemoryStream ();
1056 BinaryFormatter bf = new BinaryFormatter ();
1057 bf.Serialize (ms, info);
1060 DirectoryInfo clone = (DirectoryInfo) bf.Deserialize (ms);
1061 Assert.AreEqual (info.Name, clone.Name, "#1");
1062 Assert.AreEqual (info.FullName, clone.FullName, "#2");
1065 // Needed so that UnixSymbolicLinkInfo doesn't have to
1066 // be JITted on windows
1067 private void Symlink_helper ()
1069 string path = TempFolder + DSC + "DIT.Symlink";
1070 string dir = path + DSC + "dir";
1071 string link = path + DSC + "link";
1076 Directory.CreateDirectory (path);
1077 Directory.CreateDirectory (dir);
1078 Mono.Unix.UnixSymbolicLinkInfo li = new Mono.Unix.UnixSymbolicLinkInfo (link);
1079 li.CreateSymbolicLinkTo (dir);
1081 DirectoryInfo info = new DirectoryInfo (path);
1082 DirectoryInfo[] dirs = info.GetDirectories ();
1083 Assert.AreEqual (2, dirs.Length, "#1");
1090 [Category ("NotDotNet")]
1091 public void Symlink ()
1093 // This test only applies to Linux and
1094 // Linux-like platforms but mono-on-windows
1095 // doesn't set the NotDotNet category
1096 if (!RunningOnUnix) {
1103 static bool RunningOnUnix {
1105 int p = (int) Environment.OSVersion.Platform;
1106 return ((p == 4) || (p == 128) || (p == 6));
1110 void WindowsParentFullName (string name, string expected)
1112 DirectoryInfo di = new DirectoryInfo (name);
1113 if (di.Parent == null)
1114 Assert.IsNull (expected, name);
1116 Assert.AreEqual (expected, di.Parent.FullName, name);
1119 void CheckName (string name)
1121 DirectoryInfo di = new DirectoryInfo (name);
1122 Assert.AreEqual ("share", di.Name, name + ".Name");
1123 Assert.AreEqual ("usr", di.Parent.Name, name + ".Parent.Name");
1126 void DeleteDir (string path)
1128 if (Directory.Exists (path))
1129 Directory.Delete (path, true);