[Cleanup] Removed TARGET_JVM
[mono-project.git] / mcs / class / corlib / Test / System.IO / DirectoryInfoTest.cs
blob699f94be8031c1c8a562a270552f2c2cd2cb87dd
1 // DirectoryInfoTest.cs - NUnit Test Cases for System.IO.DirectoryInfo class
2 //
3 // Authors
4 // Ville Palo (vi64pa@koti.soon.fi)
5 // Sebastien Pouliot <sebastien@ximian.com>
6 //
7 // (C) 2003 Ville Palo
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
11 using System;
12 using System.Collections;
13 using System.IO;
14 using System.Runtime.Serialization;
15 using System.Runtime.Serialization.Formatters.Binary;
17 using NUnit.Framework;
19 namespace MonoTests.System.IO
21 [TestFixture]
22 public class DirectoryInfoTest
24 string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
26 static readonly char DSC = Path.DirectorySeparatorChar;
27 string current;
29 [SetUp]
30 protected void SetUp ()
32 current = Directory.GetCurrentDirectory ();
33 if (Directory.Exists (TempFolder))
34 Directory.Delete (TempFolder, true);
35 Directory.CreateDirectory (TempFolder);
38 [TearDown]
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";
50 DeleteDir (path);
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 ()
62 try {
63 new DirectoryInfo (null);
64 Assert.Fail ("#1");
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 ()
76 try {
77 new DirectoryInfo (string.Empty);
78 Assert.Fail ("#1");
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 ()
91 try {
92 new DirectoryInfo (" ");
93 Assert.Fail ("#1");
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)
108 path += c;
109 try {
110 new DirectoryInfo (path);
111 Assert.Fail ("#1");
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");
121 [Test]
122 public void Exists ()
124 string path = TempFolder + DSC + "DIT.Exists.Test";
125 DeleteDir (path);
127 try {
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");
135 } finally {
136 DeleteDir (path);
140 [Test]
141 [Category ("MobileNotWorking")]
142 public void Name ()
144 string path = TempFolder + DSC + "DIT.Name.Test";
145 DeleteDir (path);
147 try {
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");
157 if (RunningOnUnix) {
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");
169 } else {
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");
182 } finally {
183 DeleteDir (path);
187 [Test]
188 public void Parent ()
190 string path = TempFolder + DSC + "DIT.Parent.Test";
191 DeleteDir (path);
193 try {
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");
203 if (RunningOnUnix) {
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");
218 } else {
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");
233 } finally {
234 DeleteDir (path);
238 [Test]
239 public void Create ()
241 string path = TempFolder + DSC + "DIT.Create.Test";
242 DeleteDir (path);
244 try {
245 DirectoryInfo info = new DirectoryInfo (path);
246 Assert.IsFalse (info.Exists, "#1");
247 info.Create ();
248 Assert.IsFalse (info.Exists, "#2");
249 info = new DirectoryInfo (path);
250 Assert.IsTrue (info.Exists, "#3");
251 } finally {
252 DeleteDir (path);
256 [Test]
257 public void CreateSubdirectory ()
259 string sub_path = Path.Combine ("test01", "test02");
260 try {
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");
266 } finally {
267 DeleteDir (Path.Combine (TempFolder, sub_path));
271 [Test]
272 public void CreateSubdirectory_Path_Null ()
274 DirectoryInfo info = new DirectoryInfo (TempFolder);
275 try {
276 info.CreateSubdirectory (null);
277 Assert.Fail ("#1");
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");
286 [Test]
287 public void CreateSubdirectory_Path_Empty ()
289 DirectoryInfo info = new DirectoryInfo (TempFolder);
290 try {
291 info.CreateSubdirectory (string.Empty);
292 Assert.Fail ("#1");
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");
301 [Test] // Delete ()
302 public void Delete1 ()
304 string path = TempFolder + DSC + "DIT.Delete1.Test";
305 DeleteDir (path);
307 try {
308 Directory.CreateDirectory (path);
309 DirectoryInfo info = new DirectoryInfo (path);
310 Assert.IsTrue (info.Exists, "#1");
312 info.Delete ();
313 Assert.IsTrue (info.Exists, "#2");
315 info = new DirectoryInfo (path);
316 Assert.IsFalse (info.Exists, "#3");
317 } finally {
318 DeleteDir (path);
322 [Test] // Delete ()
323 public void Delete1_DirectoryNotEmpty ()
325 string path = TempFolder + DSC + "DIT.DeleteIOException1.Test";
326 DeleteDir (path);
328 try {
329 Directory.CreateDirectory (path);
330 File.Create (path + DSC + "test").Close ();
331 DirectoryInfo info = new DirectoryInfo (path);
332 try {
333 info.Delete ();
334 Assert.Fail ("#1");
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");
341 } finally {
342 DeleteDir (path);
346 [Test] // Delete (Boolean)
347 public void Delete2 ()
349 string path = TempFolder + DSC + "DIT.Delete2.Test";
350 DeleteDir (path);
352 try {
353 Directory.CreateDirectory (path);
354 File.Create (path + DSC + "test").Close ();
355 DirectoryInfo info = new DirectoryInfo (path);
356 Assert.IsTrue (info.Exists, "#1");
358 info.Delete (true);
359 Assert.IsTrue (info.Exists, "#2");
361 info = new DirectoryInfo (path);
362 Assert.IsFalse (info.Exists, "#3");
363 } finally {
364 DeleteDir (path);
368 [Test] // Delete (Boolean)
369 public void Delete2_DirectoryNotEmpty ()
371 string path = TempFolder + DSC + "DIT.DeleteIOException2.Test";
372 DeleteDir (path);
374 try {
375 Directory.CreateDirectory (path);
376 File.Create (path + DSC + "test").Close ();
377 DirectoryInfo info = new DirectoryInfo (path);
378 try {
379 info.Delete (false);
380 Assert.Fail ("#1");
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");
387 } finally {
388 DeleteDir (path);
392 [Test] // bug #75443
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");
407 if (RunningOnUnix) {
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");
419 } else {
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");
434 [Test]
435 public void FullName_RootDirectory ()
437 DirectoryInfo di = new DirectoryInfo (String.Empty + Path.DirectorySeparatorChar);
438 if (RunningOnUnix) {
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);
445 if (RunningOnUnix) {
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";
457 try {
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");
468 } finally {
469 DeleteDir (path);
473 [Test] // GetDirectories ()
474 public void GetDirectories1_DirectoryDoesNotExist ()
476 string path = TempFolder + DSC + "DIT.GetDirectoriesDirectoryNotFoundException1.Test";
477 DeleteDir (path);
479 try {
480 DirectoryInfo info = new DirectoryInfo (path);
481 info.GetDirectories ();
482 Assert.Fail ("#1");
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");
489 } finally {
490 DeleteDir (path);
494 [Test] // GetDirectories (String)
495 public void GetDirectories2 ()
497 string path = TempFolder + DSC + "DIT.GetDirectories2.Test";
499 try {
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");
518 #if NET_2_0
519 Assert.AreEqual (6, info.GetDirectories ("*", SearchOption.AllDirectories).Length, "#9");
520 #endif
521 } finally {
522 DeleteDir (path);
526 [Test] // GetDirectories (String)
527 public void GetDirectories2_DirectoryDoesNotExist ()
529 string path = TempFolder + DSC + "DIT.GetDirectoriesDirectoryNotFoundException2.Test";
530 DeleteDir (path);
532 try {
533 DirectoryInfo info = new DirectoryInfo (path);
534 info.GetDirectories ("*");
535 Assert.Fail ("#1");
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");
542 } finally {
543 DeleteDir (path);
547 [Test] // GetDirectories (String)
548 public void GetDirectories2_SearchPattern_Null ()
550 DirectoryInfo info = new DirectoryInfo (TempFolder);
551 try {
552 info.GetDirectories (null);
553 Assert.Fail ("#1");
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.");
574 #if NET_2_0
575 [Test] // GetDirectories (String, SearchOption)
576 public void GetDirectories3_SearchPattern_Null ()
578 DirectoryInfo info = new DirectoryInfo (TempFolder);
579 try {
580 info.GetDirectories (null, SearchOption.AllDirectories);
581 Assert.Fail ("#1");
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");
589 #endif
591 [Test] // GetFiles ()
592 public void GetFiles1 ()
594 string path = TempFolder + DSC + "DIT.GetFiles1.Test";
595 DeleteDir (path);
597 try {
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");
604 } finally {
605 DeleteDir (path);
609 [Test] // GetFiles ()
610 public void GetFiles1_DirectoryDoesNotExist ()
612 string path = TempFolder + DSC + "DIT.GetFilesDirectoryNotFoundException1.Test";
613 DeleteDir (path);
615 try {
616 DirectoryInfo info = new DirectoryInfo (path);
617 info.GetFiles ();
618 Assert.Fail ("#1");
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");
625 } finally {
626 DeleteDir (path);
630 [Test] // GetFiles (String)
631 public void GetFiles2 ()
633 string path = TempFolder + DSC + "DIT.GetFiles2.Test";
634 DeleteDir (path);
636 try {
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");
653 } finally {
654 DeleteDir (path);
658 [Test] // GetFiles (String)
659 public void GetFiles2_DirectoryDoesNotExist ()
661 string path = TempFolder + DSC + "DIT.GetFilesDirectoryNotFoundException2.Test";
662 DeleteDir (path);
664 try {
665 DirectoryInfo info = new DirectoryInfo (path);
666 info.GetFiles ("*");
667 Assert.Fail ("#1");
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");
674 } finally {
675 DeleteDir (path);
679 [Test] // GetFiles (String)
680 public void GetFiles2_SearchPattern_Null ()
682 DirectoryInfo info = new DirectoryInfo (TempFolder);
683 try {
684 info.GetFiles (null);
685 Assert.Fail ("#1");
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");
694 #if NET_2_0
695 [Test] // GetFiles (String, SearchOption)
696 public void GetFiles3_SearchPattern_Null ()
698 DirectoryInfo info = new DirectoryInfo (TempFolder);
699 try {
700 info.GetFiles (null, SearchOption.TopDirectoryOnly);
701 Assert.Fail ("#1");
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");
709 #endif
711 [Test]
712 public void GetFileSystemInfos2_SearchPattern_Null ()
714 DirectoryInfo info = new DirectoryInfo (TempFolder);
715 try {
716 info.GetFileSystemInfos (null);
717 Assert.Fail ("#1");
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");
726 [Test]
727 public void MoveTo ()
729 string path1 = TempFolder + DSC + "DIT.MoveTo.Soucre.Test";
730 string path2 = TempFolder + DSC + "DIT.MoveTo.Dest.Test";
731 DeleteDir (path1);
732 DeleteDir (path2);
734 try {
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");
749 } finally {
750 DeleteDir (path1);
751 DeleteDir (path2);
755 [Test]
756 public void MoveTo_DestDirName_Empty ()
758 string path = TempFolder + DSC + "DIT.MoveToArgumentException1.Test";
759 DeleteDir (path);
761 try {
762 DirectoryInfo info = Directory.CreateDirectory (path);
763 try {
764 info.MoveTo (string.Empty);
765 Assert.Fail ("#1");
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");
773 } finally {
774 DeleteDir (path);
778 [Test]
779 public void MoveTo_DestDirName_Null ()
781 string path = TempFolder + DSC + "DIT.MoveToArgumentNullException.Test";
782 DeleteDir (path);
784 try {
785 DirectoryInfo info = Directory.CreateDirectory (path);
786 try {
787 info.MoveTo (null);
788 Assert.Fail ("#1");
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");
795 } finally {
796 DeleteDir (path);
800 [Test]
801 public void MoveTo_DestDirName_InvalidPathChars ()
803 string path = TempFolder + DSC + "DIT.MoveToArgumentException3.Test";
804 DeleteDir (path);
806 try {
807 DirectoryInfo info = Directory.CreateDirectory (path);
808 try {
809 info.MoveTo (Path.InvalidPathChars [0].ToString ());
810 Assert.Fail ("#1");
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");
818 } finally {
819 DeleteDir (path);
823 [Test]
824 public void MoveTo_DestDirName_Whitespace ()
826 string path = TempFolder + DSC + "DIT.MoveToArgumentException2.Test";
827 DeleteDir (path);
829 try {
830 DirectoryInfo info = Directory.CreateDirectory (path);
831 try {
832 info.MoveTo (" ");
833 Assert.Fail ("#1");
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");
841 } finally {
842 DeleteDir (path);
846 [Test]
847 public void MoveTo_SourceDest_NotDifferent ()
849 string path = TempFolder + DSC + "DIT.MoveToIOException1.Test";
850 DeleteDir (path);
852 try {
853 DirectoryInfo info = Directory.CreateDirectory (path);
854 try {
855 info.MoveTo (path);
856 Assert.Fail ("#A1");
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");
863 } finally {
864 DeleteDir (path);
867 try {
868 DirectoryInfo info = new DirectoryInfo (path);
869 try {
870 info.MoveTo (path);
871 Assert.Fail ("#B1");
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");
878 } finally {
879 DeleteDir (path);
883 [Test]
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";
889 DeleteDir (path);
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 ());
901 info.MoveTo (path3);
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 ());
909 [Test]
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);
916 string dummy = null;
917 foreach (FileInfo f in i.GetFiles ())
918 dummy = f.Name;
921 [Test]
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");
945 [Test]
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");
969 [Test]
970 public void LastAccessTime ()
972 DirectoryInfo info = new DirectoryInfo (TempFolder);
973 info.LastAccessTime = DateTime.Now;
976 [Test]
977 public void LastAccessTimeUtc ()
979 DirectoryInfo info = new DirectoryInfo (TempFolder);
980 info.LastAccessTimeUtc = DateTime.Now;
983 [Test]
984 public void CreationTime ()
986 DirectoryInfo info = new DirectoryInfo (TempFolder);
987 info.CreationTime = DateTime.Now;
990 [Test]
991 public void CreationTimeUtc ()
993 DirectoryInfo info = new DirectoryInfo (TempFolder);
994 info.CreationTimeUtc = DateTime.Now;
997 [Test]
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/.");
1008 [Test]
1009 public void Hang_76191 ()
1011 // from bug #76191 (hangs on Windows)
1012 DirectoryInfo di = new DirectoryInfo (Environment.CurrentDirectory);
1013 Stack s = new Stack ();
1014 s.Push (di);
1015 while (di.Parent != null) {
1016 di = di.Parent;
1017 s.Push (di);
1019 while (s.Count > 0) {
1020 di = (DirectoryInfo) s.Pop ();
1021 Assert.IsTrue (di.Exists, di.Name);
1025 [Test]
1026 public void WindowsSystem32_76191 ()
1028 if (RunningOnUnix)
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");
1040 [Test]
1041 public void Parent_Bug77090 ()
1043 DirectoryInfo di = new DirectoryInfo ("/home");
1044 if (Path.DirectorySeparatorChar == '\\') {
1045 Assert.IsTrue (di.Parent.Name.EndsWith (":\\"), "#1");
1046 } else
1047 Assert.AreEqual ("/", di.Parent.Name, "#1");
1048 Assert.IsNull (di.Parent.Parent, "#2");
1051 [Test]
1052 public void ToStringTest ()
1054 DirectoryInfo info;
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 ());
1062 #if !MOBILE
1063 [Test]
1064 public void Serialization ()
1066 DirectoryInfo info;
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");
1086 [Test]
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);
1094 ms.Position = 0;
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";
1109 DeleteDir (path);
1111 try {
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");
1120 } finally {
1121 DeleteDir (path);
1125 [Test]
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.");
1136 Symlink_helper ();
1138 #endif
1139 static bool RunningOnUnix {
1140 get {
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);
1151 else
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);