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
250 public void GetDirectories1 ()
\r
252 string path = TempFolder + DSC + "DIT.GetDirectories1.Test";
\r
255 DirectoryInfo info = Directory.CreateDirectory (path);
\r
256 AssertEquals ("test#01", 0, info.GetDirectories ().Length);
\r
258 Directory.CreateDirectory (path + DSC + "1");
\r
259 Directory.CreateDirectory (path + DSC + "2");
\r
260 File.Create (path + DSC + "filetest").Close ();
\r
261 AssertEquals ("test#02", 2, info.GetDirectories ().Length);
\r
263 Directory.Delete (path + DSC + 2);
\r
264 AssertEquals ("test#02", 1, info.GetDirectories ().Length);
\r
272 public void GetDirectories2 ()
\r
274 string path = TempFolder + DSC + "DIT.GetDirectories2.Test";
\r
277 DirectoryInfo info = Directory.CreateDirectory (path);
\r
278 AssertEquals ("test#01", 0, info.GetDirectories ("*").Length);
\r
280 Directory.CreateDirectory (path + DSC + "test120");
\r
281 Directory.CreateDirectory (path + DSC + "test210");
\r
282 Directory.CreateDirectory (path + DSC + "atest330");
\r
283 Directory.CreateDirectory (path + DSC + "test220");
\r
284 File.Create (path + DSC + "filetest").Close ();
\r
286 AssertEquals ("test#02", 4, info.GetDirectories ("*").Length);
\r
287 AssertEquals ("test#03", 3, info.GetDirectories ("test*").Length);
\r
288 AssertEquals ("test#04", 2, info.GetDirectories ("test?20").Length);
\r
289 AssertEquals ("test#05", 0, info.GetDirectories ("test?").Length);
\r
290 AssertEquals ("test#06", 0, info.GetDirectories ("test[12]*").Length);
\r
291 AssertEquals ("test#07", 2, info.GetDirectories ("test2*0").Length);
\r
292 AssertEquals ("test#08", 4, info.GetDirectories ("*test*").Length);
\r
300 [ExpectedException (typeof (DirectoryNotFoundException))]
\r
301 public void GetDirectoriesDirectoryNotFoundException1 ()
\r
303 string path = TempFolder + DSC + "DIT.GetDirectoriesDirectoryNotFoundException1.Test";
\r
307 DirectoryInfo info = new DirectoryInfo (path);
\r
308 info.GetDirectories ();
\r
315 [ExpectedException (typeof (DirectoryNotFoundException))]
\r
316 public void GetDirectoriesDirectoryNotFoundException2 ()
\r
318 string path = TempFolder + DSC + "DIT.GetDirectoriesDirectoryNotFoundException2.Test";
\r
322 DirectoryInfo info = new DirectoryInfo (path);
\r
323 info.GetDirectories ("*");
\r
330 [ExpectedException (typeof (ArgumentNullException))]
\r
331 public void GetDirectoriesArgumentNullException ()
\r
333 string path = TempFolder + DSC + "DIT.GetDirectoriesArgumentNullException.Test";
\r
337 DirectoryInfo info = new DirectoryInfo (path);
\r
338 info.GetDirectories (null);
\r
345 public void GetFiles1 ()
\r
347 string path = TempFolder + DSC + "DIT.GetFiles1.Test";
\r
351 DirectoryInfo info = Directory.CreateDirectory (path);
\r
352 AssertEquals ("test#01", 0, info.GetFiles ().Length);
\r
353 File.Create (path + DSC + "file1").Close ();
\r
354 File.Create (path + DSC + "file2").Close ();
\r
355 Directory.CreateDirectory (path + DSC + "directory1");
\r
356 AssertEquals ("test#02", 2, info.GetFiles ().Length);
\r
364 public void GetFiles2()
\r
366 string path = TempFolder + DSC + "DIT.GetFiles2.Test";
\r
370 DirectoryInfo info = Directory.CreateDirectory (path);
\r
371 AssertEquals ("test#01", 0, info.GetFiles ("*").Length);
\r
372 File.Create (path + DSC + "file120file").Close ();
\r
373 File.Create (path + DSC + "file220file").Close ();
\r
374 File.Create (path + DSC + "afile330file").Close ();
\r
375 File.Create (path + DSC + "test.abc").Close ();
\r
376 File.Create (path + DSC + "test.abcd").Close ();
\r
377 File.Create (path + DSC + "test.abcdef").Close ();
\r
378 Directory.CreateDirectory (path + DSC + "dir");
\r
380 AssertEquals ("test#02", 6, info.GetFiles ("*").Length);
\r
381 AssertEquals ("test#03", 2, info.GetFiles ("file*file").Length);
\r
382 AssertEquals ("test#04", 3, info.GetFiles ("*file*").Length);
\r
383 AssertEquals ("test#05", 2, info.GetFiles ("file?20file").Length);
\r
384 AssertEquals ("test#07", 1, info.GetFiles ("*.abcd").Length);
\r
385 AssertEquals ("test#08", 2, info.GetFiles ("*.abcd*").Length);
\r
392 [ExpectedException (typeof (DirectoryNotFoundException))]
\r
393 public void GetFilesDirectoryNotFoundException1 ()
\r
395 string path = TempFolder + DSC + "DIT.GetFilesDirectoryNotFoundException1.Test";
\r
399 DirectoryInfo info = new DirectoryInfo (path);
\r
408 [ExpectedException (typeof (DirectoryNotFoundException))]
\r
409 public void GetFilesDirectoryNotFoundException2 ()
\r
411 string path = TempFolder + DSC + "DIT.GetFilesDirectoryNotFoundException2.Test";
\r
415 DirectoryInfo info = new DirectoryInfo (path);
\r
416 info.GetFiles ("*");
\r
424 [ExpectedException (typeof (ArgumentNullException))]
\r
425 public void GetFilesArgumentNullException ()
\r
427 string path = TempFolder + DSC + "DIT.GetFilesArgumentNullException.Test";
\r
431 DirectoryInfo info = new DirectoryInfo (path);
\r
432 info.GetFiles (null);
\r
439 public void MoveTo ()
\r
441 string path1 = TempFolder + DSC + "DIT.MoveTo.Soucre.Test";
\r
442 string path2 = TempFolder + DSC + "DIT.MoveTo.Dest.Test";
\r
447 DirectoryInfo info1 = Directory.CreateDirectory (path1);
\r
448 DirectoryInfo info2 = new DirectoryInfo (path2);
\r
450 AssertEquals ("test#01", true, info1.Exists);
\r
451 AssertEquals ("test#02", false, info2.Exists);
\r
453 info1.MoveTo (path2);
\r
454 AssertEquals ("test#03", true, info1.Exists);
\r
455 AssertEquals ("test#04", false, info2.Exists);
\r
457 info1 = new DirectoryInfo (path1);
\r
458 info2 = new DirectoryInfo (path2);
\r
459 AssertEquals ("test#05", false, info1.Exists);
\r
460 AssertEquals ("test#06", true, info2.Exists);
\r
469 [ExpectedException (typeof (ArgumentNullException))]
\r
470 public void MoveToArgumentNullException ()
\r
472 string path = TempFolder + DSC + "DIT.MoveToArgumentNullException.Test";
\r
476 DirectoryInfo info = Directory.CreateDirectory (path);
\r
477 info.MoveTo (null);
\r
485 [ExpectedException (typeof (IOException))]
\r
486 public void MoveToIOException1 ()
\r
488 string path = TempFolder + DSC + "DIT.MoveToIOException1.Test";
\r
492 DirectoryInfo info = Directory.CreateDirectory (path);
\r
493 info.MoveTo (path);
\r
500 [ExpectedException (typeof (ArgumentException))]
\r
501 public void MoveToArgumentException1 ()
\r
503 string path = TempFolder + DSC + "DIT.MoveToArgumentException1.Test";
\r
507 DirectoryInfo info = Directory.CreateDirectory (path);
\r
515 [ExpectedException (typeof (ArgumentException))]
\r
516 public void MoveToArgumentException2 ()
\r
518 string path = TempFolder + DSC + "DIT.MoveToArgumentException2.Test";
\r
522 DirectoryInfo info = Directory.CreateDirectory (path);
\r
530 [ExpectedException (typeof (ArgumentException))]
\r
531 public void MoveToArgumentException3 ()
\r
533 string path = TempFolder + DSC + "DIT.MoveToArgumentException3.Test";
\r
537 DirectoryInfo info = Directory.CreateDirectory (path);
\r
538 info.MoveTo (Path.InvalidPathChars [0].ToString ());
\r
545 [ExpectedException (typeof (IOException))]
\r
546 public void MoveToIOException2 ()
\r
548 string path = TempFolder + DSC + "DIT.MoveToIOException2.Test";
\r
552 DirectoryInfo info = new DirectoryInfo (path);
\r
553 info.MoveTo (path);
\r
559 private void DeleteDir (string path)
\r
561 if (Directory.Exists (path))
\r
562 Directory.Delete (path, true);
\r