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 DirectoryNameWithSpace ()
885 DeleteDir ("this has a space at the end ");
886 string path = Path.Combine (TempFolder, "this has a space at the end ");
887 Directory.CreateDirectory (path);
888 DirectoryInfo i = new DirectoryInfo (path);
890 foreach (FileInfo f in i.GetFiles ())
895 public void LastWriteTime ()
897 DirectoryInfo info = new DirectoryInfo (TempFolder);
898 info.LastWriteTime = new DateTime (2003, 6, 4, 6, 4, 0);
900 DateTime time = Directory.GetLastWriteTime (TempFolder);
901 Assert.AreEqual (2003, time.Year, "#A1");
902 Assert.AreEqual (6, time.Month, "#A2");
903 Assert.AreEqual (4, time.Day, "#A3");
904 Assert.AreEqual (6, time.Hour, "#A4");
905 Assert.AreEqual (4, time.Minute, "#A5");
906 Assert.AreEqual (0, time.Second, "#A6");
908 time = TimeZone.CurrentTimeZone.ToLocalTime (
909 Directory.GetLastWriteTimeUtc (TempFolder));
910 Assert.AreEqual (2003, time.Year, "#B1");
911 Assert.AreEqual (6, time.Month, "#B2");
912 Assert.AreEqual (4, time.Day, "#B3");
913 Assert.AreEqual (6, time.Hour, "#B4");
914 Assert.AreEqual (4, time.Minute, "#B5");
915 Assert.AreEqual (0, time.Second, "#B6");
919 public void LastWriteTimeUtc ()
921 DirectoryInfo info = new DirectoryInfo (TempFolder);
922 info.LastWriteTimeUtc = new DateTime (2003, 6, 4, 6, 4, 0);
924 DateTime time = TimeZone.CurrentTimeZone.ToUniversalTime (
925 Directory.GetLastWriteTime (TempFolder));
926 Assert.AreEqual (2003, time.Year, "#A1");
927 Assert.AreEqual (6, time.Month, "#A2");
928 Assert.AreEqual (4, time.Day, "#A3");
929 Assert.AreEqual (6, time.Hour, "#A4");
930 Assert.AreEqual (4, time.Minute, "#A5");
931 Assert.AreEqual (0, time.Second, "#A6");
933 time = Directory.GetLastWriteTimeUtc (TempFolder);
934 Assert.AreEqual (2003, time.Year, "#B1");
935 Assert.AreEqual (6, time.Month, "#B2");
936 Assert.AreEqual (4, time.Day, "#B3");
937 Assert.AreEqual (6, time.Hour, "#B4");
938 Assert.AreEqual (4, time.Minute, "#B5");
939 Assert.AreEqual (0, time.Second, "#B6");
943 [Category("TargetJvmNotSupported")] // LastAccessTime not supported for TARGET_JVM
944 public void LastAccessTime ()
946 DirectoryInfo info = new DirectoryInfo (TempFolder);
947 info.LastAccessTime = DateTime.Now;
951 [Category("TargetJvmNotSupported")] // LastAccessTime not supported for TARGET_JVM
952 public void LastAccessTimeUtc ()
954 DirectoryInfo info = new DirectoryInfo (TempFolder);
955 info.LastAccessTimeUtc = DateTime.Now;
959 [Category("TargetJvmNotSupported")] // CreationTime not supported for TARGET_JVM
960 public void CreationTime ()
962 DirectoryInfo info = new DirectoryInfo (TempFolder);
963 info.CreationTime = DateTime.Now;
967 [Category("TargetJvmNotSupported")] // CreationTime not supported for TARGET_JVM
968 public void CreationTimeUtc ()
970 DirectoryInfo info = new DirectoryInfo (TempFolder);
971 info.CreationTimeUtc = DateTime.Now;
975 public void Name_Bug76903 ()
977 CheckName ("/usr/share");
978 CheckName ("/usr/share/");
979 CheckName ("/usr/share/.");
980 CheckName ("/usr/share/./");
981 CheckName ("/usr/share/blabla/../");
982 CheckName ("/usr/lib/../share/.");
986 public void Hang_76191 ()
988 // from bug #76191 (hangs on Windows)
989 DirectoryInfo di = new DirectoryInfo (Environment.CurrentDirectory);
990 Stack s = new Stack ();
992 while (di.Parent != null) {
996 while (s.Count > 0) {
997 di = (DirectoryInfo) s.Pop ();
998 Assert.IsTrue (di.Exists, di.Name);
1003 public void WindowsSystem32_76191 ()
1008 Directory.SetCurrentDirectory (@"C:\WINDOWS\system32");
1009 WindowsParentFullName ("C:", "C:\\WINDOWS");
1010 WindowsParentFullName ("C:\\", null);
1011 WindowsParentFullName ("C:\\dir", "C:\\");
1012 WindowsParentFullName ("C:\\dir\\", "C:\\");
1013 WindowsParentFullName ("C:\\dir\\dir", "C:\\dir");
1014 WindowsParentFullName ("C:\\dir\\dir\\", "C:\\dir");
1018 public void Parent_Bug77090 ()
1020 DirectoryInfo di = new DirectoryInfo ("/home");
1021 if (Path.DirectorySeparatorChar == '\\') {
1022 Assert.IsTrue (di.Parent.Name.EndsWith (":\\"), "#1");
1024 Assert.AreEqual ("/", di.Parent.Name, "#1");
1025 Assert.IsNull (di.Parent.Parent, "#2");
1029 public void ToStringTest ()
1033 info = new DirectoryInfo ("Test");
1034 Assert.AreEqual ("Test", info.ToString (), "#1");
1035 info = new DirectoryInfo (TempFolder + DSC + "ToString.Test");
1036 Assert.AreEqual (TempFolder + DSC + "ToString.Test", info.ToString ());
1040 public void Serialization ()
1043 SerializationInfo si;
1045 info = new DirectoryInfo ("Test");
1046 si = new SerializationInfo (typeof (DirectoryInfo), new FormatterConverter ());
1047 info.GetObjectData (si, new StreamingContext ());
1049 Assert.AreEqual (2, si.MemberCount, "#A1");
1050 Assert.AreEqual ("Test", si.GetString ("OriginalPath"), "#A2");
1051 Assert.AreEqual (Path.Combine (Directory.GetCurrentDirectory (), "Test"), si.GetString ("FullPath"), "#A3");
1053 info = new DirectoryInfo (TempFolder);
1054 si = new SerializationInfo (typeof (DirectoryInfo), new FormatterConverter ());
1055 info.GetObjectData (si, new StreamingContext ());
1057 Assert.AreEqual (2, si.MemberCount, "#B1");
1058 Assert.AreEqual (TempFolder, si.GetString ("OriginalPath"), "#B2");
1059 Assert.AreEqual (TempFolder, si.GetString ("FullPath"), "#B3");
1063 public void Deserialization ()
1065 DirectoryInfo info = new DirectoryInfo ("Test");
1067 MemoryStream ms = new MemoryStream ();
1068 BinaryFormatter bf = new BinaryFormatter ();
1069 bf.Serialize (ms, info);
1072 DirectoryInfo clone = (DirectoryInfo) bf.Deserialize (ms);
1073 Assert.AreEqual (info.Name, clone.Name, "#1");
1074 Assert.AreEqual (info.FullName, clone.FullName, "#2");
1077 // Needed so that UnixSymbolicLinkInfo doesn't have to
1078 // be JITted on windows
1079 private void Symlink_helper ()
1081 string path = TempFolder + DSC + "DIT.Symlink";
1082 string dir = path + DSC + "dir";
1083 string link = path + DSC + "link";
1088 Directory.CreateDirectory (path);
1089 Directory.CreateDirectory (dir);
1090 Mono.Unix.UnixSymbolicLinkInfo li = new Mono.Unix.UnixSymbolicLinkInfo (link);
1091 li.CreateSymbolicLinkTo (dir);
1093 DirectoryInfo info = new DirectoryInfo (path);
1094 DirectoryInfo[] dirs = info.GetDirectories ();
1095 Assert.AreEqual (2, dirs.Length, "#1");
1102 [Category ("NotDotNet")]
1103 public void Symlink ()
1105 // This test only applies to Linux and
1106 // Linux-like platforms but mono-on-windows
1107 // doesn't set the NotDotNet category
1108 if (!RunningOnUnix) {
1115 static bool RunningOnUnix {
1117 int p = (int) Environment.OSVersion.Platform;
1118 return ((p == 4) || (p == 128) || (p == 6));
1122 void WindowsParentFullName (string name, string expected)
1124 DirectoryInfo di = new DirectoryInfo (name);
1125 if (di.Parent == null)
1126 Assert.IsNull (expected, name);
1128 Assert.AreEqual (expected, di.Parent.FullName, name);
1131 void CheckName (string name)
1133 DirectoryInfo di = new DirectoryInfo (name);
1134 Assert.AreEqual ("share", di.Name, name + ".Name");
1135 Assert.AreEqual ("usr", di.Parent.Name, name + ".Parent.Name");
1138 void DeleteDir (string path)
1140 if (Directory.Exists (path))
1141 Directory.Delete (path, true);