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
18 protected void SetUp() {
\r
19 if (Directory.Exists (TempFolder))
\r
20 Directory.Delete (TempFolder, true);
\r
21 Directory.CreateDirectory (TempFolder);
\r
25 protected void TearDown() {
\r
26 if (Directory.Exists (TempFolder))
\r
27 Directory.Delete (TempFolder, true);
\r
33 string path = TempFolder + "/DIT.Ctr.Test";
\r
36 FileInfo info = new FileInfo (path);
\r
37 AssertEquals ("test#01", true, info.DirectoryName.EndsWith (".Tests"));
\r
38 AssertEquals ("test#02", false, info.Exists);
\r
39 AssertEquals ("test#03", ".Test", info.Extension);
\r
40 AssertEquals ("test#05", "DIT.Ctr.Test", info.Name);
\r
44 [ExpectedException(typeof(ArgumentNullException))]
\r
45 public void CtorArgumentNullException ()
\r
47 DirectoryInfo info = new DirectoryInfo (null);
\r
51 [ExpectedException(typeof(ArgumentException))]
\r
52 public void CtorArgumentException1 ()
\r
54 DirectoryInfo info = new DirectoryInfo ("");
\r
58 [ExpectedException(typeof(ArgumentException))]
\r
59 public void CtorArgumentException2 ()
\r
61 DirectoryInfo info = new DirectoryInfo (" ");
\r
65 [ExpectedException(typeof(ArgumentException))]
\r
66 public void CtorArgumentException3 ()
\r
69 foreach (char c in Path.InvalidPathChars) {
\r
72 DirectoryInfo info = new DirectoryInfo (path);
\r
76 public void Exists ()
\r
78 string path = TempFolder + "/DIT.Exists.Test";
\r
82 DirectoryInfo info = new DirectoryInfo (path);
\r
83 AssertEquals ("test#01", false, info.Exists);
\r
85 Directory.CreateDirectory (path);
\r
86 AssertEquals ("test#02", false, info.Exists);
\r
87 info = new DirectoryInfo (path);
\r
88 AssertEquals ("test#03", true, info.Exists);
\r
97 string path = TempFolder + "/DIT.Name.Test";
\r
101 DirectoryInfo info = new DirectoryInfo (path);
\r
102 AssertEquals ("test#01", "DIT.Name.Test", info.Name);
\r
104 info = Directory.CreateDirectory (path);
\r
105 AssertEquals ("test#02", "DIT.Name.Test", info.Name);
\r
114 public void Parent ()
\r
116 string path = TempFolder + "/DIT.Parent.Test";
\r
120 DirectoryInfo info = new DirectoryInfo (path);
\r
121 AssertEquals ("test#01", "MonoTests.System.IO.Tests", info.Parent.Name);
\r
123 info = Directory.CreateDirectory (path);
\r
124 AssertEquals ("test#02", "MonoTests.System.IO.Tests", info.Parent.Name);
\r
132 public void Create ()
\r
134 string path = TempFolder + "/DIT.Create.Test";
\r
138 DirectoryInfo info = new DirectoryInfo (path);
\r
139 AssertEquals ("test#01", false, info.Exists);
\r
141 AssertEquals ("test#02", false, info.Exists);
\r
142 info = new DirectoryInfo (path);
\r
143 AssertEquals ("test#03", true, info.Exists);
\r
150 public void CreateSubdirectory ()
\r
152 string sub_path = Path.Combine ("test01", "test02");
\r
154 DirectoryInfo info = new DirectoryInfo (TempFolder);
\r
155 info.CreateSubdirectory (sub_path);
\r
156 Assert ("test#01", Directory.Exists (Path.Combine (TempFolder, sub_path)));
\r
158 DeleteDir (Path.Combine (TempFolder, sub_path));
\r
164 public void Delete1 ()
\r
166 string path = TempFolder + "/DIT.Delete1.Test";
\r
170 Directory.CreateDirectory (path);
\r
171 DirectoryInfo info = new DirectoryInfo (path);
\r
172 AssertEquals ("test#01", true, info.Exists);
\r
175 AssertEquals ("test#02", true, info.Exists);
\r
177 info = new DirectoryInfo (path);
\r
178 AssertEquals ("test#03", false, info.Exists);
\r
185 public void Delete2 ()
\r
187 string path = TempFolder + "/DIT.Delete2.Test";
\r
191 Directory.CreateDirectory (path);
\r
192 File.Create (path + "/test").Close ();
\r
193 DirectoryInfo info = new DirectoryInfo (path);
\r
194 AssertEquals ("test#01", true, info.Exists);
\r
196 info.Delete (true);
\r
197 AssertEquals ("test#02", true, info.Exists);
\r
199 info = new DirectoryInfo (path);
\r
200 AssertEquals ("test#03", false, info.Exists);
\r
207 [ExpectedException (typeof (IOException))]
\r
208 public void DeleteIOException1 ()
\r
210 string path = TempFolder + "/DIT.DeleteIOException1.Test";
\r
214 Directory.CreateDirectory (path);
\r
215 File.Create (path + "/test").Close ();
\r
216 DirectoryInfo info = new DirectoryInfo (path);
\r
224 [ExpectedException (typeof (IOException))]
\r
225 public void DeleteIOException2 ()
\r
227 string path = TempFolder + "/DIT.DeleteIOException2.Test";
\r
231 Directory.CreateDirectory (path);
\r
232 File.Create (path + "/test").Close ();
\r
233 DirectoryInfo info = new DirectoryInfo (path);
\r
234 info.Delete (false);
\r
241 public void GetDirectories1 ()
\r
243 string path = TempFolder + "/DIT.GetDirectories1.Test";
\r
246 DirectoryInfo info = Directory.CreateDirectory (path);
\r
247 AssertEquals ("test#01", 0, info.GetDirectories ().Length);
\r
249 Directory.CreateDirectory (path + "/" + "1");
\r
250 Directory.CreateDirectory (path + "/" + "2");
\r
251 File.Create (path + "/" + "filetest").Close ();
\r
252 AssertEquals ("test#02", 2, info.GetDirectories ().Length);
\r
254 Directory.Delete (path + "/" + 2);
\r
255 AssertEquals ("test#02", 1, info.GetDirectories ().Length);
\r
263 public void GetDirectories2 ()
\r
265 string path = TempFolder + "/DIT.GetDirectories2.Test";
\r
268 DirectoryInfo info = Directory.CreateDirectory (path);
\r
269 AssertEquals ("test#01", 0, info.GetDirectories ("*").Length);
\r
271 Directory.CreateDirectory (path + "/" + "test120");
\r
272 Directory.CreateDirectory (path + "/" + "test210");
\r
273 Directory.CreateDirectory (path + "/" + "atest330");
\r
274 Directory.CreateDirectory (path + "/" + "test220");
\r
275 File.Create (path + "/" + "filetest").Close ();
\r
277 AssertEquals ("test#02", 4, info.GetDirectories ("*").Length);
\r
278 AssertEquals ("test#03", 3, info.GetDirectories ("test*").Length);
\r
279 AssertEquals ("test#04", 2, info.GetDirectories ("test?20").Length);
\r
280 AssertEquals ("test#05", 0, info.GetDirectories ("test?").Length);
\r
281 AssertEquals ("test#06", 0, info.GetDirectories ("test[12]*").Length);
\r
282 AssertEquals ("test#07", 2, info.GetDirectories ("test2*0").Length);
\r
283 AssertEquals ("test#08", 4, info.GetDirectories ("*test*").Length);
\r
291 [ExpectedException (typeof (DirectoryNotFoundException))]
\r
292 public void GetDirectoriesDirectoryNotFoundException1 ()
\r
294 string path = TempFolder + "/DIT.GetDirectoriesDirectoryNotFoundException1.Test";
\r
298 DirectoryInfo info = new DirectoryInfo (path);
\r
299 info.GetDirectories ();
\r
306 [ExpectedException (typeof (DirectoryNotFoundException))]
\r
307 public void GetDirectoriesDirectoryNotFoundException2 ()
\r
309 string path = TempFolder + "/DIT.GetDirectoriesDirectoryNotFoundException2.Test";
\r
313 DirectoryInfo info = new DirectoryInfo (path);
\r
314 info.GetDirectories ("*");
\r
321 [ExpectedException (typeof (ArgumentNullException))]
\r
322 public void GetDirectoriesArgumentNullException ()
\r
324 string path = TempFolder + "/DIT.GetDirectoriesArgumentNullException.Test";
\r
328 DirectoryInfo info = new DirectoryInfo (path);
\r
329 info.GetDirectories (null);
\r
336 public void GetFiles1 ()
\r
338 string path = TempFolder + "/DIT.GetFiles1.Test";
\r
342 DirectoryInfo info = Directory.CreateDirectory (path);
\r
343 AssertEquals ("test#01", 0, info.GetFiles ().Length);
\r
344 File.Create (path + "/" + "file1").Close ();
\r
345 File.Create (path + "/" + "file2").Close ();
\r
346 Directory.CreateDirectory (path + "/" + "directory1");
\r
347 AssertEquals ("test#02", 2, info.GetFiles ().Length);
\r
355 public void GetFiles2()
\r
357 string path = TempFolder + "/DIT.GetFiles2.Test";
\r
361 DirectoryInfo info = Directory.CreateDirectory (path);
\r
362 AssertEquals ("test#01", 0, info.GetFiles ("*").Length);
\r
363 File.Create (path + "/" + "file120file").Close ();
\r
364 File.Create (path + "/" + "file220file").Close ();
\r
365 File.Create (path + "/" + "afile330file").Close ();
\r
366 File.Create (path + "/" + "test.abc").Close ();
\r
367 File.Create (path + "/" + "test.abcd").Close ();
\r
368 File.Create (path + "/" + "test.abcdef").Close ();
\r
369 Directory.CreateDirectory (path + "/" + "dir");
\r
371 AssertEquals ("test#02", 6, info.GetFiles ("*").Length);
\r
372 AssertEquals ("test#03", 2, info.GetFiles ("file*file").Length);
\r
373 AssertEquals ("test#04", 3, info.GetFiles ("*file*").Length);
\r
374 AssertEquals ("test#05", 2, info.GetFiles ("file?20file").Length);
\r
375 AssertEquals ("test#07", 1, info.GetFiles ("*.abcd").Length);
\r
376 AssertEquals ("test#08", 2, info.GetFiles ("*.abcd*").Length);
\r
383 [ExpectedException (typeof (DirectoryNotFoundException))]
\r
384 public void GetFilesDirectoryNotFoundException1 ()
\r
386 string path = TempFolder + "/DIT.GetFilesDirectoryNotFoundException1.Test";
\r
390 DirectoryInfo info = new DirectoryInfo (path);
\r
399 [ExpectedException (typeof (DirectoryNotFoundException))]
\r
400 public void GetFilesDirectoryNotFoundException2 ()
\r
402 string path = TempFolder + "/DIT.GetFilesDirectoryNotFoundException2.Test";
\r
406 DirectoryInfo info = new DirectoryInfo (path);
\r
407 info.GetFiles ("*");
\r
415 [ExpectedException (typeof (ArgumentNullException))]
\r
416 public void GetFilesArgumentNullException ()
\r
418 string path = TempFolder + "/DIT.GetFilesArgumentNullException.Test";
\r
422 DirectoryInfo info = new DirectoryInfo (path);
\r
423 info.GetFiles (null);
\r
430 public void MoveTo ()
\r
432 string path1 = TempFolder + "/DIT.MoveTo.Soucre.Test";
\r
433 string path2 = TempFolder + "/DIT.MoveTo.Dest.Test";
\r
438 DirectoryInfo info1 = Directory.CreateDirectory (path1);
\r
439 DirectoryInfo info2 = new DirectoryInfo (path2);
\r
441 AssertEquals ("test#01", true, info1.Exists);
\r
442 AssertEquals ("test#02", false, info2.Exists);
\r
444 info1.MoveTo (path2);
\r
445 AssertEquals ("test#03", true, info1.Exists);
\r
446 AssertEquals ("test#04", false, info2.Exists);
\r
448 info1 = new DirectoryInfo (path1);
\r
449 info2 = new DirectoryInfo (path2);
\r
450 AssertEquals ("test#05", false, info1.Exists);
\r
451 AssertEquals ("test#06", true, info2.Exists);
\r
460 [ExpectedException (typeof (ArgumentNullException))]
\r
461 public void MoveToArgumentNullException ()
\r
463 string path = TempFolder + "/DIT.MoveToArgumentNullException.Test";
\r
467 DirectoryInfo info = Directory.CreateDirectory (path);
\r
468 info.MoveTo (null);
\r
476 [ExpectedException (typeof (IOException))]
\r
477 public void MoveToIOException1 ()
\r
479 string path = TempFolder + "/DIT.MoveToIOException1.Test";
\r
483 DirectoryInfo info = Directory.CreateDirectory (path);
\r
484 info.MoveTo (path);
\r
491 [ExpectedException (typeof (ArgumentException))]
\r
492 public void MoveToArgumentException1 ()
\r
494 string path = TempFolder + "/DIT.MoveToArgumentException1.Test";
\r
498 DirectoryInfo info = Directory.CreateDirectory (path);
\r
506 [ExpectedException (typeof (ArgumentException))]
\r
507 public void MoveToArgumentException2 ()
\r
509 string path = TempFolder + "/DIT.MoveToArgumentException2.Test";
\r
513 DirectoryInfo info = Directory.CreateDirectory (path);
\r
521 [ExpectedException (typeof (ArgumentException))]
\r
522 public void MoveToArgumentException3 ()
\r
524 string path = TempFolder + "/DIT.MoveToArgumentException3.Test";
\r
528 DirectoryInfo info = Directory.CreateDirectory (path);
\r
529 info.MoveTo (Path.InvalidPathChars [0].ToString ());
\r
536 [ExpectedException (typeof (IOException))]
\r
537 public void MoveToIOException2 ()
\r
539 string path = TempFolder + "/DIT.MoveToIOException2.Test";
\r
543 DirectoryInfo info = new DirectoryInfo (path);
\r
544 info.MoveTo (path);
\r
550 private void DeleteDir (string path)
\r
552 if (Directory.Exists (path))
\r
553 Directory.Delete (path, true);
\r