**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / Test / System.IO / FileInfoTest.cs
blob232e3667bdfa18e6a89dcf565d4fbb0e372fb569
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 //
9 using NUnit.Framework;
10 using System;
11 using System.IO;
13 namespace MonoTests.System.IO
15 [TestFixture]
16 public class FileInfoTest : Assertion
18 string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
19 static readonly char DSC = Path.DirectorySeparatorChar;
21 [SetUp]
22 protected void SetUp() {
23 if (Directory.Exists (TempFolder))
24 Directory.Delete (TempFolder, true);
25 Directory.CreateDirectory (TempFolder);
28 [TearDown]
29 protected void TearDown() {
30 if (Directory.Exists (TempFolder))
31 Directory.Delete (TempFolder, true);
34 [Test]
35 public void Ctr ()
37 string path = TempFolder + DSC + "FIT.Ctr.Test";
38 DeleteFile (path);
40 FileInfo info = new FileInfo (path);
41 AssertEquals ("test#01", true, info.DirectoryName.EndsWith (".Tests"));
42 AssertEquals ("test#02", false, info.Exists);
43 AssertEquals ("test#03", ".Test", info.Extension);
44 AssertEquals ("test#05", "FIT.Ctr.Test", info.Name);
47 [Test]
48 [ExpectedException(typeof(ArgumentNullException))]
49 public void CtorArgumentNullException ()
51 FileInfo info = new FileInfo (null);
54 [Test]
55 [ExpectedException(typeof(ArgumentException))]
56 public void CtorArgumentException1 ()
58 FileInfo info = new FileInfo ("");
61 [Test]
62 [ExpectedException(typeof(ArgumentException))]
63 public void CtorArgumentException2 ()
65 FileInfo info = new FileInfo (" ");
68 [Test]
69 [ExpectedException(typeof(ArgumentException))]
70 public void CtorArgumentException3 ()
72 string path = "";
73 foreach (char c in Path.InvalidPathChars) {
74 path += c;
76 FileInfo info = new FileInfo (path);
79 [Test]
80 public void DirectoryTest ()
82 string path = TempFolder + DSC + "FIT.Directory.Test";
83 DeleteFile (path);
85 FileInfo info = new FileInfo (path);
86 DirectoryInfo dir = info.Directory;
87 AssertEquals ("test#01", "MonoTests.System.IO.Tests", dir.Name);
90 [Test]
91 public void Exists ()
93 string path = TempFolder + DSC + "FIT.Exists.Test";
94 DeleteFile (path);
96 try {
97 FileInfo info = new FileInfo (path);
98 AssertEquals ("test#01", false, info.Exists);
100 File.Create (path).Close ();
101 AssertEquals ("test#02", false, info.Exists);
102 info = new FileInfo (path);
103 AssertEquals ("test#03", true, info.Exists);
104 } finally {
105 DeleteFile (path);
109 [Test]
110 public void Length ()
112 string path = TempFolder + DSC + "FIT.Length.Test";
113 DeleteFile (path);
115 try {
116 FileStream stream = File.Create (path);
117 FileInfo info = new FileInfo (path);
118 AssertEquals ("test#01", 0, info.Length);
119 stream.WriteByte (12);
120 stream.Flush ();
121 AssertEquals ("test#02", 0, info.Length);
122 info = new FileInfo (path);
123 AssertEquals ("test#03", 1, info.Length);
124 stream.Close ();
125 } finally {
126 DeleteFile (path);
130 [Test]
131 [ExpectedException (typeof (FileNotFoundException))]
132 public void LengthException ()
134 string path = TempFolder + DSC + "FIT.LengthException.Test";
135 DeleteFile (path);
136 FileInfo info = new FileInfo (path);
137 long l = info.Length;
140 [Test]
141 public void AppendText ()
143 string path = TempFolder + DSC + "FIT.AppendText.Test";
144 DeleteFile (path);
146 try {
147 FileInfo info = new FileInfo (path);
149 AssertEquals ("test#01", false, info.Exists);
151 StreamWriter writer = info.AppendText ();
152 info = new FileInfo (path);
153 AssertEquals ("test#02", true, info.Exists );
155 writer.Write ("aaa");
156 writer.Flush ();
157 writer.Close ();
159 AssertEquals ("test#03", 0, info.Length);
160 info = new FileInfo (path);
161 AssertEquals ("test#04", 3, info.Length);
162 } finally {
163 DeleteFile (path);
168 [Test]
169 public void CopyTo ()
171 string path1 = TempFolder + DSC + "FIT.CopyTo.Source.Test";
172 string path2 = TempFolder + DSC + "FIT.CopyTo.Dest.Test";
173 DeleteFile (path1);
174 DeleteFile (path2);
175 try {
176 File.Create (path1).Close ();
178 FileInfo info = new FileInfo (path1);
179 AssertEquals ("test#01", true, info.Exists);
181 FileInfo info2 = info.CopyTo (path2);
182 info = new FileInfo (path1);
183 AssertEquals ("test#02", true, info2.Exists);
184 } finally {
185 DeleteFile (path1);
186 DeleteFile (path2);
190 [Test]
191 public void CopyTo2 ()
193 string path1 = TempFolder + DSC + "FIT.CopyTo2.Source.Test";
194 string path2 = TempFolder + DSC + "FIT.CopyTo2.Dest.Test";
195 DeleteFile (path1);
196 DeleteFile (path2);
197 try {
198 File.Create (path1).Close ();
199 File.Create (path2).Close ();
200 FileInfo info = new FileInfo (path1);
202 FileInfo info2 = info.CopyTo (path2, true);
203 info = new FileInfo (path1);
204 AssertEquals ("test#01", true, info.Exists);
205 AssertEquals ("test#02", true, info2.Exists);
206 } finally {
207 DeleteFile (path1);
208 DeleteFile (path2);
212 [Test]
213 [ExpectedException (typeof (IOException))]
214 public void CopyToIOException ()
216 string path1 = TempFolder + DSC + "FIT.CopyToException.Source.Test";
217 string path2 = TempFolder + DSC + "FIT.CopyToException.Dest.Test";
219 try {
220 DeleteFile (path1);
221 DeleteFile (path2);
222 File.Create (path1).Close ();
223 File.Create (path2).Close ();
224 FileInfo info = new FileInfo (path1);
225 FileInfo info2 = info.CopyTo (path2);
226 } finally {
227 DeleteFile (path1);
228 DeleteFile (path2);
232 [Test]
233 [ExpectedException (typeof (IOException))]
234 public void CopyToIOException2 ()
236 string path1 = TempFolder + DSC + "FIT.CopyToException.Source.Test";
237 string path2 = TempFolder + DSC + "FIT.CopyToException.Dest.Test";
239 try {
240 DeleteFile (path1);
241 DeleteFile (path2);
242 File.Create (path1).Close ();
243 File.Create (path2).Close ();
244 FileInfo info = new FileInfo (path1);
245 FileInfo info2 = info.CopyTo (path2, false);
246 } finally {
247 DeleteFile (path1);
248 DeleteFile (path2);
252 [Test]
253 [ExpectedException (typeof (ArgumentNullException))]
254 public void CopyToArgumentNullException ()
256 string path = TempFolder + DSC + "FIT.CopyToArgumentNullException.Test";
257 DeleteFile (path);
258 try {
259 File.Create (path).Close ();
260 FileInfo info = new FileInfo (path);
261 info.CopyTo (null, false);
262 } finally {
263 DeleteFile (path);
267 [Test]
268 [ExpectedException (typeof (ArgumentException))]
269 public void CopyToArgumentException1 ()
271 string path = TempFolder + DSC + "FIT.CopyToArgument1Exception.Test";
272 DeleteFile (path);
274 try {
275 File.Create (path).Close ();
276 FileInfo info = new FileInfo (path);
277 info.CopyTo ("", false);
278 } finally {
279 DeleteFile (path);
283 [Test]
284 [ExpectedException (typeof (ArgumentException))]
285 public void CopyToArgumentException2 ()
287 string path = TempFolder + DSC + "FIT.CopyToArgument2Exception.Test";
288 DeleteFile (path);
290 try {
291 File.Create (path).Close ();
292 FileInfo info = new FileInfo (path);
293 info.CopyTo (" ", false);
294 } finally {
295 DeleteFile (path);
299 [Test]
300 [ExpectedException (typeof (ArgumentException))]
301 public void CopyToArgumentException3 ()
303 string path = TempFolder + DSC + "FIT.CopyToArgument3Exception.Test";
304 DeleteFile (path);
306 try {
307 File.Create (path).Close ();
308 FileInfo info = new FileInfo (path);
309 info.CopyTo (" ", false);
310 } finally {
311 DeleteFile (path);
315 [Test]
316 [ExpectedException (typeof (ArgumentException))]
317 public void CopyToArgumentException4 ()
319 string path = TempFolder + DSC + "FIT.CopyToArgument4Exception.Test";
320 string path2 = "";
321 DeleteFile (path);
323 try {
324 File.Create (path).Close ();
325 FileInfo info = new FileInfo (path);
327 foreach (char c in Path.InvalidPathChars) {
328 path2 += c;
330 info.CopyTo (path2, false);
331 } finally {
332 DeleteFile (path);
336 [Test]
337 public void Create ()
339 string path = TempFolder + DSC + "FIT.Create.Test";
340 DeleteFile (path);
342 try {
343 FileInfo info = new FileInfo (path);
344 AssertEquals ("test#01", false, info.Exists);
345 FileStream stream = info.Create ();
346 AssertEquals ("test#02", false, info.Exists);
347 info = new FileInfo (path);
348 AssertEquals ("test#03", true, info.Exists);
349 AssertEquals ("test#04", true, stream.CanRead);
350 AssertEquals ("test#05", true, stream.CanWrite);
351 AssertEquals ("test#06", true, stream.CanSeek);
352 stream.Close ();
353 } finally {
354 DeleteFile (path);
358 [Test]
359 public void CreateText ()
361 string path = TempFolder + DSC + "FIT.CreateText.Test";
362 DeleteFile (path);
364 try {
365 FileInfo info = new FileInfo (path);
366 AssertEquals ("test#01", false, info.Exists);
367 StreamWriter writer = info.CreateText ();
368 writer.WriteLine ("test");
369 writer.Close ();
370 info = new FileInfo (path);
371 AssertEquals ("test#02", true, info.Exists);
372 } finally {
373 DeleteFile (path);
377 [Test]
378 [ExpectedException (typeof (UnauthorizedAccessException))]
379 public void CreateTextUnauthorizedAccessException ()
381 string path = TempFolder;
383 FileInfo info = new FileInfo (path);
384 AssertEquals ("test#01", false, info.Exists);
385 StreamWriter writer = info.CreateText ();
389 [Test]
390 public void Delete ()
392 string path = TempFolder + DSC + "FIT.Delete.Test";
393 DeleteFile (path);
395 try {
396 FileInfo info = new FileInfo (path);
397 AssertEquals ("test#01", false, info.Exists);
398 info.Create ().Close ();
399 info = new FileInfo (path);
400 AssertEquals ("test#02", true, info.Exists);
401 info.Delete ();
402 AssertEquals ("test#03", true, info.Exists);
403 info = new FileInfo (path);
404 AssertEquals ("test#04", false, info.Exists);
405 } finally {
406 DeleteFile (path);
410 [Test]
411 [ExpectedException (typeof (UnauthorizedAccessException))]
412 public void DeleteUnauthorizedAccessException ()
414 string path = TempFolder;
415 FileInfo info = new FileInfo (path);
416 info.Delete ();
419 [Test]
420 public void MoveTo ()
422 string path1 = TempFolder + DSC + "FIT.MoveTo.Source.Test";
423 string path2 = TempFolder + DSC + "FIT.MoveTo.Dest.Test";
424 DeleteFile (path1);
425 DeleteFile (path2);
427 try {
428 File.Create (path1).Close ();
429 FileInfo info = new FileInfo (path1);
430 FileInfo info2 = new FileInfo (path2);
431 AssertEquals ("test#01", false, info2.Exists);
433 info.MoveTo (path2);
434 info2 = new FileInfo (path2);
435 AssertEquals ("test#02", true, info2.Exists);
436 } finally {
437 DeleteFile (path1);
438 DeleteFile (path2);
442 [Test]
443 [ExpectedException (typeof (IOException))]
444 public void MoveToIOException ()
446 string path1 = TempFolder + DSC + "FIT.MoveToIOException.Source.Test";
447 string path2 = TempFolder + DSC + "FIT.MoveToIOException.Dest.Test";
448 DeleteFile (path1);
449 DeleteFile (path2);
451 try {
452 File.Create (path1).Close ();
453 File.Create (path2).Close ();
455 FileInfo info = new FileInfo (path1);
456 info.MoveTo (path2);
457 } finally {
458 DeleteFile (path1);
459 DeleteFile (path2);
463 [Test]
464 [ExpectedException (typeof (ArgumentNullException))]
465 public void MoveToArgumentNullException ()
467 string path = TempFolder + DSC + "FIT.MoveToArgumentNullException.Test";
468 DeleteFile (path);
470 try {
471 File.Create (path).Close ();
472 FileInfo info = new FileInfo (path);
473 info.MoveTo (null);
474 } finally {
475 DeleteFile (path);
479 [Test]
480 [ExpectedException (typeof (ArgumentException))]
481 public void MoveToArgumentException ()
483 string path = TempFolder + DSC + "FIT.MoveToArgumentException.Test";
484 DeleteFile (path);
486 try {
487 File.Create (path).Close ();
488 FileInfo info = new FileInfo (path);
489 info.MoveTo (" ");
490 } finally {
491 DeleteFile (path);
495 /// <summary>
496 /// msdn says this should throw UnauthorizedAccessException, byt
497 /// it throws IOException.
498 /// </summary>
499 [Test]
500 [ExpectedException (typeof (IOException))]
501 public void MoveToUnauthorizedAccessException ()
503 string path = TempFolder + DSC + "FIT.UnauthorizedAccessException.Test";
504 DeleteFile (path);
506 try {
507 File.Create (path).Close ();
508 FileInfo info = new FileInfo (path);
509 info.MoveTo (TempFolder);
510 } finally {
511 DeleteFile (path);
515 [Test]
516 [ExpectedException (typeof (FileNotFoundException))]
517 public void MoveToFileNotFoundException ()
519 string path1 = TempFolder + DSC + "FIT.MoveToFileNotFoundException.Src";
520 string path2 = TempFolder + DSC + "FIT.MoveToFileNotFoundException.Dst";
521 DeleteFile (path1);
522 DeleteFile (path2);
524 try {
525 FileInfo info = new FileInfo (path1);
526 info.MoveTo (path2);
527 } finally {
528 DeleteFile (path1);
529 DeleteFile (path2);
533 [Test]
534 public void Open ()
536 string path = TempFolder + DSC + "FIT.Open.Test";
537 DeleteFile (path);
538 FileStream stream = null;
539 try {
540 FileInfo info = new FileInfo (path);
541 stream = info.Open (FileMode.CreateNew);
542 AssertEquals ("test#01", true, stream.CanRead);
543 AssertEquals ("test#02", true, stream.CanSeek);
544 AssertEquals ("test#03", true, stream.CanWrite);
545 stream.Close ();
547 stream = info.Open (FileMode.Open);
548 AssertEquals ("test#04", true, stream.CanRead);
549 AssertEquals ("test#05", true, stream.CanSeek);
550 AssertEquals ("test#06", true, stream.CanWrite);
551 stream.Close ();
553 stream = info.Open (FileMode.Append, FileAccess.Write);
554 AssertEquals ("test#07", false, stream.CanRead);
555 AssertEquals ("test#08", true, stream.CanSeek);
556 AssertEquals ("test#09", true, stream.CanWrite);
557 stream.Close ();
559 stream = info.Open (FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
560 AssertEquals ("test#10", true, stream.CanRead);
561 AssertEquals ("test#11", true, stream.CanSeek);
562 AssertEquals ("test#12", true, stream.CanWrite);
563 stream.Close ();
564 } finally {
565 if (stream != null)
566 stream.Close ();
567 DeleteFile (path);
571 [Test]
572 [ExpectedException(typeof(FileNotFoundException))]
573 public void OpenFileNotFoundException ()
575 string path = TempFolder + DSC + "FIT.OpenFileNotFoundException.Test";
576 DeleteFile (path);
578 FileInfo info = new FileInfo (path);
579 info.Open (FileMode.Open);
582 [Test]
583 public void OpenRead ()
585 string path = TempFolder + DSC + "FIT.OpenRead.Test";
586 DeleteFile (path);
587 FileStream stream = null;
588 try {
589 File.Create (path).Close ();
590 FileInfo info = new FileInfo (path);
591 stream = info.OpenRead ();
592 AssertEquals ("test#01", true, stream.CanRead);
593 AssertEquals ("test#02", true, stream.CanSeek);
594 AssertEquals ("test#03", false, stream.CanWrite);
595 stream.Close ();
597 } finally {
598 if (stream != null)
599 stream.Close ();
600 DeleteFile (path);
604 [Test]
605 [ExpectedException(typeof (IOException))]
606 public void OpenReadIOException ()
608 string path = TempFolder + DSC + "FIT.OpenReadIOException.Test";
609 DeleteFile (path);
610 FileStream stream = null;
612 try {
613 stream = File.Create (path);
614 FileInfo info = new FileInfo (path);
615 info.OpenRead ();
616 } finally {
617 if (stream != null)
618 stream.Close ();
619 DeleteFile (path);
623 [Test]
624 [ExpectedException(typeof (UnauthorizedAccessException))]
625 public void OpenReadUnauthorizedAccessException ()
627 string path = TempFolder;
629 FileInfo info = new FileInfo (path);
630 info.OpenRead ();
633 [Test]
634 public void OpenText ()
636 string path = TempFolder + DSC + "FIT.OpenText.Test";
637 DeleteFile (path);
638 StreamReader reader = null;
639 try {
640 File.Create (path).Close ();
641 FileInfo info = new FileInfo (path);
642 reader = info.OpenText ();
643 AssertEquals ("test#01", -1, reader.Peek ());
644 } finally {
646 if (reader != null)
647 reader.Close ();
648 DeleteFile (path);
652 [Test]
653 [ExpectedException(typeof (FileNotFoundException))]
654 public void OpenTextFileNotFoundException ()
656 string path = TempFolder + DSC + "FIT.OpenTextFileNotFoundException.Test";
657 DeleteFile (path);
658 FileInfo info = new FileInfo (path);
659 info.OpenText ();
662 [Test]
663 [ExpectedException(typeof (UnauthorizedAccessException))]
664 public void OpenTextUnauthorizedAccessException ()
666 string path = TempFolder;
668 FileInfo info = new FileInfo (path);
669 info.OpenText ();
672 [Test]
673 public void OpenWrite ()
675 string path = TempFolder + DSC + "FIT.OpenWrite.Test";
676 DeleteFile (path);
677 FileStream stream = null;
678 try {
679 File.Create (path).Close ();
680 FileInfo info = new FileInfo (path);
681 stream = info.OpenWrite ();
682 AssertEquals ("test#01", false, stream.CanRead);
683 AssertEquals ("test#02", true, stream.CanSeek);
684 AssertEquals ("test#03", true, stream.CanWrite);
685 } finally {
687 if (stream != null)
688 stream.Close ();
689 DeleteFile (path);
693 [Test]
694 [ExpectedException(typeof (UnauthorizedAccessException))]
695 public void OpenWriteUnauthorizedAccessException ()
697 string path = TempFolder;
699 FileInfo info = new FileInfo (path);
700 info.OpenWrite ();
703 private void DeleteFile (string path)
705 if (File.Exists (path))
706 File.Delete (path);
709 [Test]
710 public void ToStringVariety ()
712 AssertEquals ("foo", new FileInfo ("foo").ToString ());
713 AssertEquals ("c:/foo", new FileInfo ("c:/foo").ToString ());
714 AssertEquals ("/usr/local/foo", new FileInfo ("/usr/local/foo").ToString ());
715 AssertEquals ("c:\\foo", new FileInfo ("c:\\foo").ToString ());
716 AssertEquals ("/usr/local\\foo", new FileInfo ("/usr/local\\foo").ToString ());
717 AssertEquals ("foo/BAR/baz", new FileInfo ("foo/BAR/baz").ToString ());
718 AssertEquals ("c:/documents and settings", new FileInfo ("c:/documents and settings").ToString ());
719 AssertEquals ("c:/docUme~1", new FileInfo ("c:/docUme~1").ToString ());