1 // FileInfoTest.cs - NUnit Test Cases for System.IO.FileInfo class
\r
3 // Ville Palo (vi64pa@koti.soon.fi)
\r
5 // (C) 2003 Ville Palo
\r
9 using NUnit.Framework;
\r
13 namespace MonoTests.System.IO
\r
16 public class FileInfoTest : Assertion
\r
18 string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
\r
19 static readonly char DSC = Path.DirectorySeparatorChar;
\r
22 protected void SetUp() {
\r
23 if (Directory.Exists (TempFolder))
\r
24 Directory.Delete (TempFolder, true);
\r
25 Directory.CreateDirectory (TempFolder);
\r
29 protected void TearDown() {
\r
30 if (Directory.Exists (TempFolder))
\r
31 Directory.Delete (TempFolder, true);
\r
37 string path = TempFolder + DSC + "FIT.Ctr.Test";
\r
40 FileInfo info = new FileInfo (path);
\r
41 AssertEquals ("test#01", true, info.DirectoryName.EndsWith (".Tests"));
\r
42 AssertEquals ("test#02", false, info.Exists);
\r
43 AssertEquals ("test#03", ".Test", info.Extension);
\r
44 AssertEquals ("test#05", "FIT.Ctr.Test", info.Name);
\r
48 [ExpectedException(typeof(ArgumentNullException))]
\r
49 public void CtorArgumentNullException ()
\r
51 FileInfo info = new FileInfo (null);
\r
55 [ExpectedException(typeof(ArgumentException))]
\r
56 public void CtorArgumentException1 ()
\r
58 FileInfo info = new FileInfo ("");
\r
62 [ExpectedException(typeof(ArgumentException))]
\r
63 public void CtorArgumentException2 ()
\r
65 FileInfo info = new FileInfo (" ");
\r
69 [ExpectedException(typeof(ArgumentException))]
\r
70 public void CtorArgumentException3 ()
\r
73 foreach (char c in Path.InvalidPathChars) {
\r
76 FileInfo info = new FileInfo (path);
\r
80 public void DirectoryTest ()
\r
82 string path = TempFolder + DSC + "FIT.Directory.Test";
\r
85 FileInfo info = new FileInfo (path);
\r
86 DirectoryInfo dir = info.Directory;
\r
87 AssertEquals ("test#01", "MonoTests.System.IO.Tests", dir.Name);
\r
91 public void Exists ()
\r
93 string path = TempFolder + DSC + "FIT.Exists.Test";
\r
97 FileInfo info = new FileInfo (path);
\r
98 AssertEquals ("test#01", false, info.Exists);
\r
100 File.Create (path).Close ();
\r
101 AssertEquals ("test#02", false, info.Exists);
\r
102 info = new FileInfo (path);
\r
103 AssertEquals ("test#03", true, info.Exists);
\r
110 public void Length ()
\r
112 string path = TempFolder + DSC + "FIT.Length.Test";
\r
116 FileStream stream = File.Create (path);
\r
117 FileInfo info = new FileInfo (path);
\r
118 AssertEquals ("test#01", 0, info.Length);
\r
119 stream.WriteByte (12);
\r
121 AssertEquals ("test#02", 0, info.Length);
\r
122 info = new FileInfo (path);
\r
123 AssertEquals ("test#03", 1, info.Length);
\r
131 [ExpectedException (typeof (FileNotFoundException))]
\r
132 public void LengthException ()
\r
134 string path = TempFolder + DSC + "FIT.LengthException.Test";
\r
136 FileInfo info = new FileInfo (path);
\r
137 long l = info.Length;
\r
141 public void AppendText ()
\r
143 string path = TempFolder + DSC + "FIT.AppendText.Test";
\r
147 FileInfo info = new FileInfo (path);
\r
149 AssertEquals ("test#01", false, info.Exists);
\r
151 StreamWriter writer = info.AppendText ();
\r
152 info = new FileInfo (path);
\r
153 AssertEquals ("test#02", true, info.Exists );
\r
155 writer.Write ("aaa");
\r
159 AssertEquals ("test#03", 0, info.Length);
\r
160 info = new FileInfo (path);
\r
161 AssertEquals ("test#04", 3, info.Length);
\r
169 public void CopyTo ()
\r
171 string path1 = TempFolder + DSC + "FIT.CopyTo.Source.Test";
\r
172 string path2 = TempFolder + DSC + "FIT.CopyTo.Dest.Test";
\r
173 DeleteFile (path1);
\r
174 DeleteFile (path2);
\r
176 File.Create (path1).Close ();
\r
178 FileInfo info = new FileInfo (path1);
\r
179 AssertEquals ("test#01", true, info.Exists);
\r
181 FileInfo info2 = info.CopyTo (path2);
\r
182 info = new FileInfo (path1);
\r
183 AssertEquals ("test#02", true, info2.Exists);
\r
185 DeleteFile (path1);
\r
186 DeleteFile (path2);
\r
191 public void CopyTo2 ()
\r
193 string path1 = TempFolder + DSC + "FIT.CopyTo2.Source.Test";
\r
194 string path2 = TempFolder + DSC + "FIT.CopyTo2.Dest.Test";
\r
195 DeleteFile (path1);
\r
196 DeleteFile (path2);
\r
198 File.Create (path1).Close ();
\r
199 File.Create (path2).Close ();
\r
200 FileInfo info = new FileInfo (path1);
\r
202 FileInfo info2 = info.CopyTo (path2, true);
\r
203 info = new FileInfo (path1);
\r
204 AssertEquals ("test#01", true, info.Exists);
\r
205 AssertEquals ("test#02", true, info2.Exists);
\r
207 DeleteFile (path1);
\r
208 DeleteFile (path2);
\r
213 [ExpectedException (typeof (IOException))]
\r
214 public void CopyToIOException ()
\r
216 string path1 = TempFolder + DSC + "FIT.CopyToException.Source.Test";
\r
217 string path2 = TempFolder + DSC + "FIT.CopyToException.Dest.Test";
\r
220 DeleteFile (path1);
\r
221 DeleteFile (path2);
\r
222 File.Create (path1).Close ();
\r
223 File.Create (path2).Close ();
\r
224 FileInfo info = new FileInfo (path1);
\r
225 FileInfo info2 = info.CopyTo (path2);
\r
227 DeleteFile (path1);
\r
228 DeleteFile (path2);
\r
233 [ExpectedException (typeof (IOException))]
\r
234 public void CopyToIOException2 ()
\r
236 string path1 = TempFolder + DSC + "FIT.CopyToException.Source.Test";
\r
237 string path2 = TempFolder + DSC + "FIT.CopyToException.Dest.Test";
\r
240 DeleteFile (path1);
\r
241 DeleteFile (path2);
\r
242 File.Create (path1).Close ();
\r
243 File.Create (path2).Close ();
\r
244 FileInfo info = new FileInfo (path1);
\r
245 FileInfo info2 = info.CopyTo (path2, false);
\r
247 DeleteFile (path1);
\r
248 DeleteFile (path2);
\r
253 [ExpectedException (typeof (ArgumentNullException))]
\r
254 public void CopyToArgumentNullException ()
\r
256 string path = TempFolder + DSC + "FIT.CopyToArgumentNullException.Test";
\r
259 File.Create (path).Close ();
\r
260 FileInfo info = new FileInfo (path);
\r
261 info.CopyTo (null, false);
\r
268 [ExpectedException (typeof (ArgumentException))]
\r
269 public void CopyToArgumentException1 ()
\r
271 string path = TempFolder + DSC + "FIT.CopyToArgument1Exception.Test";
\r
275 File.Create (path).Close ();
\r
276 FileInfo info = new FileInfo (path);
\r
277 info.CopyTo ("", false);
\r
284 [ExpectedException (typeof (ArgumentException))]
\r
285 public void CopyToArgumentException2 ()
\r
287 string path = TempFolder + DSC + "FIT.CopyToArgument2Exception.Test";
\r
291 File.Create (path).Close ();
\r
292 FileInfo info = new FileInfo (path);
\r
293 info.CopyTo (" ", false);
\r
300 [ExpectedException (typeof (ArgumentException))]
\r
301 public void CopyToArgumentException3 ()
\r
303 string path = TempFolder + DSC + "FIT.CopyToArgument3Exception.Test";
\r
307 File.Create (path).Close ();
\r
308 FileInfo info = new FileInfo (path);
\r
309 info.CopyTo (" ", false);
\r
316 [ExpectedException (typeof (ArgumentException))]
\r
317 public void CopyToArgumentException4 ()
\r
319 string path = TempFolder + DSC + "FIT.CopyToArgument4Exception.Test";
\r
324 File.Create (path).Close ();
\r
325 FileInfo info = new FileInfo (path);
\r
327 foreach (char c in Path.InvalidPathChars) {
\r
330 info.CopyTo (path2, false);
\r
337 public void Create ()
\r
339 string path = TempFolder + DSC + "FIT.Create.Test";
\r
343 FileInfo info = new FileInfo (path);
\r
344 AssertEquals ("test#01", false, info.Exists);
\r
345 FileStream stream = info.Create ();
\r
346 AssertEquals ("test#02", false, info.Exists);
\r
347 info = new FileInfo (path);
\r
348 AssertEquals ("test#03", true, info.Exists);
\r
349 AssertEquals ("test#04", true, stream.CanRead);
\r
350 AssertEquals ("test#05", true, stream.CanWrite);
\r
351 AssertEquals ("test#06", true, stream.CanSeek);
\r
359 public void CreateText ()
\r
361 string path = TempFolder + DSC + "FIT.CreateText.Test";
\r
365 FileInfo info = new FileInfo (path);
\r
366 AssertEquals ("test#01", false, info.Exists);
\r
367 StreamWriter writer = info.CreateText ();
\r
368 writer.WriteLine ("test");
\r
370 info = new FileInfo (path);
\r
371 AssertEquals ("test#02", true, info.Exists);
\r
378 [ExpectedException (typeof (UnauthorizedAccessException))]
\r
379 public void CreateTextUnauthorizedAccessException ()
\r
381 string path = TempFolder;
\r
383 FileInfo info = new FileInfo (path);
\r
384 AssertEquals ("test#01", false, info.Exists);
\r
385 StreamWriter writer = info.CreateText ();
\r
390 public void Delete ()
\r
392 string path = TempFolder + DSC + "FIT.Delete.Test";
\r
396 FileInfo info = new FileInfo (path);
\r
397 AssertEquals ("test#01", false, info.Exists);
\r
398 info.Create ().Close ();
\r
399 info = new FileInfo (path);
\r
400 AssertEquals ("test#02", true, info.Exists);
\r
402 AssertEquals ("test#03", true, info.Exists);
\r
403 info = new FileInfo (path);
\r
404 AssertEquals ("test#04", false, info.Exists);
\r
411 [ExpectedException (typeof (UnauthorizedAccessException))]
\r
412 public void DeleteUnauthorizedAccessException ()
\r
414 string path = TempFolder;
\r
415 FileInfo info = new FileInfo (path);
\r
420 public void MoveTo ()
\r
422 string path1 = TempFolder + DSC + "FIT.MoveTo.Source.Test";
\r
423 string path2 = TempFolder + DSC + "FIT.MoveTo.Dest.Test";
\r
424 DeleteFile (path1);
\r
425 DeleteFile (path2);
\r
428 File.Create (path1).Close ();
\r
429 FileInfo info = new FileInfo (path1);
\r
430 FileInfo info2 = new FileInfo (path2);
\r
431 AssertEquals ("test#01", false, info2.Exists);
\r
433 info.MoveTo (path2);
\r
434 info2 = new FileInfo (path2);
\r
435 AssertEquals ("test#02", true, info2.Exists);
\r
437 DeleteFile (path1);
\r
438 DeleteFile (path2);
\r
443 [ExpectedException (typeof (IOException))]
\r
444 public void MoveToIOException ()
\r
446 string path1 = TempFolder + DSC + "FIT.MoveToIOException.Source.Test";
\r
447 string path2 = TempFolder + DSC + "FIT.MoveToIOException.Dest.Test";
\r
448 DeleteFile (path1);
\r
449 DeleteFile (path2);
\r
452 File.Create (path1).Close ();
\r
453 File.Create (path2).Close ();
\r
455 FileInfo info = new FileInfo (path1);
\r
456 info.MoveTo (path2);
\r
458 DeleteFile (path1);
\r
459 DeleteFile (path2);
\r
464 [ExpectedException (typeof (ArgumentNullException))]
\r
465 public void MoveToArgumentNullException ()
\r
467 string path = TempFolder + DSC + "FIT.MoveToArgumentNullException.Test";
\r
471 File.Create (path).Close ();
\r
472 FileInfo info = new FileInfo (path);
\r
473 info.MoveTo (null);
\r
480 [ExpectedException (typeof (ArgumentException))]
\r
481 public void MoveToArgumentException ()
\r
483 string path = TempFolder + DSC + "FIT.MoveToArgumentException.Test";
\r
487 File.Create (path).Close ();
\r
488 FileInfo info = new FileInfo (path);
\r
496 /// msdn says this should throw UnauthorizedAccessException, byt
\r
497 /// it throws IOException.
\r
500 [ExpectedException (typeof (IOException))]
\r
501 public void MoveToUnauthorizedAccessException ()
\r
503 string path = TempFolder + DSC + "FIT.UnauthorizedAccessException.Test";
\r
507 File.Create (path).Close ();
\r
508 FileInfo info = new FileInfo (path);
\r
509 info.MoveTo (TempFolder);
\r
516 [ExpectedException (typeof (FileNotFoundException))]
\r
517 public void MoveToFileNotFoundException ()
\r
519 string path1 = TempFolder + DSC + "FIT.MoveToFileNotFoundException.Src";
\r
520 string path2 = TempFolder + DSC + "FIT.MoveToFileNotFoundException.Dst";
\r
521 DeleteFile (path1);
\r
522 DeleteFile (path2);
\r
525 FileInfo info = new FileInfo (path1);
\r
526 info.MoveTo (path2);
\r
528 DeleteFile (path1);
\r
529 DeleteFile (path2);
\r
534 public void Open ()
\r
536 string path = TempFolder + DSC + "FIT.Open.Test";
\r
538 FileStream stream = null;
\r
540 FileInfo info = new FileInfo (path);
\r
541 stream = info.Open (FileMode.CreateNew);
\r
542 AssertEquals ("test#01", true, stream.CanRead);
\r
543 AssertEquals ("test#02", true, stream.CanSeek);
\r
544 AssertEquals ("test#03", true, stream.CanWrite);
\r
547 stream = info.Open (FileMode.Open);
\r
548 AssertEquals ("test#04", true, stream.CanRead);
\r
549 AssertEquals ("test#05", true, stream.CanSeek);
\r
550 AssertEquals ("test#06", true, stream.CanWrite);
\r
553 stream = info.Open (FileMode.Append, FileAccess.Write);
\r
554 AssertEquals ("test#07", false, stream.CanRead);
\r
555 AssertEquals ("test#08", true, stream.CanSeek);
\r
556 AssertEquals ("test#09", true, stream.CanWrite);
\r
559 stream = info.Open (FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
\r
560 AssertEquals ("test#10", true, stream.CanRead);
\r
561 AssertEquals ("test#11", true, stream.CanSeek);
\r
562 AssertEquals ("test#12", true, stream.CanWrite);
\r
565 if (stream != null)
\r
572 [ExpectedException(typeof(FileNotFoundException))]
\r
573 public void OpenFileNotFoundException ()
\r
575 string path = TempFolder + DSC + "FIT.OpenFileNotFoundException.Test";
\r
578 FileInfo info = new FileInfo (path);
\r
579 info.Open (FileMode.Open);
\r
583 public void OpenRead ()
\r
585 string path = TempFolder + DSC + "FIT.OpenRead.Test";
\r
587 FileStream stream = null;
\r
589 File.Create (path).Close ();
\r
590 FileInfo info = new FileInfo (path);
\r
591 stream = info.OpenRead ();
\r
592 AssertEquals ("test#01", true, stream.CanRead);
\r
593 AssertEquals ("test#02", true, stream.CanSeek);
\r
594 AssertEquals ("test#03", false, stream.CanWrite);
\r
598 if (stream != null)
\r
605 [ExpectedException(typeof (IOException))]
\r
606 public void OpenReadIOException ()
\r
608 string path = TempFolder + DSC + "FIT.OpenReadIOException.Test";
\r
610 FileStream stream = null;
\r
613 stream = File.Create (path);
\r
614 FileInfo info = new FileInfo (path);
\r
617 if (stream != null)
\r
624 [ExpectedException(typeof (UnauthorizedAccessException))]
\r
625 public void OpenReadUnauthorizedAccessException ()
\r
627 string path = TempFolder;
\r
629 FileInfo info = new FileInfo (path);
\r
634 public void OpenText ()
\r
636 string path = TempFolder + DSC + "FIT.OpenText.Test";
\r
638 StreamReader reader = null;
\r
640 File.Create (path).Close ();
\r
641 FileInfo info = new FileInfo (path);
\r
642 reader = info.OpenText ();
\r
643 AssertEquals ("test#01", -1, reader.Peek ());
\r
646 if (reader != null)
\r
653 [ExpectedException(typeof (FileNotFoundException))]
\r
654 public void OpenTextFileNotFoundException ()
\r
656 string path = TempFolder + DSC + "FIT.OpenTextFileNotFoundException.Test";
\r
658 FileInfo info = new FileInfo (path);
\r
663 [ExpectedException(typeof (UnauthorizedAccessException))]
\r
664 public void OpenTextUnauthorizedAccessException ()
\r
666 string path = TempFolder;
\r
668 FileInfo info = new FileInfo (path);
\r
673 public void OpenWrite ()
\r
675 string path = TempFolder + DSC + "FIT.OpenWrite.Test";
\r
677 FileStream stream = null;
\r
679 File.Create (path).Close ();
\r
680 FileInfo info = new FileInfo (path);
\r
681 stream = info.OpenWrite ();
\r
682 AssertEquals ("test#01", false, stream.CanRead);
\r
683 AssertEquals ("test#02", true, stream.CanSeek);
\r
684 AssertEquals ("test#03", true, stream.CanWrite);
\r
687 if (stream != null)
\r
694 [ExpectedException(typeof (UnauthorizedAccessException))]
\r
695 public void OpenWriteUnauthorizedAccessException ()
\r
697 string path = TempFolder;
\r
699 FileInfo info = new FileInfo (path);
\r
703 private void DeleteFile (string path)
\r
705 if (File.Exists (path))
\r
706 File.Delete (path);
\r
710 public void ToStringVariety ()
\r
712 AssertEquals ("foo", new FileInfo ("foo").ToString ());
\r
713 AssertEquals ("c:/foo", new FileInfo ("c:/foo").ToString ());
\r
714 AssertEquals ("/usr/local/foo", new FileInfo ("/usr/local/foo").ToString ());
\r
715 AssertEquals ("c:\\foo", new FileInfo ("c:\\foo").ToString ());
\r
716 AssertEquals ("/usr/local\\foo", new FileInfo ("/usr/local\\foo").ToString ());
\r
717 AssertEquals ("foo/BAR/baz", new FileInfo ("foo/BAR/baz").ToString ());
\r
718 AssertEquals ("c:/documents and settings", new FileInfo ("c:/documents and settings").ToString ());
\r
719 AssertEquals ("c:/docUme~1", new FileInfo ("c:/docUme~1").ToString ());
\r