Merge pull request #2957 from akoeplinger/fix-corlib-mobile-tests
[mono.git] / mcs / class / corlib / Test / System.IO / FileInfoTest.cs
1 // FileInfoTest.cs - NUnit Test Cases for System.IO.FileInfo class
2 //
3 // Ville Palo (vi64pa@koti.soon.fi)
4 // 
5 // (C) 2003 Ville Palo
6 // 
7
8 using System;
9 using System.IO;
10 using System.Runtime.Serialization;
11 using System.Runtime.Serialization.Formatters.Binary;
12
13 using NUnit.Framework;
14
15 namespace MonoTests.System.IO
16 {
17         [TestFixture]
18         public class FileInfoTest
19         {
20                 string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
21                 static readonly char DSC = Path.DirectorySeparatorChar;
22
23                 [SetUp]
24                 public void SetUp ()
25                 {
26                         DeleteDirectory (TempFolder);
27                         Directory.CreateDirectory (TempFolder);
28                 }
29
30                 [TearDown]
31                 public void TearDown ()
32                 {
33                         DeleteDirectory (TempFolder);
34                 }
35
36                 [Test] // ctor (String)
37                 public void Constructor1 ()
38                 {
39                         string path = TempFolder + DSC + "FIT.Ctr.Test";
40                         DeleteFile (path);
41
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");
47                 }
48
49                 [Test] // ctor (String)
50                 public void Constructor1_FileName_Null ()
51                 {
52                         try {
53                                 new FileInfo (null);
54                                 Assert.Fail ("#1");
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");
60                         }
61                 }
62
63                 [Test] // ctor (String)
64                 public void Constructor1_FileName_Empty ()
65                 {
66                         try {
67                                 new FileInfo (string.Empty);
68                                 Assert.Fail ("#1");
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");
75                         }
76                 }
77
78                 [Test] // ctor (String)
79                 public void Constructor1_FileName_InvalidPathChars ()
80                 {
81                         string path = string.Empty;
82                         foreach (char c in Path.InvalidPathChars)
83                                 path += c;
84                         try {
85                                 new FileInfo (path);
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");
92                         }
93                 }
94
95                 [Test] // ctor (String)
96                 public void Constructor1_FileName_Whitespace ()
97                 {
98                         try {
99                                 new FileInfo ("      ");
100                                 Assert.Fail ("#1");
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");
107                         }
108                 }
109
110                 [Test]
111                 public void DirectoryTest ()
112                 {
113                         string path = TempFolder + DSC + "FIT.Directory.Test";
114                         DeleteFile (path);
115                         
116                         FileInfo info = new FileInfo (path);
117                         DirectoryInfo dir = info.Directory;
118                         Assert.AreEqual ("MonoTests.System.IO.Tests", dir.Name);
119                 }
120                 
121                 [Test]
122                 public void Exists ()
123                 {
124                         string path = TempFolder + DSC + "FIT.Exists.Test";
125                         DeleteFile (path);
126                         
127                         try {
128                                 FileInfo info = new FileInfo (path);
129                                 Assert.IsFalse (info.Exists, "#1");
130                         
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");
137                         } finally {
138                                 DeleteFile (path);
139                         }
140                 }
141
142 #if !MOBILE
143                 [Test]
144                 public void IsReadOnly ()
145                 {
146                         string path = TempFolder + DSC + "FIT.IsReadOnly.Test";
147                         DeleteFile (path);
148                         
149                         try {
150                                 using (FileStream stream = File.Create (path)) {
151                                         stream.WriteByte (12);
152                                         stream.Close ();
153                                 }
154
155                                 FileInfo info1 = new FileInfo (path);
156                                 Assert.IsFalse (info1.IsReadOnly, "#1");
157
158                                 FileInfo info2 = new FileInfo (path);
159                                 info2.IsReadOnly = true;
160                                 Assert.IsFalse (info1.IsReadOnly, "#2");
161                                 Assert.IsTrue (info2.IsReadOnly, "#3");
162
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");
169                         } finally {
170                                 File.SetAttributes (path, FileAttributes.Normal);
171                                 DeleteFile (path);
172                         }
173                 }
174 #endif
175
176                 [Test]
177                 public void Length ()
178                 {
179                         string path = TempFolder + DSC + "FIT.Length.Test";
180                         DeleteFile (path);
181                         
182                         try {
183                                 FileStream stream = File.Create (path);
184                                 FileInfo info = new FileInfo (path);
185                                 Assert.AreEqual (0, info.Length, "#1");
186                                 stream.WriteByte (12);
187                                 stream.Flush ();
188                                 Assert.AreEqual (0, info.Length, "#2");
189                                 info = new FileInfo (path);
190                                 Assert.AreEqual (1, info.Length, "#3");
191                                 stream.Close ();
192                         } finally {
193                                 DeleteFile (path);
194                         }
195                 }
196                 
197                 [Test]
198                 public void Length_FileDoesNotExist ()
199                 {
200                         string path = TempFolder + DSC + "FIT.LengthException.Test";
201                         DeleteFile (path);
202                         FileInfo info = new FileInfo (path);
203                         try {
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");
211                         }
212                 }
213                 
214                 [Test]
215                 public void AppendText ()
216                 {
217                         string path = TempFolder + DSC + "FIT.AppendText.Test";
218                         DeleteFile (path);
219                         
220                         try {
221                                 FileInfo info = new FileInfo (path);
222                                 Assert.IsFalse (info.Exists, "#1");
223                         
224                                 StreamWriter writer = info.AppendText ();
225                                 info = new FileInfo (path);
226                                 Assert.IsTrue (info.Exists, "#2");
227                                 
228                                 writer.Write ("aaa");
229                                 writer.Flush ();
230                                 writer.Close ();
231                         
232                                 Assert.AreEqual (0, info.Length, "#3");
233                                 info = new FileInfo (path);
234                                 Assert.AreEqual (3, info.Length, "#4");
235                         } finally {
236                                 DeleteFile (path);
237                         }
238                 }
239
240                 [Test] // CopyTo (String)
241                 public void CopyTo1 ()
242                 {
243                         string path1 = TempFolder + DSC + "FIT.CopyTo.Source.Test";
244                         string path2 = TempFolder + DSC + "FIT.CopyTo.Dest.Test";
245                         DeleteFile (path1);
246                         DeleteFile (path2);
247                         try {
248                                 File.Create (path1).Close ();
249                         
250                                 FileInfo info = new FileInfo (path1);
251                                 Assert.IsTrue (info.Exists, "#1");
252
253                                 FileInfo info2 = info.CopyTo (path2);
254                                 info = new FileInfo (path1);
255                                 Assert.IsTrue (info2.Exists, "#2");
256                         } finally {
257                                 DeleteFile (path1);
258                                 DeleteFile (path2);
259                         }
260                 }
261
262                 [Test] // CopyTo (String)
263                 public void CopyTo1_DestFileName_AlreadyExists ()
264                 {
265                         string path1 = TempFolder + DSC + "FIT.CopyToException.Source.Test";
266                         string path2 = TempFolder + DSC + "FIT.CopyToException.Dest.Test";
267
268                         try {
269                                 DeleteFile (path1);
270                                 DeleteFile (path2);
271                                 File.Create (path1).Close ();
272                                 File.Create (path2).Close ();
273                                 FileInfo info = new FileInfo (path1);
274                                 try {
275                                         info.CopyTo (path2);
276                                         Assert.Fail ("#1");
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");
283                                 }
284                         } finally {
285                                 DeleteFile (path1);
286                                 DeleteFile (path2);
287                         }
288                 }
289
290                 [Test] // CopyTo (String)
291                 public void CopyTo1_DestFileName_Null ()
292                 {
293                         string path = TempFolder + DSC + "FIT.CopyToArgumentNullException.Test";
294                         DeleteFile (path);
295                         try {
296                                 File.Create (path).Close ();
297                                 FileInfo info = new FileInfo (path);
298                                 try {
299                                         info.CopyTo (null);
300                                         Assert.Fail ("#1");
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");
306                                 }
307                         } finally {
308                                 DeleteFile (path);
309                         }
310                 }
311
312                 [Test] // CopyTo (String)
313                 public void CopyTo1_DestFileName_Empty ()
314                 {
315                         string path = TempFolder + DSC + "FIT.CopyToArgument1Exception.Test";
316                         DeleteFile (path);
317
318                         try {
319                                 File.Create (path).Close ();
320                                 FileInfo info = new FileInfo (path);
321                                 try {
322                                         info.CopyTo (string.Empty);
323                                         Assert.Fail ("#1");
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");
330                                 }
331                         } finally {
332                                 DeleteFile (path);
333                         }
334                 }
335
336                 [Test] // CopyTo (String)
337                 public void CopyTo1_DestFileName_Whitespace ()
338                 {
339                         string path = TempFolder + DSC + "FIT.CopyToArgument2Exception.Test";
340                         DeleteFile (path);
341
342                         try {
343                                 File.Create (path).Close ();
344                                 FileInfo info = new FileInfo (path);
345                                 try {
346                                         info.CopyTo ("    ");
347                                         Assert.Fail ("#1");
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");
354                                 }
355                         } finally {
356                                 DeleteFile (path);
357                         }
358                 }
359
360                 [Test] // CopyTo (String)
361                 public void CopyTo1_DestFileName_InvalidPathChars ()
362                 {
363                         string path = TempFolder + DSC + "FIT.CopyToArgument4Exception.Test";
364                         string path2 = string.Empty;
365                         DeleteFile (path);
366
367                         try {
368                                 File.Create (path).Close ();
369                                 FileInfo info = new FileInfo (path);
370                                 foreach (char c in Path.InvalidPathChars)
371                                         path2 += c;
372                                 try {
373                                         info.CopyTo (path2);
374                                         Assert.Fail ("#1");
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");
381                                 }
382                         } finally {
383                                 DeleteFile (path);
384                         }
385                 }
386
387                 [Test] // CopyTo (String, Boolean)
388                 public void CopyTo2 ()
389                 {
390                         string path1 = TempFolder + DSC + "FIT.CopyTo2.Source.Test";
391                         string path2 = TempFolder + DSC + "FIT.CopyTo2.Dest.Test";
392                         DeleteFile (path1);
393                         DeleteFile (path2);
394                         try {
395                                 File.Create (path1).Close ();
396                                 File.Create (path2).Close ();
397                                 FileInfo info = new FileInfo (path1);
398
399                                 FileInfo info2 = info.CopyTo (path2, true);
400                                 info = new FileInfo (path1);
401                                 Assert.IsTrue (info.Exists, "#1");
402                                 Assert.IsTrue (info2.Exists, "#2");
403                         } finally {
404                                 DeleteFile (path1);
405                                 DeleteFile (path2);
406                         }
407                 }
408
409                 [Test] // CopyTo (String, Boolean)
410                 public void CopyTo2_DestFileName_AlreadyExists ()
411                 {
412                         string path1 = TempFolder + DSC + "FIT.CopyToException.Source.Test";
413                         string path2 = TempFolder + DSC + "FIT.CopyToException.Dest.Test";
414
415                         try {
416                                 DeleteFile (path1);
417                                 DeleteFile (path2);
418                                 File.Create (path1).Close ();
419                                 File.Create (path2).Close ();
420                                 FileInfo info = new FileInfo (path1);
421                                 try {
422                                         info.CopyTo (path2, false);
423                                         Assert.Fail ("#1");
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");
430                                 }
431                         } finally {
432                                 DeleteFile (path1);
433                                 DeleteFile (path2);
434                         }
435                 }
436
437                 [Test] // CopyTo (String, Boolean)
438                 public void CopyTo2_DestFileName_Null ()
439                 {
440                         string path = TempFolder + DSC + "FIT.CopyToArgumentNullException.Test";
441                         DeleteFile (path);
442                         try {
443                                 File.Create (path).Close ();
444                                 FileInfo info = new FileInfo (path);
445                                 try {
446                                         info.CopyTo (null, false);
447                                         Assert.Fail ("#1");
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");
453                                 }
454                         } finally {
455                                 DeleteFile (path);
456                         }
457                 }
458
459                 [Test] // CopyTo (String, Boolean)
460                 public void CopyTo2_DestFileName_Empty ()
461                 {
462                         string path = TempFolder + DSC + "FIT.CopyToArgument1Exception.Test";
463                         DeleteFile (path);
464                         
465                         try {
466                                 File.Create (path).Close ();
467                                 FileInfo info = new FileInfo (path);
468                                 try {
469                                         info.CopyTo (string.Empty, false);
470                                         Assert.Fail ("#1");
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");
477                                 }
478                         } finally {
479                                 DeleteFile (path);
480                         }
481                 }
482
483                 [Test] // CopyTo (String, Boolean)
484                 public void CopyTo2_DestFileName_Whitespace ()
485                 {
486                         string path = TempFolder + DSC + "FIT.CopyToArgument2Exception.Test";
487                         DeleteFile (path);
488                         
489                         try {
490                                 File.Create (path).Close ();
491                                 FileInfo info = new FileInfo (path);
492                                 try {
493                                         info.CopyTo ("    ", false);
494                                         Assert.Fail ("#1");
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");
501                                 }
502                         } finally {
503                                 DeleteFile (path);
504                         }
505                 }
506
507                 [Test] // CopyTo (String, Boolean)
508                 public void CopyTo2_DestFileName_InvalidPathChars ()
509                 {
510                         string path = TempFolder + DSC + "FIT.CopyToArgument4Exception.Test";
511                         string path2 = string.Empty;
512                         DeleteFile (path);
513                         
514                         try {
515                                 File.Create (path).Close ();
516                                 FileInfo info = new FileInfo (path);
517                                 foreach (char c in Path.InvalidPathChars)
518                                         path2 += c;
519                                 try {
520                                         info.CopyTo (path2, false);
521                                         Assert.Fail ("#1");
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");
528                                 }
529                         } finally {
530                                 DeleteFile (path);
531                         }
532                 }
533                 
534                 [Test]
535                 public void Create ()
536                 {
537                         string path = TempFolder + DSC + "FIT.Create.Test";
538                         DeleteFile (path);
539                         
540                         try {
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");
550                                 stream.Close ();
551                         } finally {
552                                 DeleteFile (path);
553                         }
554                 }
555                 
556                 [Test]
557                 public void CreateText ()
558                 {
559                         string path = TempFolder + DSC + "FIT.CreateText.Test";
560                         DeleteFile (path);
561                         
562                         try {
563                                 FileInfo info = new FileInfo (path);
564                                 Assert.IsFalse (info.Exists, "#1");
565                                 StreamWriter writer = info.CreateText ();
566                                 writer.WriteLine ("test");
567                                 writer.Close ();
568                                 info = new FileInfo (path);
569                                 Assert.IsTrue (info.Exists, "#2");
570                         } finally {
571                                 DeleteFile (path);
572                         }
573                 }
574                 
575                 [Test]
576                 public void CreateText_Directory ()
577                 {
578                         FileInfo info = new FileInfo (TempFolder);
579                         try {
580                                 info.CreateText ();
581                                 Assert.Fail ("#1");
582                         } catch (UnauthorizedAccessException ex) {
583                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
584                                 Assert.IsNull (ex.InnerException, "#3");
585                                 Assert.IsNotNull (ex.Message, "#4");
586                         }
587                 }
588                 
589                 [Test]
590                 public void Delete ()
591                 {
592                         string path = TempFolder + DSC + "FIT.Delete.Test";
593                         DeleteFile (path);
594                         
595                         try {
596                                 FileInfo info = new FileInfo (path);
597                                 Assert.IsFalse (info.Exists, "#1");
598                                 info.Delete ();
599                                 Assert.IsFalse (info.Exists, "#1a");
600                                 info.Create ().Close ();
601                                 info = new FileInfo (path);
602                                 Assert.IsTrue (info.Exists, "#2");
603                                 info.Delete ();
604                                 Assert.IsTrue (info.Exists, "#3");
605                                 info = new FileInfo (path);
606                                 Assert.IsFalse (info.Exists, "#4");
607                         } finally {
608                                 DeleteFile (path);
609                         }
610                 }
611                 
612                 [Test]
613                 public void Delete_Directory ()
614                 {
615                         FileInfo info = new FileInfo (TempFolder);
616                         try {
617                                 info.Delete ();
618                                 Assert.Fail ("#1");
619                         } catch (UnauthorizedAccessException ex) {
620                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
621                                 Assert.IsNull (ex.InnerException, "#3");
622                                 Assert.IsNotNull (ex.Message, "#4");
623                         }
624                 }
625                 
626                 [Test]
627                 public void MoveTo ()
628                 {
629                         string path1 = TempFolder + DSC + "FIT.MoveTo.Source.Test";
630                         string path2 = TempFolder + DSC + "FIT.MoveTo.Dest.Test";
631                         DeleteFile (path1);
632                         DeleteFile (path2);
633                         
634                         try {
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");
642
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");
649                         } finally {
650                                 DeleteFile (path1);
651                                 DeleteFile (path2);
652                         }
653                 }
654
655                 [Test] //Covers #18361
656                 public void MoveTo_SameName ()
657                 {
658                         string name = "FIT.MoveTo.SameName.Test";
659                         string path1 = TempFolder + DSC + name;
660                         string path2 = TempFolder + DSC + "same";
661                         Directory.CreateDirectory (path2);
662                         path2 += DSC + name;
663                         DeleteFile (path1);
664                         DeleteFile (path2);
665                         
666                         try {
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");
672
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");
678                         } finally {
679                                 DeleteFile (path1);
680                                 DeleteFile (path2);
681                         }
682                 }
683
684                 [Test]
685                 public void MoveTo_DestFileName_AlreadyExists ()
686                 {
687                         string sourceFile = TempFolder + DSC + "FIT.MoveTo.Source.Test";
688                         string destFile;
689                         FileInfo info;
690
691                         // move to same directory
692                         File.Create (sourceFile).Close ();
693                         info = new FileInfo (sourceFile);
694                         try {
695                                 info.MoveTo (TempFolder);
696                                 Assert.Fail ("#A1");
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");
704                         } finally {
705                                 DeleteFile (sourceFile);
706                         }
707
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);
713                         try {
714                                 info.MoveTo (destFile);
715                                 Assert.Fail ("#B1");
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");
723                         } finally {
724                                 DeleteFile (sourceFile);
725                                 DeleteFile (destFile);
726                         }
727
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);
733                         try {
734                                 info.MoveTo (destFile);
735                                 Assert.Fail ("#C1");
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");
743                         } finally {
744                                 DeleteFile (sourceFile);
745                                 DeleteDirectory (destFile);
746                         }
747                 }
748
749                 [Test]
750                 public void MoveTo_DestFileName_DirectoryDoesNotExist ()
751                 {
752                         string sourceFile = TempFolder + Path.DirectorySeparatorChar + "foo";
753                         string destFile = Path.Combine (Path.Combine (TempFolder, "doesnotexist"), "b");
754                         DeleteFile (sourceFile);
755                         try {
756                                 File.Create (sourceFile).Close ();
757                                 FileInfo info = new FileInfo (sourceFile);
758                                 try {
759                                         info.MoveTo (destFile);
760                                         Assert.Fail ("#1");
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");
766                                 }
767                         } finally {
768                                 DeleteFile (sourceFile);
769                         }
770                 }
771
772                 [Test]
773                 public void MoveTo_DestFileName_Null ()
774                 {
775                         string path = TempFolder + DSC + "FIT.MoveToArgumentNullException.Test";
776                         DeleteFile (path);
777
778                         try {
779                                 File.Create (path).Close ();
780                                 FileInfo info = new FileInfo (path);
781                                 try {
782                                         info.MoveTo (null);
783                                         Assert.Fail ("#1");
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");
789                                 }
790                         } finally {
791                                 DeleteFile (path);
792                         }
793                 }
794
795                 [Test]
796                 public void MoveTo_DestFileName_Empty ()
797                 {
798                         string path = TempFolder + DSC + "FIT.MoveToArgumentException.Test";
799                         DeleteFile (path);
800
801                         try {
802                                 File.Create (path).Close ();
803                                 FileInfo info = new FileInfo (path);
804                                 try {
805                                         info.MoveTo (string.Empty);
806                                         Assert.Fail ("#1");
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");
813                                 }
814                         } finally {
815                                 DeleteFile (path);
816                         }
817                 }
818
819                 [Test]
820                 public void MoveTo_DestFileName_Whitespace ()
821                 {
822                         string path = TempFolder + DSC + "FIT.MoveToArgumentException.Test";
823                         DeleteFile (path);
824
825                         try {
826                                 File.Create (path).Close ();
827                                 FileInfo info = new FileInfo (path);
828                                 try {
829                                         info.MoveTo ("   ");
830                                         Assert.Fail ("#1");
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");
837                                 }
838                         } finally {
839                                 DeleteFile (path);
840                         }
841                 }
842
843                 [Test]
844                 public void MoveTo_FileDoesNotExist ()
845                 {
846                         string path1 = TempFolder + DSC + "FIT.MoveToFileNotFoundException.Src";
847                         string path2 = TempFolder + DSC + "FIT.MoveToFileNotFoundException.Dst";
848                         DeleteFile (path1);
849                         DeleteFile (path2);
850                         
851                         try {
852                                 FileInfo info = new FileInfo (path1);
853                                 try {
854                                         info.MoveTo (path2);
855                                         Assert.Fail ("#1");
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");
862                                 }
863                         } finally {
864                                 DeleteFile (path1);
865                                 DeleteFile (path2);
866                         }
867                 }
868
869                 [Test]
870                 public void MoveTo_Same ()
871                 {
872                         string path = TempFolder + DSC + "FIT.MoveToSame.Test";
873                         DeleteFile (path);
874
875                         try {
876                                 File.Create (path).Close ();
877                                 FileInfo info = new FileInfo (path);
878                                 info.MoveTo (path);
879                                 Assert.IsTrue (info.Exists, "#1");
880                                 Assert.IsTrue (File.Exists (path), "#2");
881                         } finally {
882                                 DeleteFile (path);
883                         }
884                 }
885
886                 [Test] //Covers #38796
887                 public void ToStringAfterMoveTo ()
888                 {
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;
893                         DeleteFile (path1);
894                         DeleteFile (path2);
895                         
896                         try {
897                                 File.Create (path1).Close ();
898                                 FileInfo info = new FileInfo (path1);
899                                 Assert.AreEqual (path1, info.ToString (), "#A");
900
901                                 info.MoveTo (path2);
902                                 Assert.AreEqual (path2, info.ToString (), "#B");
903                         } finally {
904                                 DeleteFile (path1);
905                                 DeleteFile (path2);
906                         }
907                 }
908
909 #if !MOBILE
910                 [Test]
911                 public void Replace1 ()
912                 {
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";
916                         
917                         DeleteFile (path1);
918                         DeleteFile (path2);
919                         DeleteFile (path3);
920                         try {
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");                                                     
930                         } finally {
931                                 DeleteFile (path2);
932                                 DeleteFile (path3);
933                         }
934                 }
935
936                 [Test]
937                 public void Replace1_Backup_Null ()
938                 {
939                         string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
940                         string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
941                         
942                         DeleteFile (path1);
943                         DeleteFile (path2);
944                         try {
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");                             
953                         } finally {
954                                 DeleteFile (path2);
955                         } 
956                 }
957
958                 [Test]
959                 public void Replace1_DestFileName_Null ()
960                 {
961                         string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
962                         DeleteFile (path1);
963                         try {
964                                 try {
965                                         File.Create (path1).Close ();
966                                         FileInfo info = new FileInfo (path1);
967                                         info.Replace (null, null);
968                                         Assert.Fail ("#1");                                             
969                                 } catch (ArgumentNullException ex) {
970                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");                          
971                                         Assert.IsNull (ex.InnerException, "#3");
972                                         Assert.IsNotNull (ex.Message, "#4");    
973                                 }
974                         } finally {
975                                 DeleteFile (path1);
976                         }
977                 }
978
979                 [Test]
980                 public void Replace1_DestFileName_Empty ()
981                 {
982                         string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
983                         DeleteFile (path1);
984                         try {
985                                 try {
986                                         File.Create (path1).Close ();
987                                         FileInfo info = new FileInfo (path1);
988                                         info.Replace (string.Empty, null);
989                                         Assert.Fail ("#1");
990                                 } catch (ArgumentException ex) {
991                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
992                                         Assert.IsNull (ex.InnerException, "#3");
993                                         Assert.IsNotNull (ex.Message, "#4");
994                                 }
995                         } finally {
996                                 DeleteFile (path1);
997                         }
998                 }
999                 
1000                 [Test]
1001                 public void Replace1_DestFileName_WhiteSpace ()
1002                 {
1003                         string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
1004                         DeleteFile (path1);
1005                         try {
1006                                 try {
1007                                         File.Create (path1).Close ();
1008                                         FileInfo info = new FileInfo (path1);
1009                                         info.Replace ("     ", null);
1010                                         Assert.Fail ("#1");
1011                                 } catch (ArgumentException ex) {
1012                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1013                                         Assert.IsNull (ex.InnerException, "#3");
1014                                         Assert.IsNotNull (ex.Message, "#4");
1015                                 }
1016                         } finally {
1017                                 DeleteFile (path1);
1018                         }
1019                 }
1020
1021                 [Test] 
1022                 public void Replace1_DestFileName_InvalidPathChars ()
1023                 {
1024                         string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
1025                         string path2 = string.Empty;
1026                         DeleteFile (path1);
1027
1028                         try {
1029                                 File.Create (path1).Close ();
1030                                 FileInfo info = new FileInfo (path1);
1031                                 foreach (char c in Path.InvalidPathChars)
1032                                         path2 += c;
1033                                 try {
1034                                         info.Replace (path2, null);
1035                                         Assert.Fail ("#1");
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");
1042                                 }
1043                         } finally {
1044                                 DeleteFile (path1);
1045                         }
1046                 }
1047
1048                 [Test]
1049                 public void Replace1_DestFileName_FileNotFound ()
1050                 {
1051                         string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
1052                         string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
1053                         DeleteFile (path1);
1054                         DeleteFile (path2);
1055
1056                         try {
1057                                 try {
1058                                         File.Create (path1).Close ();
1059                                         FileInfo info = new FileInfo (path1);
1060                                         info.Replace (path2, null);
1061                                         Assert.Fail ("#1");
1062                                 } catch (FileNotFoundException ex) {
1063                                         Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1064                                         Assert.IsNull (ex.InnerException, "#3");
1065                                         Assert.IsNotNull (ex.Message, "#4");
1066                                 }
1067                         } finally {
1068                                 DeleteFile (path1);                     
1069                         }
1070                 }
1071
1072                 [Test]
1073                 public void Replace1_DestFileName_IsReadOnly ()
1074                 {
1075                         string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
1076                         string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
1077                         DeleteFile (path1);
1078                         DeleteFile (path2);
1079
1080                         try {
1081                                 try {
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);
1087                                         Assert.Fail ("#1");
1088                                 } catch (UnauthorizedAccessException ex) {
1089                                         Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
1090                                         Assert.IsNull (ex.InnerException, "#3");
1091                                         Assert.IsNotNull (ex.Message, "#4");
1092                                 }
1093                         } finally {
1094                                 File.SetAttributes (path2, FileAttributes.Normal);
1095                                 DeleteFile (path1);
1096                                 DeleteFile (path2);                     
1097                         }
1098                 }
1099
1100                 [Test]
1101                 public void Replace1_Source_FileNotFound ()
1102                 {
1103                         string path1 = TempFolder + DSC + "FIT.Replace.Source.Test";
1104                         string path2 = TempFolder + DSC + "FIT.Replace.Dest.Test";
1105                         DeleteFile (path2);
1106
1107                         try {
1108                                 try {
1109                                         File.Create (path2).Close ();
1110                                         FileInfo info = new FileInfo (path1);
1111                                         info.Replace (path2, null);
1112                                         Assert.Fail ("#1");
1113                                 } catch (FileNotFoundException ex) {
1114                                         Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1115                                         Assert.IsNull (ex.InnerException, "#3");
1116                                         Assert.IsNotNull (ex.Message, "#4");
1117                                 }
1118                         } finally {
1119                                 DeleteFile (path2);                     
1120                         }               
1121                 }
1122 #endif
1123
1124                 [Test]
1125                 public void Open ()
1126                 {
1127                         string path = TempFolder + DSC + "FIT.Open.Test";
1128                         DeleteFile (path);
1129                         FileStream stream = null;
1130                         try {
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");
1136                                 stream.Close ();
1137                                 
1138                                 stream = info.Open (FileMode.Open);
1139                                 Assert.IsTrue (stream.CanRead, "#B1");
1140                                 Assert.IsTrue (stream.CanSeek, "#B2");
1141                                 Assert.IsTrue (stream.CanWrite, "#B3");
1142                                 stream.Close ();
1143                                 
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");
1148                                 stream.Close ();
1149
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");
1154                                 stream.Close ();
1155                         } finally {
1156                                 if (stream != null)
1157                                         stream.Close ();
1158                                 DeleteFile (path);
1159                         }
1160                 }
1161                 
1162                 [Test]
1163                 public void Open_FileDoesNotExist ()
1164                 {
1165                         string path = TempFolder + DSC + "FIT.OpenFileNotFoundException.Test";
1166                         DeleteFile (path);
1167                         
1168                         FileInfo info = new FileInfo (path);
1169                         try {
1170                                 info.Open (FileMode.Open);
1171                                 Assert.Fail ("#1");
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");
1177                         }
1178                 }
1179                 
1180                 [Test]
1181                 public void OpenRead ()
1182                 {
1183                         string path = TempFolder + DSC + "FIT.OpenRead.Test";
1184                         DeleteFile (path);
1185                         FileStream stream = null;
1186                         try {
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");
1193                                 stream.Close ();
1194                                 
1195                         } finally {
1196                                 if (stream != null)
1197                                         stream.Close ();
1198                                 DeleteFile (path);
1199                         }
1200                 }
1201                 
1202                 [Test]
1203                 public void OpenRead_FileLock ()
1204                 {
1205                         string path = TempFolder + DSC + "FIT.OpenReadIOException.Test";
1206                         DeleteFile (path);
1207                         FileStream stream = null;
1208                         
1209                         try {
1210                                 stream = File.Create (path);
1211                                 FileInfo info = new FileInfo (path);
1212                                 try {
1213                                         info.OpenRead ();
1214                                         Assert.Fail ("#1");
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");
1221                                 }
1222                         } finally {
1223                                 if (stream != null)
1224                                         stream.Close ();
1225                                 DeleteFile (path);
1226                         }
1227                 }
1228
1229                 [Test]
1230                 public void OpenRead_Directory ()
1231                 {
1232                         FileInfo info = new FileInfo (TempFolder);
1233                         try {
1234                                 info.OpenRead ();
1235                                 Assert.Fail ("#1");
1236                         } catch (UnauthorizedAccessException ex) {
1237                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
1238                                 Assert.IsNull (ex.InnerException, "#3");
1239                                 Assert.IsNotNull (ex.Message, "#4");
1240                         }
1241                 }
1242                 
1243                 [Test]
1244                 public void OpenText ()
1245                 {
1246                         string path = TempFolder + DSC + "FIT.OpenText.Test";
1247                         DeleteFile (path);
1248                         StreamReader reader = null;
1249                         try {
1250                                 File.Create (path).Close ();
1251                                 FileInfo info = new FileInfo (path);
1252                                 reader = info.OpenText ();
1253                                 Assert.AreEqual (-1, reader.Peek ());
1254                         } finally {
1255                                 if (reader != null)
1256                                         reader.Close ();
1257                                 DeleteFile (path);
1258                         }
1259                 }
1260                 
1261                 [Test]
1262                 public void OpenText_FileDoesNotExist ()
1263                 {
1264                         string path = TempFolder + DSC + "FIT.OpenTextFileNotFoundException.Test";
1265                         DeleteFile (path);
1266                         FileInfo info = new FileInfo (path);
1267                         try {
1268                                 info.OpenText ();
1269                                 Assert.Fail ("#1");
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");
1275                         }
1276                 }
1277
1278                 [Test]
1279                 public void OpenText_Directory ()
1280                 {
1281                         FileInfo info = new FileInfo (TempFolder);
1282                         try {
1283                                 info.OpenText ();
1284                                 Assert.Fail ("#1");
1285                         } catch (UnauthorizedAccessException ex) {
1286                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
1287                                 Assert.IsNull (ex.InnerException, "#3");
1288                                 Assert.IsNotNull (ex.Message, "#4");
1289                         }
1290                 }
1291
1292                 [Test]
1293                 public void OpenWrite ()
1294                 {
1295                         string path = TempFolder + DSC + "FIT.OpenWrite.Test";
1296                         DeleteFile (path);
1297                         FileStream stream = null;
1298                         try {
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");
1305                         } finally {
1306                                 if (stream != null)
1307                                         stream.Close ();
1308                                 DeleteFile (path);
1309                         }
1310                 }
1311                 
1312                 [Test]
1313                 public void OpenWrite_Directory ()
1314                 {
1315                         FileInfo info = new FileInfo (TempFolder);
1316                         try {
1317                                 info.OpenWrite ();
1318                                 Assert.Fail ("#1");
1319                         } catch (UnauthorizedAccessException ex) {
1320                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
1321                                 Assert.IsNull (ex.InnerException, "#3");
1322                                 Assert.IsNotNull (ex.Message, "#4");
1323                         }
1324                 }
1325 #if !MOBILE                     
1326                 [Test]
1327                 public void Serialization ()
1328                 {
1329                         FileInfo info;
1330                         SerializationInfo si;
1331                         
1332                         info = new FileInfo ("Test");
1333                         si = new SerializationInfo (typeof (FileInfo), new FormatterConverter ());
1334                         info.GetObjectData (si, new StreamingContext ());
1335
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");
1339
1340                         info = new FileInfo (TempFolder);
1341                         si = new SerializationInfo (typeof (FileInfo), new FormatterConverter ());
1342                         info.GetObjectData (si, new StreamingContext ());
1343
1344                         Assert.AreEqual (2, si.MemberCount, "#B1");
1345                         Assert.AreEqual (TempFolder, si.GetString ("OriginalPath"), "#B2");
1346                         Assert.AreEqual (TempFolder, si.GetString ("FullPath"), "#B3");
1347                 }
1348
1349                 [Test]
1350                 public void Deserialization ()
1351                 {
1352                         FileInfo info = new FileInfo ("Test");
1353
1354                         MemoryStream ms = new MemoryStream ();
1355                         BinaryFormatter bf = new BinaryFormatter ();
1356                         bf.Serialize (ms, info);
1357                         ms.Position = 0;
1358
1359                         FileInfo clone = (FileInfo) bf.Deserialize (ms);
1360                         Assert.AreEqual (info.Name, clone.Name, "#1");
1361                         Assert.AreEqual (info.FullName, clone.FullName, "#2");
1362                 }
1363 #endif
1364
1365                 [Test]
1366                 [Category ("MobileNotWorking")]
1367                 public void ToStringVariety ()
1368                 {
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 ());
1377                 }
1378
1379                 void DeleteFile (string path)
1380                 {
1381                         if (File.Exists (path))
1382                                 File.Delete (path);
1383                 }
1384
1385                 void DeleteDirectory (string path)
1386                 {
1387                         if (Directory.Exists (path))
1388                                 Directory.Delete (path, true);
1389                 }
1390         }
1391 }