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