**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / Test / System.IO / FileTest.cs
blob21f11655f870e0629a46663d25e570e93c44c3c5
1 //
2 // FileTest.cs: Test cases for System.IO.File
3 //
4 // Author:
5 // Duncan Mak (duncan@ximian.com)
6 // Ville Palo (vi64pa@kolumbus.fi)
7 //
8 // (C) 2002 Ximian, Inc. http://www.ximian.com
9 //
10 // TODO: Find out why ArgumentOutOfRangeExceptions does not manage to close streams properly
12 using NUnit.Framework;
13 using System;
14 using System.IO;
15 using System.Globalization;
16 using System.Threading;
18 namespace MonoTests.System.IO
20 [TestFixture]
21 public class FileTest : Assertion
23 static string TempFolder = Path.Combine (Path.GetTempPath (), "MonoTests.System.IO.Tests");
25 [SetUp]
26 public void SetUp ()
28 if (Directory.Exists (TempFolder))
29 Directory.Delete (TempFolder, true);
30 Directory.CreateDirectory (TempFolder);
32 Thread.CurrentThread.CurrentCulture = new CultureInfo ("EN-us");
35 [TearDown]
36 public void TearDown ()
38 if (Directory.Exists (TempFolder))
39 Directory.Delete (TempFolder, true);
42 [Test]
43 public void TestExists ()
45 int i = 0;
46 FileStream s = null;
47 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
48 try {
49 Assert ("null filename should not exist", !File.Exists (null));
50 i++;
51 Assert ("empty filename should not exist", !File.Exists (""));
52 i++;
53 Assert ("whitespace filename should not exist", !File.Exists (" \t\t \t \n\t\n \n"));
54 i++;
55 DeleteFile (path);
56 s = File.Create (path);
57 s.Close ();
58 Assert ("File " + path + " should exists", File.Exists (path));
59 i++;
60 Assert ("File resources" + Path.DirectorySeparatorChar + "doesnotexist should not exist", !File.Exists (TempFolder + Path.DirectorySeparatorChar + "doesnotexist"));
61 } catch (Exception e) {
62 Fail ("Unexpected exception at i = " + i + ". e=" + e);
63 } finally {
64 if (s != null)
65 s.Close ();
66 DeleteFile (path);
70 [Test]
71 public void Exists_InvalidFileName ()
73 Assert ("><|", !File.Exists ("><|"));
76 [Test]
77 public void Exists_InvalidDirectory ()
79 Assert ("InvalidDirectory", !File.Exists (Path.Combine ("does not exist", "file.txt")));
82 [Test]
83 [ExpectedException(typeof (ArgumentNullException))]
84 public void CtorArgumentNullException1 ()
86 FileStream stream = File.Create (null);
89 [Test]
90 [ExpectedException(typeof (ArgumentException))]
91 public void CtorArgumentException1 ()
93 FileStream stream = File.Create ("");
96 [Test]
97 [ExpectedException(typeof (ArgumentException))]
98 public void CtorArgumentException2 ()
100 FileStream stream = File.Create (" ");
103 [Test]
104 [ExpectedException(typeof (DirectoryNotFoundException))]
105 public void CtorDirectoryNotFoundException ()
107 FileStream stream = null;
108 string path = TempFolder + Path.DirectorySeparatorChar + "directory_does_not_exist" + Path.DirectorySeparatorChar + "foo";
110 try {
111 stream = File.Create (path);
112 } finally {
113 if (stream != null)
114 stream.Close ();
115 DeleteFile (path);
119 [Test]
120 public void TestCreate ()
122 FileStream stream = null;
123 string path = "";
124 /* positive test: create resources/foo */
125 try {
126 path = TempFolder + Path.DirectorySeparatorChar + "foo";
127 stream = File.Create (path);
128 Assert ("File should exist", File.Exists (path));
129 stream.Close ();
130 } catch (Exception e) {
131 Fail ("File.Create(resources/foo) unexpected exception caught: e=" + e.ToString());
132 } finally {
133 if (stream != null)
134 stream.Close ();
135 DeleteFile (path);
138 path = "";
139 stream = null;
141 /* positive test: repeat test above again to test for overwriting file */
142 try {
143 path = TempFolder + Path.DirectorySeparatorChar + "foo";
144 stream = File.Create (path);
145 Assert ("File should exist", File.Exists (path));
146 stream.Close ();
147 } catch (Exception e) {
148 Fail ("File.Create(resources/foo) unexpected exception caught: e=" + e.ToString());
149 } finally {
150 if (stream != null)
151 stream.Close ();
152 DeleteFile (path);
156 [Test]
157 [ExpectedException(typeof(ArgumentNullException))]
158 public void CopyArgumentNullException1 ()
160 File.Copy (null, "b");
163 [Test]
164 [ExpectedException(typeof(ArgumentNullException))]
165 public void CopyArgumentNullException2 ()
167 File.Copy ("a", null);
170 [Test]
171 [ExpectedException(typeof(ArgumentException))]
172 public void CopyArgumentException1 ()
174 File.Copy ("", "b");
177 [Test]
178 [ExpectedException(typeof(ArgumentException))]
179 public void CopyArgumentException2 ()
181 File.Copy ("a", "");
184 [Test]
185 [ExpectedException(typeof(ArgumentException))]
186 public void CopyArgumentException3 ()
188 File.Copy (" ", "b");
191 [Test]
192 [ExpectedException(typeof(ArgumentException))]
193 public void CopyArgumentException4 ()
195 File.Copy ("a", " ");
198 [Test]
199 [ExpectedException(typeof(FileNotFoundException))]
200 public void CopyFileNotFoundException ()
202 File.Copy ("doesnotexist", "b");
205 [ExpectedException(typeof(IOException))]
206 public void CopyIOException ()
208 DeleteFile (TempFolder + Path.DirectorySeparatorChar + "bar");
209 DeleteFile (TempFolder + Path.DirectorySeparatorChar + "AFile.txt");
210 try {
211 File.Create (TempFolder + Path.DirectorySeparatorChar + "AFile.txt").Close ();
212 File.Copy (TempFolder + Path.DirectorySeparatorChar + "AFile.txt", TempFolder + Path.DirectorySeparatorChar + "bar");
213 File.Copy (TempFolder + Path.DirectorySeparatorChar + "AFile.txt", TempFolder + Path.DirectorySeparatorChar + "bar");
214 } finally {
215 DeleteFile (TempFolder + Path.DirectorySeparatorChar + "bar");
216 DeleteFile (TempFolder + Path.DirectorySeparatorChar + "AFile.txt");
220 [Test]
221 public void TestCopy ()
223 string path1 = TempFolder + Path.DirectorySeparatorChar + "bar";
224 string path2 = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
225 /* positive test: copy resources/AFile.txt to resources/bar */
226 try {
227 try {
228 DeleteFile (path1);
229 DeleteFile (path2);
231 File.Create (path2).Close ();
232 File.Copy (path2, path1);
233 Assert ("File AFile.txt should still exist", File.Exists (path2));
234 Assert ("File bar should exist after File.Copy", File.Exists (path1));
235 } catch (Exception e) {
236 Fail ("#1 File.Copy('resources/AFile.txt', 'resources/bar') unexpected exception caught: e=" + e.ToString());
239 /* positive test: copy resources/AFile.txt to resources/bar, overwrite */
240 try {
241 Assert ("File bar should exist before File.Copy", File.Exists (path1));
242 File.Copy (path2, path1, true);
243 Assert ("File AFile.txt should still exist", File.Exists (path2));
244 Assert ("File bar should exist after File.Copy", File.Exists (path1));
245 } catch (Exception e) {
246 Fail ("File.Copy('resources/AFile.txt', 'resources/bar', true) unexpected exception caught: e=" + e.ToString());
248 }finally {
249 DeleteFile (path1);
250 DeleteFile (path2);
254 [Test]
255 [ExpectedException (typeof (ArgumentNullException))]
256 public void DeleteArgumentNullException ()
258 File.Delete (null);
261 [Test]
262 [ExpectedException (typeof (ArgumentException))]
263 public void DeleteArgumentException1 ()
265 File.Delete ("");
268 [Test]
269 [ExpectedException (typeof (ArgumentException))]
270 public void DeleteArgumentException2 ()
272 File.Delete (" ");
275 [Test]
276 [ExpectedException (typeof (DirectoryNotFoundException))]
277 public void DeleteDirectoryNotFoundException ()
279 string path = TempFolder + Path.DirectorySeparatorChar + "directory_does_not_exist" + Path.DirectorySeparatorChar + "foo";
280 if (Directory.Exists (path))
281 Directory.Delete (path, true);
282 File.Delete (path);
286 [Test]
287 public void TestDelete ()
289 string foopath = TempFolder + Path.DirectorySeparatorChar + "foo";
290 DeleteFile (foopath);
291 try {
292 File.Create (foopath).Close ();
294 try {
295 File.Delete (foopath);
296 } catch (Exception e) {
297 Fail ("Unable to delete " + foopath + " e=" + e.ToString());
299 Assert ("File " + foopath + " should not exist after File.Delete", !File.Exists (foopath));
300 } finally {
301 DeleteFile (foopath);
305 [Test]
306 [ExpectedException(typeof (IOException))]
307 public void DeleteOpenStreamException ()
309 string path = TempFolder + Path.DirectorySeparatorChar + "DeleteOpenStreamException";
310 DeleteFile (path);
311 FileStream stream = null;
312 try {
313 stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
314 File.Delete (path);
315 } finally {
316 if (stream != null)
317 stream.Close ();
318 DeleteFile (path);
322 [Test]
323 [ExpectedException(typeof (ArgumentNullException))]
324 public void MoveException1 ()
326 File.Move (null, "b");
329 [Test]
330 [ExpectedException(typeof (ArgumentNullException))]
331 public void MoveException2 ()
333 File.Move ("a", null);
336 [Test]
337 [ExpectedException(typeof (ArgumentException))]
338 public void MoveException3 ()
340 File.Move ("", "b");
343 [Test]
344 [ExpectedException(typeof (ArgumentException))]
345 public void MoveException4 ()
347 File.Move ("a", "");
350 [Test]
351 [ExpectedException(typeof (ArgumentException))]
352 public void MoveException5 ()
354 File.Move (" ", "b");
357 [Test]
358 [ExpectedException(typeof (ArgumentException))]
359 public void MoveException6 ()
361 File.Move ("a", " ");
364 [Test]
365 [ExpectedException(typeof (FileNotFoundException))]
366 public void MoveException7 ()
368 DeleteFile (TempFolder + Path.DirectorySeparatorChar + "doesnotexist");
369 File.Move (TempFolder + Path.DirectorySeparatorChar + "doesnotexist", "b");
372 [Test]
373 [ExpectedException(typeof (DirectoryNotFoundException))]
374 public void MoveException8 ()
376 string path = TempFolder + Path.DirectorySeparatorChar + "foo";
377 DeleteFile (path);
378 try {
379 File.Create (TempFolder + Path.DirectorySeparatorChar + "AFile.txt").Close ();
380 File.Copy(TempFolder + Path.DirectorySeparatorChar + "AFile.txt", path, true);
381 DeleteFile (TempFolder + Path.DirectorySeparatorChar + "doesnotexist" + Path.DirectorySeparatorChar + "b");
382 File.Move (TempFolder + Path.DirectorySeparatorChar + "foo", TempFolder + Path.DirectorySeparatorChar + "doesnotexist" + Path.DirectorySeparatorChar + "b");
383 } finally {
384 DeleteFile (TempFolder + Path.DirectorySeparatorChar + "AFile.txt");
385 DeleteFile (path);
389 [Test]
390 [ExpectedException(typeof (IOException))]
391 public void MoveException9 ()
393 File.Create (TempFolder + Path.DirectorySeparatorChar + "foo").Close ();
394 try {
395 File.Move (TempFolder + Path.DirectorySeparatorChar + "foo", TempFolder);
396 } finally {
397 DeleteFile (TempFolder + Path.DirectorySeparatorChar + "foo");
401 [Test]
402 public void TestMove ()
404 string bar = TempFolder + Path.DirectorySeparatorChar + "bar";
405 string baz = TempFolder + Path.DirectorySeparatorChar + "baz";
406 if (!File.Exists (bar)) {
407 FileStream f = File.Create(bar);
408 f.Close();
411 Assert ("File " + TempFolder + Path.DirectorySeparatorChar + "bar should exist", File.Exists (bar));
412 File.Move (bar, baz);
413 Assert ("File " + TempFolder + Path.DirectorySeparatorChar + "bar should not exist", !File.Exists (bar));
414 Assert ("File " + TempFolder + Path.DirectorySeparatorChar + "baz should exist", File.Exists (baz));
416 // Test moving of directories
417 string dir = Path.Combine (TempFolder, "dir");
418 string dir2 = Path.Combine (TempFolder, "dir2");
419 string dir_foo = Path.Combine (dir, "foo");
420 string dir2_foo = Path.Combine (dir2, "foo");
422 if (Directory.Exists (dir))
423 Directory.Delete (dir, true);
425 Directory.CreateDirectory (dir);
426 Directory.CreateDirectory (dir2);
427 File.Create (dir_foo).Close ();
428 File.Move (dir_foo, dir2_foo);
429 Assert (File.Exists (dir2_foo));
431 Directory.Delete (dir, true);
432 Directory.Delete (dir2, true);
433 DeleteFile (dir_foo);
434 DeleteFile (dir2_foo);
437 [Test]
438 public void TestOpen ()
440 string path = "";
441 FileStream stream = null;
442 try {
443 path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
444 if (!File.Exists (path))
445 stream = File.Create (path);
446 stream.Close ();
447 stream = File.Open (path, FileMode.Open);
448 stream.Close ();
449 } catch (Exception e) {
450 Fail ("Unable to open " + TempFolder + Path.DirectorySeparatorChar + "AFile.txt: e=" + e.ToString());
451 } finally {
452 if (stream != null)
453 stream.Close ();
454 DeleteFile (path);
457 path = "";
458 stream = null;
459 /* Exception tests */
460 try {
461 path = TempFolder + Path.DirectorySeparatorChar + "filedoesnotexist";
462 stream = File.Open (path, FileMode.Open);
463 Fail ("File 'filedoesnotexist' should not exist");
464 } catch (FileNotFoundException) {
465 // do nothing, this is what we expect
466 } catch (Exception e) {
467 Fail ("Unexpect exception caught: e=" + e.ToString());
468 } finally {
469 if (stream != null)
470 stream.Close ();
471 DeleteFile (path);
475 [Test]
476 public void Open ()
478 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
479 if (!File.Exists (path))
480 File.Create (path).Close ();
481 FileStream stream = null;
482 try {
484 stream = File.Open (path, FileMode.Open);
486 Assertion.AssertEquals ("test#01", true, stream.CanRead);
487 Assertion.AssertEquals ("test#02", true, stream.CanSeek);
488 Assertion.AssertEquals ("test#03", true, stream.CanWrite);
489 stream.Close ();
491 stream = File.Open (path, FileMode.Open, FileAccess.Write);
492 Assertion.AssertEquals ("test#04", false, stream.CanRead);
493 Assertion.AssertEquals ("test#05", true, stream.CanSeek);
494 Assertion.AssertEquals ("test#06", true, stream.CanWrite);
495 stream.Close ();
497 stream = File.Open (path, FileMode.Open, FileAccess.Read);
498 Assertion.AssertEquals ("test#04", true, stream.CanRead);
499 Assertion.AssertEquals ("test#05", true, stream.CanSeek);
500 Assertion.AssertEquals ("test#06", false, stream.CanWrite);
501 stream.Close ();
503 } finally {
504 if (stream != null)
505 stream.Close ();
506 DeleteFile (path);
510 [Test]
511 [ExpectedException(typeof(ArgumentException))]
512 public void OpenException1 ()
514 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
515 FileStream stream = null;
516 // CreateNew + Read throws an exceptoin
517 try {
518 stream = File.Open (TempFolder + Path.DirectorySeparatorChar + "AFile.txt", FileMode.CreateNew, FileAccess.Read);
519 } finally {
520 if (stream != null)
521 stream.Close ();
522 DeleteFile (path);
526 [Test]
527 [ExpectedException(typeof(ArgumentException))]
528 public void OpenException2 ()
530 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
531 FileStream s = null;
532 // Append + Read throws an exceptoin
533 if (!File.Exists (path))
534 File.Create (path).Close ();
535 try {
536 s = File.Open (path, FileMode.Append, FileAccess.Read);
537 } finally {
538 if (s != null)
539 s.Close ();
540 DeleteFile (path);
544 [Test]
545 public void OpenRead ()
547 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
548 if (!File.Exists (path))
549 File.Create (path).Close ();
550 FileStream stream = null;
552 try {
553 stream = File.OpenRead (path);
555 Assertion.AssertEquals ("test#01", true, stream.CanRead);
556 Assertion.AssertEquals ("test#02", true, stream.CanSeek);
557 Assertion.AssertEquals ("test#03", false, stream.CanWrite);
559 } finally {
560 if (stream != null)
561 stream.Close ();
562 DeleteFile (path);
566 [Test]
567 public void OpenWrite ()
569 string path = TempFolder + Path.DirectorySeparatorChar + "AFile.txt";
570 if (!File.Exists (path))
571 File.Create (path).Close ();
572 FileStream stream = null;
574 try {
575 stream = File.OpenWrite (path);
576 Assertion.AssertEquals ("test#01", false, stream.CanRead);
577 Assertion.AssertEquals ("test#02", true, stream.CanSeek);
578 Assertion.AssertEquals ("test#03", true, stream.CanWrite);
579 stream.Close ();
580 } finally {
581 if (stream != null)
582 stream.Close ();
583 DeleteFile (path);
587 [Test]
588 public void TestGetCreationTime ()
590 string path = TempFolder + Path.DirectorySeparatorChar + "baz";
591 DeleteFile (path);
593 try {
594 File.Create (path).Close();
595 DateTime time = File.GetCreationTime (path);
596 Assert ("GetCreationTime incorrect", (DateTime.Now - time).TotalSeconds < 10);
597 } finally {
598 DeleteFile (path);
602 [Test]
603 [ExpectedException(typeof(IOException))]
604 public void TestGetCreationTimeException ()
606 // Test nonexistent files
607 string path2 = TempFolder + Path.DirectorySeparatorChar + "filedoesnotexist";
608 DeleteFile (path2);
609 // should throw an exception
610 File.GetCreationTime (path2);
615 [Test]
616 public void CreationTime ()
618 string path = TempFolder + Path.DirectorySeparatorChar + "creationTime";
619 if (File.Exists (path))
620 File.Delete (path);
621 FileStream stream = null;
623 try {
624 stream = File.Create (path);
625 stream.Close ();
627 File.SetCreationTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
628 DateTime time = File.GetCreationTime (path);
629 Assertion.AssertEquals ("test#01", 2002, time.Year);
630 Assertion.AssertEquals ("test#02", 4, time.Month);
631 Assertion.AssertEquals ("test#03", 6, time.Day);
632 Assertion.AssertEquals ("test#04", 4, time.Hour);
633 Assertion.AssertEquals ("test#05", 4, time.Second);
635 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetCreationTimeUtc (path));
636 Assertion.AssertEquals ("test#06", 2002, time.Year);
637 Assertion.AssertEquals ("test#07", 4, time.Month);
638 Assertion.AssertEquals ("test#08", 6, time.Day);
639 Assertion.AssertEquals ("test#09", 4, time.Hour);
640 Assertion.AssertEquals ("test#10", 4, time.Second);
642 File.SetCreationTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
643 time = File.GetCreationTimeUtc (path);
644 Assertion.AssertEquals ("test#11", 2002, time.Year);
645 Assertion.AssertEquals ("test#12", 4, time.Month);
646 Assertion.AssertEquals ("test#13", 6, time.Day);
647 Assertion.AssertEquals ("test#14", 4, time.Hour);
648 Assertion.AssertEquals ("test#15", 4, time.Second);
650 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetCreationTime (path));
651 Assertion.AssertEquals ("test#16", 2002, time.Year);
652 Assertion.AssertEquals ("test#17", 4, time.Month);
653 Assertion.AssertEquals ("test#18", 6, time.Day);
654 Assertion.AssertEquals ("test#19", 4, time.Hour);
655 Assertion.AssertEquals ("test#20", 4, time.Second);
656 } finally {
657 if (stream != null)
658 stream.Close ();
659 DeleteFile (path);
663 [Test]
664 public void LastAccessTime ()
666 string path = TempFolder + Path.DirectorySeparatorChar + "lastAccessTime";
667 if (File.Exists (path))
668 File.Delete (path);
669 FileStream stream = null;
670 try {
671 stream = File.Create (path);
672 stream.Close ();
674 File.SetLastAccessTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
675 DateTime time = File.GetLastAccessTime (path);
676 Assertion.AssertEquals ("test#01", 2002, time.Year);
677 Assertion.AssertEquals ("test#02", 4, time.Month);
678 Assertion.AssertEquals ("test#03", 6, time.Day);
679 Assertion.AssertEquals ("test#04", 4, time.Hour);
680 Assertion.AssertEquals ("test#05", 4, time.Second);
682 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastAccessTimeUtc (path));
683 Assertion.AssertEquals ("test#06", 2002, time.Year);
684 Assertion.AssertEquals ("test#07", 4, time.Month);
685 Assertion.AssertEquals ("test#08", 6, time.Day);
686 Assertion.AssertEquals ("test#09", 4, time.Hour);
687 Assertion.AssertEquals ("test#10", 4, time.Second);
689 File.SetLastAccessTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
690 time = File.GetLastAccessTimeUtc (path);
691 Assertion.AssertEquals ("test#11", 2002, time.Year);
692 Assertion.AssertEquals ("test#12", 4, time.Month);
693 Assertion.AssertEquals ("test#13", 6, time.Day);
694 Assertion.AssertEquals ("test#14", 4, time.Hour);
695 Assertion.AssertEquals ("test#15", 4, time.Second);
697 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastAccessTime (path));
698 Assertion.AssertEquals ("test#16", 2002, time.Year);
699 Assertion.AssertEquals ("test#17", 4, time.Month);
700 Assertion.AssertEquals ("test#18", 6, time.Day);
701 Assertion.AssertEquals ("test#19", 4, time.Hour);
702 Assertion.AssertEquals ("test#20", 4, time.Second);
703 } finally {
704 if (stream != null)
705 stream.Close ();
706 DeleteFile (path);
710 [Test]
711 public void LastWriteTime ()
713 string path = TempFolder + Path.DirectorySeparatorChar + "lastWriteTime";
714 if (File.Exists (path))
715 File.Delete (path);
716 FileStream stream = null;
717 try {
718 stream = File.Create (path);
719 stream.Close ();
721 File.SetLastWriteTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
722 DateTime time = File.GetLastWriteTime (path);
723 Assertion.AssertEquals ("test#01", 2002, time.Year);
724 Assertion.AssertEquals ("test#02", 4, time.Month);
725 Assertion.AssertEquals ("test#03", 6, time.Day);
726 Assertion.AssertEquals ("test#04", 4, time.Hour);
727 Assertion.AssertEquals ("test#05", 4, time.Second);
729 time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastWriteTimeUtc (path));
730 Assertion.AssertEquals ("test#06", 2002, time.Year);
731 Assertion.AssertEquals ("test#07", 4, time.Month);
732 Assertion.AssertEquals ("test#08", 6, time.Day);
733 Assertion.AssertEquals ("test#09", 4, time.Hour);
734 Assertion.AssertEquals ("test#10", 4, time.Second);
736 File.SetLastWriteTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
737 time = File.GetLastWriteTimeUtc (path);
738 Assertion.AssertEquals ("test#11", 2002, time.Year);
739 Assertion.AssertEquals ("test#12", 4, time.Month);
740 Assertion.AssertEquals ("test#13", 6, time.Day);
741 Assertion.AssertEquals ("test#14", 4, time.Hour);
742 Assertion.AssertEquals ("test#15", 4, time.Second);
744 time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastWriteTime (path));
745 Assertion.AssertEquals ("test#16", 2002, time.Year);
746 Assertion.AssertEquals ("test#17", 4, time.Month);
747 Assertion.AssertEquals ("test#18", 6, time.Day);
748 Assertion.AssertEquals ("test#19", 4, time.Hour);
749 Assertion.AssertEquals ("test#20", 4, time.Second);
750 } finally {
751 if (stream != null)
752 stream.Close ();
753 DeleteFile (path);
757 [Test]
758 [ExpectedException(typeof(ArgumentNullException))]
759 public void GetCreationTimeException1 ()
761 File.GetCreationTime (null as string);
764 [Test]
765 [ExpectedException(typeof(ArgumentException))]
766 public void GetCreationTimeException2 ()
768 File.GetCreationTime ("");
771 [Test]
772 [ExpectedException(typeof(IOException))]
773 public void GetCreationTimeException3 ()
775 string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeException3";
776 DeleteFile (path);
777 File.GetCreationTime (path);
780 [Test]
781 [ExpectedException(typeof(ArgumentException))]
782 public void GetCreationTimeException4 ()
784 File.GetCreationTime (" ");
787 [Test]
788 [ExpectedException(typeof(ArgumentException))]
789 public void GetCreationTimeException5 ()
791 File.GetCreationTime (Path.InvalidPathChars [0].ToString ());
794 [Test]
795 [ExpectedException(typeof(ArgumentNullException))]
796 public void GetCreationTimeUtcException1 ()
798 File.GetCreationTimeUtc (null as string);
801 [Test]
802 [ExpectedException(typeof(ArgumentException))]
803 public void GetCreationTimeUtcException2 ()
805 File.GetCreationTimeUtc ("");
808 [Test]
809 [ExpectedException(typeof(IOException))]
810 public void GetCreationTimeUtcException3 ()
812 string path = TempFolder + Path.DirectorySeparatorChar + "GetCreationTimeUtcException3";
813 DeleteFile (path);
814 File.GetCreationTimeUtc (path);
817 [Test]
818 [ExpectedException(typeof(ArgumentException))]
819 public void GetCreationTimeUtcException4 ()
821 File.GetCreationTimeUtc (" ");
824 [Test]
825 [ExpectedException(typeof(ArgumentException))]
826 public void GetCreationTimeUtcException5 ()
828 File.GetCreationTime (Path.InvalidPathChars [0].ToString ());
831 [Test]
832 [ExpectedException(typeof(ArgumentNullException))]
833 public void GetLastAccessTimeException1 ()
835 File.GetLastAccessTime (null as string);
838 [Test]
839 [ExpectedException(typeof(ArgumentException))]
840 public void GetLastAccessTimeException2 ()
842 File.GetLastAccessTime ("");
845 [Test]
846 [ExpectedException(typeof(IOException))]
847 public void GetLastAccessTimeException3 ()
849 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeException3";
850 DeleteFile (path);
851 File.GetLastAccessTime (path);
854 [Test]
855 [ExpectedException(typeof(ArgumentException))]
856 public void GetLastAccessTimeException4 ()
858 File.GetLastAccessTime (" ");
861 [Test]
862 [ExpectedException(typeof(ArgumentException))]
863 public void GetLastAccessTimeException5 ()
865 File.GetLastAccessTime (Path.InvalidPathChars [0].ToString ());
868 [Test]
869 [ExpectedException(typeof(ArgumentNullException))]
870 public void GetLastAccessTimeUtcException1 ()
872 File.GetLastAccessTimeUtc (null as string);
875 [Test]
876 [ExpectedException(typeof(ArgumentException))]
877 public void GetLastAccessTimeUtcException2 ()
879 File.GetLastAccessTimeUtc ("");
882 [Test]
883 [ExpectedException(typeof(IOException))]
884 public void GetLastAccessTimeUtcException3 ()
886 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
887 DeleteFile (path);
888 File.GetLastAccessTimeUtc (path);
891 [Test]
892 [ExpectedException(typeof(ArgumentException))]
893 public void GetLastAccessTimeUtcException4 ()
895 File.GetLastAccessTimeUtc (" ");
898 [Test]
899 [ExpectedException(typeof(ArgumentException))]
900 public void GetLastAccessTimeUtcException5 ()
902 File.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ());
905 [Test]
906 [ExpectedException(typeof(ArgumentNullException))]
907 public void GetLastWriteTimeException1 ()
909 File.GetLastWriteTime (null as string);
912 [Test]
913 [ExpectedException(typeof(ArgumentException))]
914 public void GetLastWriteTimeException2 ()
916 File.GetLastWriteTime ("");
919 [Test]
920 [ExpectedException(typeof(IOException))]
921 public void GetLastWriteTimeException3 ()
923 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
924 DeleteFile (path);
925 File.GetLastWriteTime (path);
928 [Test]
929 [ExpectedException(typeof(ArgumentException))]
930 public void GetLastWriteTimeException4 ()
932 File.GetLastWriteTime (" ");
935 [Test]
936 [ExpectedException(typeof(ArgumentException))]
937 public void GetLastWriteTimeException5 ()
939 File.GetLastWriteTime (Path.InvalidPathChars [0].ToString ());
942 [Test]
943 [ExpectedException(typeof(ArgumentNullException))]
944 public void GetLastWriteTimeUtcException1 ()
946 File.GetLastWriteTimeUtc (null as string);
949 [Test]
950 [ExpectedException(typeof(ArgumentException))]
951 public void GetLastWriteTimeUtcException2 ()
953 File.GetLastAccessTimeUtc ("");
956 [Test]
957 [ExpectedException(typeof(IOException))]
958 public void GetLastWriteTimeUtcException3 ()
960 string path = TempFolder + Path.DirectorySeparatorChar + "GetLastWriteTimeUtcException3";
961 DeleteFile (path);
962 File.GetLastAccessTimeUtc (path);
965 [Test]
966 [ExpectedException(typeof(ArgumentException))]
967 public void GetLastWriteTimeUtcException4 ()
969 File.GetLastAccessTimeUtc (" ");
972 [Test]
973 [ExpectedException(typeof(ArgumentException))]
974 public void GetLastWriteTimeUtcException5 ()
976 File.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ());
979 [Test]
980 [ExpectedException(typeof(IOException))]
981 public void FileStreamCloseException ()
983 string path = TempFolder + Path.DirectorySeparatorChar + "FileStreamCloseException";
984 DeleteFile (path);
985 FileStream stream = null;
986 try {
987 stream = File.Create (path);
988 File.Delete (path);
989 } finally {
990 if (stream != null)
991 stream.Close ();
992 DeleteFile (path);
996 [Test]
997 public void FileStreamClose ()
999 string path = TempFolder + Path.DirectorySeparatorChar + "FileStreamClose";
1000 FileStream stream = null;
1001 try {
1002 stream = File.Create (path);
1003 stream.Close ();
1004 File.Delete (path);
1005 } finally {
1006 if (stream != null)
1007 stream.Close ();
1008 DeleteFile (path);
1012 // SetCreationTime and SetCreationTimeUtc exceptions
1014 [Test]
1015 [ExpectedException(typeof (ArgumentNullException))]
1016 public void SetCreationTimeArgumentNullException1 ()
1018 File.SetCreationTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1021 [Test]
1022 [ExpectedException(typeof (ArgumentException))]
1023 public void SetCreationTimeArgumenException1 ()
1025 File.SetCreationTime ("", new DateTime (2000, 12, 12, 11, 59, 59));
1028 [Test]
1029 [ExpectedException(typeof (ArgumentException))]
1030 public void SetCreationTimeArgumenException2 ()
1032 File.SetCreationTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1035 [Test]
1036 [ExpectedException(typeof (ArgumentException))]
1037 public void SetCreationTimeArgumenException3 ()
1039 File.SetCreationTime (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
1042 [Test]
1043 [ExpectedException(typeof (FileNotFoundException))]
1044 public void SetCreationTimeFileNotFoundException1 ()
1046 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeFileNotFoundException1";
1047 DeleteFile (path);
1049 File.SetCreationTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
1052 // [Test]
1053 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1054 // public void SetCreationTimeArgumentOutOfRangeException1 ()
1055 // {
1056 // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeArgumentOutOfRangeException1";
1057 // FileStream stream = null;
1058 // DeleteFile (path);
1059 // try {
1060 // stream = File.Create (path);
1061 // stream.Close ();
1062 // File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1063 // } finally {
1064 // if (stream != null)
1065 // stream.Close ();
1066 // DeleteFile (path);
1067 // }
1068 // }
1070 [Test]
1071 [ExpectedException(typeof (IOException))]
1072 public void SetCreationTimeIOException1 ()
1074 string path = TempFolder + Path.DirectorySeparatorChar + "CreationTimeIOException1";
1075 DeleteFile (path);
1076 FileStream stream = null;
1077 try {
1078 stream = File.Create (path);
1079 File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1080 } finally {
1081 if (stream != null)
1082 stream.Close ();
1083 DeleteFile (path);
1087 [Test]
1088 [ExpectedException(typeof (ArgumentNullException))]
1089 public void SetCreationTimeUtcArgumentNullException1 ()
1091 File.SetCreationTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1094 [Test]
1095 [ExpectedException(typeof (ArgumentException))]
1096 public void SetCreationTimeUtcArgumenException1 ()
1098 File.SetCreationTimeUtc ("", new DateTime (2000, 12, 12, 11, 59, 59));
1101 [Test]
1102 [ExpectedException(typeof (ArgumentException))]
1103 public void SetCreationTimeUtcArgumenException2 ()
1105 File.SetCreationTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1108 [Test]
1109 [ExpectedException(typeof (ArgumentException))]
1110 public void SetCreationTimeUtcArgumenException3 ()
1112 File.SetCreationTimeUtc (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
1115 [Test]
1116 [ExpectedException(typeof (FileNotFoundException))]
1117 public void SetCreationTimeUtcFileNotFoundException1 ()
1119 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcFileNotFoundException1";
1120 DeleteFile (path);
1122 File.SetCreationTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
1125 // [Test]
1126 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1127 // public void SetCreationTimeUtcArgumentOutOfRangeException1 ()
1128 // {
1129 // string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcArgumentOutOfRangeException1";
1130 // DeleteFile (path);
1131 // FileStream stream = null;
1132 // try {
1133 // stream = File.Create (path);
1134 // stream.Close ();
1135 // File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1136 // } finally {
1137 // if (stream != null)
1138 // stream.Close();
1139 // DeleteFile (path);
1140 // }
1141 // }
1143 [Test]
1144 [ExpectedException(typeof (IOException))]
1145 public void SetCreationTimeUtcIOException1 ()
1147 string path = TempFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcIOException1";
1148 DeleteFile (path);
1149 FileStream stream = null;
1150 try {
1151 stream = File.Create (path);
1152 File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1153 } finally {
1154 if (stream != null)
1155 stream.Close ();
1156 DeleteFile (path);
1160 // SetLastAccessTime and SetLastAccessTimeUtc exceptions
1162 [Test]
1163 [ExpectedException(typeof (ArgumentNullException))]
1164 public void SetLastAccessTimeArgumentNullException1 ()
1166 File.SetLastAccessTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1169 [Test]
1170 [ExpectedException(typeof (ArgumentException))]
1171 public void SetLastAccessTimeArgumenException1 ()
1173 File.SetLastAccessTime ("", new DateTime (2000, 12, 12, 11, 59, 59));
1176 [Test]
1177 [ExpectedException(typeof (ArgumentException))]
1178 public void SetLastAccessTimeArgumenException2 ()
1180 File.SetLastAccessTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1183 [Test]
1184 [ExpectedException(typeof (ArgumentException))]
1185 public void SetLastAccessTimeArgumenException3 ()
1187 File.SetLastAccessTime (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
1190 [Test]
1191 [ExpectedException(typeof (FileNotFoundException))]
1192 public void SetLastAccessTimeFileNotFoundException1 ()
1194 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeFileNotFoundException1";
1195 DeleteFile (path);
1197 File.SetLastAccessTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
1200 // [Test]
1201 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1202 // public void SetLastAccessTimeArgumentOutOfRangeException1 ()
1203 // {
1204 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastTimeArgumentOutOfRangeException1";
1205 // DeleteFile (path);
1206 // FileStream stream = null;
1207 // try {
1208 // stream = File.Create (path);
1209 // stream.Close ();
1210 // File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1211 // } finally {
1212 // if (stream != null)
1213 // stream.Close ();
1214 // DeleteFile (path);
1215 // }
1216 // }
1218 [Test]
1219 [ExpectedException(typeof (IOException))]
1220 public void SetLastAccessTimeIOException1 ()
1222 string path = TempFolder + Path.DirectorySeparatorChar + "LastAccessIOException1";
1223 DeleteFile (path);
1224 FileStream stream = null;
1225 try {
1226 stream = File.Create (path);
1227 File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1228 } finally {
1229 if (stream != null)
1230 stream.Close ();
1231 DeleteFile (path);
1235 [Test]
1236 [ExpectedException(typeof (ArgumentNullException))]
1237 public void SetLastAccessTimeUtcArgumentNullException1 ()
1239 File.SetLastAccessTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1242 [Test]
1243 [ExpectedException(typeof (ArgumentException))]
1244 public void SetCLastAccessTimeUtcArgumenException1 ()
1246 File.SetLastAccessTimeUtc ("", new DateTime (2000, 12, 12, 11, 59, 59));
1249 [Test]
1250 [ExpectedException(typeof (ArgumentException))]
1251 public void SetLastAccessTimeUtcArgumenException2 ()
1253 File.SetLastAccessTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1256 [Test]
1257 [ExpectedException(typeof (ArgumentException))]
1258 public void SetLastAccessTimeUtcArgumenException3 ()
1260 File.SetLastAccessTimeUtc (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
1263 [Test]
1264 [ExpectedException(typeof (FileNotFoundException))]
1265 public void SetLastAccessTimeUtcFileNotFoundException1 ()
1267 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcFileNotFoundException1";
1268 DeleteFile (path);
1270 File.SetLastAccessTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
1273 // [Test]
1274 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1275 // public void SetLastAccessTimeUtcArgumentOutOfRangeException1 ()
1276 // {
1277 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcArgumentOutOfRangeException1";
1278 // DeleteFile (path);
1279 // FileStream stream = null;
1280 // try {
1281 // stream = File.Create (path);
1282 // stream.Close ();
1283 // File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1284 // } finally {
1285 // if (stream != null)
1286 // stream.Close ();
1287 // DeleteFile (path);
1288 // }
1289 // }
1291 [Test]
1292 [ExpectedException(typeof (IOException))]
1293 public void SetLastAccessTimeUtcIOException1 ()
1295 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcIOException1";
1296 DeleteFile (path);
1297 FileStream stream = null;
1298 try {
1299 stream = File.Create (path);
1300 File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1301 } finally {
1302 if (stream != null)
1303 stream.Close ();
1304 DeleteFile (path);
1308 // SetLastWriteTime and SetLastWriteTimeUtc exceptions
1310 [Test]
1311 [ExpectedException(typeof (ArgumentNullException))]
1312 public void SetLastWriteTimeArgumentNullException1 ()
1314 File.SetLastWriteTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1317 [Test]
1318 [ExpectedException(typeof (ArgumentException))]
1319 public void SetLastWriteTimeArgumenException1 ()
1321 File.SetLastWriteTime ("", new DateTime (2000, 12, 12, 11, 59, 59));
1324 [Test]
1325 [ExpectedException(typeof (ArgumentException))]
1326 public void SetLastWriteTimeArgumenException2 ()
1328 File.SetLastWriteTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1331 [Test]
1332 [ExpectedException(typeof (ArgumentException))]
1333 public void SetLastWriteTimeArgumenException3 ()
1335 File.SetLastWriteTime (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
1338 [Test]
1339 [ExpectedException(typeof (FileNotFoundException))]
1340 public void SetLastWriteTimeFileNotFoundException1 ()
1342 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeFileNotFoundException1";
1343 DeleteFile (path);
1345 File.SetLastWriteTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
1348 // [Test]
1349 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1350 // public void SetLastWriteTimeArgumentOutOfRangeException1 ()
1351 // {
1352 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeArgumentOutOfRangeException1";
1353 // DeleteFile (path);
1354 // FileStream stream = null;
1355 // try {
1356 // stream = File.Create (path);
1357 // stream.Close ();
1358 // File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1359 // } finally {
1360 // if (stream != null)
1361 // stream.Close ();
1362 // DeleteFile (path);
1363 // }
1364 // }
1366 [Test]
1367 [ExpectedException(typeof (IOException))]
1368 public void SetLastWriteTimeIOException1 ()
1370 string path = TempFolder + Path.DirectorySeparatorChar + "LastWriteTimeIOException1";
1371 DeleteFile (path);
1372 FileStream stream = null;
1373 try {
1374 stream = File.Create (path);
1375 File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
1376 } finally {
1377 if (stream != null)
1378 stream.Close ();
1379 DeleteFile (path);
1383 [Test]
1384 [ExpectedException(typeof (ArgumentNullException))]
1385 public void SetLastWriteTimeUtcArgumentNullException1 ()
1387 File.SetLastWriteTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
1390 [Test]
1391 [ExpectedException(typeof (ArgumentException))]
1392 public void SetCLastWriteTimeUtcArgumenException1 ()
1394 File.SetLastWriteTimeUtc ("", new DateTime (2000, 12, 12, 11, 59, 59));
1397 [Test]
1398 [ExpectedException(typeof (ArgumentException))]
1399 public void SetLastWriteTimeUtcArgumenException2 ()
1401 File.SetLastWriteTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
1404 [Test]
1405 [ExpectedException(typeof (ArgumentException))]
1406 public void SetLastWriteTimeUtcArgumenException3 ()
1408 File.SetLastWriteTimeUtc (Path.InvalidPathChars [1].ToString (), new DateTime (2000, 12, 12, 11, 59, 59));
1411 [Test]
1412 [ExpectedException(typeof (FileNotFoundException))]
1413 public void SetLastWriteTimeUtcFileNotFoundException1 ()
1415 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcFileNotFoundException1";
1416 DeleteFile (path);
1418 File.SetLastAccessTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
1421 // [Test]
1422 // [ExpectedException(typeof (ArgumentOutOfRangeException))]
1423 // public void SetLastWriteTimeUtcArgumentOutOfRangeException1 ()
1424 // {
1425 // string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcArgumentOutOfRangeException1";
1426 // DeleteFile (path);
1427 // FileStream stream = null;
1428 // try {
1429 // stream = File.Create (path);
1430 // stream.Close ();
1431 // File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1432 // } finally {
1433 // if (stream != null)
1434 // stream.Close ();
1435 // DeleteFile (path);
1436 // }
1437 // }
1439 [Test]
1440 [ExpectedException(typeof (IOException))]
1441 public void SetLastWriteTimeUtcIOException1 ()
1443 string path = TempFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcIOException1";
1444 DeleteFile (path);
1445 FileStream stream = null;
1446 try {
1447 stream = File.Create (path);
1448 File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
1449 } finally {
1450 if (stream != null)
1451 stream.Close ();
1452 DeleteFile (path);
1456 private void DeleteFile (string path)
1458 if (File.Exists (path))
1459 File.Delete (path);