1 // DirectoryInfoTest.cs - NUnit Test Cases for System.IO.DirectoryInfo class
\r
3 // Ville Palo (vi64pa@koti.soon.fi)
\r
5 // (C) 2003 Ville Palo
\r
7 using NUnit.Framework;
\r
10 namespace MonoTests.System.IO
\r
13 public class DirectoryInfoTest : Assertion
\r
15 string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
\r
17 static readonly char DSC = Path.DirectorySeparatorChar;
\r
20 protected void SetUp() {
\r
21 if (Directory.Exists (TempFolder))
\r
22 Directory.Delete (TempFolder, true);
\r
23 Directory.CreateDirectory (TempFolder);
\r
27 protected void TearDown() {
\r
28 if (Directory.Exists (TempFolder))
\r
29 Directory.Delete (TempFolder, true);
\r
35 string path = TempFolder + DSC + "DIT.Ctr.Test";
\r
38 FileInfo info = new FileInfo (path);
\r
39 AssertEquals ("test#01", true, info.DirectoryName.EndsWith (".Tests"));
\r
40 AssertEquals ("test#02", false, info.Exists);
\r
41 AssertEquals ("test#03", ".Test", info.Extension);
\r
42 AssertEquals ("test#05", "DIT.Ctr.Test", info.Name);
\r
46 [ExpectedException(typeof(ArgumentNullException))]
\r
47 public void CtorArgumentNullException ()
\r
49 DirectoryInfo info = new DirectoryInfo (null);
\r
53 [ExpectedException(typeof(ArgumentException))]
\r
54 public void CtorArgumentException1 ()
\r
56 DirectoryInfo info = new DirectoryInfo ("");
\r
60 [ExpectedException(typeof(ArgumentException))]
\r
61 public void CtorArgumentException2 ()
\r
63 DirectoryInfo info = new DirectoryInfo (" ");
\r
67 [ExpectedException(typeof(ArgumentException))]
\r
68 public void CtorArgumentException3 ()
\r
71 foreach (char c in Path.InvalidPathChars) {
\r
74 DirectoryInfo info = new DirectoryInfo (path);
\r
78 public void Exists ()
\r
80 string path = TempFolder + DSC + "DIT.Exists.Test";
\r
84 DirectoryInfo info = new DirectoryInfo (path);
\r
85 AssertEquals ("test#01", false, info.Exists);
\r
87 Directory.CreateDirectory (path);
\r
88 AssertEquals ("test#02", false, info.Exists);
\r
89 info = new DirectoryInfo (path);
\r
90 AssertEquals ("test#03", true, info.Exists);
\r
99 string path = TempFolder + DSC + "DIT.Name.Test";
\r
103 DirectoryInfo info = new DirectoryInfo (path);
\r
104 AssertEquals ("test#01", "DIT.Name.Test", info.Name);
\r
106 info = Directory.CreateDirectory (path);
\r
107 AssertEquals ("test#02", "DIT.Name.Test", info.Name);
\r
116 public void Parent ()
\r
118 string path = TempFolder + DSC + "DIT.Parent.Test";
\r
122 DirectoryInfo info = new DirectoryInfo (path);
\r
123 AssertEquals ("test#01", "MonoTests.System.IO.Tests", info.Parent.Name);
\r
125 info = Directory.CreateDirectory (path);
\r
126 AssertEquals ("test#02", "MonoTests.System.IO.Tests", info.Parent.Name);
\r
134 public void Create ()
\r
136 string path = TempFolder + DSC + "DIT.Create.Test";
\r
140 DirectoryInfo info = new DirectoryInfo (path);
\r
141 AssertEquals ("test#01", false, info.Exists);
\r
143 AssertEquals ("test#02", false, info.Exists);
\r
144 info = new DirectoryInfo (path);
\r
145 AssertEquals ("test#03", true, info.Exists);
\r
152 public void CreateSubdirectory ()
\r
154 string sub_path = Path.Combine ("test01", "test02");
\r
156 DirectoryInfo info = new DirectoryInfo (TempFolder);
\r
157 info.CreateSubdirectory (sub_path);
\r
158 Assert ("test#01", Directory.Exists (Path.Combine (TempFolder, sub_path)));
\r
160 DeleteDir (Path.Combine (TempFolder, sub_path));
\r
166 [ExpectedException(typeof(ArgumentException))]
167 public void CreateSubdirectoryEmptyString ()
169 new DirectoryInfo (".").CreateSubdirectory ("");
173 public void Delete1 ()
\r
175 string path = TempFolder + DSC + "DIT.Delete1.Test";
\r
179 Directory.CreateDirectory (path);
\r
180 DirectoryInfo info = new DirectoryInfo (path);
\r
181 AssertEquals ("test#01", true, info.Exists);
\r
184 AssertEquals ("test#02", true, info.Exists);
\r
186 info = new DirectoryInfo (path);
\r
187 AssertEquals ("test#03", false, info.Exists);
\r
194 public void Delete2 ()
\r
196 string path = TempFolder + DSC + "DIT.Delete2.Test";
\r
200 Directory.CreateDirectory (path);
\r
201 File.Create (path + DSC + "test").Close ();
\r
202 DirectoryInfo info = new DirectoryInfo (path);
\r
203 AssertEquals ("test#01", true, info.Exists);
\r
205 info.Delete (true);
\r
206 AssertEquals ("test#02", true, info.Exists);
\r
208 info = new DirectoryInfo (path);
\r
209 AssertEquals ("test#03", false, info.Exists);
\r
216 [ExpectedException (typeof (IOException))]
\r
217 public void DeleteIOException1 ()
\r
219 string path = TempFolder + DSC + "DIT.DeleteIOException1.Test";
\r
223 Directory.CreateDirectory (path);
\r
224 File.Create (path + DSC + "test").Close ();
\r
225 DirectoryInfo info = new DirectoryInfo (path);
\r
233 [ExpectedException (typeof (IOException))]
\r
234 public void DeleteIOException2 ()
\r
236 string path = TempFolder + DSC + "DIT.DeleteIOException2.Test";
\r
240 Directory.CreateDirectory (path);
\r
241 File.Create (path + DSC + "test").Close ();
\r
242 DirectoryInfo info = new DirectoryInfo (path);
\r
243 info.Delete (false);
\r
251 public void FullName ()
253 DirectoryInfo di = new DirectoryInfo ("something");
254 Assert ("Exists", !di.Exists);
255 Assert ("FullName", di.FullName.EndsWith ("something"));
257 di = new DirectoryInfo ("something" + Path.DirectorySeparatorChar);
258 AssertEquals ("DirectorySeparatorChar", Path.DirectorySeparatorChar, di.FullName [di.FullName.Length - 1]);
260 di = new DirectoryInfo ("something" + Path.AltDirectorySeparatorChar);
261 AssertEquals ("AltDirectorySeparatorChar", Path.DirectorySeparatorChar, di.FullName [di.FullName.Length - 1]);
265 public void FullName_RootDirectory ()
267 DirectoryInfo di = new DirectoryInfo (String.Empty + Path.DirectorySeparatorChar);
268 if (Path.DirectorySeparatorChar == '/') {
269 // can't be sure of the root drive under windows
270 AssertEquals ("FullName", di.FullName, "/");
272 AssertNull ("Parent", di.Parent);
274 di = new DirectoryInfo (String.Empty + Path.AltDirectorySeparatorChar);
275 if (Path.DirectorySeparatorChar == '/') {
276 // can't be sure of the root drive under windows
277 AssertEquals ("FullName-Alt", di.FullName, "/");
279 AssertNull ("Parent-Alt", di.Parent);
283 public void GetDirectories1 ()
\r
285 string path = TempFolder + DSC + "DIT.GetDirectories1.Test";
\r
288 DirectoryInfo info = Directory.CreateDirectory (path);
\r
289 AssertEquals ("test#01", 0, info.GetDirectories ().Length);
\r
291 Directory.CreateDirectory (path + DSC + "1");
\r
292 Directory.CreateDirectory (path + DSC + "2");
\r
293 File.Create (path + DSC + "filetest").Close ();
\r
294 AssertEquals ("test#02", 2, info.GetDirectories ().Length);
\r
296 Directory.Delete (path + DSC + 2);
\r
297 AssertEquals ("test#02", 1, info.GetDirectories ().Length);
\r
305 public void GetDirectories2 ()
\r
307 string path = TempFolder + DSC + "DIT.GetDirectories2.Test";
\r
310 DirectoryInfo info = Directory.CreateDirectory (path);
\r
311 AssertEquals ("test#01", 0, info.GetDirectories ("*").Length);
\r
313 Directory.CreateDirectory (path + DSC + "test120");
\r
314 Directory.CreateDirectory (path + DSC + "test210");
\r
315 Directory.CreateDirectory (path + DSC + "atest330");
\r
316 Directory.CreateDirectory (path + DSC + "test220");
\r
317 File.Create (path + DSC + "filetest").Close ();
\r
319 AssertEquals ("test#02", 4, info.GetDirectories ("*").Length);
\r
320 AssertEquals ("test#03", 3, info.GetDirectories ("test*").Length);
\r
321 AssertEquals ("test#04", 2, info.GetDirectories ("test?20").Length);
\r
322 AssertEquals ("test#05", 0, info.GetDirectories ("test?").Length);
\r
323 AssertEquals ("test#06", 0, info.GetDirectories ("test[12]*").Length);
\r
324 AssertEquals ("test#07", 2, info.GetDirectories ("test2*0").Length);
\r
325 AssertEquals ("test#08", 4, info.GetDirectories ("*test*").Length);
\r
333 [ExpectedException (typeof (DirectoryNotFoundException))]
\r
334 public void GetDirectoriesDirectoryNotFoundException1 ()
\r
336 string path = TempFolder + DSC + "DIT.GetDirectoriesDirectoryNotFoundException1.Test";
\r
340 DirectoryInfo info = new DirectoryInfo (path);
\r
341 info.GetDirectories ();
\r
348 [ExpectedException (typeof (DirectoryNotFoundException))]
\r
349 public void GetDirectoriesDirectoryNotFoundException2 ()
\r
351 string path = TempFolder + DSC + "DIT.GetDirectoriesDirectoryNotFoundException2.Test";
\r
355 DirectoryInfo info = new DirectoryInfo (path);
\r
356 info.GetDirectories ("*");
\r
363 [ExpectedException (typeof (ArgumentNullException))]
\r
364 public void GetDirectoriesArgumentNullException ()
\r
366 string path = TempFolder + DSC + "DIT.GetDirectoriesArgumentNullException.Test";
\r
370 DirectoryInfo info = new DirectoryInfo (path);
\r
371 info.GetDirectories (null);
\r
378 public void GetFiles1 ()
\r
380 string path = TempFolder + DSC + "DIT.GetFiles1.Test";
\r
384 DirectoryInfo info = Directory.CreateDirectory (path);
\r
385 AssertEquals ("test#01", 0, info.GetFiles ().Length);
\r
386 File.Create (path + DSC + "file1").Close ();
\r
387 File.Create (path + DSC + "file2").Close ();
\r
388 Directory.CreateDirectory (path + DSC + "directory1");
\r
389 AssertEquals ("test#02", 2, info.GetFiles ().Length);
\r
397 public void GetFiles2()
\r
399 string path = TempFolder + DSC + "DIT.GetFiles2.Test";
\r
403 DirectoryInfo info = Directory.CreateDirectory (path);
\r
404 AssertEquals ("test#01", 0, info.GetFiles ("*").Length);
\r
405 File.Create (path + DSC + "file120file").Close ();
\r
406 File.Create (path + DSC + "file220file").Close ();
\r
407 File.Create (path + DSC + "afile330file").Close ();
\r
408 File.Create (path + DSC + "test.abc").Close ();
\r
409 File.Create (path + DSC + "test.abcd").Close ();
\r
410 File.Create (path + DSC + "test.abcdef").Close ();
\r
411 Directory.CreateDirectory (path + DSC + "dir");
\r
413 AssertEquals ("test#02", 6, info.GetFiles ("*").Length);
\r
414 AssertEquals ("test#03", 2, info.GetFiles ("file*file").Length);
\r
415 AssertEquals ("test#04", 3, info.GetFiles ("*file*").Length);
\r
416 AssertEquals ("test#05", 2, info.GetFiles ("file?20file").Length);
\r
417 AssertEquals ("test#07", 1, info.GetFiles ("*.abcd").Length);
\r
418 AssertEquals ("test#08", 2, info.GetFiles ("*.abcd*").Length);
\r
425 [ExpectedException (typeof (DirectoryNotFoundException))]
\r
426 public void GetFilesDirectoryNotFoundException1 ()
\r
428 string path = TempFolder + DSC + "DIT.GetFilesDirectoryNotFoundException1.Test";
\r
432 DirectoryInfo info = new DirectoryInfo (path);
\r
441 [ExpectedException (typeof (DirectoryNotFoundException))]
\r
442 public void GetFilesDirectoryNotFoundException2 ()
\r
444 string path = TempFolder + DSC + "DIT.GetFilesDirectoryNotFoundException2.Test";
\r
448 DirectoryInfo info = new DirectoryInfo (path);
\r
449 info.GetFiles ("*");
\r
457 [ExpectedException (typeof (ArgumentNullException))]
\r
458 public void GetFilesArgumentNullException ()
\r
460 string path = TempFolder + DSC + "DIT.GetFilesArgumentNullException.Test";
\r
464 DirectoryInfo info = new DirectoryInfo (path);
\r
465 info.GetFiles (null);
\r
472 public void MoveTo ()
\r
474 string path1 = TempFolder + DSC + "DIT.MoveTo.Soucre.Test";
\r
475 string path2 = TempFolder + DSC + "DIT.MoveTo.Dest.Test";
\r
480 DirectoryInfo info1 = Directory.CreateDirectory (path1);
\r
481 DirectoryInfo info2 = new DirectoryInfo (path2);
\r
483 AssertEquals ("test#01", true, info1.Exists);
\r
484 AssertEquals ("test#02", false, info2.Exists);
\r
486 info1.MoveTo (path2);
\r
487 AssertEquals ("test#03", true, info1.Exists);
\r
488 AssertEquals ("test#04", false, info2.Exists);
\r
490 info1 = new DirectoryInfo (path1);
\r
491 info2 = new DirectoryInfo (path2);
\r
492 AssertEquals ("test#05", false, info1.Exists);
\r
493 AssertEquals ("test#06", true, info2.Exists);
\r
502 [ExpectedException (typeof (ArgumentNullException))]
\r
503 public void MoveToArgumentNullException ()
\r
505 string path = TempFolder + DSC + "DIT.MoveToArgumentNullException.Test";
\r
509 DirectoryInfo info = Directory.CreateDirectory (path);
\r
510 info.MoveTo (null);
\r
518 [ExpectedException (typeof (IOException))]
\r
519 public void MoveToIOException1 ()
\r
521 string path = TempFolder + DSC + "DIT.MoveToIOException1.Test";
\r
525 DirectoryInfo info = Directory.CreateDirectory (path);
\r
526 info.MoveTo (path);
\r
533 [ExpectedException (typeof (ArgumentException))]
\r
534 public void MoveToArgumentException1 ()
\r
536 string path = TempFolder + DSC + "DIT.MoveToArgumentException1.Test";
\r
540 DirectoryInfo info = Directory.CreateDirectory (path);
\r
548 [ExpectedException (typeof (ArgumentException))]
\r
549 public void MoveToArgumentException2 ()
\r
551 string path = TempFolder + DSC + "DIT.MoveToArgumentException2.Test";
\r
555 DirectoryInfo info = Directory.CreateDirectory (path);
\r
563 [ExpectedException (typeof (ArgumentException))]
\r
564 public void MoveToArgumentException3 ()
\r
566 string path = TempFolder + DSC + "DIT.MoveToArgumentException3.Test";
\r
570 DirectoryInfo info = Directory.CreateDirectory (path);
\r
571 info.MoveTo (Path.InvalidPathChars [0].ToString ());
\r
578 [ExpectedException (typeof (IOException))]
\r
579 public void MoveToIOException2 ()
\r
581 string path = TempFolder + DSC + "DIT.MoveToIOException2.Test";
\r
585 DirectoryInfo info = new DirectoryInfo (path);
\r
586 info.MoveTo (path);
\r
592 private void DeleteDir (string path)
\r
594 if (Directory.Exists (path))
\r
595 Directory.Delete (path, true);
\r
599 public void DirectoryNameWithSpace ()
601 // check for Unix platforms - see FAQ for more details
602 // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
603 int platform = (int) Environment.OSVersion.Platform;
604 if ((platform == 4) || (platform == 128)) {
605 DeleteDir ("this has a space at the end ");
606 string path = Path.Combine (TempFolder, "this has a space at the end ");
607 Directory.CreateDirectory (path);
608 DirectoryInfo i = new DirectoryInfo (path);
610 foreach (FileInfo f in i.GetFiles ()) // This used to throw
616 public void LastWriteTime ()
\r
618 DirectoryInfo info = new DirectoryInfo (TempFolder);
\r
619 info.LastWriteTime = new DateTime (2003, 6, 4, 6, 4, 0);
\r
621 DateTime time = Directory.GetLastWriteTime (TempFolder);
\r
622 AssertEquals ("test#01", 2003, time.Year);
\r
623 AssertEquals ("test#02", 6, time.Month);
\r
624 AssertEquals ("test#03", 4, time.Day);
\r
625 AssertEquals ("test#04", 6, time.Hour);
\r
626 AssertEquals ("test#05", 4, time.Minute);
\r
627 AssertEquals ("test#06", 0, time.Second);
\r
629 time = TimeZone.CurrentTimeZone.ToLocalTime (
\r
630 Directory.GetLastWriteTimeUtc (TempFolder));
\r
631 AssertEquals ("test#07", 2003, time.Year);
\r
632 AssertEquals ("test#08", 6, time.Month);
\r
633 AssertEquals ("test#09", 4, time.Day);
\r
634 AssertEquals ("test#10", 6, time.Hour);
\r
635 AssertEquals ("test#11", 4, time.Minute);
\r
636 AssertEquals ("test#12", 0, time.Second);
\r
640 public void LastWriteTimeUtc ()
\r
642 DirectoryInfo info = new DirectoryInfo (TempFolder);
\r
643 info.LastWriteTimeUtc = new DateTime (2003, 6, 4, 6, 4, 0);
\r
645 DateTime time = TimeZone.CurrentTimeZone.ToUniversalTime (
\r
646 Directory.GetLastWriteTime (TempFolder));
\r
647 AssertEquals ("test#1", 2003, time.Year);
\r
648 AssertEquals ("test#2", 6, time.Month);
\r
649 AssertEquals ("test#3", 4, time.Day);
\r
650 AssertEquals ("test#4", 6, time.Hour);
\r
651 AssertEquals ("test#5", 4, time.Minute);
\r
652 AssertEquals ("test#6", 0, time.Second);
\r
654 time = Directory.GetLastWriteTimeUtc (TempFolder);
\r
655 AssertEquals ("test#7", 2003, time.Year);
\r
656 AssertEquals ("test#8", 6, time.Month);
\r
657 AssertEquals ("test#9", 4, time.Day);
\r
658 AssertEquals ("test#10", 6, time.Hour);
\r
659 AssertEquals ("test#11", 4, time.Minute);
\r
660 AssertEquals ("test#12", 0, time.Second);
\r
664 public void LastAccessTime ()
\r
666 DirectoryInfo info = new DirectoryInfo (TempFolder);
\r
667 info.LastAccessTime = DateTime.Now;
\r
671 public void LastAccessTimeUtc ()
\r
673 DirectoryInfo info = new DirectoryInfo (TempFolder);
\r
674 info.LastAccessTimeUtc = DateTime.Now;
\r
678 public void CreationTime ()
\r
680 DirectoryInfo info = new DirectoryInfo (TempFolder);
\r
681 info.CreationTime = DateTime.Now;
\r
685 public void CreationTimeUtc ()
\r
687 DirectoryInfo info = new DirectoryInfo (TempFolder);
\r
688 info.CreationTimeUtc = DateTime.Now;
\r