1 // FileInfoTest.cs - NUnit Test Cases for System.IO.FileInfo class
3 // Ville Palo (vi64pa@koti.soon.fi)
10 using System.Runtime.Serialization;
11 using System.Runtime.Serialization.Formatters.Binary;
13 using NUnit.Framework;
15 namespace MonoTests.System.IO
18 public class FileInfoTest
20 string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
21 static readonly char DSC = Path.DirectorySeparatorChar;
26 DeleteDirectory (TempFolder);
27 Directory.CreateDirectory (TempFolder);
31 public void TearDown ()
33 DeleteDirectory (TempFolder);
36 [Test] // ctor (String)
37 public void Constructor1 ()
39 string path = TempFolder + DSC + "FIT.Ctr.Test";
42 FileInfo info = new FileInfo (path);
43 Assert.IsTrue (info.DirectoryName.EndsWith (".Tests"), "#1");
44 Assert.IsFalse (info.Exists, "#2");
45 Assert.AreEqual (".Test", info.Extension, "#3");
46 Assert.AreEqual ("FIT.Ctr.Test", info.Name, "#4");
49 [Test] // ctor (String)
50 public void Constructor1_FileName_Null ()
55 } catch (ArgumentNullException ex) {
56 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
57 Assert.IsNull (ex.InnerException, "#3");
58 Assert.IsNotNull (ex.Message, "#4");
59 Assert.AreEqual ("fileName", ex.ParamName, "#5");
63 [Test] // ctor (String)
64 public void Constructor1_FileName_Empty ()
67 new FileInfo (string.Empty);
69 } catch (ArgumentException ex) {
70 // Empty file name is not legal
71 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
72 Assert.IsNull (ex.InnerException, "#3");
73 Assert.IsNotNull (ex.Message, "#4");
74 Assert.IsNull (ex.ParamName, "#5");
78 [Test] // ctor (String)
79 public void Constructor1_FileName_InvalidPathChars ()
81 string path = string.Empty;
82 foreach (char c in Path.InvalidPathChars)
86 } catch (ArgumentException ex) {
87 // The path contains illegal characters
88 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
89 Assert.IsNull (ex.InnerException, "#3");
90 Assert.IsNotNull (ex.Message, "#4");
91 Assert.IsNull (ex.ParamName, "#5");
95 [Test] // ctor (String)
96 public void Constructor1_FileName_Whitespace ()
101 } catch (ArgumentException ex) {
102 // The path is not of a legal form
103 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
104 Assert.IsNull (ex.InnerException, "#3");
105 Assert.IsNotNull (ex.Message, "#4");
106 Assert.IsNull (ex.ParamName, "#5");
111 public void DirectoryTest ()
113 string path = TempFolder + DSC + "FIT.Directory.Test";
116 FileInfo info = new FileInfo (path);
117 DirectoryInfo dir = info.Directory;
118 Assert.AreEqual ("MonoTests.System.IO.Tests", dir.Name);
122 public void Exists ()
124 string path = TempFolder + DSC + "FIT.Exists.Test";
128 FileInfo info = new FileInfo (path);
129 Assert.IsFalse (info.Exists, "#1");
131 File.Create (path).Close ();
132 Assert.IsFalse (info.Exists, "#2");
133 info = new FileInfo (path);
134 Assert.IsTrue (info.Exists, "#3");
135 info = new FileInfo (TempFolder);
136 Assert.IsFalse (info.Exists, "#4");
144 [Category ("NotWorking")]
145 public void IsReadOnly ()
147 string path = TempFolder + DSC + "FIT.IsReadOnly.Test";
151 using (FileStream stream = File.Create (path)) {
152 stream.WriteByte (12);
156 FileInfo info1 = new FileInfo (path);
157 Assert.IsFalse (info1.IsReadOnly, "#1");
159 FileInfo info2 = new FileInfo (path);
160 info2.IsReadOnly = true;
161 Assert.IsFalse (info1.IsReadOnly, "#2");
162 Assert.IsTrue (info2.IsReadOnly, "#3");
164 FileInfo info3 = new FileInfo (path);
165 Assert.IsTrue (info3.IsReadOnly, "#4");
166 info3.IsReadOnly = false;
167 Assert.IsFalse (info1.IsReadOnly, "#4");
168 Assert.IsTrue (info2.IsReadOnly, "#5");
169 Assert.IsFalse (info3.IsReadOnly, "#6");
171 File.SetAttributes (path, FileAttributes.Normal);
178 public void Length ()
180 string path = TempFolder + DSC + "FIT.Length.Test";
184 FileStream stream = File.Create (path);
185 FileInfo info = new FileInfo (path);
186 Assert.AreEqual (0, info.Length, "#1");
187 stream.WriteByte (12);
189 Assert.AreEqual (0, info.Length, "#2");
190 info = new FileInfo (path);
191 Assert.AreEqual (1, info.Length, "#3");
199 public void Length_FileDoesNotExist ()
201 string path = TempFolder + DSC + "FIT.LengthException.Test";
203 FileInfo info = new FileInfo (path);
205 long l = info.Length;
206 Assert.Fail ("#1:" + l);
207 } catch (FileNotFoundException ex) {
208 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
209 Assert.AreEqual (path, ex.FileName, "#3");
210 Assert.IsNull (ex.InnerException, "#4");
211 Assert.IsNotNull (ex.Message, "#5");
216 public void AppendText ()
218 string path = TempFolder + DSC + "FIT.AppendText.Test";
222 FileInfo info = new FileInfo (path);
223 Assert.IsFalse (info.Exists, "#1");
225 StreamWriter writer = info.AppendText ();
226 info = new FileInfo (path);
227 Assert.IsTrue (info.Exists, "#2");
229 writer.Write ("aaa");
233 Assert.AreEqual (0, info.Length, "#3");
234 info = new FileInfo (path);
235 Assert.AreEqual (3, info.Length, "#4");
241 [Test] // CopyTo (String)
242 public void CopyTo1 ()
244 string path1 = TempFolder + DSC + "FIT.CopyTo.Source.Test";
245 string path2 = TempFolder + DSC + "FIT.CopyTo.Dest.Test";
249 File.Create (path1).Close ();
251 FileInfo info = new FileInfo (path1);
252 Assert.IsTrue (info.Exists, "#1");
254 FileInfo info2 = info.CopyTo (path2);
255 info = new FileInfo (path1);
256 Assert.IsTrue (info2.Exists, "#2");
263 [Test] // CopyTo (String)
264 public void CopyTo1_DestFileName_AlreadyExists ()
266 string path1 = TempFolder + DSC + "FIT.CopyToException.Source.Test";
267 string path2 = TempFolder + DSC + "FIT.CopyToException.Dest.Test";
272 File.Create (path1).Close ();
273 File.Create (path2).Close ();
274 FileInfo info = new FileInfo (path1);
278 } catch (IOException ex) {
279 // The file '...' already exists.
280 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
281 Assert.IsNull (ex.InnerException, "#3");
282 Assert.IsNotNull (ex.Message, "#4");
283 Assert.IsTrue (ex.Message.IndexOf (path2) != -1, "#5");
291 [Test] // CopyTo (String)
292 public void CopyTo1_DestFileName_Null ()
294 string path = TempFolder + DSC + "FIT.CopyToArgumentNullException.Test";
297 File.Create (path).Close ();
298 FileInfo info = new FileInfo (path);
302 } catch (ArgumentNullException ex) {
303 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
304 Assert.IsNull (ex.InnerException, "#3");
305 Assert.IsNotNull (ex.Message, "#4");
306 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
313 [Test] // CopyTo (String)
314 public void CopyTo1_DestFileName_Empty ()
316 string path = TempFolder + DSC + "FIT.CopyToArgument1Exception.Test";
320 File.Create (path).Close ();
321 FileInfo info = new FileInfo (path);
323 info.CopyTo (string.Empty);
325 } catch (ArgumentException ex) {
326 // Empty file name is not legal
327 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
328 Assert.IsNull (ex.InnerException, "#3");
329 Assert.IsNotNull (ex.Message, "#4");
330 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
337 [Test] // CopyTo (String)
338 public void CopyTo1_DestFileName_Whitespace ()
340 string path = TempFolder + DSC + "FIT.CopyToArgument2Exception.Test";
344 File.Create (path).Close ();
345 FileInfo info = new FileInfo (path);
349 } catch (ArgumentException ex) {
350 // The path is not of a legal form
351 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
352 Assert.IsNull (ex.InnerException, "#3");
353 Assert.IsNotNull (ex.Message, "#4");
354 Assert.IsNull (ex.ParamName, "#5");
361 [Test] // CopyTo (String)
362 public void CopyTo1_DestFileName_InvalidPathChars ()
364 string path = TempFolder + DSC + "FIT.CopyToArgument4Exception.Test";
365 string path2 = string.Empty;
369 File.Create (path).Close ();
370 FileInfo info = new FileInfo (path);
371 foreach (char c in Path.InvalidPathChars)
376 } catch (ArgumentException ex) {
377 // Illegal characters in path
378 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
379 Assert.IsNull (ex.InnerException, "#3");
380 Assert.IsNotNull (ex.Message, "#4");
381 Assert.IsNull (ex.ParamName, "#5");
388 [Test] // CopyTo (String, Boolean)
389 public void CopyTo2 ()
391 string path1 = TempFolder + DSC + "FIT.CopyTo2.Source.Test";
392 string path2 = TempFolder + DSC + "FIT.CopyTo2.Dest.Test";
396 File.Create (path1).Close ();
397 File.Create (path2).Close ();
398 FileInfo info = new FileInfo (path1);
400 FileInfo info2 = info.CopyTo (path2, true);
401 info = new FileInfo (path1);
402 Assert.IsTrue (info.Exists, "#1");
403 Assert.IsTrue (info2.Exists, "#2");
410 [Test] // CopyTo (String, Boolean)
411 public void CopyTo2_DestFileName_AlreadyExists ()
413 string path1 = TempFolder + DSC + "FIT.CopyToException.Source.Test";
414 string path2 = TempFolder + DSC + "FIT.CopyToException.Dest.Test";
419 File.Create (path1).Close ();
420 File.Create (path2).Close ();
421 FileInfo info = new FileInfo (path1);
423 info.CopyTo (path2, false);
425 } catch (IOException ex) {
426 // The file '...' already exists.
427 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
428 Assert.IsNull (ex.InnerException, "#3");
429 Assert.IsNotNull (ex.Message, "#4");
430 Assert.IsTrue (ex.Message.IndexOf (path2) != -1, "#5");
438 [Test] // CopyTo (String, Boolean)
439 public void CopyTo2_DestFileName_Null ()
441 string path = TempFolder + DSC + "FIT.CopyToArgumentNullException.Test";
444 File.Create (path).Close ();
445 FileInfo info = new FileInfo (path);
447 info.CopyTo (null, false);
449 } catch (ArgumentNullException ex) {
450 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
451 Assert.IsNull (ex.InnerException, "#3");
452 Assert.IsNotNull (ex.Message, "#4");
453 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
460 [Test] // CopyTo (String, Boolean)
461 public void CopyTo2_DestFileName_Empty ()
463 string path = TempFolder + DSC + "FIT.CopyToArgument1Exception.Test";
467 File.Create (path).Close ();
468 FileInfo info = new FileInfo (path);
470 info.CopyTo (string.Empty, false);
472 } catch (ArgumentException ex) {
473 // Empty file name is not legal
474 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
475 Assert.IsNull (ex.InnerException, "#3");
476 Assert.IsNotNull (ex.Message, "#4");
477 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
484 [Test] // CopyTo (String, Boolean)
485 public void CopyTo2_DestFileName_Whitespace ()
487 string path = TempFolder + DSC + "FIT.CopyToArgument2Exception.Test";
491 File.Create (path).Close ();
492 FileInfo info = new FileInfo (path);
494 info.CopyTo (" ", false);
496 } catch (ArgumentException ex) {
497 // The path is not of a legal form
498 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
499 Assert.IsNull (ex.InnerException, "#3");
500 Assert.IsNotNull (ex.Message, "#4");
501 Assert.IsNull (ex.ParamName, "#5");
508 [Test] // CopyTo (String, Boolean)
509 public void CopyTo2_DestFileName_InvalidPathChars ()
511 string path = TempFolder + DSC + "FIT.CopyToArgument4Exception.Test";
512 string path2 = string.Empty;
516 File.Create (path).Close ();
517 FileInfo info = new FileInfo (path);
518 foreach (char c in Path.InvalidPathChars)
521 info.CopyTo (path2, false);
523 } catch (ArgumentException ex) {
524 // Illegal characters in path
525 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
526 Assert.IsNull (ex.InnerException, "#3");
527 Assert.IsNotNull (ex.Message, "#4");
528 Assert.IsNull (ex.ParamName, "#5");
536 public void Create ()
538 string path = TempFolder + DSC + "FIT.Create.Test";
542 FileInfo info = new FileInfo (path);
543 Assert.IsFalse (info.Exists, "#1");
544 FileStream stream = info.Create ();
545 Assert.IsFalse (info.Exists, "#2");
546 info = new FileInfo (path);
547 Assert.IsTrue (info.Exists, "#3");
548 Assert.IsTrue (stream.CanRead, "#4");
549 Assert.IsTrue (stream.CanWrite, "#5");
550 Assert.IsTrue (stream.CanSeek, "#6");
558 public void CreateText ()
560 string path = TempFolder + DSC + "FIT.CreateText.Test";
564 FileInfo info = new FileInfo (path);
565 Assert.IsFalse (info.Exists, "#1");
566 StreamWriter writer = info.CreateText ();
567 writer.WriteLine ("test");
569 info = new FileInfo (path);
570 Assert.IsTrue (info.Exists, "#2");
577 public void CreateText_Directory ()
579 FileInfo info = new FileInfo (TempFolder);
583 } catch (UnauthorizedAccessException ex) {
584 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
585 Assert.IsNull (ex.InnerException, "#3");
586 Assert.IsNotNull (ex.Message, "#4");
591 public void Delete ()
593 string path = TempFolder + DSC + "FIT.Delete.Test";
597 FileInfo info = new FileInfo (path);
598 Assert.IsFalse (info.Exists, "#1");
599 info.Create ().Close ();
600 info = new FileInfo (path);
601 Assert.IsTrue (info.Exists, "#2");
603 Assert.IsTrue (info.Exists, "#3");
604 info = new FileInfo (path);
605 Assert.IsFalse (info.Exists, "#4");
612 public void Delete_Directory ()
614 FileInfo info = new FileInfo (TempFolder);
618 } catch (UnauthorizedAccessException ex) {
619 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
620 Assert.IsNull (ex.InnerException, "#3");
621 Assert.IsNotNull (ex.Message, "#4");
626 public void MoveTo ()
628 string path1 = TempFolder + DSC + "FIT.MoveTo.Source.Test";
629 string path2 = TempFolder + DSC + "FIT.MoveTo.Dest.Test";
634 File.Create (path1).Close ();
635 FileInfo info1 = new FileInfo (path1);
636 FileInfo info2 = new FileInfo (path2);
637 Assert.IsTrue (info1.Exists, "#A1");
638 Assert.AreEqual (path1, info1.FullName, "#A2");
639 Assert.IsFalse (info2.Exists, "#A3");
640 Assert.AreEqual (path2, info2.FullName, "#A4");
642 info1.MoveTo (path2);
643 info2 = new FileInfo (path2);
644 Assert.IsTrue (info1.Exists, "#B1");
645 Assert.AreEqual (path2, info1.FullName, "#B2");
646 Assert.IsTrue (info2.Exists, "#B3");
647 Assert.AreEqual (path2, info2.FullName, "#B4");
654 [Test] //Covers #18361
655 public void MoveTo_SameName ()
657 string name = "FIT.MoveTo.SameName.Test";
658 string path1 = TempFolder + DSC + name;
664 File.Create (path1).Close ();
665 FileInfo info1 = new FileInfo (path1);
666 FileInfo info2 = new FileInfo (path2);
667 Assert.IsTrue (info1.Exists, "#A1");
668 Assert.IsFalse (info2.Exists, "#A2");
670 info1.MoveTo (path2);
671 info1 = new FileInfo (path1);
672 info2 = new FileInfo (path2);
673 Assert.IsFalse (info1.Exists, "#B1");
674 Assert.IsTrue (info2.Exists, "#B2");
682 public void MoveTo_DestFileName_AlreadyExists ()
684 string sourceFile = TempFolder + DSC + "FIT.MoveTo.Source.Test";
688 // move to same directory
689 File.Create (sourceFile).Close ();
690 info = new FileInfo (sourceFile);
692 info.MoveTo (TempFolder);
694 } catch (IOException ex) {
695 // Cannot create a file when that file already exists
696 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
697 Assert.IsNull (ex.InnerException, "#A3");
698 Assert.IsNotNull (ex.Message, "#A4");
699 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#A5");
700 Assert.IsFalse (ex.Message.IndexOf (TempFolder) != -1, "#A6");
702 DeleteFile (sourceFile);
705 // move to exist file
706 File.Create (sourceFile).Close ();
707 destFile = TempFolder + DSC + "FIT.MoveTo.Dest.Test";
708 File.Create (destFile).Close ();
709 info = new FileInfo (sourceFile);
711 info.MoveTo (destFile);
713 } catch (IOException ex) {
714 // Cannot create a file when that file already exists
715 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
716 Assert.IsNull (ex.InnerException, "#B3");
717 Assert.IsNotNull (ex.Message, "#B4");
718 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#B5");
719 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#B6");
721 DeleteFile (sourceFile);
722 DeleteFile (destFile);
725 // move to existing directory
726 File.Create (sourceFile).Close ();
727 destFile = TempFolder + Path.DirectorySeparatorChar + "bar";
728 Directory.CreateDirectory (destFile);
729 info = new FileInfo (sourceFile);
731 info.MoveTo (destFile);
733 } catch (IOException ex) {
734 // Cannot create a file when that file already exists
735 Assert.AreEqual (typeof (IOException), ex.GetType (), "#C2");
736 Assert.IsNull (ex.InnerException, "#C3");
737 Assert.IsNotNull (ex.Message, "#C4");
738 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#C5");
739 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#C6");
741 DeleteFile (sourceFile);
742 DeleteDirectory (destFile);
747 public void MoveTo_DestFileName_DirectoryDoesNotExist ()
749 string sourceFile = TempFolder + Path.DirectorySeparatorChar + "foo";
750 string destFile = Path.Combine (Path.Combine (TempFolder, "doesnotexist"), "b");
751 DeleteFile (sourceFile);
753 File.Create (sourceFile).Close ();
754 FileInfo info = new FileInfo (sourceFile);
756 info.MoveTo (destFile);
758 } catch (DirectoryNotFoundException ex) {
759 // Could not find a part of the path
760 Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
761 Assert.IsNull (ex.InnerException, "#3");
762 Assert.IsNotNull (ex.Message, "#4");
765 DeleteFile (sourceFile);
770 public void MoveTo_DestFileName_Null ()
772 string path = TempFolder + DSC + "FIT.MoveToArgumentNullException.Test";
776 File.Create (path).Close ();
777 FileInfo info = new FileInfo (path);
781 } catch (ArgumentNullException ex) {
782 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
783 Assert.IsNull (ex.InnerException, "#3");
784 Assert.IsNotNull (ex.Message, "#4");
785 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
793 public void MoveTo_DestFileName_Empty ()
795 string path = TempFolder + DSC + "FIT.MoveToArgumentException.Test";
799 File.Create (path).Close ();
800 FileInfo info = new FileInfo (path);
802 info.MoveTo (string.Empty);
804 } catch (ArgumentException ex) {
805 // Empty file name is not legal
806 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
807 Assert.IsNull (ex.InnerException, "#3");
808 Assert.IsNotNull (ex.Message, "#4");
809 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
817 public void MoveTo_DestFileName_Whitespace ()
819 string path = TempFolder + DSC + "FIT.MoveToArgumentException.Test";
823 File.Create (path).Close ();
824 FileInfo info = new FileInfo (path);
828 } catch (ArgumentException ex) {
829 // The path is not of a legal form
830 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
831 Assert.IsNull (ex.InnerException, "#3");
832 Assert.IsNotNull (ex.Message, "#4");
833 Assert.IsNull (ex.ParamName, "#5");
841 public void MoveTo_FileDoesNotExist ()
843 string path1 = TempFolder + DSC + "FIT.MoveToFileNotFoundException.Src";
844 string path2 = TempFolder + DSC + "FIT.MoveToFileNotFoundException.Dst";
849 FileInfo info = new FileInfo (path1);
853 } catch (FileNotFoundException ex) {
854 // Unable to find the specified file
855 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
856 Assert.IsNull (ex.FileName, "#2");
857 Assert.IsNull (ex.InnerException, "#3");
858 Assert.IsNotNull (ex.Message, "#4");
867 public void MoveTo_Same ()
869 string path = TempFolder + DSC + "FIT.MoveToSame.Test";
873 File.Create (path).Close ();
874 FileInfo info = new FileInfo (path);
876 Assert.IsTrue (info.Exists, "#1");
877 Assert.IsTrue (File.Exists (path), "#2");
883 [Test] //Covers #38796
884 public void ToStringAfterMoveTo ()
886 string name1 = "FIT.ToStringAfterMoveTo.Test";
887 string name2 = "FIT.ToStringAfterMoveTo.Test.Alt";
888 string path1 = TempFolder + DSC + name1;
889 string path2 = TempFolder + DSC + name2;
894 File.Create (path1).Close ();
895 FileInfo info = new FileInfo (path1);
896 Assert.AreEqual (path1, info.ToString (), "#A");
899 Assert.AreEqual (path2, info.ToString (), "#B");
908 public void Replace1 ()
910 string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
911 string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
912 string path3 = TempFolder + DSC + "FIT.Replace.Back.Test";
918 File.Create (path1).Close ();
919 File.Create (path2).Close ();
920 File.Create (path3).Close ();
921 FileInfo info = new FileInfo (path1);
922 Assert.IsTrue (info.Exists, "#1");
923 FileInfo info2 = info.Replace (path2, path3);
924 Assert.IsTrue (info2.Exists, "#2");
925 FileInfo info3 = new FileInfo (path3);
926 Assert.IsTrue (info3.Exists, "#3");
934 public void Replace1_Backup_Null ()
936 string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
937 string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
942 File.Create (path1).Close ();
943 File.Create (path2).Close ();
944 FileInfo info = new FileInfo (path1);
945 Assert.IsTrue (info.Exists, "#1");
946 FileInfo info2 = info.Replace (path2, null);
947 Assert.IsTrue (info2.Exists, "#2");
948 info = new FileInfo (path1);
949 Assert.IsFalse (info.Exists, "#3");
956 public void Replace1_DestFileName_Null ()
958 string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
962 File.Create (path1).Close ();
963 FileInfo info = new FileInfo (path1);
964 info.Replace (null, null);
966 } catch (ArgumentNullException ex) {
967 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
968 Assert.IsNull (ex.InnerException, "#3");
969 Assert.IsNotNull (ex.Message, "#4");
977 public void Replace1_DestFileName_Empty ()
979 string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
983 File.Create (path1).Close ();
984 FileInfo info = new FileInfo (path1);
985 info.Replace (string.Empty, null);
987 } catch (ArgumentException ex) {
988 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
989 Assert.IsNull (ex.InnerException, "#3");
990 Assert.IsNotNull (ex.Message, "#4");
998 public void Replace1_DestFileName_WhiteSpace ()
1000 string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
1004 File.Create (path1).Close ();
1005 FileInfo info = new FileInfo (path1);
1006 info.Replace (" ", null);
1008 } catch (ArgumentException ex) {
1009 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1010 Assert.IsNull (ex.InnerException, "#3");
1011 Assert.IsNotNull (ex.Message, "#4");
1019 public void Replace1_DestFileName_InvalidPathChars ()
1021 string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
1022 string path2 = string.Empty;
1026 File.Create (path1).Close ();
1027 FileInfo info = new FileInfo (path1);
1028 foreach (char c in Path.InvalidPathChars)
1031 info.Replace (path2, null);
1033 } catch (ArgumentException ex) {
1034 // Illegal characters in path
1035 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1036 Assert.IsNull (ex.InnerException, "#3");
1037 Assert.IsNotNull (ex.Message, "#4");
1038 Assert.IsNull (ex.ParamName, "#5");
1046 public void Replace1_DestFileName_FileNotFound ()
1048 string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
1049 string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
1055 File.Create (path1).Close ();
1056 FileInfo info = new FileInfo (path1);
1057 info.Replace (path2, null);
1059 } catch (FileNotFoundException ex) {
1060 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1061 Assert.IsNull (ex.InnerException, "#3");
1062 Assert.IsNotNull (ex.Message, "#4");
1070 public void Replace1_DestFileName_IsReadOnly ()
1072 string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
1073 string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
1079 File.Create (path1).Close ();
1080 File.Create (path2).Close ();
1081 File.SetAttributes (path2, FileAttributes.ReadOnly);
1082 FileInfo info = new FileInfo (path1);
1083 info.Replace (path2, null);
1085 } catch (UnauthorizedAccessException ex) {
1086 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
1087 Assert.IsNull (ex.InnerException, "#3");
1088 Assert.IsNotNull (ex.Message, "#4");
1091 File.SetAttributes (path2, FileAttributes.Normal);
1098 public void Replace1_Source_FileNotFound ()
1100 string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
1101 string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
1106 File.Create (path2).Close ();
1107 FileInfo info = new FileInfo (path1);
1108 info.Replace (path2, null);
1110 } catch (FileNotFoundException ex) {
1111 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1112 Assert.IsNull (ex.InnerException, "#3");
1113 Assert.IsNotNull (ex.Message, "#4");
1124 string path = TempFolder + DSC + "FIT.Open.Test";
1126 FileStream stream = null;
1128 FileInfo info = new FileInfo (path);
1129 stream = info.Open (FileMode.CreateNew);
1130 Assert.IsTrue (stream.CanRead, "#A1");
1131 Assert.IsTrue (stream.CanSeek, "#A2");
1132 Assert.IsTrue (stream.CanWrite, "#A3");
1135 stream = info.Open (FileMode.Open);
1136 Assert.IsTrue (stream.CanRead, "#B1");
1137 Assert.IsTrue (stream.CanSeek, "#B2");
1138 Assert.IsTrue (stream.CanWrite, "#B3");
1141 stream = info.Open (FileMode.Append, FileAccess.Write);
1142 Assert.IsFalse (stream.CanRead, "#C1");
1143 Assert.IsTrue (stream.CanSeek, "#C2");
1144 Assert.IsTrue (stream.CanWrite, "#C3");
1147 stream = info.Open (FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
1148 Assert.IsTrue (stream.CanRead, "#D1");
1149 Assert.IsTrue (stream.CanSeek, "#D2");
1150 Assert.IsTrue (stream.CanWrite, "#D3");
1160 public void Open_FileDoesNotExist ()
1162 string path = TempFolder + DSC + "FIT.OpenFileNotFoundException.Test";
1165 FileInfo info = new FileInfo (path);
1167 info.Open (FileMode.Open);
1169 } catch (FileNotFoundException ex) {
1170 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1171 Assert.AreEqual (path, ex.FileName, "#3");
1172 Assert.IsNull (ex.InnerException, "#4");
1173 Assert.IsNotNull (ex.Message, "#5");
1178 public void OpenRead ()
1180 string path = TempFolder + DSC + "FIT.OpenRead.Test";
1182 FileStream stream = null;
1184 File.Create (path).Close ();
1185 FileInfo info = new FileInfo (path);
1186 stream = info.OpenRead ();
1187 Assert.IsTrue (stream.CanRead, "#1");
1188 Assert.IsTrue (stream.CanSeek, "#2");
1189 Assert.IsFalse (stream.CanWrite, "#3");
1200 public void OpenRead_FileLock ()
1202 string path = TempFolder + DSC + "FIT.OpenReadIOException.Test";
1204 FileStream stream = null;
1207 stream = File.Create (path);
1208 FileInfo info = new FileInfo (path);
1212 } catch (IOException ex) {
1213 // The process cannot access the file because
1214 // it is being used by another process
1215 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1216 Assert.IsNull (ex.InnerException, "#3");
1217 Assert.IsNotNull (ex.Message, "#4");
1227 public void OpenRead_Directory ()
1229 FileInfo info = new FileInfo (TempFolder);
1233 } catch (UnauthorizedAccessException ex) {
1234 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
1235 Assert.IsNull (ex.InnerException, "#3");
1236 Assert.IsNotNull (ex.Message, "#4");
1241 public void OpenText ()
1243 string path = TempFolder + DSC + "FIT.OpenText.Test";
1245 StreamReader reader = null;
1247 File.Create (path).Close ();
1248 FileInfo info = new FileInfo (path);
1249 reader = info.OpenText ();
1250 Assert.AreEqual (-1, reader.Peek ());
1259 public void OpenText_FileDoesNotExist ()
1261 string path = TempFolder + DSC + "FIT.OpenTextFileNotFoundException.Test";
1263 FileInfo info = new FileInfo (path);
1267 } catch (FileNotFoundException ex) {
1268 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1269 Assert.AreEqual (path, ex.FileName, "#3");
1270 Assert.IsNull (ex.InnerException, "#4");
1271 Assert.IsNotNull (ex.Message, "#5");
1276 public void OpenText_Directory ()
1278 FileInfo info = new FileInfo (TempFolder);
1282 } catch (UnauthorizedAccessException ex) {
1283 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
1284 Assert.IsNull (ex.InnerException, "#3");
1285 Assert.IsNotNull (ex.Message, "#4");
1290 public void OpenWrite ()
1292 string path = TempFolder + DSC + "FIT.OpenWrite.Test";
1294 FileStream stream = null;
1296 File.Create (path).Close ();
1297 FileInfo info = new FileInfo (path);
1298 stream = info.OpenWrite ();
1299 Assert.IsFalse (stream.CanRead, "#1");
1300 Assert.IsTrue (stream.CanSeek, "#2");
1301 Assert.IsTrue (stream.CanWrite, "#3");
1310 public void OpenWrite_Directory ()
1312 FileInfo info = new FileInfo (TempFolder);
1316 } catch (UnauthorizedAccessException ex) {
1317 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
1318 Assert.IsNull (ex.InnerException, "#3");
1319 Assert.IsNotNull (ex.Message, "#4");
1324 public void Serialization ()
1327 SerializationInfo si;
1329 info = new FileInfo ("Test");
1330 si = new SerializationInfo (typeof (FileInfo), new FormatterConverter ());
1331 info.GetObjectData (si, new StreamingContext ());
1333 Assert.AreEqual (2, si.MemberCount, "#A1");
1334 Assert.AreEqual ("Test", si.GetString ("OriginalPath"), "#A2");
1335 Assert.AreEqual (Path.Combine (Directory.GetCurrentDirectory (), "Test"), si.GetString ("FullPath"), "#A3");
1337 info = new FileInfo (TempFolder);
1338 si = new SerializationInfo (typeof (FileInfo), new FormatterConverter ());
1339 info.GetObjectData (si, new StreamingContext ());
1341 Assert.AreEqual (2, si.MemberCount, "#B1");
1342 Assert.AreEqual (TempFolder, si.GetString ("OriginalPath"), "#B2");
1343 Assert.AreEqual (TempFolder, si.GetString ("FullPath"), "#B3");
1347 public void Deserialization ()
1349 FileInfo info = new FileInfo ("Test");
1351 MemoryStream ms = new MemoryStream ();
1352 BinaryFormatter bf = new BinaryFormatter ();
1353 bf.Serialize (ms, info);
1356 FileInfo clone = (FileInfo) bf.Deserialize (ms);
1357 Assert.AreEqual (info.Name, clone.Name, "#1");
1358 Assert.AreEqual (info.FullName, clone.FullName, "#2");
1363 [Category ("MobileNotWorking")]
1364 public void ToStringVariety ()
1366 Assert.AreEqual ("foo", new FileInfo ("foo").ToString ());
1367 Assert.AreEqual ("c:/foo", new FileInfo ("c:/foo").ToString ());
1368 Assert.AreEqual ("/usr/local/foo", new FileInfo ("/usr/local/foo").ToString ());
1369 Assert.AreEqual ("c:\\foo", new FileInfo ("c:\\foo").ToString ());
1370 Assert.AreEqual ("/usr/local\\foo", new FileInfo ("/usr/local\\foo").ToString ());
1371 Assert.AreEqual ("foo/BAR/baz", new FileInfo ("foo/BAR/baz").ToString ());
1372 Assert.AreEqual ("c:/documents and settings", new FileInfo ("c:/documents and settings").ToString ());
1373 Assert.AreEqual ("c:/docUme~1", new FileInfo ("c:/docUme~1").ToString ());
1376 void DeleteFile (string path)
1378 if (File.Exists (path))
1382 void DeleteDirectory (string path)
1384 if (Directory.Exists (path))
1385 Directory.Delete (path, true);