Fixed the tests checking for PlatformID.
[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 NET_2_0
143                 [Test]
144                 [Category ("NotWorking")]
145                 public void IsReadOnly ()
146                 {
147                         string path = TempFolder + DSC + "FIT.IsReadOnly.Test";
148                         DeleteFile (path);
149                         
150                         try {
151                                 using (FileStream stream = File.Create (path)) {
152                                         stream.WriteByte (12);
153                                         stream.Close ();
154                                 }
155
156                                 FileInfo info1 = new FileInfo (path);
157                                 Assert.IsFalse (info1.IsReadOnly, "#1");
158
159                                 FileInfo info2 = new FileInfo (path);
160                                 info2.IsReadOnly = true;
161                                 Assert.IsFalse (info1.IsReadOnly, "#2");
162                                 Assert.IsTrue (info2.IsReadOnly, "#3");
163
164                                 FileInfo info3 = new FileInfo (path);
165                                 Assert.IsTrue (info3.IsReadOnly, "#4");
166                                 info3.IsReadOnly = false;
167                                 Assert.IsFalse (info1.IsReadOnly, "#4");
168                                 Assert.IsTrue (info2.IsReadOnly, "#5");
169                                 Assert.IsFalse (info3.IsReadOnly, "#6");
170                         } finally {
171                                 File.SetAttributes (path, FileAttributes.Normal);
172                                 DeleteFile (path);
173                         }
174                 }
175 #endif
176
177                 [Test]
178                 public void Length ()
179                 {
180                         string path = TempFolder + DSC + "FIT.Length.Test";
181                         DeleteFile (path);
182                         
183                         try {
184                                 FileStream stream = File.Create (path);
185                                 FileInfo info = new FileInfo (path);
186                                 Assert.AreEqual (0, info.Length, "#1");
187                                 stream.WriteByte (12);
188                                 stream.Flush ();
189                                 Assert.AreEqual (0, info.Length, "#2");
190                                 info = new FileInfo (path);
191                                 Assert.AreEqual (1, info.Length, "#3");
192                                 stream.Close ();
193                         } finally {
194                                 DeleteFile (path);
195                         }
196                 }
197                 
198                 [Test]
199                 public void Length_FileDoesNotExist ()
200                 {
201                         string path = TempFolder + DSC + "FIT.LengthException.Test";
202                         DeleteFile (path);
203                         FileInfo info = new FileInfo (path);
204                         try {
205                                 long l = info.Length;
206                                 Assert.Fail ("#1:" + l);
207                         } catch (FileNotFoundException ex) {
208                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
209                                 Assert.AreEqual (path, ex.FileName, "#3");
210                                 Assert.IsNull (ex.InnerException, "#4");
211                                 Assert.IsNotNull (ex.Message, "#5");
212                         }
213                 }
214                 
215                 [Test]
216                 public void AppendText ()
217                 {
218                         string path = TempFolder + DSC + "FIT.AppendText.Test";
219                         DeleteFile (path);
220                         
221                         try {
222                                 FileInfo info = new FileInfo (path);
223                                 Assert.IsFalse (info.Exists, "#1");
224                         
225                                 StreamWriter writer = info.AppendText ();
226                                 info = new FileInfo (path);
227                                 Assert.IsTrue (info.Exists, "#2");
228                                 
229                                 writer.Write ("aaa");
230                                 writer.Flush ();
231                                 writer.Close ();
232                         
233                                 Assert.AreEqual (0, info.Length, "#3");
234                                 info = new FileInfo (path);
235                                 Assert.AreEqual (3, info.Length, "#4");
236                         } finally {
237                                 DeleteFile (path);
238                         }
239                 }
240
241                 [Test] // CopyTo (String)
242                 public void CopyTo1 ()
243                 {
244                         string path1 = TempFolder + DSC + "FIT.CopyTo.Source.Test";
245                         string path2 = TempFolder + DSC + "FIT.CopyTo.Dest.Test";
246                         DeleteFile (path1);
247                         DeleteFile (path2);
248                         try {
249                                 File.Create (path1).Close ();
250                         
251                                 FileInfo info = new FileInfo (path1);
252                                 Assert.IsTrue (info.Exists, "#1");
253
254                                 FileInfo info2 = info.CopyTo (path2);
255                                 info = new FileInfo (path1);
256                                 Assert.IsTrue (info2.Exists, "#2");
257                         } finally {
258                                 DeleteFile (path1);
259                                 DeleteFile (path2);
260                         }
261                 }
262
263                 [Test] // CopyTo (String)
264                 public void CopyTo1_DestFileName_AlreadyExists ()
265                 {
266                         string path1 = TempFolder + DSC + "FIT.CopyToException.Source.Test";
267                         string path2 = TempFolder + DSC + "FIT.CopyToException.Dest.Test";
268
269                         try {
270                                 DeleteFile (path1);
271                                 DeleteFile (path2);
272                                 File.Create (path1).Close ();
273                                 File.Create (path2).Close ();
274                                 FileInfo info = new FileInfo (path1);
275                                 try {
276                                         info.CopyTo (path2);
277                                         Assert.Fail ("#1");
278                                 } catch (IOException ex) {
279                                         // The file '...' already exists.
280                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
281                                         Assert.IsNull (ex.InnerException, "#3");
282                                         Assert.IsNotNull (ex.Message, "#4");
283                                         Assert.IsTrue (ex.Message.IndexOf (path2) != -1, "#5");
284                                 }
285                         } finally {
286                                 DeleteFile (path1);
287                                 DeleteFile (path2);
288                         }
289                 }
290
291                 [Test] // CopyTo (String)
292                 public void CopyTo1_DestFileName_Null ()
293                 {
294                         string path = TempFolder + DSC + "FIT.CopyToArgumentNullException.Test";
295                         DeleteFile (path);
296                         try {
297                                 File.Create (path).Close ();
298                                 FileInfo info = new FileInfo (path);
299                                 try {
300                                         info.CopyTo (null);
301                                         Assert.Fail ("#1");
302                                 } catch (ArgumentNullException ex) {
303                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
304                                         Assert.IsNull (ex.InnerException, "#3");
305                                         Assert.IsNotNull (ex.Message, "#4");
306                                         Assert.AreEqual ("destFileName", ex.ParamName, "#5");
307                                 }
308                         } finally {
309                                 DeleteFile (path);
310                         }
311                 }
312
313                 [Test] // CopyTo (String)
314                 public void CopyTo1_DestFileName_Empty ()
315                 {
316                         string path = TempFolder + DSC + "FIT.CopyToArgument1Exception.Test";
317                         DeleteFile (path);
318
319                         try {
320                                 File.Create (path).Close ();
321                                 FileInfo info = new FileInfo (path);
322                                 try {
323                                         info.CopyTo (string.Empty);
324                                         Assert.Fail ("#1");
325                                 } catch (ArgumentException ex) {
326                                         // Empty file name is not legal
327                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
328                                         Assert.IsNull (ex.InnerException, "#3");
329                                         Assert.IsNotNull (ex.Message, "#4");
330                                         Assert.AreEqual ("destFileName", ex.ParamName, "#5");
331                                 }
332                         } finally {
333                                 DeleteFile (path);
334                         }
335                 }
336
337                 [Test] // CopyTo (String)
338                 public void CopyTo1_DestFileName_Whitespace ()
339                 {
340                         string path = TempFolder + DSC + "FIT.CopyToArgument2Exception.Test";
341                         DeleteFile (path);
342
343                         try {
344                                 File.Create (path).Close ();
345                                 FileInfo info = new FileInfo (path);
346                                 try {
347                                         info.CopyTo ("    ");
348                                         Assert.Fail ("#1");
349                                 } catch (ArgumentException ex) {
350                                         // The path is not of a legal form
351                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
352                                         Assert.IsNull (ex.InnerException, "#3");
353                                         Assert.IsNotNull (ex.Message, "#4");
354                                         Assert.IsNull (ex.ParamName, "#5");
355                                 }
356                         } finally {
357                                 DeleteFile (path);
358                         }
359                 }
360
361                 [Test] // CopyTo (String)
362                 public void CopyTo1_DestFileName_InvalidPathChars ()
363                 {
364                         string path = TempFolder + DSC + "FIT.CopyToArgument4Exception.Test";
365                         string path2 = string.Empty;
366                         DeleteFile (path);
367
368                         try {
369                                 File.Create (path).Close ();
370                                 FileInfo info = new FileInfo (path);
371                                 foreach (char c in Path.InvalidPathChars)
372                                         path2 += c;
373                                 try {
374                                         info.CopyTo (path2);
375                                         Assert.Fail ("#1");
376                                 } catch (ArgumentException ex) {
377                                         // Illegal characters in path
378                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
379                                         Assert.IsNull (ex.InnerException, "#3");
380                                         Assert.IsNotNull (ex.Message, "#4");
381                                         Assert.IsNull (ex.ParamName, "#5");
382                                 }
383                         } finally {
384                                 DeleteFile (path);
385                         }
386                 }
387
388                 [Test] // CopyTo (String, Boolean)
389                 public void CopyTo2 ()
390                 {
391                         string path1 = TempFolder + DSC + "FIT.CopyTo2.Source.Test";
392                         string path2 = TempFolder + DSC + "FIT.CopyTo2.Dest.Test";
393                         DeleteFile (path1);
394                         DeleteFile (path2);
395                         try {
396                                 File.Create (path1).Close ();
397                                 File.Create (path2).Close ();
398                                 FileInfo info = new FileInfo (path1);
399
400                                 FileInfo info2 = info.CopyTo (path2, true);
401                                 info = new FileInfo (path1);
402                                 Assert.IsTrue (info.Exists, "#1");
403                                 Assert.IsTrue (info2.Exists, "#2");
404                         } finally {
405                                 DeleteFile (path1);
406                                 DeleteFile (path2);
407                         }
408                 }
409
410                 [Test] // CopyTo (String, Boolean)
411                 public void CopyTo2_DestFileName_AlreadyExists ()
412                 {
413                         string path1 = TempFolder + DSC + "FIT.CopyToException.Source.Test";
414                         string path2 = TempFolder + DSC + "FIT.CopyToException.Dest.Test";
415
416                         try {
417                                 DeleteFile (path1);
418                                 DeleteFile (path2);
419                                 File.Create (path1).Close ();
420                                 File.Create (path2).Close ();
421                                 FileInfo info = new FileInfo (path1);
422                                 try {
423                                         info.CopyTo (path2, false);
424                                         Assert.Fail ("#1");
425                                 } catch (IOException ex) {
426                                         // The file '...' already exists.
427                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
428                                         Assert.IsNull (ex.InnerException, "#3");
429                                         Assert.IsNotNull (ex.Message, "#4");
430                                         Assert.IsTrue (ex.Message.IndexOf (path2) != -1, "#5");
431                                 }
432                         } finally {
433                                 DeleteFile (path1);
434                                 DeleteFile (path2);
435                         }
436                 }
437
438                 [Test] // CopyTo (String, Boolean)
439                 public void CopyTo2_DestFileName_Null ()
440                 {
441                         string path = TempFolder + DSC + "FIT.CopyToArgumentNullException.Test";
442                         DeleteFile (path);
443                         try {
444                                 File.Create (path).Close ();
445                                 FileInfo info = new FileInfo (path);
446                                 try {
447                                         info.CopyTo (null, false);
448                                         Assert.Fail ("#1");
449                                 } catch (ArgumentNullException ex) {
450                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
451                                         Assert.IsNull (ex.InnerException, "#3");
452                                         Assert.IsNotNull (ex.Message, "#4");
453                                         Assert.AreEqual ("destFileName", ex.ParamName, "#5");
454                                 }
455                         } finally {
456                                 DeleteFile (path);
457                         }
458                 }
459
460                 [Test] // CopyTo (String, Boolean)
461                 public void CopyTo2_DestFileName_Empty ()
462                 {
463                         string path = TempFolder + DSC + "FIT.CopyToArgument1Exception.Test";
464                         DeleteFile (path);
465                         
466                         try {
467                                 File.Create (path).Close ();
468                                 FileInfo info = new FileInfo (path);
469                                 try {
470                                         info.CopyTo (string.Empty, false);
471                                         Assert.Fail ("#1");
472                                 } catch (ArgumentException ex) {
473                                         // Empty file name is not legal
474                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
475                                         Assert.IsNull (ex.InnerException, "#3");
476                                         Assert.IsNotNull (ex.Message, "#4");
477                                         Assert.AreEqual ("destFileName", ex.ParamName, "#5");
478                                 }
479                         } finally {
480                                 DeleteFile (path);
481                         }
482                 }
483
484                 [Test] // CopyTo (String, Boolean)
485                 public void CopyTo2_DestFileName_Whitespace ()
486                 {
487                         string path = TempFolder + DSC + "FIT.CopyToArgument2Exception.Test";
488                         DeleteFile (path);
489                         
490                         try {
491                                 File.Create (path).Close ();
492                                 FileInfo info = new FileInfo (path);
493                                 try {
494                                         info.CopyTo ("    ", false);
495                                         Assert.Fail ("#1");
496                                 } catch (ArgumentException ex) {
497                                         // The path is not of a legal form
498                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
499                                         Assert.IsNull (ex.InnerException, "#3");
500                                         Assert.IsNotNull (ex.Message, "#4");
501                                         Assert.IsNull (ex.ParamName, "#5");
502                                 }
503                         } finally {
504                                 DeleteFile (path);
505                         }
506                 }
507
508                 [Test] // CopyTo (String, Boolean)
509                 public void CopyTo2_DestFileName_InvalidPathChars ()
510                 {
511                         string path = TempFolder + DSC + "FIT.CopyToArgument4Exception.Test";
512                         string path2 = string.Empty;
513                         DeleteFile (path);
514                         
515                         try {
516                                 File.Create (path).Close ();
517                                 FileInfo info = new FileInfo (path);
518                                 foreach (char c in Path.InvalidPathChars)
519                                         path2 += c;
520                                 try {
521                                         info.CopyTo (path2, false);
522                                         Assert.Fail ("#1");
523                                 } catch (ArgumentException ex) {
524                                         // Illegal characters in path
525                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
526                                         Assert.IsNull (ex.InnerException, "#3");
527                                         Assert.IsNotNull (ex.Message, "#4");
528                                         Assert.IsNull (ex.ParamName, "#5");
529                                 }
530                         } finally {
531                                 DeleteFile (path);
532                         }
533                 }
534                 
535                 [Test]
536                 public void Create ()
537                 {
538                         string path = TempFolder + DSC + "FIT.Create.Test";
539                         DeleteFile (path);
540                         
541                         try {
542                                 FileInfo info = new FileInfo (path);
543                                 Assert.IsFalse (info.Exists, "#1");
544                                 FileStream stream = info.Create ();
545                                 Assert.IsFalse (info.Exists, "#2");
546                                 info = new FileInfo (path);
547                                 Assert.IsTrue (info.Exists, "#3");
548                                 Assert.IsTrue (stream.CanRead, "#4");
549                                 Assert.IsTrue (stream.CanWrite, "#5");
550                                 Assert.IsTrue (stream.CanSeek, "#6");
551                                 stream.Close ();
552                         } finally {
553                                 DeleteFile (path);
554                         }
555                 }
556                 
557                 [Test]
558                 public void CreateText ()
559                 {
560                         string path = TempFolder + DSC + "FIT.CreateText.Test";
561                         DeleteFile (path);
562                         
563                         try {
564                                 FileInfo info = new FileInfo (path);
565                                 Assert.IsFalse (info.Exists, "#1");
566                                 StreamWriter writer = info.CreateText ();
567                                 writer.WriteLine ("test");
568                                 writer.Close ();
569                                 info = new FileInfo (path);
570                                 Assert.IsTrue (info.Exists, "#2");
571                         } finally {
572                                 DeleteFile (path);
573                         }
574                 }
575                 
576                 [Test]
577                 public void CreateText_Directory ()
578                 {
579                         FileInfo info = new FileInfo (TempFolder);
580                         try {
581                                 info.CreateText ();
582                                 Assert.Fail ("#1");
583                         } catch (UnauthorizedAccessException ex) {
584                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
585                                 Assert.IsNull (ex.InnerException, "#3");
586                                 Assert.IsNotNull (ex.Message, "#4");
587                         }
588                 }
589                 
590                 [Test]
591                 public void Delete ()
592                 {
593                         string path = TempFolder + DSC + "FIT.Delete.Test";
594                         DeleteFile (path);
595                         
596                         try {
597                                 FileInfo info = new FileInfo (path);
598                                 Assert.IsFalse (info.Exists, "#1");
599                                 info.Create ().Close ();
600                                 info = new FileInfo (path);
601                                 Assert.IsTrue (info.Exists, "#2");
602                                 info.Delete ();
603                                 Assert.IsTrue (info.Exists, "#3");
604                                 info = new FileInfo (path);
605                                 Assert.IsFalse (info.Exists, "#4");
606                         } finally {
607                                 DeleteFile (path);
608                         }
609                 }
610                 
611                 [Test]
612                 public void Delete_Directory ()
613                 {
614                         FileInfo info = new FileInfo (TempFolder);
615                         try {
616                                 info.Delete ();
617                                 Assert.Fail ("#1");
618                         } catch (UnauthorizedAccessException ex) {
619                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
620                                 Assert.IsNull (ex.InnerException, "#3");
621                                 Assert.IsNotNull (ex.Message, "#4");
622                         }
623                 }
624                 
625                 [Test]
626                 public void MoveTo ()
627                 {
628                         string path1 = TempFolder + DSC + "FIT.MoveTo.Source.Test";
629                         string path2 = TempFolder + DSC + "FIT.MoveTo.Dest.Test";
630                         DeleteFile (path1);
631                         DeleteFile (path2);
632                         
633                         try {
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");
641
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");
648                         } finally {
649                                 DeleteFile (path1);
650                                 DeleteFile (path2);
651                         }
652                 }
653
654                 [Test]
655                 public void MoveTo_DestFileName_AlreadyExists ()
656                 {
657                         string sourceFile = TempFolder + DSC + "FIT.MoveTo.Source.Test";
658                         string destFile;
659                         FileInfo info;
660
661                         // move to same directory
662                         File.Create (sourceFile).Close ();
663                         info = new FileInfo (sourceFile);
664                         try {
665                                 info.MoveTo (TempFolder);
666                                 Assert.Fail ("#A1");
667                         } catch (IOException ex) {
668                                 // Cannot create a file when that file already exists
669                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
670                                 Assert.IsNull (ex.InnerException, "#A3");
671                                 Assert.IsNotNull (ex.Message, "#A4");
672                                 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#A5");
673                                 Assert.IsFalse (ex.Message.IndexOf (TempFolder) != -1, "#A6");
674                         } finally {
675                                 DeleteFile (sourceFile);
676                         }
677
678                         // move to exist file
679                         File.Create (sourceFile).Close ();
680                         destFile = TempFolder + DSC + "FIT.MoveTo.Dest.Test";
681                         File.Create (destFile).Close ();
682                         info = new FileInfo (sourceFile);
683                         try {
684                                 info.MoveTo (destFile);
685                                 Assert.Fail ("#B1");
686                         } catch (IOException ex) {
687                                 // Cannot create a file when that file already exists
688                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
689                                 Assert.IsNull (ex.InnerException, "#B3");
690                                 Assert.IsNotNull (ex.Message, "#B4");
691                                 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#B5");
692                                 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#B6");
693                         } finally {
694                                 DeleteFile (sourceFile);
695                                 DeleteFile (destFile);
696                         }
697
698                         // move to existing directory
699                         File.Create (sourceFile).Close ();
700                         destFile = TempFolder + Path.DirectorySeparatorChar + "bar";
701                         Directory.CreateDirectory (destFile);
702                         info = new FileInfo (sourceFile);
703                         try {
704                                 info.MoveTo (destFile);
705                                 Assert.Fail ("#C1");
706                         } catch (IOException ex) {
707                                 // Cannot create a file when that file already exists
708                                 Assert.AreEqual (typeof (IOException), ex.GetType (), "#C2");
709                                 Assert.IsNull (ex.InnerException, "#C3");
710                                 Assert.IsNotNull (ex.Message, "#C4");
711                                 Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#C5");
712                                 Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#C6");
713                         } finally {
714                                 DeleteFile (sourceFile);
715                                 DeleteDirectory (destFile);
716                         }
717                 }
718
719                 [Test]
720                 public void MoveTo_DestFileName_DirectoryDoesNotExist ()
721                 {
722                         string sourceFile = TempFolder + Path.DirectorySeparatorChar + "foo";
723                         string destFile = Path.Combine (Path.Combine (TempFolder, "doesnotexist"), "b");
724                         DeleteFile (sourceFile);
725                         try {
726                                 File.Create (sourceFile).Close ();
727                                 FileInfo info = new FileInfo (sourceFile);
728                                 try {
729                                         info.MoveTo (destFile);
730                                         Assert.Fail ("#1");
731                                 } catch (DirectoryNotFoundException ex) {
732                                         // Could not find a part of the path
733                                         Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
734                                         Assert.IsNull (ex.InnerException, "#3");
735                                         Assert.IsNotNull (ex.Message, "#4");
736                                 }
737                         } finally {
738                                 DeleteFile (sourceFile);
739                         }
740                 }
741
742                 [Test]
743                 public void MoveTo_DestFileName_Null ()
744                 {
745                         string path = TempFolder + DSC + "FIT.MoveToArgumentNullException.Test";
746                         DeleteFile (path);
747
748                         try {
749                                 File.Create (path).Close ();
750                                 FileInfo info = new FileInfo (path);
751                                 try {
752                                         info.MoveTo (null);
753                                         Assert.Fail ("#1");
754                                 } catch (ArgumentNullException ex) {
755                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
756                                         Assert.IsNull (ex.InnerException, "#3");
757                                         Assert.IsNotNull (ex.Message, "#4");
758                                         Assert.AreEqual ("destFileName", ex.ParamName, "#5");
759                                 }
760                         } finally {
761                                 DeleteFile (path);
762                         }
763                 }
764
765                 [Test]
766                 public void MoveTo_DestFileName_Empty ()
767                 {
768                         string path = TempFolder + DSC + "FIT.MoveToArgumentException.Test";
769                         DeleteFile (path);
770
771                         try {
772                                 File.Create (path).Close ();
773                                 FileInfo info = new FileInfo (path);
774                                 try {
775                                         info.MoveTo (string.Empty);
776                                         Assert.Fail ("#1");
777                                 } catch (ArgumentException ex) {
778                                         // Empty file name is not legal
779                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
780                                         Assert.IsNull (ex.InnerException, "#3");
781                                         Assert.IsNotNull (ex.Message, "#4");
782                                         Assert.AreEqual ("destFileName", ex.ParamName, "#5");
783                                 }
784                         } finally {
785                                 DeleteFile (path);
786                         }
787                 }
788
789                 [Test]
790                 public void MoveTo_DestFileName_Whitespace ()
791                 {
792                         string path = TempFolder + DSC + "FIT.MoveToArgumentException.Test";
793                         DeleteFile (path);
794
795                         try {
796                                 File.Create (path).Close ();
797                                 FileInfo info = new FileInfo (path);
798                                 try {
799                                         info.MoveTo ("   ");
800                                         Assert.Fail ("#1");
801                                 } catch (ArgumentException ex) {
802                                         // The path is not of a legal form
803                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
804                                         Assert.IsNull (ex.InnerException, "#3");
805                                         Assert.IsNotNull (ex.Message, "#4");
806                                         Assert.IsNull (ex.ParamName, "#5");
807                                 }
808                         } finally {
809                                 DeleteFile (path);
810                         }
811                 }
812
813                 [Test]
814                 public void MoveTo_FileDoesNotExist ()
815                 {
816                         string path1 = TempFolder + DSC + "FIT.MoveToFileNotFoundException.Src";
817                         string path2 = TempFolder + DSC + "FIT.MoveToFileNotFoundException.Dst";
818                         DeleteFile (path1);
819                         DeleteFile (path2);
820                         
821                         try {
822                                 FileInfo info = new FileInfo (path1);
823                                 try {
824                                         info.MoveTo (path2);
825                                         Assert.Fail ("#1");
826                                 } catch (FileNotFoundException ex) {
827                                         // Unable to find the specified file
828                                         Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
829                                         Assert.IsNull (ex.FileName, "#2");
830                                         Assert.IsNull (ex.InnerException, "#3");
831                                         Assert.IsNotNull (ex.Message, "#4");
832                                 }
833                         } finally {
834                                 DeleteFile (path1);
835                                 DeleteFile (path2);
836                         }
837                 }
838
839                 [Test]
840                 public void MoveTo_Same ()
841                 {
842                         string path = TempFolder + DSC + "FIT.MoveToSame.Test";
843                         DeleteFile (path);
844
845                         try {
846                                 File.Create (path).Close ();
847                                 FileInfo info = new FileInfo (path);
848                                 info.MoveTo (path);
849                                 Assert.IsTrue (info.Exists, "#1");
850                                 Assert.IsTrue (File.Exists (path), "#2");
851                         } finally {
852                                 DeleteFile (path);
853                         }
854                 }
855
856                 [Test]
857                 public void Open ()
858                 {
859                         string path = TempFolder + DSC + "FIT.Open.Test";
860                         DeleteFile (path);
861                         FileStream stream = null;
862                         try {
863                                 FileInfo info = new FileInfo (path);
864                                 stream = info.Open (FileMode.CreateNew);
865                                 Assert.IsTrue (stream.CanRead, "#A1");
866                                 Assert.IsTrue (stream.CanSeek, "#A2");
867                                 Assert.IsTrue (stream.CanWrite, "#A3");
868                                 stream.Close ();
869                                 
870                                 stream = info.Open (FileMode.Open);
871                                 Assert.IsTrue (stream.CanRead, "#B1");
872                                 Assert.IsTrue (stream.CanSeek, "#B2");
873                                 Assert.IsTrue (stream.CanWrite, "#B3");
874                                 stream.Close ();
875                                 
876                                 stream = info.Open (FileMode.Append, FileAccess.Write);
877                                 Assert.IsFalse (stream.CanRead, "#C1");
878                                 Assert.IsTrue (stream.CanSeek, "#C2");
879                                 Assert.IsTrue (stream.CanWrite, "#C3");
880                                 stream.Close ();
881
882                                 stream = info.Open (FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
883                                 Assert.IsTrue (stream.CanRead, "#D1");
884                                 Assert.IsTrue (stream.CanSeek, "#D2");
885                                 Assert.IsTrue (stream.CanWrite, "#D3");
886                                 stream.Close ();
887                         } finally {
888                                 if (stream != null)
889                                         stream.Close ();
890                                 DeleteFile (path);
891                         }
892                 }
893                 
894                 [Test]
895                 public void Open_FileDoesNotExist ()
896                 {
897                         string path = TempFolder + DSC + "FIT.OpenFileNotFoundException.Test";
898                         DeleteFile (path);
899                         
900                         FileInfo info = new FileInfo (path);
901                         try {
902                                 info.Open (FileMode.Open);
903                                 Assert.Fail ("#1");
904                         } catch (FileNotFoundException ex) {
905                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
906                                 Assert.AreEqual (path, ex.FileName, "#3");
907                                 Assert.IsNull (ex.InnerException, "#4");
908                                 Assert.IsNotNull (ex.Message, "#5");
909                         }
910                 }
911                 
912                 [Test]
913                 public void OpenRead ()
914                 {
915                         string path = TempFolder + DSC + "FIT.OpenRead.Test";
916                         DeleteFile (path);
917                         FileStream stream = null;
918                         try {
919                                 File.Create (path).Close ();
920                                 FileInfo info = new FileInfo (path);
921                                 stream = info.OpenRead ();
922                                 Assert.IsTrue (stream.CanRead, "#1");
923                                 Assert.IsTrue (stream.CanSeek, "#2");
924                                 Assert.IsFalse (stream.CanWrite, "#3");
925                                 stream.Close ();
926                                 
927                         } finally {
928                                 if (stream != null)
929                                         stream.Close ();
930                                 DeleteFile (path);
931                         }
932                 }
933                 
934                 [Test]
935                 [Category("TargetJvmNotSupported")] // File sharing not supported for TARGET_JVM
936                 public void OpenRead_FileLock ()
937                 {
938                         string path = TempFolder + DSC + "FIT.OpenReadIOException.Test";
939                         DeleteFile (path);
940                         FileStream stream = null;
941                         
942                         try {
943                                 stream = File.Create (path);
944                                 FileInfo info = new FileInfo (path);
945                                 try {
946                                         info.OpenRead ();
947                                         Assert.Fail ("#1");
948                                 } catch (IOException ex) {
949                                         // The process cannot access the file because
950                                         // it is being used by another process
951                                         Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
952                                         Assert.IsNull (ex.InnerException, "#3");
953                                         Assert.IsNotNull (ex.Message, "#4");
954                                 }
955                         } finally {
956                                 if (stream != null)
957                                         stream.Close ();
958                                 DeleteFile (path);
959                         }
960                 }
961
962                 [Test]
963                 public void OpenRead_Directory ()
964                 {
965                         FileInfo info = new FileInfo (TempFolder);
966                         try {
967                                 info.OpenRead ();
968                                 Assert.Fail ("#1");
969                         } catch (UnauthorizedAccessException ex) {
970                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
971                                 Assert.IsNull (ex.InnerException, "#3");
972                                 Assert.IsNotNull (ex.Message, "#4");
973                         }
974                 }
975                 
976                 [Test]
977                 public void OpenText ()
978                 {
979                         string path = TempFolder + DSC + "FIT.OpenText.Test";
980                         DeleteFile (path);
981                         StreamReader reader = null;
982                         try {
983                                 File.Create (path).Close ();
984                                 FileInfo info = new FileInfo (path);
985                                 reader = info.OpenText ();
986                                 Assert.AreEqual (-1, reader.Peek ());
987                         } finally {
988                                 if (reader != null)
989                                         reader.Close ();
990                                 DeleteFile (path);
991                         }
992                 }
993                 
994                 [Test]
995                 public void OpenText_FileDoesNotExist ()
996                 {
997                         string path = TempFolder + DSC + "FIT.OpenTextFileNotFoundException.Test";
998                         DeleteFile (path);
999                         FileInfo info = new FileInfo (path);
1000                         try {
1001                                 info.OpenText ();
1002                                 Assert.Fail ("#1");
1003                         } catch (FileNotFoundException ex) {
1004                                 Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
1005                                 Assert.AreEqual (path, ex.FileName, "#3");
1006                                 Assert.IsNull (ex.InnerException, "#4");
1007                                 Assert.IsNotNull (ex.Message, "#5");
1008                         }
1009                 }
1010
1011                 [Test]
1012                 public void OpenText_Directory ()
1013                 {
1014                         FileInfo info = new FileInfo (TempFolder);
1015                         try {
1016                                 info.OpenText ();
1017                                 Assert.Fail ("#1");
1018                         } catch (UnauthorizedAccessException ex) {
1019                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
1020                                 Assert.IsNull (ex.InnerException, "#3");
1021                                 Assert.IsNotNull (ex.Message, "#4");
1022                         }
1023                 }
1024
1025                 [Test]
1026                 public void OpenWrite ()
1027                 {
1028                         string path = TempFolder + DSC + "FIT.OpenWrite.Test";
1029                         DeleteFile (path);
1030                         FileStream stream = null;
1031                         try {
1032                                 File.Create (path).Close ();
1033                                 FileInfo info = new FileInfo (path);
1034                                 stream = info.OpenWrite ();
1035                                 Assert.IsFalse (stream.CanRead, "#1");
1036                                 Assert.IsTrue (stream.CanSeek, "#2");
1037                                 Assert.IsTrue (stream.CanWrite, "#3");
1038                         } finally {
1039                                 if (stream != null)
1040                                         stream.Close ();
1041                                 DeleteFile (path);
1042                         }
1043                 }
1044                 
1045                 [Test]
1046                 public void OpenWrite_Directory ()
1047                 {
1048                         FileInfo info = new FileInfo (TempFolder);
1049                         try {
1050                                 info.OpenWrite ();
1051                                 Assert.Fail ("#1");
1052                         } catch (UnauthorizedAccessException ex) {
1053                                 Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
1054                                 Assert.IsNull (ex.InnerException, "#3");
1055                                 Assert.IsNotNull (ex.Message, "#4");
1056                         }
1057                 }
1058
1059                 [Test]
1060                 public void Serialization ()
1061                 {
1062                         FileInfo info;
1063                         SerializationInfo si;
1064                         
1065                         info = new FileInfo ("Test");
1066                         si = new SerializationInfo (typeof (FileInfo), new FormatterConverter ());
1067                         info.GetObjectData (si, new StreamingContext ());
1068
1069                         Assert.AreEqual (2, si.MemberCount, "#A1");
1070                         Assert.AreEqual ("Test", si.GetString ("OriginalPath"), "#A2");
1071                         Assert.AreEqual (Path.Combine (Directory.GetCurrentDirectory (), "Test"), si.GetString ("FullPath"), "#A3");
1072
1073                         info = new FileInfo (TempFolder);
1074                         si = new SerializationInfo (typeof (FileInfo), new FormatterConverter ());
1075                         info.GetObjectData (si, new StreamingContext ());
1076
1077                         Assert.AreEqual (2, si.MemberCount, "#B1");
1078                         Assert.AreEqual (TempFolder, si.GetString ("OriginalPath"), "#B2");
1079                         Assert.AreEqual (TempFolder, si.GetString ("FullPath"), "#B3");
1080                 }
1081
1082                 [Test]
1083                 public void Deserialization ()
1084                 {
1085                         FileInfo info = new FileInfo ("Test");
1086
1087                         MemoryStream ms = new MemoryStream ();
1088                         BinaryFormatter bf = new BinaryFormatter ();
1089                         bf.Serialize (ms, info);
1090                         ms.Position = 0;
1091
1092                         FileInfo clone = (FileInfo) bf.Deserialize (ms);
1093                         Assert.AreEqual (info.Name, clone.Name, "#1");
1094                         Assert.AreEqual (info.FullName, clone.FullName, "#2");
1095                 }
1096
1097                 [Test]
1098                 public void ToStringVariety ()
1099                 {
1100                         Assert.AreEqual ("foo", new FileInfo ("foo").ToString ());
1101                         Assert.AreEqual ("c:/foo", new FileInfo ("c:/foo").ToString ());
1102                         Assert.AreEqual ("/usr/local/foo", new FileInfo ("/usr/local/foo").ToString ());
1103                         Assert.AreEqual ("c:\\foo", new FileInfo ("c:\\foo").ToString ());
1104                         Assert.AreEqual ("/usr/local\\foo", new FileInfo ("/usr/local\\foo").ToString ());
1105                         Assert.AreEqual ("foo/BAR/baz", new FileInfo ("foo/BAR/baz").ToString ());
1106                         Assert.AreEqual ("c:/documents and settings", new FileInfo ("c:/documents and settings").ToString ());
1107                         Assert.AreEqual ("c:/docUme~1", new FileInfo ("c:/docUme~1").ToString ());
1108                 }
1109
1110                 void DeleteFile (string path)
1111                 {
1112                         if (File.Exists (path))
1113                                 File.Delete (path);
1114                 }
1115
1116                 void DeleteDirectory (string path)
1117                 {
1118                         if (Directory.Exists (path))
1119                                 Directory.Delete (path, true);
1120                 }
1121         }
1122 }