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");
141 [Category ("MobileNotWorking")]
144 string path
= TempFolder
+ DSC
+ "DIT.Name.Test";
148 DirectoryInfo info
= new DirectoryInfo (path
);
149 Assert
.AreEqual ("DIT.Name.Test", info
.Name
, "#1");
151 info
= Directory
.CreateDirectory (path
);
152 Assert
.AreEqual ("DIT.Name.Test", info
.Name
, "#2");
154 info
= Directory
.CreateDirectory ("whatever");
155 Assert
.AreEqual ("whatever", info
.Name
, "#3");
158 info
= new DirectoryInfo ("/");
159 Assert
.AreEqual ("/", info
.Name
, "#4");
161 info
= new DirectoryInfo ("test/");
162 Assert
.AreEqual ("test", info
.Name
, "#5");
164 info
= new DirectoryInfo ("/test");
165 Assert
.AreEqual ("test", info
.Name
, "#4");
167 info
= new DirectoryInfo ("/test/");
168 Assert
.AreEqual ("test", info
.Name
, "#4");
170 info
= new DirectoryInfo (@"c:");
171 Assert
.AreEqual (@"C:\", info
.Name
, "#4");
173 info
= new DirectoryInfo (@"c:\");
174 Assert
.AreEqual (@"c:\", info
.Name
, "#5");
176 info
= new DirectoryInfo (@"c:\test");
177 Assert
.AreEqual ("test", info
.Name
, "#6");
179 info
= new DirectoryInfo (@"c:\test\");
180 Assert
.AreEqual ("test", info
.Name
, "#7");
188 public void Parent ()
190 string path
= TempFolder
+ DSC
+ "DIT.Parent.Test";
194 DirectoryInfo info
= new DirectoryInfo (path
);
195 Assert
.AreEqual ("MonoTests.System.IO.Tests", info
.Parent
.Name
, "#1");
197 info
= Directory
.CreateDirectory (path
);
198 Assert
.AreEqual ("MonoTests.System.IO.Tests", info
.Parent
.Name
, "#2");
200 info
= new DirectoryInfo ("test");
201 Assert
.AreEqual (Directory
.GetCurrentDirectory (), info
.Parent
.FullName
, "#3");
204 info
= new DirectoryInfo ("/");
205 Assert
.IsNull (info
.Parent
, "#4");
207 info
= new DirectoryInfo ("test/");
208 Assert
.IsNotNull (info
.Parent
, "#5a");
209 Assert
.AreEqual (Directory
.GetCurrentDirectory (), info
.Parent
.FullName
, "#5b");
211 info
= new DirectoryInfo ("/test");
212 Assert
.IsNotNull (info
.Parent
, "#6a");
213 Assert
.AreEqual ("/", info
.Parent
.FullName
, "#6b");
215 info
= new DirectoryInfo ("/test/");
216 Assert
.IsNotNull (info
.Parent
, "#7a");
217 Assert
.AreEqual ("/", info
.Parent
.FullName
, "#7b");
219 info
= new DirectoryInfo (@"c:");
220 Assert
.IsNull (info
.Parent
, "#4");
222 info
= new DirectoryInfo (@"c:\");
223 Assert
.IsNull (info
.Parent
, "#5");
225 info
= new DirectoryInfo (@"c:\test");
226 Assert
.IsNotNull (info
.Parent
, "#6a");
227 Assert
.AreEqual (@"c:\", info
.Parent
.FullName
, "#6b");
229 info
= new DirectoryInfo (@"c:\test\");
230 Assert
.IsNotNull (info
.Parent
, "#7a");
231 Assert
.AreEqual (@"c:\", info
.Parent
.FullName
, "#7b");
239 public void Create ()
241 string path
= TempFolder
+ DSC
+ "DIT.Create.Test";
245 DirectoryInfo info
= new DirectoryInfo (path
);
246 Assert
.IsFalse (info
.Exists
, "#1");
248 Assert
.IsFalse (info
.Exists
, "#2");
249 info
= new DirectoryInfo (path
);
250 Assert
.IsTrue (info
.Exists
, "#3");
257 public void CreateSubdirectory ()
259 string sub_path
= Path
.Combine ("test01", "test02");
261 DirectoryInfo info
= new DirectoryInfo (TempFolder
);
262 DirectoryInfo sub
= info
.CreateSubdirectory (sub_path
);
263 Assert
.IsNotNull (sub
, "#1");
264 Assert
.AreEqual (Path
.Combine (TempFolder
, sub_path
), sub
.FullName
, "#2");
265 Assert
.IsTrue (Directory
.Exists (sub
.FullName
), "#3");
267 DeleteDir (Path
.Combine (TempFolder
, sub_path
));
272 public void CreateSubdirectory_Path_Null ()
274 DirectoryInfo info
= new DirectoryInfo (TempFolder
);
276 info
.CreateSubdirectory (null);
278 } catch (ArgumentNullException ex
) {
279 Assert
.AreEqual (typeof (ArgumentNullException
), ex
.GetType (), "#2");
280 Assert
.IsNull (ex
.InnerException
, "#3");
281 Assert
.IsNotNull (ex
.Message
, "#4");
282 Assert
.AreEqual ("path", ex
.ParamName
, "#5");
287 public void CreateSubdirectory_Path_Empty ()
289 DirectoryInfo info
= new DirectoryInfo (TempFolder
);
291 info
.CreateSubdirectory (string.Empty
);
293 } catch (ArgumentException ex
) {
294 // Empty file name is not legal
295 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#2");
296 Assert
.IsNull (ex
.InnerException
, "#3");
297 Assert
.IsNotNull (ex
.Message
, "#4");
302 public void Delete1 ()
304 string path
= TempFolder
+ DSC
+ "DIT.Delete1.Test";
308 Directory
.CreateDirectory (path
);
309 DirectoryInfo info
= new DirectoryInfo (path
);
310 Assert
.IsTrue (info
.Exists
, "#1");
313 Assert
.IsTrue (info
.Exists
, "#2");
315 info
= new DirectoryInfo (path
);
316 Assert
.IsFalse (info
.Exists
, "#3");
323 public void Delete1_DirectoryNotEmpty ()
325 string path
= TempFolder
+ DSC
+ "DIT.DeleteIOException1.Test";
329 Directory
.CreateDirectory (path
);
330 File
.Create (path
+ DSC
+ "test").Close ();
331 DirectoryInfo info
= new DirectoryInfo (path
);
335 } catch (IOException ex
) {
336 // The directory is not empty
337 Assert
.AreEqual (typeof (IOException
), ex
.GetType (), "#2");
338 Assert
.IsNull (ex
.InnerException
, "#3");
339 Assert
.IsNotNull (ex
.Message
, "#4");
346 [Test
] // Delete (Boolean)
347 public void Delete2 ()
349 string path
= TempFolder
+ DSC
+ "DIT.Delete2.Test";
353 Directory
.CreateDirectory (path
);
354 File
.Create (path
+ DSC
+ "test").Close ();
355 DirectoryInfo info
= new DirectoryInfo (path
);
356 Assert
.IsTrue (info
.Exists
, "#1");
359 Assert
.IsTrue (info
.Exists
, "#2");
361 info
= new DirectoryInfo (path
);
362 Assert
.IsFalse (info
.Exists
, "#3");
368 [Test
] // Delete (Boolean)
369 public void Delete2_DirectoryNotEmpty ()
371 string path
= TempFolder
+ DSC
+ "DIT.DeleteIOException2.Test";
375 Directory
.CreateDirectory (path
);
376 File
.Create (path
+ DSC
+ "test").Close ();
377 DirectoryInfo info
= new DirectoryInfo (path
);
381 } catch (IOException ex
) {
382 // The directory is not empty
383 Assert
.AreEqual (typeof (IOException
), ex
.GetType (), "#2");
384 Assert
.IsNull (ex
.InnerException
, "#3");
385 Assert
.IsNotNull (ex
.Message
, "#4");
393 public void FullName ()
395 DirectoryInfo di
= new DirectoryInfo ("something");
396 Assert
.IsFalse (di
.Exists
, "#A1");
397 Assert
.AreEqual (Path
.Combine (Directory
.GetCurrentDirectory (), "something"), di
.FullName
, "#A2");
399 di
= new DirectoryInfo ("something" + Path
.DirectorySeparatorChar
);
400 Assert
.IsFalse (di
.Exists
, "#B1");
401 Assert
.AreEqual (Path
.DirectorySeparatorChar
, di
.FullName
[di
.FullName
.Length
- 1], "#B2");
403 di
= new DirectoryInfo ("something" + Path
.AltDirectorySeparatorChar
);
404 Assert
.IsFalse (di
.Exists
, "#C1");
405 Assert
.AreEqual (Path
.DirectorySeparatorChar
, di
.FullName
[di
.FullName
.Length
- 1], "#C2");
408 di
= new DirectoryInfo ("/");
409 Assert
.AreEqual ("/", di
.FullName
, "#D1");
411 di
= new DirectoryInfo ("test/");
412 Assert
.AreEqual (Path
.Combine (Directory
.GetCurrentDirectory (), "test/"), di
.FullName
, "#D2");
414 di
= new DirectoryInfo ("/test");
415 Assert
.AreEqual ("/test", di
.FullName
, "#D3");
417 di
= new DirectoryInfo ("/test/");
418 Assert
.AreEqual ("/test/", di
.FullName
, "#D4");
420 di
= new DirectoryInfo (@"c:");
421 Assert
.AreEqual (@"C:\", di
.FullName
, "#D1");
423 di
= new DirectoryInfo (@"c:\");
424 Assert
.AreEqual (@"c:\", di
.FullName
, "#D2");
426 di
= new DirectoryInfo (@"c:\test");
427 Assert
.AreEqual (@"c:\test", di
.FullName
, "#D3");
429 di
= new DirectoryInfo (@"c:\test\");
430 Assert
.AreEqual (@"c:\test\", di
.FullName
, "#D4");
435 public void FullName_RootDirectory ()
437 DirectoryInfo di
= new DirectoryInfo (String
.Empty
+ Path
.DirectorySeparatorChar
);
439 // can't be sure of the root drive under windows
440 Assert
.AreEqual ("/", di
.FullName
, "#1");
442 Assert
.IsNull (di
.Parent
, "#2");
444 di
= new DirectoryInfo (String
.Empty
+ Path
.AltDirectorySeparatorChar
);
446 // can't be sure of the root drive under windows
447 Assert
.AreEqual ("/", di
.FullName
, "#3");
449 Assert
.IsNull (di
.Parent
, "#4");
452 [Test
] // GetDirectories ()
453 public void GetDirectories1 ()
455 string path
= TempFolder
+ DSC
+ "DIT.GetDirectories1.Test";
458 DirectoryInfo info
= Directory
.CreateDirectory (path
);
459 Assert
.AreEqual (0, info
.GetDirectories ().Length
, "#1");
461 Directory
.CreateDirectory (path
+ DSC
+ "1");
462 Directory
.CreateDirectory (path
+ DSC
+ "2");
463 File
.Create (path
+ DSC
+ "filetest").Close ();
464 Assert
.AreEqual (2, info
.GetDirectories ().Length
, "#2");
466 Directory
.Delete (path
+ DSC
+ 2);
467 Assert
.AreEqual (1, info
.GetDirectories ().Length
, "#3");
473 [Test
] // GetDirectories ()
474 public void GetDirectories1_DirectoryDoesNotExist ()
476 string path
= TempFolder
+ DSC
+ "DIT.GetDirectoriesDirectoryNotFoundException1.Test";
480 DirectoryInfo info
= new DirectoryInfo (path
);
481 info
.GetDirectories ();
483 } catch (DirectoryNotFoundException ex
) {
484 // Could not find a part of '...'
485 Assert
.AreEqual (typeof (DirectoryNotFoundException
), ex
.GetType (), "#2");
486 Assert
.IsNull (ex
.InnerException
, "#3");
487 Assert
.IsNotNull (ex
.Message
, "#4");
488 Assert
.IsTrue (ex
.Message
.IndexOf (path
) != -1, "#5");
494 [Test
] // GetDirectories (String)
495 public void GetDirectories2 ()
497 string path
= TempFolder
+ DSC
+ "DIT.GetDirectories2.Test";
500 DirectoryInfo info
= Directory
.CreateDirectory (path
);
501 Assert
.AreEqual (0, info
.GetDirectories ("*").Length
, "#1");
503 Directory
.CreateDirectory (path
+ DSC
+ "test120");
504 Directory
.CreateDirectory (path
+ DSC
+ "test210");
505 Directory
.CreateDirectory (path
+ DSC
+ "atest330");
506 Directory
.CreateDirectory (path
+ DSC
+ "test220");
507 Directory
.CreateDirectory (path
+ DSC
+ "rest");
508 Directory
.CreateDirectory (path
+ DSC
+ "rest" + DSC
+ "subdir");
509 File
.Create (path
+ DSC
+ "filetest").Close ();
511 Assert
.AreEqual (5, info
.GetDirectories ("*").Length
, "#2");
512 Assert
.AreEqual (3, info
.GetDirectories ("test*").Length
, "#3");
513 Assert
.AreEqual (2, info
.GetDirectories ("test?20").Length
, "#4");
514 Assert
.AreEqual (0, info
.GetDirectories ("test?").Length
, "#5");
515 Assert
.AreEqual (0, info
.GetDirectories ("test[12]*").Length
, "#6");
516 Assert
.AreEqual (2, info
.GetDirectories ("test2*0").Length
, "#7");
517 Assert
.AreEqual (4, info
.GetDirectories ("*test*").Length
, "#8");
519 Assert
.AreEqual (6, info
.GetDirectories ("*", SearchOption
.AllDirectories
).Length
, "#9");
526 [Test
] // GetDirectories (String)
527 public void GetDirectories2_DirectoryDoesNotExist ()
529 string path
= TempFolder
+ DSC
+ "DIT.GetDirectoriesDirectoryNotFoundException2.Test";
533 DirectoryInfo info
= new DirectoryInfo (path
);
534 info
.GetDirectories ("*");
536 } catch (DirectoryNotFoundException ex
) {
537 // Could not find a part of '...'
538 Assert
.AreEqual (typeof (DirectoryNotFoundException
), ex
.GetType (), "#2");
539 Assert
.IsNull (ex
.InnerException
, "#3");
540 Assert
.IsNotNull (ex
.Message
, "#4");
541 Assert
.IsTrue (ex
.Message
.IndexOf (path
) != -1, "#5");
547 [Test
] // GetDirectories (String)
548 public void GetDirectories2_SearchPattern_Null ()
550 DirectoryInfo info
= new DirectoryInfo (TempFolder
);
552 info
.GetDirectories (null);
554 } catch (ArgumentNullException ex
) {
555 Assert
.AreEqual (typeof (ArgumentNullException
), ex
.GetType (), "#2");
556 Assert
.IsNull (ex
.InnerException
, "#3");
557 Assert
.IsNotNull (ex
.Message
, "#4");
558 Assert
.AreEqual ("searchPattern", ex
.ParamName
, "#5");
562 [Test
] //GetDirectories (String, SearchOptions)
563 public void GetDirectiories_SearchOptionAllDirectories ()
565 string directoryToBeLookedFor
= "lookforme";
566 DirectoryInfo baseDir
= Directory
.CreateDirectory(Path
.Combine(TempFolder
, "GetDirectiories_SearchOptionAllDirectories"));
567 DirectoryInfo subdir
= baseDir
.CreateSubdirectory("subdir");
568 DirectoryInfo subsubdir
= subdir
.CreateSubdirectory(directoryToBeLookedFor
);
569 DirectoryInfo
[] directoriesFound
= baseDir
.GetDirectories(directoryToBeLookedFor
, SearchOption
.AllDirectories
);
570 Assert
.AreEqual(1, directoriesFound
.Length
, "There should be exactly one directory with the specified name.");
571 Assert
.AreEqual(directoryToBeLookedFor
, directoriesFound
[0].Name
, "The name of the directory found should match the expected one.");
575 [Test
] // GetDirectories (String, SearchOption)
576 public void GetDirectories3_SearchPattern_Null ()
578 DirectoryInfo info
= new DirectoryInfo (TempFolder
);
580 info
.GetDirectories (null, SearchOption
.AllDirectories
);
582 } catch (ArgumentNullException ex
) {
583 Assert
.AreEqual (typeof (ArgumentNullException
), ex
.GetType (), "#2");
584 Assert
.IsNull (ex
.InnerException
, "#3");
585 Assert
.IsNotNull (ex
.Message
, "#4");
586 Assert
.AreEqual ("searchPattern", ex
.ParamName
, "#5");
591 [Test
] // GetFiles ()
592 public void GetFiles1 ()
594 string path
= TempFolder
+ DSC
+ "DIT.GetFiles1.Test";
598 DirectoryInfo info
= Directory
.CreateDirectory (path
);
599 Assert
.AreEqual (0, info
.GetFiles ().Length
, "#1");
600 File
.Create (path
+ DSC
+ "file1").Close ();
601 File
.Create (path
+ DSC
+ "file2").Close ();
602 Directory
.CreateDirectory (path
+ DSC
+ "directory1");
603 Assert
.AreEqual (2, info
.GetFiles ().Length
, "#2");
609 [Test
] // GetFiles ()
610 public void GetFiles1_DirectoryDoesNotExist ()
612 string path
= TempFolder
+ DSC
+ "DIT.GetFilesDirectoryNotFoundException1.Test";
616 DirectoryInfo info
= new DirectoryInfo (path
);
619 } catch (DirectoryNotFoundException ex
) {
620 // Could not find a part of '...'
621 Assert
.AreEqual (typeof (DirectoryNotFoundException
), ex
.GetType (), "#2");
622 Assert
.IsNull (ex
.InnerException
, "#3");
623 Assert
.IsNotNull (ex
.Message
, "#4");
624 Assert
.IsTrue (ex
.Message
.IndexOf (path
) != -1, "#5");
630 [Test
] // GetFiles (String)
631 public void GetFiles2 ()
633 string path
= TempFolder
+ DSC
+ "DIT.GetFiles2.Test";
637 DirectoryInfo info
= Directory
.CreateDirectory (path
);
638 Assert
.AreEqual (0, info
.GetFiles ("*").Length
, "#1");
639 File
.Create (path
+ DSC
+ "file120file").Close ();
640 File
.Create (path
+ DSC
+ "file220file").Close ();
641 File
.Create (path
+ DSC
+ "afile330file").Close ();
642 File
.Create (path
+ DSC
+ "test.abc").Close ();
643 File
.Create (path
+ DSC
+ "test.abcd").Close ();
644 File
.Create (path
+ DSC
+ "test.abcdef").Close ();
645 Directory
.CreateDirectory (path
+ DSC
+ "dir");
647 Assert
.AreEqual (6, info
.GetFiles ("*").Length
, "#2");
648 Assert
.AreEqual (2, info
.GetFiles ("file*file").Length
, "#3");
649 Assert
.AreEqual (3, info
.GetFiles ("*file*").Length
, "#4");
650 Assert
.AreEqual (2, info
.GetFiles ("file?20file").Length
, "#5");
651 Assert
.AreEqual (1, info
.GetFiles ("*.abcd").Length
, "#6");
652 Assert
.AreEqual (2, info
.GetFiles ("*.abcd*").Length
, "#7");
658 [Test
] // GetFiles (String)
659 public void GetFiles2_DirectoryDoesNotExist ()
661 string path
= TempFolder
+ DSC
+ "DIT.GetFilesDirectoryNotFoundException2.Test";
665 DirectoryInfo info
= new DirectoryInfo (path
);
668 } catch (DirectoryNotFoundException ex
) {
669 // Could not find a part of '...'
670 Assert
.AreEqual (typeof (DirectoryNotFoundException
), ex
.GetType (), "#2");
671 Assert
.IsNull (ex
.InnerException
, "#3");
672 Assert
.IsNotNull (ex
.Message
, "#4");
673 Assert
.IsTrue (ex
.Message
.IndexOf (path
) != -1, "#5");
679 [Test
] // GetFiles (String)
680 public void GetFiles2_SearchPattern_Null ()
682 DirectoryInfo info
= new DirectoryInfo (TempFolder
);
684 info
.GetFiles (null);
686 } catch (ArgumentNullException ex
) {
687 Assert
.AreEqual (typeof (ArgumentNullException
), ex
.GetType (), "#2");
688 Assert
.IsNull (ex
.InnerException
, "#3");
689 Assert
.IsNotNull (ex
.Message
, "#4");
690 Assert
.AreEqual ("searchPattern", ex
.ParamName
, "#5");
695 [Test
] // GetFiles (String, SearchOption)
696 public void GetFiles3_SearchPattern_Null ()
698 DirectoryInfo info
= new DirectoryInfo (TempFolder
);
700 info
.GetFiles (null, SearchOption
.TopDirectoryOnly
);
702 } catch (ArgumentNullException ex
) {
703 Assert
.AreEqual (typeof (ArgumentNullException
), ex
.GetType (), "#2");
704 Assert
.IsNull (ex
.InnerException
, "#3");
705 Assert
.IsNotNull (ex
.Message
, "#4");
706 Assert
.AreEqual ("searchPattern", ex
.ParamName
, "#5");
712 public void GetFileSystemInfos2_SearchPattern_Null ()
714 DirectoryInfo info
= new DirectoryInfo (TempFolder
);
716 info
.GetFileSystemInfos (null);
718 } catch (ArgumentNullException ex
) {
719 Assert
.AreEqual (typeof (ArgumentNullException
), ex
.GetType (), "#2");
720 Assert
.IsNull (ex
.InnerException
, "#3");
721 Assert
.IsNotNull (ex
.Message
, "#4");
722 Assert
.AreEqual ("searchPattern", ex
.ParamName
, "#5");
727 public void MoveTo ()
729 string path1
= TempFolder
+ DSC
+ "DIT.MoveTo.Soucre.Test";
730 string path2
= TempFolder
+ DSC
+ "DIT.MoveTo.Dest.Test";
735 DirectoryInfo info1
= Directory
.CreateDirectory (path1
);
736 DirectoryInfo info2
= new DirectoryInfo (path2
);
738 Assert
.IsTrue (info1
.Exists
, "#A1");
739 Assert
.IsFalse (info2
.Exists
, "#A2");
741 info1
.MoveTo (path2
);
742 Assert
.IsTrue (info1
.Exists
, "#B1");
743 Assert
.IsFalse (info2
.Exists
, "#B2");
745 info1
= new DirectoryInfo (path1
);
746 info2
= new DirectoryInfo (path2
);
747 Assert
.IsFalse (info1
.Exists
, "#C1");
748 Assert
.IsTrue (info2
.Exists
, "#C2");
756 public void MoveTo_DestDirName_Empty ()
758 string path
= TempFolder
+ DSC
+ "DIT.MoveToArgumentException1.Test";
762 DirectoryInfo info
= Directory
.CreateDirectory (path
);
764 info
.MoveTo (string.Empty
);
766 } catch (ArgumentException ex
) {
767 // Empty file name is not legal
768 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#2");
769 Assert
.IsNull (ex
.InnerException
, "#3");
770 Assert
.IsNotNull (ex
.Message
, "#4");
771 Assert
.AreEqual ("destDirName", ex
.ParamName
, "#5");
779 public void MoveTo_DestDirName_Null ()
781 string path
= TempFolder
+ DSC
+ "DIT.MoveToArgumentNullException.Test";
785 DirectoryInfo info
= Directory
.CreateDirectory (path
);
789 } catch (ArgumentNullException ex
) {
790 Assert
.AreEqual (typeof (ArgumentNullException
), ex
.GetType (), "#2");
791 Assert
.IsNull (ex
.InnerException
, "#3");
792 Assert
.IsNotNull (ex
.Message
, "#4");
793 Assert
.AreEqual ("destDirName", ex
.ParamName
, "#5");
801 public void MoveTo_DestDirName_InvalidPathChars ()
803 string path
= TempFolder
+ DSC
+ "DIT.MoveToArgumentException3.Test";
807 DirectoryInfo info
= Directory
.CreateDirectory (path
);
809 info
.MoveTo (Path
.InvalidPathChars
[0].ToString ());
811 } catch (ArgumentException ex
) {
812 // The path contains illegal characters
813 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#2");
814 Assert
.IsNull (ex
.InnerException
, "#3");
815 Assert
.IsNotNull (ex
.Message
, "#4");
816 Assert
.IsNull (ex
.ParamName
, "#5");
824 public void MoveTo_DestDirName_Whitespace ()
826 string path
= TempFolder
+ DSC
+ "DIT.MoveToArgumentException2.Test";
830 DirectoryInfo info
= Directory
.CreateDirectory (path
);
834 } catch (ArgumentException ex
) {
835 // The path is not of a legal form
836 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#2");
837 Assert
.IsNull (ex
.InnerException
, "#3");
838 Assert
.IsNotNull (ex
.Message
, "#4");
839 Assert
.IsNull (ex
.ParamName
, "#5");
847 public void MoveTo_SourceDest_NotDifferent ()
849 string path
= TempFolder
+ DSC
+ "DIT.MoveToIOException1.Test";
853 DirectoryInfo info
= Directory
.CreateDirectory (path
);
857 } catch (IOException ex
) {
858 // Source and destination path must be different
859 Assert
.AreEqual (typeof (IOException
), ex
.GetType (), "#A2");
860 Assert
.IsNull (ex
.InnerException
, "#A3");
861 Assert
.IsNotNull (ex
.Message
, "#A4");
868 DirectoryInfo info
= new DirectoryInfo (path
);
872 } catch (IOException ex
) {
873 // Source and destination path must be different
874 Assert
.AreEqual (typeof (IOException
), ex
.GetType (), "#B2");
875 Assert
.IsNull (ex
.InnerException
, "#B3");
876 Assert
.IsNotNull (ex
.Message
, "#B4");
884 public void MoveTo_UpdateProperties ()
886 string path
= TempFolder
+ DSC
+ "DIT.MoveToUpdateProperties.Test";
887 string path2
= TempFolder
+ DSC
+ "DIT.MoveToUpdateProperties2.Test";
888 string path3
= path2
+ DSC
+ "DIT.MoveToUpdateProperties3.Test";
890 Directory
.CreateDirectory (path
);
891 Directory
.CreateDirectory (path2
);
893 DirectoryInfo info
= new DirectoryInfo(path
);
895 Assert
.IsTrue (Directory
.Exists(info
.FullName
));
896 Assert
.AreEqual (path
, info
.FullName
);
897 Assert
.AreEqual ("DIT.MoveToUpdateProperties.Test", info
.Name
);
898 Assert
.AreEqual (TempFolder
, info
.Parent
.FullName
);
899 Assert
.AreEqual (path
, info
.ToString ());
902 Assert
.IsTrue (Directory
.Exists(info
.FullName
));
903 Assert
.AreEqual (path3
, info
.FullName
);
904 Assert
.AreEqual ("DIT.MoveToUpdateProperties3.Test", info
.Name
);
905 Assert
.AreEqual (path2
, info
.Parent
.FullName
);
906 Assert
.AreEqual (path3
, info
.ToString ());
910 public void DirectoryNameWithSpace ()
912 DeleteDir ("this has a space at the end ");
913 string path
= Path
.Combine (TempFolder
, "this has a space at the end ");
914 Directory
.CreateDirectory (path
);
915 DirectoryInfo i
= new DirectoryInfo (path
);
917 foreach (FileInfo f
in i
.GetFiles ())
922 public void LastWriteTime ()
924 DirectoryInfo info
= new DirectoryInfo (TempFolder
);
925 info
.LastWriteTime
= new DateTime (2003, 6, 4, 6, 4, 0);
927 DateTime time
= Directory
.GetLastWriteTime (TempFolder
);
928 Assert
.AreEqual (2003, time
.Year
, "#A1");
929 Assert
.AreEqual (6, time
.Month
, "#A2");
930 Assert
.AreEqual (4, time
.Day
, "#A3");
931 Assert
.AreEqual (6, time
.Hour
, "#A4");
932 Assert
.AreEqual (4, time
.Minute
, "#A5");
933 Assert
.AreEqual (0, time
.Second
, "#A6");
935 time
= TimeZone
.CurrentTimeZone
.ToLocalTime (
936 Directory
.GetLastWriteTimeUtc (TempFolder
));
937 Assert
.AreEqual (2003, time
.Year
, "#B1");
938 Assert
.AreEqual (6, time
.Month
, "#B2");
939 Assert
.AreEqual (4, time
.Day
, "#B3");
940 Assert
.AreEqual (6, time
.Hour
, "#B4");
941 Assert
.AreEqual (4, time
.Minute
, "#B5");
942 Assert
.AreEqual (0, time
.Second
, "#B6");
946 public void LastWriteTimeUtc ()
948 DirectoryInfo info
= new DirectoryInfo (TempFolder
);
949 info
.LastWriteTimeUtc
= new DateTime (2003, 6, 4, 6, 4, 0);
951 DateTime time
= TimeZone
.CurrentTimeZone
.ToUniversalTime (
952 Directory
.GetLastWriteTime (TempFolder
));
953 Assert
.AreEqual (2003, time
.Year
, "#A1");
954 Assert
.AreEqual (6, time
.Month
, "#A2");
955 Assert
.AreEqual (4, time
.Day
, "#A3");
956 Assert
.AreEqual (6, time
.Hour
, "#A4");
957 Assert
.AreEqual (4, time
.Minute
, "#A5");
958 Assert
.AreEqual (0, time
.Second
, "#A6");
960 time
= Directory
.GetLastWriteTimeUtc (TempFolder
);
961 Assert
.AreEqual (2003, time
.Year
, "#B1");
962 Assert
.AreEqual (6, time
.Month
, "#B2");
963 Assert
.AreEqual (4, time
.Day
, "#B3");
964 Assert
.AreEqual (6, time
.Hour
, "#B4");
965 Assert
.AreEqual (4, time
.Minute
, "#B5");
966 Assert
.AreEqual (0, time
.Second
, "#B6");
970 public void LastAccessTime ()
972 DirectoryInfo info
= new DirectoryInfo (TempFolder
);
973 info
.LastAccessTime
= DateTime
.Now
;
977 public void LastAccessTimeUtc ()
979 DirectoryInfo info
= new DirectoryInfo (TempFolder
);
980 info
.LastAccessTimeUtc
= DateTime
.Now
;
984 public void CreationTime ()
986 DirectoryInfo info
= new DirectoryInfo (TempFolder
);
987 info
.CreationTime
= DateTime
.Now
;
991 public void CreationTimeUtc ()
993 DirectoryInfo info
= new DirectoryInfo (TempFolder
);
994 info
.CreationTimeUtc
= DateTime
.Now
;
998 public void Name_Bug76903 ()
1000 CheckName ("/usr/share");
1001 CheckName ("/usr/share/");
1002 CheckName ("/usr/share/.");
1003 CheckName ("/usr/share/./");
1004 CheckName ("/usr/share/blabla/../");
1005 CheckName ("/usr/lib/../share/.");
1009 public void Hang_76191 ()
1011 // from bug #76191 (hangs on Windows)
1012 DirectoryInfo di
= new DirectoryInfo (Environment
.CurrentDirectory
);
1013 Stack s
= new Stack ();
1015 while (di
.Parent
!= null) {
1019 while (s
.Count
> 0) {
1020 di
= (DirectoryInfo
) s
.Pop ();
1021 Assert
.IsTrue (di
.Exists
, di
.Name
);
1026 public void WindowsSystem32_76191 ()
1029 Assert
.Ignore ("Running on Unix.");
1031 Directory
.SetCurrentDirectory (@"C:\WINDOWS\system32");
1032 WindowsParentFullName ("C:", "C:\\WINDOWS");
1033 WindowsParentFullName ("C:\\", null);
1034 WindowsParentFullName ("C:\\dir", "C:\\");
1035 WindowsParentFullName ("C:\\dir\\", "C:\\");
1036 WindowsParentFullName ("C:\\dir\\dir", "C:\\dir");
1037 WindowsParentFullName ("C:\\dir\\dir\\", "C:\\dir");
1041 public void Parent_Bug77090 ()
1043 DirectoryInfo di
= new DirectoryInfo ("/home");
1044 if (Path
.DirectorySeparatorChar
== '\\') {
1045 Assert
.IsTrue (di
.Parent
.Name
.EndsWith (":\\"), "#1");
1047 Assert
.AreEqual ("/", di
.Parent
.Name
, "#1");
1048 Assert
.IsNull (di
.Parent
.Parent
, "#2");
1052 public void ToStringTest ()
1056 info
= new DirectoryInfo ("Test");
1057 Assert
.AreEqual ("Test", info
.ToString (), "#1");
1058 info
= new DirectoryInfo (TempFolder
+ DSC
+ "ToString.Test");
1059 Assert
.AreEqual (TempFolder
+ DSC
+ "ToString.Test", info
.ToString ());
1064 public void Serialization ()
1067 SerializationInfo si
;
1069 info
= new DirectoryInfo ("Test");
1070 si
= new SerializationInfo (typeof (DirectoryInfo
), new FormatterConverter ());
1071 info
.GetObjectData (si
, new StreamingContext ());
1073 Assert
.AreEqual (2, si
.MemberCount
, "#A1");
1074 Assert
.AreEqual ("Test", si
.GetString ("OriginalPath"), "#A2");
1075 Assert
.AreEqual (Path
.Combine (Directory
.GetCurrentDirectory (), "Test"), si
.GetString ("FullPath"), "#A3");
1077 info
= new DirectoryInfo (TempFolder
);
1078 si
= new SerializationInfo (typeof (DirectoryInfo
), new FormatterConverter ());
1079 info
.GetObjectData (si
, new StreamingContext ());
1081 Assert
.AreEqual (2, si
.MemberCount
, "#B1");
1082 Assert
.AreEqual (TempFolder
, si
.GetString ("OriginalPath"), "#B2");
1083 Assert
.AreEqual (TempFolder
, si
.GetString ("FullPath"), "#B3");
1087 public void Deserialization ()
1089 DirectoryInfo info
= new DirectoryInfo ("Test");
1091 MemoryStream ms
= new MemoryStream ();
1092 BinaryFormatter bf
= new BinaryFormatter ();
1093 bf
.Serialize (ms
, info
);
1096 DirectoryInfo clone
= (DirectoryInfo
) bf
.Deserialize (ms
);
1097 Assert
.AreEqual (info
.Name
, clone
.Name
, "#1");
1098 Assert
.AreEqual (info
.FullName
, clone
.FullName
, "#2");
1101 // Needed so that UnixSymbolicLinkInfo doesn't have to
1102 // be JITted on windows
1103 private void Symlink_helper ()
1105 string path
= TempFolder
+ DSC
+ "DIT.Symlink";
1106 string dir
= path
+ DSC
+ "dir";
1107 string link
= path
+ DSC
+ "link";
1112 Directory
.CreateDirectory (path
);
1113 Directory
.CreateDirectory (dir
);
1114 Mono
.Unix
.UnixSymbolicLinkInfo li
= new Mono
.Unix
.UnixSymbolicLinkInfo (link
);
1115 li
.CreateSymbolicLinkTo (dir
);
1117 DirectoryInfo info
= new DirectoryInfo (path
);
1118 DirectoryInfo
[] dirs
= info
.GetDirectories ();
1119 Assert
.AreEqual (2, dirs
.Length
, "#1");
1126 [Category ("NotDotNet")]
1127 public void Symlink ()
1129 // This test only applies to Linux and
1130 // Linux-like platforms but mono-on-windows
1131 // doesn't set the NotDotNet category
1132 if (!RunningOnUnix
) {
1133 Assert
.Ignore ("Not running on Unix.");
1139 static bool RunningOnUnix
{
1141 int p
= (int) Environment
.OSVersion
.Platform
;
1142 return ((p
== 4) || (p
== 128) || (p
== 6));
1146 void WindowsParentFullName (string name
, string expected
)
1148 DirectoryInfo di
= new DirectoryInfo (name
);
1149 if (di
.Parent
== null)
1150 Assert
.IsNull (expected
, name
);
1152 Assert
.AreEqual (expected
, di
.Parent
.FullName
, name
);
1155 void CheckName (string name
)
1157 DirectoryInfo di
= new DirectoryInfo (name
);
1158 Assert
.AreEqual ("share", di
.Name
, name
+ ".Name");
1159 Assert
.AreEqual ("usr", di
.Parent
.Name
, name
+ ".Parent.Name");
1162 void DeleteDir (string path
)
1164 if (Directory
.Exists (path
))
1165 Directory
.Delete (path
, true);