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;
13 using System.Collections.Generic;
15 using System.Runtime.Serialization;
16 using System.Runtime.Serialization.Formatters.Binary;
18 using NUnit.Framework;
20 namespace MonoTests.System.IO
23 public class DirectoryInfoTest
25 string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
27 static readonly char DSC = Path.DirectorySeparatorChar;
31 protected void SetUp ()
33 current = Directory.GetCurrentDirectory ();
34 if (Directory.Exists (TempFolder))
35 Directory.Delete (TempFolder, true);
36 Directory.CreateDirectory (TempFolder);
40 protected void TearDown ()
42 if (Directory.Exists (TempFolder))
43 Directory.Delete (TempFolder, true);
44 Directory.SetCurrentDirectory (current);
47 [Test] // ctor (String)
48 public void Constructor1 ()
50 string path = TempFolder + DSC + "DIT.Ctr.Test";
53 DirectoryInfo info = new DirectoryInfo (path);
54 Assert.AreEqual ("DIT.Ctr.Test", info.Name, "#1");
55 Assert.IsFalse (info.Exists, "#2");
56 Assert.AreEqual (".Test", info.Extension, "#3");
57 Assert.AreEqual ("DIT.Ctr.Test", info.Name, "#4");
60 [Test] // ctor (String)
61 public void Constructor1_Path_Null ()
64 new DirectoryInfo (null);
66 } catch (ArgumentNullException ex) {
67 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
68 Assert.IsNull (ex.InnerException, "#3");
69 Assert.IsNotNull (ex.Message, "#4");
70 Assert.AreEqual ("path", ex.ParamName, "#5");
74 [Test] // ctor (String)
75 public void Constructor1_Path_Empty ()
78 new DirectoryInfo (string.Empty);
80 } catch (ArgumentException ex) {
81 // Empty file name is not legal
82 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
83 Assert.IsNull (ex.InnerException, "#3");
84 Assert.IsNotNull (ex.Message, "#4");
85 Assert.IsNull (ex.ParamName, "#5");
89 [Test] // ctor (String)
90 public void Constructor1_Path_Whitespace ()
93 new DirectoryInfo (" ");
95 } catch (ArgumentException ex) {
96 // The path is not of a legal form
97 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
98 Assert.IsNull (ex.InnerException, "#3");
99 Assert.IsNotNull (ex.Message, "#4");
100 Assert.IsNull (ex.ParamName, "#5");
104 [Test] // ctor (String)
105 public void Constructor1_Path_InvalidPathChars ()
107 string path = string.Empty;
108 foreach (char c in Path.InvalidPathChars)
111 new DirectoryInfo (path);
113 } catch (ArgumentException ex) {
114 // The path contains illegal characters
115 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
116 Assert.IsNull (ex.InnerException, "#3");
117 Assert.IsNotNull (ex.Message, "#4");
118 Assert.IsNull (ex.ParamName, "#5");
123 public void Exists ()
125 string path = TempFolder + DSC + "DIT.Exists.Test";
129 DirectoryInfo info = new DirectoryInfo (path);
130 Assert.IsFalse (info.Exists, "#1");
132 Directory.CreateDirectory (path);
133 Assert.IsFalse (info.Exists, "#2");
134 info = new DirectoryInfo (path);
135 Assert.IsTrue (info.Exists, "#3");
142 [Category ("MobileNotWorking")]
145 string path = TempFolder + DSC + "DIT.Name.Test";
149 DirectoryInfo info = new DirectoryInfo (path);
150 Assert.AreEqual ("DIT.Name.Test", info.Name, "#1");
152 info = Directory.CreateDirectory (path);
153 Assert.AreEqual ("DIT.Name.Test", info.Name, "#2");
155 info = Directory.CreateDirectory ("whatever");
156 Assert.AreEqual ("whatever", info.Name, "#3");
159 info = new DirectoryInfo ("/");
160 Assert.AreEqual ("/", info.Name, "#4");
162 info = new DirectoryInfo ("test/");
163 Assert.AreEqual ("test", info.Name, "#5");
165 info = new DirectoryInfo ("/test");
166 Assert.AreEqual ("test", info.Name, "#4");
168 info = new DirectoryInfo ("/test/");
169 Assert.AreEqual ("test", info.Name, "#4");
171 info = new DirectoryInfo (@"c:");
172 Assert.AreEqual (@"C:\", info.Name, "#4");
174 info = new DirectoryInfo (@"c:\");
175 Assert.AreEqual (@"c:\", info.Name, "#5");
177 info = new DirectoryInfo (@"c:\test");
178 Assert.AreEqual ("test", info.Name, "#6");
180 info = new DirectoryInfo (@"c:\test\");
181 Assert.AreEqual ("test", info.Name, "#7");
189 public void Parent ()
191 string path = TempFolder + DSC + "DIT.Parent.Test";
195 DirectoryInfo info = new DirectoryInfo (path);
196 Assert.AreEqual ("MonoTests.System.IO.Tests", info.Parent.Name, "#1");
198 info = Directory.CreateDirectory (path);
199 Assert.AreEqual ("MonoTests.System.IO.Tests", info.Parent.Name, "#2");
201 info = new DirectoryInfo ("test");
202 Assert.AreEqual (Directory.GetCurrentDirectory (), info.Parent.FullName, "#3");
205 info = new DirectoryInfo ("/");
206 Assert.IsNull (info.Parent, "#4");
208 info = new DirectoryInfo ("test/");
209 Assert.IsNotNull (info.Parent, "#5a");
210 Assert.AreEqual (Directory.GetCurrentDirectory (), info.Parent.FullName, "#5b");
212 info = new DirectoryInfo ("/test");
213 Assert.IsNotNull (info.Parent, "#6a");
214 Assert.AreEqual ("/", info.Parent.FullName, "#6b");
216 info = new DirectoryInfo ("/test/");
217 Assert.IsNotNull (info.Parent, "#7a");
218 Assert.AreEqual ("/", info.Parent.FullName, "#7b");
220 info = new DirectoryInfo (@"c:");
221 Assert.IsNull (info.Parent, "#4");
223 info = new DirectoryInfo (@"c:\");
224 Assert.IsNull (info.Parent, "#5");
226 info = new DirectoryInfo (@"c:\test");
227 Assert.IsNotNull (info.Parent, "#6a");
228 Assert.AreEqual (@"c:\", info.Parent.FullName, "#6b");
230 info = new DirectoryInfo (@"c:\test\");
231 Assert.IsNotNull (info.Parent, "#7a");
232 Assert.AreEqual (@"c:\", info.Parent.FullName, "#7b");
240 public void Create ()
242 string path = TempFolder + DSC + "DIT.Create.Test";
246 DirectoryInfo info = new DirectoryInfo (path);
247 Assert.IsFalse (info.Exists, "#1");
249 Assert.IsFalse (info.Exists, "#2");
250 info = new DirectoryInfo (path);
251 Assert.IsTrue (info.Exists, "#3");
258 public void CreateSubdirectory ()
260 string sub_path = Path.Combine ("test01", "test02");
262 DirectoryInfo info = new DirectoryInfo (TempFolder);
263 DirectoryInfo sub = info.CreateSubdirectory (sub_path);
264 Assert.IsNotNull (sub, "#1");
265 Assert.AreEqual (Path.Combine (TempFolder, sub_path), sub.FullName, "#2");
266 Assert.IsTrue (Directory.Exists (sub.FullName), "#3");
268 DeleteDir (Path.Combine (TempFolder, sub_path));
273 public void CreateSubdirectory_Path_Null ()
275 DirectoryInfo info = new DirectoryInfo (TempFolder);
277 info.CreateSubdirectory (null);
279 } catch (ArgumentNullException ex) {
280 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
281 Assert.IsNull (ex.InnerException, "#3");
282 Assert.IsNotNull (ex.Message, "#4");
283 Assert.AreEqual ("path", ex.ParamName, "#5");
288 public void CreateSubdirectory_Path_Empty ()
290 DirectoryInfo info = new DirectoryInfo (TempFolder);
292 info.CreateSubdirectory (string.Empty);
294 } catch (ArgumentException ex) {
295 // Empty file name is not legal
296 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
297 Assert.IsNull (ex.InnerException, "#3");
298 Assert.IsNotNull (ex.Message, "#4");
303 public void Delete1 ()
305 string path = TempFolder + DSC + "DIT.Delete1.Test";
309 Directory.CreateDirectory (path);
310 DirectoryInfo info = new DirectoryInfo (path);
311 Assert.IsTrue (info.Exists, "#1");
314 Assert.IsTrue (info.Exists, "#2");
316 info = new DirectoryInfo (path);
317 Assert.IsFalse (info.Exists, "#3");
324 public void Delete1_DirectoryNotEmpty ()
326 string path = TempFolder + DSC + "DIT.DeleteIOException1.Test";
330 Directory.CreateDirectory (path);
331 File.Create (path + DSC + "test").Close ();
332 DirectoryInfo info = new DirectoryInfo (path);
336 } catch (IOException ex) {
337 // The directory is not empty
338 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
339 Assert.IsNull (ex.InnerException, "#3");
340 Assert.IsNotNull (ex.Message, "#4");
347 [Test] // Delete (Boolean)
348 public void Delete2 ()
350 string path = TempFolder + DSC + "DIT.Delete2.Test";
354 Directory.CreateDirectory (path);
355 File.Create (path + DSC + "test").Close ();
356 DirectoryInfo info = new DirectoryInfo (path);
357 Assert.IsTrue (info.Exists, "#1");
360 Assert.IsTrue (info.Exists, "#2");
362 info = new DirectoryInfo (path);
363 Assert.IsFalse (info.Exists, "#3");
369 [Test] // Delete (Boolean)
370 public void Delete2_DirectoryNotEmpty ()
372 string path = TempFolder + DSC + "DIT.DeleteIOException2.Test";
376 Directory.CreateDirectory (path);
377 File.Create (path + DSC + "test").Close ();
378 DirectoryInfo info = new DirectoryInfo (path);
382 } catch (IOException ex) {
383 // The directory is not empty
384 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
385 Assert.IsNull (ex.InnerException, "#3");
386 Assert.IsNotNull (ex.Message, "#4");
394 public void FullName ()
396 DirectoryInfo di = new DirectoryInfo ("something");
397 Assert.IsFalse (di.Exists, "#A1");
398 Assert.AreEqual (Path.Combine (Directory.GetCurrentDirectory (), "something"), di.FullName, "#A2");
400 di = new DirectoryInfo ("something" + Path.DirectorySeparatorChar);
401 Assert.IsFalse (di.Exists, "#B1");
402 Assert.AreEqual (Path.DirectorySeparatorChar, di.FullName [di.FullName.Length - 1], "#B2");
404 di = new DirectoryInfo ("something" + Path.AltDirectorySeparatorChar);
405 Assert.IsFalse (di.Exists, "#C1");
406 Assert.AreEqual (Path.DirectorySeparatorChar, di.FullName [di.FullName.Length - 1], "#C2");
409 di = new DirectoryInfo ("/");
410 Assert.AreEqual ("/", di.FullName, "#D1");
412 di = new DirectoryInfo ("test/");
413 Assert.AreEqual (Path.Combine (Directory.GetCurrentDirectory (), "test/"), di.FullName, "#D2");
415 di = new DirectoryInfo ("/test");
416 Assert.AreEqual ("/test", di.FullName, "#D3");
418 di = new DirectoryInfo ("/test/");
419 Assert.AreEqual ("/test/", di.FullName, "#D4");
421 di = new DirectoryInfo (@"c:");
422 Assert.AreEqual (@"C:\", di.FullName, "#D1");
424 di = new DirectoryInfo (@"c:\");
425 Assert.AreEqual (@"c:\", di.FullName, "#D2");
427 di = new DirectoryInfo (@"c:\test");
428 Assert.AreEqual (@"c:\test", di.FullName, "#D3");
430 di = new DirectoryInfo (@"c:\test\");
431 Assert.AreEqual (@"c:\test\", di.FullName, "#D4");
436 public void FullName_RootDirectory ()
438 DirectoryInfo di = new DirectoryInfo (String.Empty + Path.DirectorySeparatorChar);
440 // can't be sure of the root drive under windows
441 Assert.AreEqual ("/", di.FullName, "#1");
443 Assert.IsNull (di.Parent, "#2");
445 di = new DirectoryInfo (String.Empty + Path.AltDirectorySeparatorChar);
447 // can't be sure of the root drive under windows
448 Assert.AreEqual ("/", di.FullName, "#3");
450 Assert.IsNull (di.Parent, "#4");
453 [Test] // GetDirectories ()
454 public void GetDirectories1 ()
456 string path = TempFolder + DSC + "DIT.GetDirectories1.Test";
459 DirectoryInfo info = Directory.CreateDirectory (path);
460 Assert.AreEqual (0, info.GetDirectories ().Length, "#1");
462 Directory.CreateDirectory (path + DSC + "1");
463 Directory.CreateDirectory (path + DSC + "2");
464 File.Create (path + DSC + "filetest").Close ();
465 Assert.AreEqual (2, info.GetDirectories ().Length, "#2");
467 Directory.Delete (path + DSC + 2);
468 Assert.AreEqual (1, info.GetDirectories ().Length, "#3");
474 [Test] // GetDirectories ()
475 public void GetDirectories1_DirectoryDoesNotExist ()
477 string path = TempFolder + DSC + "DIT.GetDirectoriesDirectoryNotFoundException1.Test";
481 DirectoryInfo info = new DirectoryInfo (path);
482 info.GetDirectories ();
484 } catch (DirectoryNotFoundException ex) {
485 // Could not find a part of '...'
486 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
487 Assert.IsNull (ex.InnerException, "#3");
488 Assert.IsNotNull (ex.Message, "#4");
489 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
495 [Test] // GetDirectories (String)
496 public void GetDirectories2 ()
498 string path = TempFolder + DSC + "DIT.GetDirectories2.Test";
501 DirectoryInfo info = Directory.CreateDirectory (path);
502 Assert.AreEqual (0, info.GetDirectories ("*").Length, "#1");
504 Directory.CreateDirectory (path + DSC + "test120");
505 Directory.CreateDirectory (path + DSC + "test210");
506 Directory.CreateDirectory (path + DSC + "atest330");
507 Directory.CreateDirectory (path + DSC + "test220");
508 Directory.CreateDirectory (path + DSC + "rest");
509 Directory.CreateDirectory (path + DSC + "rest" + DSC + "subdir");
510 File.Create (path + DSC + "filetest").Close ();
512 Assert.AreEqual (5, info.GetDirectories ("*").Length, "#2");
513 Assert.AreEqual (3, info.GetDirectories ("test*").Length, "#3");
514 Assert.AreEqual (2, info.GetDirectories ("test?20").Length, "#4");
515 Assert.AreEqual (0, info.GetDirectories ("test?").Length, "#5");
516 Assert.AreEqual (0, info.GetDirectories ("test[12]*").Length, "#6");
517 Assert.AreEqual (2, info.GetDirectories ("test2*0").Length, "#7");
518 Assert.AreEqual (4, info.GetDirectories ("*test*").Length, "#8");
519 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.");
573 [Test] // GetDirectories (String, SearchOption)
574 public void GetDirectories3_SearchPattern_Null ()
576 DirectoryInfo info = new DirectoryInfo (TempFolder);
578 info.GetDirectories (null, SearchOption.AllDirectories);
580 } catch (ArgumentNullException ex) {
581 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
582 Assert.IsNull (ex.InnerException, "#3");
583 Assert.IsNotNull (ex.Message, "#4");
584 Assert.AreEqual ("searchPattern", ex.ParamName, "#5");
588 [Test] // GetFiles ()
589 public void GetFiles1 ()
591 string path = TempFolder + DSC + "DIT.GetFiles1.Test";
595 DirectoryInfo info = Directory.CreateDirectory (path);
596 Assert.AreEqual (0, info.GetFiles ().Length, "#1");
597 File.Create (path + DSC + "file1").Close ();
598 File.Create (path + DSC + "file2").Close ();
599 Directory.CreateDirectory (path + DSC + "directory1");
600 Assert.AreEqual (2, info.GetFiles ().Length, "#2");
606 [Test] // GetFiles ()
607 public void GetFiles1_DirectoryDoesNotExist ()
609 string path = TempFolder + DSC + "DIT.GetFilesDirectoryNotFoundException1.Test";
613 DirectoryInfo info = new DirectoryInfo (path);
616 } catch (DirectoryNotFoundException ex) {
617 // Could not find a part of '...'
618 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
619 Assert.IsNull (ex.InnerException, "#3");
620 Assert.IsNotNull (ex.Message, "#4");
621 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
627 [Test] // GetFiles (String)
628 public void GetFiles2 ()
630 string path = TempFolder + DSC + "DIT.GetFiles2.Test";
634 DirectoryInfo info = Directory.CreateDirectory (path);
635 Assert.AreEqual (0, info.GetFiles ("*").Length, "#1");
636 File.Create (path + DSC + "file120file").Close ();
637 File.Create (path + DSC + "file220file").Close ();
638 File.Create (path + DSC + "afile330file").Close ();
639 File.Create (path + DSC + "test.abc").Close ();
640 File.Create (path + DSC + "test.abcd").Close ();
641 File.Create (path + DSC + "test.abcdef").Close ();
642 Directory.CreateDirectory (path + DSC + "dir");
644 Assert.AreEqual (6, info.GetFiles ("*").Length, "#2");
645 Assert.AreEqual (2, info.GetFiles ("file*file").Length, "#3");
646 Assert.AreEqual (3, info.GetFiles ("*file*").Length, "#4");
647 Assert.AreEqual (2, info.GetFiles ("file?20file").Length, "#5");
648 Assert.AreEqual (1, info.GetFiles ("*.abcd").Length, "#6");
649 Assert.AreEqual (2, info.GetFiles ("*.abcd*").Length, "#7");
655 [Test] // GetFiles (String)
656 public void GetFiles2_DirectoryDoesNotExist ()
658 string path = TempFolder + DSC + "DIT.GetFilesDirectoryNotFoundException2.Test";
662 DirectoryInfo info = new DirectoryInfo (path);
665 } catch (DirectoryNotFoundException ex) {
666 // Could not find a part of '...'
667 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
668 Assert.IsNull (ex.InnerException, "#3");
669 Assert.IsNotNull (ex.Message, "#4");
670 Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
676 [Test] // GetFiles (String)
677 public void GetFiles2_SearchPattern_Null ()
679 DirectoryInfo info = new DirectoryInfo (TempFolder);
681 info.GetFiles (null);
683 } catch (ArgumentNullException ex) {
684 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
685 Assert.IsNull (ex.InnerException, "#3");
686 Assert.IsNotNull (ex.Message, "#4");
687 Assert.AreEqual ("searchPattern", ex.ParamName, "#5");
691 [Test] // GetFiles (String, SearchOption)
692 public void GetFiles3_SearchPattern_Null ()
694 DirectoryInfo info = new DirectoryInfo (TempFolder);
696 info.GetFiles (null, SearchOption.TopDirectoryOnly);
698 } catch (ArgumentNullException ex) {
699 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
700 Assert.IsNull (ex.InnerException, "#3");
701 Assert.IsNotNull (ex.Message, "#4");
702 Assert.AreEqual ("searchPattern", ex.ParamName, "#5");
707 public void GetFileSystemInfos2_SearchPattern_Null ()
709 DirectoryInfo info = new DirectoryInfo (TempFolder);
711 info.GetFileSystemInfos (null);
713 } catch (ArgumentNullException ex) {
714 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
715 Assert.IsNull (ex.InnerException, "#3");
716 Assert.IsNotNull (ex.Message, "#4");
717 Assert.AreEqual ("searchPattern", ex.ParamName, "#5");
722 public void MoveTo ()
724 string path1 = TempFolder + DSC + "DIT.MoveTo.Soucre.Test";
725 string path2 = TempFolder + DSC + "DIT.MoveTo.Dest.Test";
730 DirectoryInfo info1 = Directory.CreateDirectory (path1);
731 DirectoryInfo info2 = new DirectoryInfo (path2);
733 Assert.IsTrue (info1.Exists, "#A1");
734 Assert.IsFalse (info2.Exists, "#A2");
736 info1.MoveTo (path2);
737 Assert.IsTrue (info1.Exists, "#B1");
738 Assert.IsFalse (info2.Exists, "#B2");
740 info1 = new DirectoryInfo (path1);
741 info2 = new DirectoryInfo (path2);
742 Assert.IsFalse (info1.Exists, "#C1");
743 Assert.IsTrue (info2.Exists, "#C2");
751 public void MoveTo_DestDirName_Empty ()
753 string path = TempFolder + DSC + "DIT.MoveToArgumentException1.Test";
757 DirectoryInfo info = Directory.CreateDirectory (path);
759 info.MoveTo (string.Empty);
761 } catch (ArgumentException ex) {
762 // Empty file name is not legal
763 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
764 Assert.IsNull (ex.InnerException, "#3");
765 Assert.IsNotNull (ex.Message, "#4");
766 Assert.AreEqual ("destDirName", ex.ParamName, "#5");
774 public void MoveTo_DestDirName_Null ()
776 string path = TempFolder + DSC + "DIT.MoveToArgumentNullException.Test";
780 DirectoryInfo info = Directory.CreateDirectory (path);
784 } catch (ArgumentNullException ex) {
785 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
786 Assert.IsNull (ex.InnerException, "#3");
787 Assert.IsNotNull (ex.Message, "#4");
788 Assert.AreEqual ("destDirName", ex.ParamName, "#5");
796 public void MoveTo_DestDirName_InvalidPathChars ()
798 string path = TempFolder + DSC + "DIT.MoveToArgumentException3.Test";
802 DirectoryInfo info = Directory.CreateDirectory (path);
804 info.MoveTo (Path.InvalidPathChars [0].ToString ());
806 } catch (ArgumentException ex) {
807 // The path contains illegal characters
808 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
809 Assert.IsNull (ex.InnerException, "#3");
810 Assert.IsNotNull (ex.Message, "#4");
811 Assert.IsNull (ex.ParamName, "#5");
819 public void MoveTo_DestDirName_Whitespace ()
821 string path = TempFolder + DSC + "DIT.MoveToArgumentException2.Test";
825 DirectoryInfo info = Directory.CreateDirectory (path);
829 } catch (ArgumentException ex) {
830 // The path is not of a legal form
831 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
832 Assert.IsNull (ex.InnerException, "#3");
833 Assert.IsNotNull (ex.Message, "#4");
834 Assert.IsNull (ex.ParamName, "#5");
842 public void MoveTo_SourceDest_NotDifferent ()
844 string path = TempFolder + DSC + "DIT.MoveToIOException1.Test";
848 DirectoryInfo info = Directory.CreateDirectory (path);
852 } catch (IOException ex) {
853 // Source and destination path must be different
854 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
855 Assert.IsNull (ex.InnerException, "#A3");
856 Assert.IsNotNull (ex.Message, "#A4");
863 DirectoryInfo info = new DirectoryInfo (path);
867 } catch (IOException ex) {
868 // Source and destination path must be different
869 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
870 Assert.IsNull (ex.InnerException, "#B3");
871 Assert.IsNotNull (ex.Message, "#B4");
879 public void MoveTo_UpdateProperties ()
881 string path = TempFolder + DSC + "DIT.MoveToUpdateProperties.Test";
882 string path2 = TempFolder + DSC + "DIT.MoveToUpdateProperties2.Test";
883 string path3 = path2 + DSC + "DIT.MoveToUpdateProperties3.Test";
885 Directory.CreateDirectory (path);
886 Directory.CreateDirectory (path2);
888 DirectoryInfo info = new DirectoryInfo(path);
890 Assert.IsTrue (Directory.Exists(info.FullName));
891 Assert.AreEqual (path, info.FullName);
892 Assert.AreEqual ("DIT.MoveToUpdateProperties.Test", info.Name);
893 Assert.AreEqual (TempFolder, info.Parent.FullName);
894 Assert.AreEqual (path, info.ToString ());
897 Assert.IsTrue (Directory.Exists(info.FullName));
898 Assert.AreEqual (path3, info.FullName);
899 Assert.AreEqual ("DIT.MoveToUpdateProperties3.Test", info.Name);
900 Assert.AreEqual (path2, info.Parent.FullName);
901 Assert.AreEqual (path3, info.ToString ());
905 public void DirectoryNameWithSpace ()
907 DeleteDir ("this has a space at the end ");
908 string path = Path.Combine (TempFolder, "this has a space at the end ");
909 Directory.CreateDirectory (path);
910 DirectoryInfo i = new DirectoryInfo (path);
912 foreach (FileInfo f in i.GetFiles ())
917 public void LastWriteTime ()
919 DirectoryInfo info = new DirectoryInfo (TempFolder);
920 info.LastWriteTime = new DateTime (2003, 6, 4, 6, 4, 0);
922 DateTime time = Directory.GetLastWriteTime (TempFolder);
923 Assert.AreEqual (2003, time.Year, "#A1");
924 Assert.AreEqual (6, time.Month, "#A2");
925 Assert.AreEqual (4, time.Day, "#A3");
926 Assert.AreEqual (6, time.Hour, "#A4");
927 Assert.AreEqual (4, time.Minute, "#A5");
928 Assert.AreEqual (0, time.Second, "#A6");
930 time = TimeZone.CurrentTimeZone.ToLocalTime (
931 Directory.GetLastWriteTimeUtc (TempFolder));
932 Assert.AreEqual (2003, time.Year, "#B1");
933 Assert.AreEqual (6, time.Month, "#B2");
934 Assert.AreEqual (4, time.Day, "#B3");
935 Assert.AreEqual (6, time.Hour, "#B4");
936 Assert.AreEqual (4, time.Minute, "#B5");
937 Assert.AreEqual (0, time.Second, "#B6");
941 public void LastWriteTimeUtc ()
943 DirectoryInfo info = new DirectoryInfo (TempFolder);
944 info.LastWriteTimeUtc = new DateTime (2003, 6, 4, 6, 4, 0);
946 DateTime time = TimeZone.CurrentTimeZone.ToUniversalTime (
947 Directory.GetLastWriteTime (TempFolder));
948 Assert.AreEqual (2003, time.Year, "#A1");
949 Assert.AreEqual (6, time.Month, "#A2");
950 Assert.AreEqual (4, time.Day, "#A3");
951 Assert.AreEqual (6, time.Hour, "#A4");
952 Assert.AreEqual (4, time.Minute, "#A5");
953 Assert.AreEqual (0, time.Second, "#A6");
955 time = Directory.GetLastWriteTimeUtc (TempFolder);
956 Assert.AreEqual (2003, time.Year, "#B1");
957 Assert.AreEqual (6, time.Month, "#B2");
958 Assert.AreEqual (4, time.Day, "#B3");
959 Assert.AreEqual (6, time.Hour, "#B4");
960 Assert.AreEqual (4, time.Minute, "#B5");
961 Assert.AreEqual (0, time.Second, "#B6");
965 public void LastAccessTime ()
967 DirectoryInfo info = new DirectoryInfo (TempFolder);
968 info.LastAccessTime = DateTime.Now;
972 public void LastAccessTimeUtc ()
974 DirectoryInfo info = new DirectoryInfo (TempFolder);
975 info.LastAccessTimeUtc = DateTime.Now;
979 public void CreationTime ()
981 DirectoryInfo info = new DirectoryInfo (TempFolder);
982 info.CreationTime = DateTime.Now;
986 public void CreationTimeUtc ()
988 DirectoryInfo info = new DirectoryInfo (TempFolder);
989 info.CreationTimeUtc = DateTime.Now;
993 public void Name_Bug76903 ()
995 CheckName ("/usr/share");
996 CheckName ("/usr/share/");
997 CheckName ("/usr/share/.");
998 CheckName ("/usr/share/./");
999 CheckName ("/usr/share/blabla/../");
1000 CheckName ("/usr/lib/../share/.");
1004 public void Hang_76191 ()
1006 // from bug #76191 (hangs on Windows)
1007 DirectoryInfo di = new DirectoryInfo (Environment.CurrentDirectory);
1008 Stack s = new Stack ();
1010 while (di.Parent != null) {
1014 while (s.Count > 0) {
1015 di = (DirectoryInfo) s.Pop ();
1016 Assert.IsTrue (di.Exists, di.Name);
1021 public void WindowsSystem32_76191 ()
1024 Assert.Ignore ("Running on Unix.");
1026 Directory.SetCurrentDirectory (@"C:\WINDOWS\system32");
1027 WindowsParentFullName ("C:", "C:\\WINDOWS");
1028 WindowsParentFullName ("C:\\", null);
1029 WindowsParentFullName ("C:\\dir", "C:\\");
1030 WindowsParentFullName ("C:\\dir\\", "C:\\");
1031 WindowsParentFullName ("C:\\dir\\dir", "C:\\dir");
1032 WindowsParentFullName ("C:\\dir\\dir\\", "C:\\dir");
1036 public void Parent_Bug77090 ()
1038 DirectoryInfo di = new DirectoryInfo ("/home");
1039 if (Path.DirectorySeparatorChar == '\\') {
1040 Assert.IsTrue (di.Parent.Name.EndsWith (":\\"), "#1");
1042 Assert.AreEqual ("/", di.Parent.Name, "#1");
1043 Assert.IsNull (di.Parent.Parent, "#2");
1047 public void ToStringTest ()
1051 info = new DirectoryInfo ("Test");
1052 Assert.AreEqual ("Test", info.ToString (), "#1");
1053 info = new DirectoryInfo (TempFolder + DSC + "ToString.Test");
1054 Assert.AreEqual (TempFolder + DSC + "ToString.Test", info.ToString ());
1058 public void EnumerateFileSystemInfosTest ()
1060 var dirInfo = new DirectoryInfo (TempFolder);
1061 dirInfo.CreateSubdirectory ("1").CreateSubdirectory ("a");
1062 dirInfo.CreateSubdirectory ("2").CreateSubdirectory ("b");
1064 var l = new List<string> ();
1065 foreach (var info in dirInfo.EnumerateFileSystemInfos ("*", SearchOption.AllDirectories))
1069 Assert.AreEqual ("1,2,a,b", string.Join (",", l), "#1");
1074 public void Serialization ()
1077 SerializationInfo si;
1079 info = new DirectoryInfo ("Test");
1080 si = new SerializationInfo (typeof (DirectoryInfo), new FormatterConverter ());
1081 info.GetObjectData (si, new StreamingContext ());
1083 Assert.AreEqual (2, si.MemberCount, "#A1");
1084 Assert.AreEqual ("Test", si.GetString ("OriginalPath"), "#A2");
1085 Assert.AreEqual (Path.Combine (Directory.GetCurrentDirectory (), "Test"), si.GetString ("FullPath"), "#A3");
1087 info = new DirectoryInfo (TempFolder);
1088 si = new SerializationInfo (typeof (DirectoryInfo), new FormatterConverter ());
1089 info.GetObjectData (si, new StreamingContext ());
1091 Assert.AreEqual (2, si.MemberCount, "#B1");
1092 Assert.AreEqual (TempFolder, si.GetString ("OriginalPath"), "#B2");
1093 Assert.AreEqual (TempFolder, si.GetString ("FullPath"), "#B3");
1097 public void Deserialization ()
1099 DirectoryInfo info = new DirectoryInfo ("Test");
1101 MemoryStream ms = new MemoryStream ();
1102 BinaryFormatter bf = new BinaryFormatter ();
1103 bf.Serialize (ms, info);
1106 DirectoryInfo clone = (DirectoryInfo) bf.Deserialize (ms);
1107 Assert.AreEqual (info.Name, clone.Name, "#1");
1108 Assert.AreEqual (info.FullName, clone.FullName, "#2");
1111 // Needed so that UnixSymbolicLinkInfo doesn't have to
1112 // be JITted on windows
1113 private void Symlink_helper ()
1115 string path = TempFolder + DSC + "DIT.Symlink";
1116 string dir = path + DSC + "dir";
1117 string link = path + DSC + "link";
1122 Directory.CreateDirectory (path);
1123 Directory.CreateDirectory (dir);
1124 global::Mono.Unix.UnixSymbolicLinkInfo li = new global::Mono.Unix.UnixSymbolicLinkInfo (link);
1125 li.CreateSymbolicLinkTo (dir);
1127 DirectoryInfo info = new DirectoryInfo (path);
1128 DirectoryInfo[] dirs = info.GetDirectories ();
1129 Assert.AreEqual (2, dirs.Length, "#1");
1136 [Category ("NotDotNet")]
1137 public void Symlink ()
1139 // This test only applies to Linux and
1140 // Linux-like platforms but mono-on-windows
1141 // doesn't set the NotDotNet category
1142 if (!RunningOnUnix) {
1143 Assert.Ignore ("Not running on Unix.");
1149 static bool RunningOnUnix {
1151 int p = (int) Environment.OSVersion.Platform;
1152 return ((p == 4) || (p == 128) || (p == 6));
1156 void WindowsParentFullName (string name, string expected)
1158 DirectoryInfo di = new DirectoryInfo (name);
1159 if (di.Parent == null)
1160 Assert.IsNull (expected, name);
1162 Assert.AreEqual (expected, di.Parent.FullName, name);
1165 void CheckName (string name)
1167 DirectoryInfo di = new DirectoryInfo (name);
1168 Assert.AreEqual ("share", di.Name, name + ".Name");
1169 Assert.AreEqual ("usr", di.Parent.Name, name + ".Parent.Name");
1172 void DeleteDir (string path)
1174 if (Directory.Exists (path))
1175 Directory.Delete (path, true);