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 public void IsReadOnly ()
146 string path = TempFolder + DSC + "FIT.IsReadOnly.Test";
150 using (FileStream stream = File.Create (path)) {
151 stream.WriteByte (12);
155 FileInfo info1 = new FileInfo (path);
156 Assert.IsFalse (info1.IsReadOnly, "#1");
158 FileInfo info2 = new FileInfo (path);
159 info2.IsReadOnly = true;
160 Assert.IsFalse (info1.IsReadOnly, "#2");
161 Assert.IsTrue (info2.IsReadOnly, "#3");
163 FileInfo info3 = new FileInfo (path);
164 Assert.IsTrue (info3.IsReadOnly, "#4");
165 info3.IsReadOnly = false;
166 Assert.IsFalse (info1.IsReadOnly, "#4");
167 Assert.IsTrue (info2.IsReadOnly, "#5");
168 Assert.IsFalse (info3.IsReadOnly, "#6");
170 File.SetAttributes (path, FileAttributes.Normal);
177 public void Length ()
179 string path = TempFolder + DSC + "FIT.Length.Test";
183 FileStream stream = File.Create (path);
184 FileInfo info = new FileInfo (path);
185 Assert.AreEqual (0, info.Length, "#1");
186 stream.WriteByte (12);
188 Assert.AreEqual (0, info.Length, "#2");
189 info = new FileInfo (path);
190 Assert.AreEqual (1, info.Length, "#3");
198 public void Length_FileDoesNotExist ()
200 string path = TempFolder + DSC + "FIT.LengthException.Test";
202 FileInfo info = new FileInfo (path);
204 long l = info.Length;
205 Assert.Fail ("#1:" + l);
206 } catch (FileNotFoundException ex) {
207 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
208 Assert.AreEqual (path, ex.FileName, "#3");
209 Assert.IsNull (ex.InnerException, "#4");
210 Assert.IsNotNull (ex.Message, "#5");
215 public void AppendText ()
217 string path = TempFolder + DSC + "FIT.AppendText.Test";
221 FileInfo info = new FileInfo (path);
222 Assert.IsFalse (info.Exists, "#1");
224 StreamWriter writer = info.AppendText ();
225 info = new FileInfo (path);
226 Assert.IsTrue (info.Exists, "#2");
228 writer.Write ("aaa");
232 Assert.AreEqual (0, info.Length, "#3");
233 info = new FileInfo (path);
234 Assert.AreEqual (3, info.Length, "#4");
240 [Test] // CopyTo (String)
241 public void CopyTo1 ()
243 string path1 = TempFolder + DSC + "FIT.CopyTo.Source.Test";
244 string path2 = TempFolder + DSC + "FIT.CopyTo.Dest.Test";
248 File.Create (path1).Close ();
250 FileInfo info = new FileInfo (path1);
251 Assert.IsTrue (info.Exists, "#1");
253 FileInfo info2 = info.CopyTo (path2);
254 info = new FileInfo (path1);
255 Assert.IsTrue (info2.Exists, "#2");
262 [Test] // CopyTo (String)
263 public void CopyTo1_DestFileName_AlreadyExists ()
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);
277 } catch (IOException ex) {
278 // The file '...' already exists.
279 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
280 Assert.IsNull (ex.InnerException, "#3");
281 Assert.IsNotNull (ex.Message, "#4");
282 Assert.IsTrue (ex.Message.IndexOf (path2) != -1, "#5");
290 [Test] // CopyTo (String)
291 public void CopyTo1_DestFileName_Null ()
293 string path = TempFolder + DSC + "FIT.CopyToArgumentNullException.Test";
296 File.Create (path).Close ();
297 FileInfo info = new FileInfo (path);
301 } catch (ArgumentNullException ex) {
302 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
303 Assert.IsNull (ex.InnerException, "#3");
304 Assert.IsNotNull (ex.Message, "#4");
305 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
312 [Test] // CopyTo (String)
313 public void CopyTo1_DestFileName_Empty ()
315 string path = TempFolder + DSC + "FIT.CopyToArgument1Exception.Test";
319 File.Create (path).Close ();
320 FileInfo info = new FileInfo (path);
322 info.CopyTo (string.Empty);
324 } catch (ArgumentException ex) {
325 // Empty file name is not legal
326 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
327 Assert.IsNull (ex.InnerException, "#3");
328 Assert.IsNotNull (ex.Message, "#4");
329 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
336 [Test] // CopyTo (String)
337 public void CopyTo1_DestFileName_Whitespace ()
339 string path = TempFolder + DSC + "FIT.CopyToArgument2Exception.Test";
343 File.Create (path).Close ();
344 FileInfo info = new FileInfo (path);
348 } catch (ArgumentException ex) {
349 // The path is not of a legal form
350 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
351 Assert.IsNull (ex.InnerException, "#3");
352 Assert.IsNotNull (ex.Message, "#4");
353 Assert.IsNull (ex.ParamName, "#5");
360 [Test] // CopyTo (String)
361 public void CopyTo1_DestFileName_InvalidPathChars ()
363 string path = TempFolder + DSC + "FIT.CopyToArgument4Exception.Test";
364 string path2 = string.Empty;
368 File.Create (path).Close ();
369 FileInfo info = new FileInfo (path);
370 foreach (char c in Path.InvalidPathChars)
375 } catch (ArgumentException ex) {
376 // Illegal characters in path
377 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
378 Assert.IsNull (ex.InnerException, "#3");
379 Assert.IsNotNull (ex.Message, "#4");
380 Assert.IsNull (ex.ParamName, "#5");
387 [Test] // CopyTo (String, Boolean)
388 public void CopyTo2 ()
390 string path1 = TempFolder + DSC + "FIT.CopyTo2.Source.Test";
391 string path2 = TempFolder + DSC + "FIT.CopyTo2.Dest.Test";
395 File.Create (path1).Close ();
396 File.Create (path2).Close ();
397 FileInfo info = new FileInfo (path1);
399 FileInfo info2 = info.CopyTo (path2, true);
400 info = new FileInfo (path1);
401 Assert.IsTrue (info.Exists, "#1");
402 Assert.IsTrue (info2.Exists, "#2");
409 [Test] // CopyTo (String, Boolean)
410 public void CopyTo2_DestFileName_AlreadyExists ()
412 string path1 = TempFolder + DSC + "FIT.CopyToException.Source.Test";
413 string path2 = TempFolder + DSC + "FIT.CopyToException.Dest.Test";
418 File.Create (path1).Close ();
419 File.Create (path2).Close ();
420 FileInfo info = new FileInfo (path1);
422 info.CopyTo (path2, false);
424 } catch (IOException ex) {
425 // The file '...' already exists.
426 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
427 Assert.IsNull (ex.InnerException, "#3");
428 Assert.IsNotNull (ex.Message, "#4");
429 Assert.IsTrue (ex.Message.IndexOf (path2) != -1, "#5");
437 [Test] // CopyTo (String, Boolean)
438 public void CopyTo2_DestFileName_Null ()
440 string path = TempFolder + DSC + "FIT.CopyToArgumentNullException.Test";
443 File.Create (path).Close ();
444 FileInfo info = new FileInfo (path);
446 info.CopyTo (null, false);
448 } catch (ArgumentNullException ex) {
449 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
450 Assert.IsNull (ex.InnerException, "#3");
451 Assert.IsNotNull (ex.Message, "#4");
452 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
459 [Test] // CopyTo (String, Boolean)
460 public void CopyTo2_DestFileName_Empty ()
462 string path = TempFolder + DSC + "FIT.CopyToArgument1Exception.Test";
466 File.Create (path).Close ();
467 FileInfo info = new FileInfo (path);
469 info.CopyTo (string.Empty, false);
471 } catch (ArgumentException ex) {
472 // Empty file name is not legal
473 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
474 Assert.IsNull (ex.InnerException, "#3");
475 Assert.IsNotNull (ex.Message, "#4");
476 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
483 [Test] // CopyTo (String, Boolean)
484 public void CopyTo2_DestFileName_Whitespace ()
486 string path = TempFolder + DSC + "FIT.CopyToArgument2Exception.Test";
490 File.Create (path).Close ();
491 FileInfo info = new FileInfo (path);
493 info.CopyTo (" ", false);
495 } catch (ArgumentException ex) {
496 // The path is not of a legal form
497 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
498 Assert.IsNull (ex.InnerException, "#3");
499 Assert.IsNotNull (ex.Message, "#4");
500 Assert.IsNull (ex.ParamName, "#5");
507 [Test] // CopyTo (String, Boolean)
508 public void CopyTo2_DestFileName_InvalidPathChars ()
510 string path = TempFolder + DSC + "FIT.CopyToArgument4Exception.Test";
511 string path2 = string.Empty;
515 File.Create (path).Close ();
516 FileInfo info = new FileInfo (path);
517 foreach (char c in Path.InvalidPathChars)
520 info.CopyTo (path2, false);
522 } catch (ArgumentException ex) {
523 // Illegal characters in path
524 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
525 Assert.IsNull (ex.InnerException, "#3");
526 Assert.IsNotNull (ex.Message, "#4");
527 Assert.IsNull (ex.ParamName, "#5");
535 public void Create ()
537 string path = TempFolder + DSC + "FIT.Create.Test";
541 FileInfo info = new FileInfo (path);
542 Assert.IsFalse (info.Exists, "#1");
543 FileStream stream = info.Create ();
544 Assert.IsFalse (info.Exists, "#2");
545 info = new FileInfo (path);
546 Assert.IsTrue (info.Exists, "#3");
547 Assert.IsTrue (stream.CanRead, "#4");
548 Assert.IsTrue (stream.CanWrite, "#5");
549 Assert.IsTrue (stream.CanSeek, "#6");
557 public void CreateText ()
559 string path = TempFolder + DSC + "FIT.CreateText.Test";
563 FileInfo info = new FileInfo (path);
564 Assert.IsFalse (info.Exists, "#1");
565 StreamWriter writer = info.CreateText ();
566 writer.WriteLine ("test");
568 info = new FileInfo (path);
569 Assert.IsTrue (info.Exists, "#2");
576 public void CreateText_Directory ()
578 FileInfo info = new FileInfo (TempFolder);
582 } catch (UnauthorizedAccessException ex) {
583 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
584 Assert.IsNull (ex.InnerException, "#3");
585 Assert.IsNotNull (ex.Message, "#4");
590 public void Delete ()
592 string path = TempFolder + DSC + "FIT.Delete.Test";
596 FileInfo info = new FileInfo (path);
597 Assert.IsFalse (info.Exists, "#1");
599 Assert.IsFalse (info.Exists, "#1a");
600 info.Create ().Close ();
601 info = new FileInfo (path);
602 Assert.IsTrue (info.Exists, "#2");
604 Assert.IsTrue (info.Exists, "#3");
605 info = new FileInfo (path);
606 Assert.IsFalse (info.Exists, "#4");
613 public void Delete_Directory ()
615 FileInfo info = new FileInfo (TempFolder);
619 } catch (UnauthorizedAccessException ex) {
620 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
621 Assert.IsNull (ex.InnerException, "#3");
622 Assert.IsNotNull (ex.Message, "#4");
627 public void MoveTo ()
629 string path1 = TempFolder + DSC + "FIT.MoveTo.Source.Test";
630 string path2 = TempFolder + DSC + "FIT.MoveTo.Dest.Test";
635 File.Create (path1).Close ();
636 FileInfo info1 = new FileInfo (path1);
637 FileInfo info2 = new FileInfo (path2);
638 Assert.IsTrue (info1.Exists, "#A1");
639 Assert.AreEqual (path1, info1.FullName, "#A2");
640 Assert.IsFalse (info2.Exists, "#A3");
641 Assert.AreEqual (path2, info2.FullName, "#A4");
643 info1.MoveTo (path2);
644 info2 = new FileInfo (path2);
645 Assert.IsTrue (info1.Exists, "#B1");
646 Assert.AreEqual (path2, info1.FullName, "#B2");
647 Assert.IsTrue (info2.Exists, "#B3");
648 Assert.AreEqual (path2, info2.FullName, "#B4");
655 [Test] //Covers #18361
656 public void MoveTo_SameName ()
658 string name = "FIT.MoveTo.SameName.Test";
659 string path1 = TempFolder + DSC + name;
660 string path2 = TempFolder + DSC + "same";
661 Directory.CreateDirectory (path2);
667 File.Create (path1).Close ();
668 FileInfo info1 = new FileInfo (path1);
669 FileInfo info2 = new FileInfo (path2);
670 Assert.IsTrue (info1.Exists, "#A1");
671 Assert.IsFalse (info2.Exists, "#A2");
673 info1.MoveTo (path2);
674 info1 = new FileInfo (path1);
675 info2 = new FileInfo (path2);
676 Assert.IsFalse (info1.Exists, "#B1");
677 Assert.IsTrue (info2.Exists, "#B2");
685 public void MoveTo_DestFileName_AlreadyExists ()
687 string sourceFile = TempFolder + DSC + "FIT.MoveTo.Source.Test";
691 // move to same directory
692 File.Create (sourceFile).Close ();
693 info = new FileInfo (sourceFile);
695 info.MoveTo (TempFolder);
697 } catch (IOException ex) {
698 // Cannot create a file when that file already exists
699 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
700 Assert.IsNull (ex.InnerException, "#A3");
701 Assert.IsNotNull (ex.Message, "#A4");
702 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#A5");
703 Assert.IsFalse (ex.Message.IndexOf (TempFolder) != -1, "#A6");
705 DeleteFile (sourceFile);
708 // move to exist file
709 File.Create (sourceFile).Close ();
710 destFile = TempFolder + DSC + "FIT.MoveTo.Dest.Test";
711 File.Create (destFile).Close ();
712 info = new FileInfo (sourceFile);
714 info.MoveTo (destFile);
716 } catch (IOException ex) {
717 // Cannot create a file when that file already exists
718 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
719 Assert.IsNull (ex.InnerException, "#B3");
720 Assert.IsNotNull (ex.Message, "#B4");
721 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#B5");
722 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#B6");
724 DeleteFile (sourceFile);
725 DeleteFile (destFile);
728 // move to existing directory
729 File.Create (sourceFile).Close ();
730 destFile = TempFolder + Path.DirectorySeparatorChar + "bar";
731 Directory.CreateDirectory (destFile);
732 info = new FileInfo (sourceFile);
734 info.MoveTo (destFile);
736 } catch (IOException ex) {
737 // Cannot create a file when that file already exists
738 Assert.AreEqual (typeof (IOException), ex.GetType (), "#C2");
739 Assert.IsNull (ex.InnerException, "#C3");
740 Assert.IsNotNull (ex.Message, "#C4");
741 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#C5");
742 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#C6");
744 DeleteFile (sourceFile);
745 DeleteDirectory (destFile);
750 public void MoveTo_DestFileName_DirectoryDoesNotExist ()
752 string sourceFile = TempFolder + Path.DirectorySeparatorChar + "foo";
753 string destFile = Path.Combine (Path.Combine (TempFolder, "doesnotexist"), "b");
754 DeleteFile (sourceFile);
756 File.Create (sourceFile).Close ();
757 FileInfo info = new FileInfo (sourceFile);
759 info.MoveTo (destFile);
761 } catch (FileNotFoundException ex) {
762 // Could not find a part of the path
763 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
764 Assert.IsNull (ex.InnerException, "#3");
765 Assert.IsNotNull (ex.Message, "#4");
768 DeleteFile (sourceFile);
773 public void MoveTo_DestFileName_Null ()
775 string path = TempFolder + DSC + "FIT.MoveToArgumentNullException.Test";
779 File.Create (path).Close ();
780 FileInfo info = new FileInfo (path);
784 } catch (ArgumentNullException ex) {
785 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
786 Assert.IsNull (ex.InnerException, "#3");
787 Assert.IsNotNull (ex.Message, "#4");
788 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
796 public void MoveTo_DestFileName_Empty ()
798 string path = TempFolder + DSC + "FIT.MoveToArgumentException.Test";
802 File.Create (path).Close ();
803 FileInfo info = new FileInfo (path);
805 info.MoveTo (string.Empty);
807 } catch (ArgumentException ex) {
808 // Empty file name is not legal
809 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
810 Assert.IsNull (ex.InnerException, "#3");
811 Assert.IsNotNull (ex.Message, "#4");
812 Assert.AreEqual ("destFileName", ex.ParamName, "#5");
820 public void MoveTo_DestFileName_Whitespace ()
822 string path = TempFolder + DSC + "FIT.MoveToArgumentException.Test";
826 File.Create (path).Close ();
827 FileInfo info = new FileInfo (path);
831 } catch (ArgumentException ex) {
832 // The path is not of a legal form
833 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
834 Assert.IsNull (ex.InnerException, "#3");
835 Assert.IsNotNull (ex.Message, "#4");
836 Assert.IsNull (ex.ParamName, "#5");
844 public void MoveTo_FileDoesNotExist ()
846 string path1 = TempFolder + DSC + "FIT.MoveToFileNotFoundException.Src";
847 string path2 = TempFolder + DSC + "FIT.MoveToFileNotFoundException.Dst";
852 FileInfo info = new FileInfo (path1);
856 } catch (FileNotFoundException ex) {
857 // Unable to find the specified file
858 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
859 Assert.IsNull (ex.FileName, "#2");
860 Assert.IsNull (ex.InnerException, "#3");
861 Assert.IsNotNull (ex.Message, "#4");
870 public void MoveTo_Same ()
872 string path = TempFolder + DSC + "FIT.MoveToSame.Test";
876 File.Create (path).Close ();
877 FileInfo info = new FileInfo (path);
879 Assert.IsTrue (info.Exists, "#1");
880 Assert.IsTrue (File.Exists (path), "#2");
886 [Test] //Covers #38796
887 public void ToStringAfterMoveTo ()
889 string name1 = "FIT.ToStringAfterMoveTo.Test";
890 string name2 = "FIT.ToStringAfterMoveTo.Test.Alt";
891 string path1 = TempFolder + DSC + name1;
892 string path2 = TempFolder + DSC + name2;
897 File.Create (path1).Close ();
898 FileInfo info = new FileInfo (path1);
899 Assert.AreEqual (path1, info.ToString (), "#A");
902 Assert.AreEqual (path2, info.ToString (), "#B");
911 public void Replace1 ()
913 string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
914 string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
915 string path3 = TempFolder + DSC + "FIT.Replace.Back.Test";
921 File.Create (path1).Close ();
922 File.Create (path2).Close ();
923 File.Create (path3).Close ();
924 FileInfo info = new FileInfo (path1);
925 Assert.IsTrue (info.Exists, "#1");
926 FileInfo info2 = info.Replace (path2, path3);
927 Assert.IsTrue (info2.Exists, "#2");
928 FileInfo info3 = new FileInfo (path3);
929 Assert.IsTrue (info3.Exists, "#3");
937 public void Replace1_Backup_Null ()
939 string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
940 string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
945 File.Create (path1).Close ();
946 File.Create (path2).Close ();
947 FileInfo info = new FileInfo (path1);
948 Assert.IsTrue (info.Exists, "#1");
949 FileInfo info2 = info.Replace (path2, null);
950 Assert.IsTrue (info2.Exists, "#2");
951 info = new FileInfo (path1);
952 Assert.IsFalse (info.Exists, "#3");
959 public void Replace1_DestFileName_Null ()
961 string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
965 File.Create (path1).Close ();
966 FileInfo info = new FileInfo (path1);
967 info.Replace (null, null);
969 } catch (ArgumentNullException ex) {
970 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
971 Assert.IsNull (ex.InnerException, "#3");
972 Assert.IsNotNull (ex.Message, "#4");
980 public void Replace1_DestFileName_Empty ()
982 string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
986 File.Create (path1).Close ();
987 FileInfo info = new FileInfo (path1);
988 info.Replace (string.Empty, null);
990 } catch (ArgumentException ex) {
991 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
992 Assert.IsNull (ex.InnerException, "#3");
993 Assert.IsNotNull (ex.Message, "#4");
1001 public void Replace1_DestFileName_WhiteSpace ()
1003 string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
1007 File.Create (path1).Close ();
1008 FileInfo info = new FileInfo (path1);
1009 info.Replace (" ", null);
1011 } catch (ArgumentException ex) {
1012 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1013 Assert.IsNull (ex.InnerException, "#3");
1014 Assert.IsNotNull (ex.Message, "#4");
1022 public void Replace1_DestFileName_InvalidPathChars ()
1024 string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
1025 string path2 = string.Empty;
1029 File.Create (path1).Close ();
1030 FileInfo info = new FileInfo (path1);
1031 foreach (char c in Path.InvalidPathChars)
1034 info.Replace (path2, null);
1036 } catch (ArgumentException ex) {
1037 // Illegal characters in path
1038 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1039 Assert.IsNull (ex.InnerException, "#3");
1040 Assert.IsNotNull (ex.Message, "#4");
1041 Assert.IsNull (ex.ParamName, "#5");
1049 public void Replace1_DestFileName_FileNotFound ()
1051 string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
1052 string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
1058 File.Create (path1).Close ();
1059 FileInfo info = new FileInfo (path1);
1060 info.Replace (path2, null);
1062 } catch (FileNotFoundException ex) {
1063 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1064 Assert.IsNull (ex.InnerException, "#3");
1065 Assert.IsNotNull (ex.Message, "#4");
1073 public void Replace1_DestFileName_IsReadOnly ()
1075 string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
1076 string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
1082 File.Create (path1).Close ();
1083 File.Create (path2).Close ();
1084 File.SetAttributes (path2, FileAttributes.ReadOnly);
1085 FileInfo info = new FileInfo (path1);
1086 info.Replace (path2, null);
1088 } catch (UnauthorizedAccessException ex) {
1089 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
1090 Assert.IsNull (ex.InnerException, "#3");
1091 Assert.IsNotNull (ex.Message, "#4");
1094 File.SetAttributes (path2, FileAttributes.Normal);
1101 public void Replace1_Source_FileNotFound ()
1103 string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
1104 string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
1109 File.Create (path2).Close ();
1110 FileInfo info = new FileInfo (path1);
1111 info.Replace (path2, null);
1113 } catch (FileNotFoundException ex) {
1114 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1115 Assert.IsNull (ex.InnerException, "#3");
1116 Assert.IsNotNull (ex.Message, "#4");
1127 string path = TempFolder + DSC + "FIT.Open.Test";
1129 FileStream stream = null;
1131 FileInfo info = new FileInfo (path);
1132 stream = info.Open (FileMode.CreateNew);
1133 Assert.IsTrue (stream.CanRead, "#A1");
1134 Assert.IsTrue (stream.CanSeek, "#A2");
1135 Assert.IsTrue (stream.CanWrite, "#A3");
1138 stream = info.Open (FileMode.Open);
1139 Assert.IsTrue (stream.CanRead, "#B1");
1140 Assert.IsTrue (stream.CanSeek, "#B2");
1141 Assert.IsTrue (stream.CanWrite, "#B3");
1144 stream = info.Open (FileMode.Append, FileAccess.Write);
1145 Assert.IsFalse (stream.CanRead, "#C1");
1146 Assert.IsTrue (stream.CanSeek, "#C2");
1147 Assert.IsTrue (stream.CanWrite, "#C3");
1150 stream = info.Open (FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
1151 Assert.IsTrue (stream.CanRead, "#D1");
1152 Assert.IsTrue (stream.CanSeek, "#D2");
1153 Assert.IsTrue (stream.CanWrite, "#D3");
1163 public void Open_FileDoesNotExist ()
1165 string path = TempFolder + DSC + "FIT.OpenFileNotFoundException.Test";
1168 FileInfo info = new FileInfo (path);
1170 info.Open (FileMode.Open);
1172 } catch (FileNotFoundException ex) {
1173 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1174 Assert.AreEqual (path, ex.FileName, "#3");
1175 Assert.IsNull (ex.InnerException, "#4");
1176 Assert.IsNotNull (ex.Message, "#5");
1181 public void OpenRead ()
1183 string path = TempFolder + DSC + "FIT.OpenRead.Test";
1185 FileStream stream = null;
1187 File.Create (path).Close ();
1188 FileInfo info = new FileInfo (path);
1189 stream = info.OpenRead ();
1190 Assert.IsTrue (stream.CanRead, "#1");
1191 Assert.IsTrue (stream.CanSeek, "#2");
1192 Assert.IsFalse (stream.CanWrite, "#3");
1203 public void OpenRead_FileLock ()
1205 string path = TempFolder + DSC + "FIT.OpenReadIOException.Test";
1207 FileStream stream = null;
1210 stream = File.Create (path);
1211 FileInfo info = new FileInfo (path);
1215 } catch (IOException ex) {
1216 // The process cannot access the file because
1217 // it is being used by another process
1218 Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
1219 Assert.IsNull (ex.InnerException, "#3");
1220 Assert.IsNotNull (ex.Message, "#4");
1230 public void OpenRead_Directory ()
1232 FileInfo info = new FileInfo (TempFolder);
1236 } catch (UnauthorizedAccessException ex) {
1237 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
1238 Assert.IsNull (ex.InnerException, "#3");
1239 Assert.IsNotNull (ex.Message, "#4");
1244 public void OpenText ()
1246 string path = TempFolder + DSC + "FIT.OpenText.Test";
1248 StreamReader reader = null;
1250 File.Create (path).Close ();
1251 FileInfo info = new FileInfo (path);
1252 reader = info.OpenText ();
1253 Assert.AreEqual (-1, reader.Peek ());
1262 public void OpenText_FileDoesNotExist ()
1264 string path = TempFolder + DSC + "FIT.OpenTextFileNotFoundException.Test";
1266 FileInfo info = new FileInfo (path);
1270 } catch (FileNotFoundException ex) {
1271 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1272 Assert.AreEqual (path, ex.FileName, "#3");
1273 Assert.IsNull (ex.InnerException, "#4");
1274 Assert.IsNotNull (ex.Message, "#5");
1279 public void OpenText_Directory ()
1281 FileInfo info = new FileInfo (TempFolder);
1285 } catch (UnauthorizedAccessException ex) {
1286 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
1287 Assert.IsNull (ex.InnerException, "#3");
1288 Assert.IsNotNull (ex.Message, "#4");
1293 public void OpenWrite ()
1295 string path = TempFolder + DSC + "FIT.OpenWrite.Test";
1297 FileStream stream = null;
1299 File.Create (path).Close ();
1300 FileInfo info = new FileInfo (path);
1301 stream = info.OpenWrite ();
1302 Assert.IsFalse (stream.CanRead, "#1");
1303 Assert.IsTrue (stream.CanSeek, "#2");
1304 Assert.IsTrue (stream.CanWrite, "#3");
1313 public void OpenWrite_Directory ()
1315 FileInfo info = new FileInfo (TempFolder);
1319 } catch (UnauthorizedAccessException ex) {
1320 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
1321 Assert.IsNull (ex.InnerException, "#3");
1322 Assert.IsNotNull (ex.Message, "#4");
1327 public void Serialization ()
1330 SerializationInfo si;
1332 info = new FileInfo ("Test");
1333 si = new SerializationInfo (typeof (FileInfo), new FormatterConverter ());
1334 info.GetObjectData (si, new StreamingContext ());
1336 Assert.AreEqual (2, si.MemberCount, "#A1");
1337 Assert.AreEqual ("Test", si.GetString ("OriginalPath"), "#A2");
1338 Assert.AreEqual (Path.Combine (Directory.GetCurrentDirectory (), "Test"), si.GetString ("FullPath"), "#A3");
1340 info = new FileInfo (TempFolder);
1341 si = new SerializationInfo (typeof (FileInfo), new FormatterConverter ());
1342 info.GetObjectData (si, new StreamingContext ());
1344 Assert.AreEqual (2, si.MemberCount, "#B1");
1345 Assert.AreEqual (TempFolder, si.GetString ("OriginalPath"), "#B2");
1346 Assert.AreEqual (TempFolder, si.GetString ("FullPath"), "#B3");
1350 public void Deserialization ()
1352 FileInfo info = new FileInfo ("Test");
1354 MemoryStream ms = new MemoryStream ();
1355 BinaryFormatter bf = new BinaryFormatter ();
1356 bf.Serialize (ms, info);
1359 FileInfo clone = (FileInfo) bf.Deserialize (ms);
1360 Assert.AreEqual (info.Name, clone.Name, "#1");
1361 Assert.AreEqual (info.FullName, clone.FullName, "#2");
1366 [Category ("MobileNotWorking")]
1367 public void ToStringVariety ()
1369 Assert.AreEqual ("foo", new FileInfo ("foo").ToString ());
1370 Assert.AreEqual ("c:/foo", new FileInfo ("c:/foo").ToString ());
1371 Assert.AreEqual ("/usr/local/foo", new FileInfo ("/usr/local/foo").ToString ());
1372 Assert.AreEqual ("c:\\foo", new FileInfo ("c:\\foo").ToString ());
1373 Assert.AreEqual ("/usr/local\\foo", new FileInfo ("/usr/local\\foo").ToString ());
1374 Assert.AreEqual ("foo/BAR/baz", new FileInfo ("foo/BAR/baz").ToString ());
1375 Assert.AreEqual ("c:/documents and settings", new FileInfo ("c:/documents and settings").ToString ());
1376 Assert.AreEqual ("c:/docUme~1", new FileInfo ("c:/docUme~1").ToString ());
1379 void DeleteFile (string path)
1381 if (File.Exists (path))
1385 void DeleteDirectory (string path)
1387 if (Directory.Exists (path))
1388 Directory.Delete (path, true);