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
.IsNotNull (ex
.Message
, "#4");
352 Assert
.IsNull (ex
.ParamName
, "#5");
359 [Test
] // CopyTo (String)
360 public void CopyTo1_DestFileName_InvalidPathChars ()
362 string path
= TempFolder
+ DSC
+ "FIT.CopyToArgument4Exception.Test";
363 string path2
= string.Empty
;
367 File
.Create (path
).Close ();
368 FileInfo info
= new FileInfo (path
);
369 foreach (char c
in Path
.InvalidPathChars
)
374 } catch (ArgumentException ex
) {
375 // Illegal characters in path
376 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#2");
377 Assert
.IsNull (ex
.InnerException
, "#3");
378 Assert
.IsNotNull (ex
.Message
, "#4");
379 Assert
.IsNull (ex
.ParamName
, "#5");
386 [Test
] // CopyTo (String, Boolean)
387 public void CopyTo2 ()
389 string path1
= TempFolder
+ DSC
+ "FIT.CopyTo2.Source.Test";
390 string path2
= TempFolder
+ DSC
+ "FIT.CopyTo2.Dest.Test";
394 File
.Create (path1
).Close ();
395 File
.Create (path2
).Close ();
396 FileInfo info
= new FileInfo (path1
);
398 FileInfo info2
= info
.CopyTo (path2
, true);
399 info
= new FileInfo (path1
);
400 Assert
.IsTrue (info
.Exists
, "#1");
401 Assert
.IsTrue (info2
.Exists
, "#2");
408 [Test
] // CopyTo (String, Boolean)
409 public void CopyTo2_DestFileName_AlreadyExists ()
411 string path1
= TempFolder
+ DSC
+ "FIT.CopyToException.Source.Test";
412 string path2
= TempFolder
+ DSC
+ "FIT.CopyToException.Dest.Test";
417 File
.Create (path1
).Close ();
418 File
.Create (path2
).Close ();
419 FileInfo info
= new FileInfo (path1
);
421 info
.CopyTo (path2
, false);
423 } catch (IOException ex
) {
424 // The file '...' already exists.
425 Assert
.AreEqual (typeof (IOException
), ex
.GetType (), "#2");
426 Assert
.IsNull (ex
.InnerException
, "#3");
427 Assert
.IsNotNull (ex
.Message
, "#4");
428 Assert
.IsTrue (ex
.Message
.IndexOf (path2
) != -1, "#5");
436 [Test
] // CopyTo (String, Boolean)
437 public void CopyTo2_DestFileName_Null ()
439 string path
= TempFolder
+ DSC
+ "FIT.CopyToArgumentNullException.Test";
442 File
.Create (path
).Close ();
443 FileInfo info
= new FileInfo (path
);
445 info
.CopyTo (null, false);
447 } catch (ArgumentNullException ex
) {
448 Assert
.AreEqual (typeof (ArgumentNullException
), ex
.GetType (), "#2");
449 Assert
.IsNull (ex
.InnerException
, "#3");
450 Assert
.IsNotNull (ex
.Message
, "#4");
451 Assert
.AreEqual ("destFileName", ex
.ParamName
, "#5");
458 [Test
] // CopyTo (String, Boolean)
459 public void CopyTo2_DestFileName_Empty ()
461 string path
= TempFolder
+ DSC
+ "FIT.CopyToArgument1Exception.Test";
465 File
.Create (path
).Close ();
466 FileInfo info
= new FileInfo (path
);
468 info
.CopyTo (string.Empty
, false);
470 } catch (ArgumentException ex
) {
471 // Empty file name is not legal
472 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#2");
473 Assert
.IsNull (ex
.InnerException
, "#3");
474 Assert
.IsNotNull (ex
.Message
, "#4");
475 Assert
.AreEqual ("destFileName", ex
.ParamName
, "#5");
482 [Test
] // CopyTo (String, Boolean)
483 public void CopyTo2_DestFileName_Whitespace ()
485 string path
= TempFolder
+ DSC
+ "FIT.CopyToArgument2Exception.Test";
489 File
.Create (path
).Close ();
490 FileInfo info
= new FileInfo (path
);
492 info
.CopyTo (" ", false);
494 } catch (ArgumentException ex
) {
495 // The path is not of a legal form
496 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#2");
497 Assert
.IsNull (ex
.InnerException
, "#3");
498 Assert
.IsNotNull (ex
.Message
, "#4");
499 Assert
.IsNull (ex
.ParamName
, "#5");
506 [Test
] // CopyTo (String, Boolean)
507 public void CopyTo2_DestFileName_InvalidPathChars ()
509 string path
= TempFolder
+ DSC
+ "FIT.CopyToArgument4Exception.Test";
510 string path2
= string.Empty
;
514 File
.Create (path
).Close ();
515 FileInfo info
= new FileInfo (path
);
516 foreach (char c
in Path
.InvalidPathChars
)
519 info
.CopyTo (path2
, false);
521 } catch (ArgumentException ex
) {
522 // Illegal characters in path
523 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#2");
524 Assert
.IsNull (ex
.InnerException
, "#3");
525 Assert
.IsNotNull (ex
.Message
, "#4");
526 Assert
.IsNull (ex
.ParamName
, "#5");
534 public void Create ()
536 string path
= TempFolder
+ DSC
+ "FIT.Create.Test";
540 FileInfo info
= new FileInfo (path
);
541 Assert
.IsFalse (info
.Exists
, "#1");
542 FileStream stream
= info
.Create ();
543 Assert
.IsFalse (info
.Exists
, "#2");
544 info
= new FileInfo (path
);
545 Assert
.IsTrue (info
.Exists
, "#3");
546 Assert
.IsTrue (stream
.CanRead
, "#4");
547 Assert
.IsTrue (stream
.CanWrite
, "#5");
548 Assert
.IsTrue (stream
.CanSeek
, "#6");
556 public void CreateText ()
558 string path
= TempFolder
+ DSC
+ "FIT.CreateText.Test";
562 FileInfo info
= new FileInfo (path
);
563 Assert
.IsFalse (info
.Exists
, "#1");
564 StreamWriter writer
= info
.CreateText ();
565 writer
.WriteLine ("test");
567 info
= new FileInfo (path
);
568 Assert
.IsTrue (info
.Exists
, "#2");
575 public void CreateText_Directory ()
577 FileInfo info
= new FileInfo (TempFolder
);
581 } catch (UnauthorizedAccessException ex
) {
582 Assert
.AreEqual (typeof (UnauthorizedAccessException
), ex
.GetType (), "#2");
583 Assert
.IsNull (ex
.InnerException
, "#3");
584 Assert
.IsNotNull (ex
.Message
, "#4");
589 public void Delete ()
591 string path
= TempFolder
+ DSC
+ "FIT.Delete.Test";
595 FileInfo info
= new FileInfo (path
);
596 Assert
.IsFalse (info
.Exists
, "#1");
598 Assert
.IsFalse (info
.Exists
, "#1a");
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
.IsNotNull (ex
.Message
, "#4");
625 [Category("AndroidSdksNotWorking")]
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 [Category("AndroidSdksNotWorking")]
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 [Category ("NotWasm")]
686 public void MoveTo_DestFileName_AlreadyExists ()
688 string sourceFile
= TempFolder
+ DSC
+ "FIT.MoveTo.Source.Test";
692 // move to same directory
693 File
.Create (sourceFile
).Close ();
694 info
= new FileInfo (sourceFile
);
696 info
.MoveTo (TempFolder
);
698 } catch (IOException ex
) {
699 // Cannot create a file when that file already exists
700 Assert
.AreEqual (typeof (IOException
), ex
.GetType (), "#A2");
701 Assert
.IsNull (ex
.InnerException
, "#A3");
702 Assert
.IsNotNull (ex
.Message
, "#A4");
703 Assert
.IsFalse (ex
.Message
.IndexOf (sourceFile
) != -1, "#A5");
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");
723 DeleteFile (sourceFile
);
724 DeleteFile (destFile
);
727 // move to existing directory
728 File
.Create (sourceFile
).Close ();
729 destFile
= TempFolder
+ Path
.DirectorySeparatorChar
+ "bar";
730 Directory
.CreateDirectory (destFile
);
731 info
= new FileInfo (sourceFile
);
733 info
.MoveTo (destFile
);
735 } catch (IOException ex
) {
736 // Cannot create a file when that file already exists
737 Assert
.AreEqual (typeof (IOException
), ex
.GetType (), "#C2");
738 Assert
.IsNull (ex
.InnerException
, "#C3");
739 Assert
.IsNotNull (ex
.Message
, "#C4");
740 Assert
.IsFalse (ex
.Message
.IndexOf (sourceFile
) != -1, "#C5");
742 DeleteFile (sourceFile
);
743 DeleteDirectory (destFile
);
748 [Category ("NotWasm")]
749 public void MoveTo_DestFileName_DirectoryDoesNotExist ()
751 string sourceFile
= TempFolder
+ Path
.DirectorySeparatorChar
+ "foo";
752 string destFile
= Path
.Combine (Path
.Combine (TempFolder
, "doesnotexist"), "b");
753 DeleteFile (sourceFile
);
755 File
.Create (sourceFile
).Close ();
756 FileInfo info
= new FileInfo (sourceFile
);
758 info
.MoveTo (destFile
);
760 } catch (DirectoryNotFoundException ex
) {
761 // Could not find a part of the path
762 Assert
.AreEqual (typeof (DirectoryNotFoundException
), ex
.GetType (), "#2");
763 Assert
.IsNull (ex
.InnerException
, "#3");
764 Assert
.IsNotNull (ex
.Message
, "#4");
767 DeleteFile (sourceFile
);
772 public void MoveTo_DestFileName_Null ()
774 string path
= TempFolder
+ DSC
+ "FIT.MoveToArgumentNullException.Test";
778 File
.Create (path
).Close ();
779 FileInfo info
= new FileInfo (path
);
783 } catch (ArgumentNullException ex
) {
784 Assert
.AreEqual (typeof (ArgumentNullException
), ex
.GetType (), "#2");
785 Assert
.IsNull (ex
.InnerException
, "#3");
786 Assert
.IsNotNull (ex
.Message
, "#4");
787 Assert
.AreEqual ("destFileName", ex
.ParamName
, "#5");
795 public void MoveTo_DestFileName_Empty ()
797 string path
= TempFolder
+ DSC
+ "FIT.MoveToArgumentException.Test";
801 File
.Create (path
).Close ();
802 FileInfo info
= new FileInfo (path
);
804 info
.MoveTo (string.Empty
);
806 } catch (ArgumentException ex
) {
807 // Empty file name is not legal
808 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#2");
809 Assert
.IsNull (ex
.InnerException
, "#3");
810 Assert
.IsNotNull (ex
.Message
, "#4");
811 Assert
.AreEqual ("destFileName", ex
.ParamName
, "#5");
819 public void MoveTo_DestFileName_Whitespace ()
821 string path
= TempFolder
+ DSC
+ "FIT.MoveToArgumentException.Test";
825 File
.Create (path
).Close ();
826 FileInfo info
= new FileInfo (path
);
830 } catch (ArgumentException ex
) {
831 // The path is not of a legal form
832 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#2");
833 Assert
.IsNull (ex
.InnerException
, "#3");
834 Assert
.IsNotNull (ex
.Message
, "#4");
835 Assert
.IsNull (ex
.ParamName
, "#5");
843 public void MoveTo_FileDoesNotExist ()
845 string path1
= TempFolder
+ DSC
+ "FIT.MoveToFileNotFoundException.Src";
846 string path2
= TempFolder
+ DSC
+ "FIT.MoveToFileNotFoundException.Dst";
851 FileInfo info
= new FileInfo (path1
);
855 } catch (FileNotFoundException ex
) {
856 // Unable to find the specified file
857 Assert
.AreEqual (typeof (FileNotFoundException
), ex
.GetType (), "#2");
858 Assert
.IsNull (ex
.InnerException
, "#3");
859 Assert
.IsNotNull (ex
.Message
, "#4");
868 public void MoveTo_Same ()
870 string path
= TempFolder
+ DSC
+ "FIT.MoveToSame.Test";
874 File
.Create (path
).Close ();
875 FileInfo info
= new FileInfo (path
);
877 Assert
.IsTrue (info
.Exists
, "#1");
878 Assert
.IsTrue (File
.Exists (path
), "#2");
884 [Test
] //Covers #38796
885 [Category("AndroidSdksNotWorking")]
886 public void ToStringAfterMoveTo ()
888 string name1
= "FIT.ToStringAfterMoveTo.Test";
889 string name2
= "FIT.ToStringAfterMoveTo.Test.Alt";
890 string path1
= TempFolder
+ DSC
+ name1
;
891 string path2
= TempFolder
+ DSC
+ name2
;
896 File
.Create (path1
).Close ();
897 FileInfo info
= new FileInfo (path1
);
898 Assert
.AreEqual (path1
, info
.ToString (), "#A");
901 Assert
.AreEqual (path2
, info
.ToString (), "#B");
910 public void Replace1 ()
912 string path1
= TempFolder
+ DSC
+ "FIT.Replace.Source.Test";
913 string path2
= TempFolder
+ DSC
+ "FIT.Replace.Dest.Test";
914 string path3
= TempFolder
+ DSC
+ "FIT.Replace.Back.Test";
920 File
.Create (path1
).Close ();
921 File
.Create (path2
).Close ();
922 File
.Create (path3
).Close ();
923 FileInfo info
= new FileInfo (path1
);
924 Assert
.IsTrue (info
.Exists
, "#1");
925 FileInfo info2
= info
.Replace (path2
, path3
);
926 Assert
.IsTrue (info2
.Exists
, "#2");
927 FileInfo info3
= new FileInfo (path3
);
928 Assert
.IsTrue (info3
.Exists
, "#3");
936 public void Replace1_Backup_Null ()
938 string path1
= TempFolder
+ DSC
+ "FIT.Replace.Source.Test";
939 string path2
= TempFolder
+ DSC
+ "FIT.Replace.Dest.Test";
944 File
.Create (path1
).Close ();
945 File
.Create (path2
).Close ();
946 FileInfo info
= new FileInfo (path1
);
947 Assert
.IsTrue (info
.Exists
, "#1");
948 FileInfo info2
= info
.Replace (path2
, null);
949 Assert
.IsTrue (info2
.Exists
, "#2");
950 info
= new FileInfo (path1
);
951 Assert
.IsFalse (info
.Exists
, "#3");
958 public void Replace1_DestFileName_Null ()
960 string path1
= TempFolder
+ DSC
+ "FIT.Replace.Source.Test";
964 File
.Create (path1
).Close ();
965 FileInfo info
= new FileInfo (path1
);
966 info
.Replace (null, null);
968 } catch (ArgumentNullException ex
) {
969 Assert
.AreEqual (typeof (ArgumentNullException
), ex
.GetType (), "#2");
970 Assert
.IsNull (ex
.InnerException
, "#3");
971 Assert
.IsNotNull (ex
.Message
, "#4");
979 public void Replace1_DestFileName_Empty ()
981 string path1
= TempFolder
+ DSC
+ "FIT.Replace.Source.Test";
985 File
.Create (path1
).Close ();
986 FileInfo info
= new FileInfo (path1
);
987 info
.Replace (string.Empty
, null);
989 } catch (ArgumentException ex
) {
990 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#2");
991 Assert
.IsNull (ex
.InnerException
, "#3");
992 Assert
.IsNotNull (ex
.Message
, "#4");
1000 public void Replace1_DestFileName_WhiteSpace ()
1002 string path1
= TempFolder
+ DSC
+ "FIT.Replace.Source.Test";
1006 File
.Create (path1
).Close ();
1007 FileInfo info
= new FileInfo (path1
);
1008 info
.Replace (" ", null);
1010 } catch (ArgumentException ex
) {
1011 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#2");
1012 Assert
.IsNull (ex
.InnerException
, "#3");
1013 Assert
.IsNotNull (ex
.Message
, "#4");
1021 public void Replace1_DestFileName_InvalidPathChars ()
1023 string path1
= TempFolder
+ DSC
+ "FIT.Replace.Source.Test";
1024 string path2
= string.Empty
;
1028 File
.Create (path1
).Close ();
1029 FileInfo info
= new FileInfo (path1
);
1030 foreach (char c
in Path
.InvalidPathChars
)
1033 info
.Replace (path2
, null);
1035 } catch (ArgumentException ex
) {
1036 // Illegal characters in path
1037 Assert
.AreEqual (typeof (ArgumentException
), ex
.GetType (), "#2");
1038 Assert
.IsNull (ex
.InnerException
, "#3");
1039 Assert
.IsNotNull (ex
.Message
, "#4");
1040 Assert
.IsNull (ex
.ParamName
, "#5");
1048 public void Replace1_DestFileName_FileNotFound ()
1050 string path1
= TempFolder
+ DSC
+ "FIT.Replace.Source.Test";
1051 string path2
= TempFolder
+ DSC
+ "FIT.Replace.Dest.Test";
1057 File
.Create (path1
).Close ();
1058 FileInfo info
= new FileInfo (path1
);
1059 info
.Replace (path2
, null);
1061 } catch (FileNotFoundException ex
) {
1062 Assert
.AreEqual (typeof (FileNotFoundException
), ex
.GetType (), "#2");
1063 Assert
.IsNull (ex
.InnerException
, "#3");
1064 Assert
.IsNotNull (ex
.Message
, "#4");
1072 public void Replace1_Source_FileNotFound ()
1074 string path1
= TempFolder
+ DSC
+ "FIT.Replace.Source.Test";
1075 string path2
= TempFolder
+ DSC
+ "FIT.Replace.Dest.Test";
1080 File
.Create (path2
).Close ();
1081 FileInfo info
= new FileInfo (path1
);
1082 info
.Replace (path2
, null);
1084 } catch (FileNotFoundException ex
) {
1085 Assert
.AreEqual (typeof (FileNotFoundException
), ex
.GetType (), "#2");
1086 Assert
.IsNull (ex
.InnerException
, "#3");
1087 Assert
.IsNotNull (ex
.Message
, "#4");
1098 string path
= TempFolder
+ DSC
+ "FIT.Open.Test";
1100 FileStream stream
= null;
1102 FileInfo info
= new FileInfo (path
);
1103 stream
= info
.Open (FileMode
.CreateNew
);
1104 Assert
.IsTrue (stream
.CanRead
, "#A1");
1105 Assert
.IsTrue (stream
.CanSeek
, "#A2");
1106 Assert
.IsTrue (stream
.CanWrite
, "#A3");
1109 stream
= info
.Open (FileMode
.Open
);
1110 Assert
.IsTrue (stream
.CanRead
, "#B1");
1111 Assert
.IsTrue (stream
.CanSeek
, "#B2");
1112 Assert
.IsTrue (stream
.CanWrite
, "#B3");
1115 stream
= info
.Open (FileMode
.Append
, FileAccess
.Write
);
1116 Assert
.IsFalse (stream
.CanRead
, "#C1");
1117 Assert
.IsTrue (stream
.CanSeek
, "#C2");
1118 Assert
.IsTrue (stream
.CanWrite
, "#C3");
1121 stream
= info
.Open (FileMode
.Open
, FileAccess
.ReadWrite
, FileShare
.ReadWrite
);
1122 Assert
.IsTrue (stream
.CanRead
, "#D1");
1123 Assert
.IsTrue (stream
.CanSeek
, "#D2");
1124 Assert
.IsTrue (stream
.CanWrite
, "#D3");
1134 public void Open_FileDoesNotExist ()
1136 string path
= TempFolder
+ DSC
+ "FIT.OpenFileNotFoundException.Test";
1139 FileInfo info
= new FileInfo (path
);
1141 info
.Open (FileMode
.Open
);
1143 } catch (FileNotFoundException ex
) {
1144 Assert
.AreEqual (typeof (FileNotFoundException
), ex
.GetType (), "#2");
1145 Assert
.AreEqual (path
, ex
.FileName
, "#3");
1146 Assert
.IsNull (ex
.InnerException
, "#4");
1147 Assert
.IsNotNull (ex
.Message
, "#5");
1152 public void OpenRead ()
1154 string path
= TempFolder
+ DSC
+ "FIT.OpenRead.Test";
1156 FileStream stream
= null;
1158 File
.Create (path
).Close ();
1159 FileInfo info
= new FileInfo (path
);
1160 stream
= info
.OpenRead ();
1161 Assert
.IsTrue (stream
.CanRead
, "#1");
1162 Assert
.IsTrue (stream
.CanSeek
, "#2");
1163 Assert
.IsFalse (stream
.CanWrite
, "#3");
1174 public void OpenRead_FileLock ()
1176 string path
= TempFolder
+ DSC
+ "FIT.OpenReadIOException.Test";
1178 FileStream stream
= null;
1181 stream
= File
.Create (path
);
1182 FileInfo info
= new FileInfo (path
);
1186 } catch (IOException ex
) {
1187 // The process cannot access the file because
1188 // it is being used by another process
1189 Assert
.AreEqual (typeof (IOException
), ex
.GetType (), "#2");
1190 Assert
.IsNull (ex
.InnerException
, "#3");
1191 Assert
.IsNotNull (ex
.Message
, "#4");
1201 public void OpenRead_Directory ()
1203 FileInfo info
= new FileInfo (TempFolder
);
1207 } catch (UnauthorizedAccessException ex
) {
1208 Assert
.AreEqual (typeof (UnauthorizedAccessException
), ex
.GetType (), "#2");
1209 Assert
.IsNull (ex
.InnerException
, "#3");
1210 Assert
.IsNotNull (ex
.Message
, "#4");
1215 public void OpenText ()
1217 string path
= TempFolder
+ DSC
+ "FIT.OpenText.Test";
1219 StreamReader reader
= null;
1221 File
.Create (path
).Close ();
1222 FileInfo info
= new FileInfo (path
);
1223 reader
= info
.OpenText ();
1224 Assert
.AreEqual (-1, reader
.Peek ());
1233 public void OpenText_FileDoesNotExist ()
1235 string path
= TempFolder
+ DSC
+ "FIT.OpenTextFileNotFoundException.Test";
1237 FileInfo info
= new FileInfo (path
);
1241 } catch (FileNotFoundException ex
) {
1242 Assert
.AreEqual (typeof (FileNotFoundException
), ex
.GetType (), "#2");
1243 Assert
.AreEqual (path
, ex
.FileName
, "#3");
1244 Assert
.IsNull (ex
.InnerException
, "#4");
1245 Assert
.IsNotNull (ex
.Message
, "#5");
1250 public void OpenText_Directory ()
1252 FileInfo info
= new FileInfo (TempFolder
);
1256 } catch (UnauthorizedAccessException ex
) {
1257 Assert
.AreEqual (typeof (UnauthorizedAccessException
), ex
.GetType (), "#2");
1258 Assert
.IsNull (ex
.InnerException
, "#3");
1259 Assert
.IsNotNull (ex
.Message
, "#4");
1264 public void OpenWrite ()
1266 string path
= TempFolder
+ DSC
+ "FIT.OpenWrite.Test";
1268 FileStream stream
= null;
1270 File
.Create (path
).Close ();
1271 FileInfo info
= new FileInfo (path
);
1272 stream
= info
.OpenWrite ();
1273 Assert
.IsFalse (stream
.CanRead
, "#1");
1274 Assert
.IsTrue (stream
.CanSeek
, "#2");
1275 Assert
.IsTrue (stream
.CanWrite
, "#3");
1284 public void OpenWrite_Directory ()
1286 FileInfo info
= new FileInfo (TempFolder
);
1290 } catch (UnauthorizedAccessException ex
) {
1291 Assert
.AreEqual (typeof (UnauthorizedAccessException
), ex
.GetType (), "#2");
1292 Assert
.IsNull (ex
.InnerException
, "#3");
1293 Assert
.IsNotNull (ex
.Message
, "#4");
1298 public void Serialization ()
1301 SerializationInfo si
;
1303 info
= new FileInfo ("Test");
1304 si
= new SerializationInfo (typeof (FileInfo
), new FormatterConverter ());
1305 info
.GetObjectData (si
, new StreamingContext ());
1307 Assert
.AreEqual (3, si
.MemberCount
, "#A1");
1308 Assert
.AreEqual ("Test", si
.GetString ("OriginalPath"), "#A2");
1309 Assert
.AreEqual (Path
.Combine (Directory
.GetCurrentDirectory (), "Test"), si
.GetString ("FullPath"), "#A3");
1311 info
= new FileInfo (TempFolder
);
1312 si
= new SerializationInfo (typeof (FileInfo
), new FormatterConverter ());
1313 info
.GetObjectData (si
, new StreamingContext ());
1315 Assert
.AreEqual (3, si
.MemberCount
, "#B1");
1316 Assert
.AreEqual (TempFolder
, si
.GetString ("OriginalPath"), "#B2");
1317 Assert
.AreEqual (TempFolder
, si
.GetString ("FullPath"), "#B3");
1321 public void Deserialization ()
1323 FileInfo info
= new FileInfo ("Test");
1325 MemoryStream ms
= new MemoryStream ();
1326 BinaryFormatter bf
= new BinaryFormatter ();
1327 bf
.Serialize (ms
, info
);
1330 FileInfo clone
= (FileInfo
) bf
.Deserialize (ms
);
1331 Assert
.AreEqual (info
.Name
, clone
.Name
, "#1");
1332 Assert
.AreEqual (info
.FullName
, clone
.FullName
, "#2");
1337 [Category ("MobileNotWorking")]
1338 public void ToStringVariety ()
1340 Assert
.AreEqual ("foo", new FileInfo ("foo").ToString ());
1341 Assert
.AreEqual ("c:/foo", new FileInfo ("c:/foo").ToString ());
1342 Assert
.AreEqual ("/usr/local/foo", new FileInfo ("/usr/local/foo").ToString ());
1343 Assert
.AreEqual ("c:\\foo", new FileInfo ("c:\\foo").ToString ());
1344 Assert
.AreEqual ("/usr/local\\foo", new FileInfo ("/usr/local\\foo").ToString ());
1345 Assert
.AreEqual ("foo/BAR/baz", new FileInfo ("foo/BAR/baz").ToString ());
1346 Assert
.AreEqual ("c:/documents and settings", new FileInfo ("c:/documents and settings").ToString ());
1347 Assert
.AreEqual ("c:/docUme~1", new FileInfo ("c:/docUme~1").ToString ());
1350 void DeleteFile (string path
)
1352 if (File
.Exists (path
))
1356 void DeleteDirectory (string path
)
1358 if (Directory
.Exists (path
))
1359 Directory
.Delete (path
, true);