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);
111 [Test, Category ("NotWorking")]
112 public void IsReadOnly ()
114 string path = TempFolder + DSC + "FIT.IsReadOnly.Test";
118 FileStream stream = File.Create (path);
119 stream.WriteByte (12);
122 FileInfo info1 = new FileInfo (path);
123 AssertEquals ("test#01", false, info1.IsReadOnly);
125 FileInfo info2 = new FileInfo (path);
126 info2.IsReadOnly = true;
127 AssertEquals ("test#02", true, info1.IsReadOnly);
129 FileInfo info3 = new FileInfo (path);
130 info2.IsReadOnly = false;
131 AssertEquals ("test#03", false, info1.IsReadOnly);
139 public void Length ()
141 string path = TempFolder + DSC + "FIT.Length.Test";
145 FileStream stream = File.Create (path);
146 FileInfo info = new FileInfo (path);
147 AssertEquals ("test#01", 0, info.Length);
148 stream.WriteByte (12);
150 AssertEquals ("test#02", 0, info.Length);
151 info = new FileInfo (path);
152 AssertEquals ("test#03", 1, info.Length);
160 [ExpectedException (typeof (FileNotFoundException))]
161 public void LengthException ()
163 string path = TempFolder + DSC + "FIT.LengthException.Test";
165 FileInfo info = new FileInfo (path);
166 long l = info.Length;
170 public void AppendText ()
172 string path = TempFolder + DSC + "FIT.AppendText.Test";
176 FileInfo info = new FileInfo (path);
178 AssertEquals ("test#01", false, info.Exists);
180 StreamWriter writer = info.AppendText ();
181 info = new FileInfo (path);
182 AssertEquals ("test#02", true, info.Exists );
184 writer.Write ("aaa");
188 AssertEquals ("test#03", 0, info.Length);
189 info = new FileInfo (path);
190 AssertEquals ("test#04", 3, info.Length);
198 public void CopyTo ()
200 string path1 = TempFolder + DSC + "FIT.CopyTo.Source.Test";
201 string path2 = TempFolder + DSC + "FIT.CopyTo.Dest.Test";
205 File.Create (path1).Close ();
207 FileInfo info = new FileInfo (path1);
208 AssertEquals ("test#01", true, info.Exists);
210 FileInfo info2 = info.CopyTo (path2);
211 info = new FileInfo (path1);
212 AssertEquals ("test#02", true, info2.Exists);
220 public void CopyTo2 ()
222 string path1 = TempFolder + DSC + "FIT.CopyTo2.Source.Test";
223 string path2 = TempFolder + DSC + "FIT.CopyTo2.Dest.Test";
227 File.Create (path1).Close ();
228 File.Create (path2).Close ();
229 FileInfo info = new FileInfo (path1);
231 FileInfo info2 = info.CopyTo (path2, true);
232 info = new FileInfo (path1);
233 AssertEquals ("test#01", true, info.Exists);
234 AssertEquals ("test#02", true, info2.Exists);
242 [ExpectedException (typeof (IOException))]
243 public void CopyToIOException ()
245 string path1 = TempFolder + DSC + "FIT.CopyToException.Source.Test";
246 string path2 = TempFolder + DSC + "FIT.CopyToException.Dest.Test";
251 File.Create (path1).Close ();
252 File.Create (path2).Close ();
253 FileInfo info = new FileInfo (path1);
254 FileInfo info2 = info.CopyTo (path2);
262 [ExpectedException (typeof (IOException))]
263 public void CopyToIOException2 ()
265 string path1 = TempFolder + DSC + "FIT.CopyToException.Source.Test";
266 string path2 = TempFolder + DSC + "FIT.CopyToException.Dest.Test";
271 File.Create (path1).Close ();
272 File.Create (path2).Close ();
273 FileInfo info = new FileInfo (path1);
274 FileInfo info2 = info.CopyTo (path2, false);
282 [ExpectedException (typeof (ArgumentNullException))]
283 public void CopyToArgumentNullException ()
285 string path = TempFolder + DSC + "FIT.CopyToArgumentNullException.Test";
288 File.Create (path).Close ();
289 FileInfo info = new FileInfo (path);
290 info.CopyTo (null, false);
297 [ExpectedException (typeof (ArgumentException))]
298 public void CopyToArgumentException1 ()
300 string path = TempFolder + DSC + "FIT.CopyToArgument1Exception.Test";
304 File.Create (path).Close ();
305 FileInfo info = new FileInfo (path);
306 info.CopyTo ("", false);
313 [ExpectedException (typeof (ArgumentException))]
314 public void CopyToArgumentException2 ()
316 string path = TempFolder + DSC + "FIT.CopyToArgument2Exception.Test";
320 File.Create (path).Close ();
321 FileInfo info = new FileInfo (path);
322 info.CopyTo (" ", false);
329 [ExpectedException (typeof (ArgumentException))]
330 public void CopyToArgumentException3 ()
332 string path = TempFolder + DSC + "FIT.CopyToArgument3Exception.Test";
336 File.Create (path).Close ();
337 FileInfo info = new FileInfo (path);
338 info.CopyTo (" ", false);
345 [ExpectedException (typeof (ArgumentException))]
346 public void CopyToArgumentException4 ()
348 string path = TempFolder + DSC + "FIT.CopyToArgument4Exception.Test";
353 File.Create (path).Close ();
354 FileInfo info = new FileInfo (path);
356 foreach (char c in Path.InvalidPathChars) {
359 info.CopyTo (path2, false);
366 public void Create ()
368 string path = TempFolder + DSC + "FIT.Create.Test";
372 FileInfo info = new FileInfo (path);
373 AssertEquals ("test#01", false, info.Exists);
374 FileStream stream = info.Create ();
375 AssertEquals ("test#02", false, info.Exists);
376 info = new FileInfo (path);
377 AssertEquals ("test#03", true, info.Exists);
378 AssertEquals ("test#04", true, stream.CanRead);
379 AssertEquals ("test#05", true, stream.CanWrite);
380 AssertEquals ("test#06", true, stream.CanSeek);
388 public void CreateText ()
390 string path = TempFolder + DSC + "FIT.CreateText.Test";
394 FileInfo info = new FileInfo (path);
395 AssertEquals ("test#01", false, info.Exists);
396 StreamWriter writer = info.CreateText ();
397 writer.WriteLine ("test");
399 info = new FileInfo (path);
400 AssertEquals ("test#02", true, info.Exists);
407 [ExpectedException (typeof (UnauthorizedAccessException))]
408 public void CreateTextUnauthorizedAccessException ()
410 string path = TempFolder;
412 FileInfo info = new FileInfo (path);
413 AssertEquals ("test#01", false, info.Exists);
414 StreamWriter writer = info.CreateText ();
419 public void Delete ()
421 string path = TempFolder + DSC + "FIT.Delete.Test";
425 FileInfo info = new FileInfo (path);
426 AssertEquals ("test#01", false, info.Exists);
427 info.Create ().Close ();
428 info = new FileInfo (path);
429 AssertEquals ("test#02", true, info.Exists);
431 AssertEquals ("test#03", true, info.Exists);
432 info = new FileInfo (path);
433 AssertEquals ("test#04", false, info.Exists);
440 [ExpectedException (typeof (UnauthorizedAccessException))]
441 public void DeleteUnauthorizedAccessException ()
443 string path = TempFolder;
444 FileInfo info = new FileInfo (path);
449 public void MoveTo ()
451 string path1 = TempFolder + DSC + "FIT.MoveTo.Source.Test";
452 string path2 = TempFolder + DSC + "FIT.MoveTo.Dest.Test";
457 File.Create (path1).Close ();
458 FileInfo info = new FileInfo (path1);
459 FileInfo info2 = new FileInfo (path2);
460 AssertEquals ("test#01", false, info2.Exists);
463 info2 = new FileInfo (path2);
464 AssertEquals ("test#02", true, info2.Exists);
472 [ExpectedException (typeof (IOException))]
473 public void MoveToIOException ()
475 string path1 = TempFolder + DSC + "FIT.MoveToIOException.Source.Test";
476 string path2 = TempFolder + DSC + "FIT.MoveToIOException.Dest.Test";
481 File.Create (path1).Close ();
482 File.Create (path2).Close ();
484 FileInfo info = new FileInfo (path1);
493 [ExpectedException (typeof (ArgumentNullException))]
494 public void MoveToArgumentNullException ()
496 string path = TempFolder + DSC + "FIT.MoveToArgumentNullException.Test";
500 File.Create (path).Close ();
501 FileInfo info = new FileInfo (path);
509 [ExpectedException (typeof (ArgumentException))]
510 public void MoveToArgumentException ()
512 string path = TempFolder + DSC + "FIT.MoveToArgumentException.Test";
516 File.Create (path).Close ();
517 FileInfo info = new FileInfo (path);
525 /// msdn says this should throw UnauthorizedAccessException, byt
526 /// it throws IOException.
529 [ExpectedException (typeof (IOException))]
530 public void MoveToUnauthorizedAccessException ()
532 string path = TempFolder + DSC + "FIT.UnauthorizedAccessException.Test";
536 File.Create (path).Close ();
537 FileInfo info = new FileInfo (path);
538 info.MoveTo (TempFolder);
545 [ExpectedException (typeof (FileNotFoundException))]
546 public void MoveToFileNotFoundException ()
548 string path1 = TempFolder + DSC + "FIT.MoveToFileNotFoundException.Src";
549 string path2 = TempFolder + DSC + "FIT.MoveToFileNotFoundException.Dst";
554 FileInfo info = new FileInfo (path1);
565 string path = TempFolder + DSC + "FIT.Open.Test";
567 FileStream stream = null;
569 FileInfo info = new FileInfo (path);
570 stream = info.Open (FileMode.CreateNew);
571 AssertEquals ("test#01", true, stream.CanRead);
572 AssertEquals ("test#02", true, stream.CanSeek);
573 AssertEquals ("test#03", true, stream.CanWrite);
576 stream = info.Open (FileMode.Open);
577 AssertEquals ("test#04", true, stream.CanRead);
578 AssertEquals ("test#05", true, stream.CanSeek);
579 AssertEquals ("test#06", true, stream.CanWrite);
582 stream = info.Open (FileMode.Append, FileAccess.Write);
583 AssertEquals ("test#07", false, stream.CanRead);
584 AssertEquals ("test#08", true, stream.CanSeek);
585 AssertEquals ("test#09", true, stream.CanWrite);
588 stream = info.Open (FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
589 AssertEquals ("test#10", true, stream.CanRead);
590 AssertEquals ("test#11", true, stream.CanSeek);
591 AssertEquals ("test#12", true, stream.CanWrite);
601 [ExpectedException(typeof(FileNotFoundException))]
602 public void OpenFileNotFoundException ()
604 string path = TempFolder + DSC + "FIT.OpenFileNotFoundException.Test";
607 FileInfo info = new FileInfo (path);
608 info.Open (FileMode.Open);
612 public void OpenRead ()
614 string path = TempFolder + DSC + "FIT.OpenRead.Test";
616 FileStream stream = null;
618 File.Create (path).Close ();
619 FileInfo info = new FileInfo (path);
620 stream = info.OpenRead ();
621 AssertEquals ("test#01", true, stream.CanRead);
622 AssertEquals ("test#02", true, stream.CanSeek);
623 AssertEquals ("test#03", false, stream.CanWrite);
634 [ExpectedException(typeof (IOException))]
635 public void OpenReadIOException ()
637 string path = TempFolder + DSC + "FIT.OpenReadIOException.Test";
639 FileStream stream = null;
642 stream = File.Create (path);
643 FileInfo info = new FileInfo (path);
653 [ExpectedException(typeof (UnauthorizedAccessException))]
654 public void OpenReadUnauthorizedAccessException ()
656 string path = TempFolder;
658 FileInfo info = new FileInfo (path);
663 public void OpenText ()
665 string path = TempFolder + DSC + "FIT.OpenText.Test";
667 StreamReader reader = null;
669 File.Create (path).Close ();
670 FileInfo info = new FileInfo (path);
671 reader = info.OpenText ();
672 AssertEquals ("test#01", -1, reader.Peek ());
682 [ExpectedException(typeof (FileNotFoundException))]
683 public void OpenTextFileNotFoundException ()
685 string path = TempFolder + DSC + "FIT.OpenTextFileNotFoundException.Test";
687 FileInfo info = new FileInfo (path);
692 [ExpectedException(typeof (UnauthorizedAccessException))]
693 public void OpenTextUnauthorizedAccessException ()
695 string path = TempFolder;
697 FileInfo info = new FileInfo (path);
702 public void OpenWrite ()
704 string path = TempFolder + DSC + "FIT.OpenWrite.Test";
706 FileStream stream = null;
708 File.Create (path).Close ();
709 FileInfo info = new FileInfo (path);
710 stream = info.OpenWrite ();
711 AssertEquals ("test#01", false, stream.CanRead);
712 AssertEquals ("test#02", true, stream.CanSeek);
713 AssertEquals ("test#03", true, stream.CanWrite);
723 [ExpectedException(typeof (UnauthorizedAccessException))]
724 public void OpenWriteUnauthorizedAccessException ()
726 string path = TempFolder;
728 FileInfo info = new FileInfo (path);
732 private void DeleteFile (string path)
734 if (File.Exists (path))
739 public void ToStringVariety ()
741 AssertEquals ("foo", new FileInfo ("foo").ToString ());
742 AssertEquals ("c:/foo", new FileInfo ("c:/foo").ToString ());
743 AssertEquals ("/usr/local/foo", new FileInfo ("/usr/local/foo").ToString ());
744 AssertEquals ("c:\\foo", new FileInfo ("c:\\foo").ToString ());
745 AssertEquals ("/usr/local\\foo", new FileInfo ("/usr/local\\foo").ToString ());
746 AssertEquals ("foo/BAR/baz", new FileInfo ("foo/BAR/baz").ToString ());
747 AssertEquals ("c:/documents and settings", new FileInfo ("c:/documents and settings").ToString ());
748 AssertEquals ("c:/docUme~1", new FileInfo ("c:/docUme~1").ToString ());