Port FileStreamTest to TARGET_JVM.
[mono.git] / mcs / class / corlib / Test / System.IO / FileStreamTest.cs
index 0d17522babe4b96fdd4c898c8eafe9eddabee8a1..f39861c86a6bafb9ae4382615e10361f2b41efc0 100644 (file)
@@ -71,15 +71,44 @@ namespace MonoTests.System.IO
                }
 
                [Test]
-               [ExpectedException (typeof (FileNotFoundException))]
-               public void CtorFileNotFoundException1 ()
+               public void CtorFileNotFoundException_Mode_Open ()
                {
+                       // absolute path
                        string path = TempFolder + DSC + "thisfileshouldnotexists.test";
                        DeleteFile (path);
                        FileStream stream = null;
                        try {
                                stream = new FileStream (TempFolder + DSC + "thisfileshouldnotexists.test", FileMode.Open);
+                               Assert.Fail ("#A1");
+                       } catch (FileNotFoundException ex) {
+                               Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#A2");
+                               Assert.AreEqual (path, ex.FileName, "#A3");
+                               Assert.IsNull (ex.InnerException, "#A4");
+                               Assert.IsNotNull (ex.Message, "#A5");
+                               Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#A6");
                        } finally {
+                               if (stream != null) {
+                                       stream.Close ();
+                                       stream = null;
+                               }
+                               DeleteFile (path);
+                       }
+
+                       // relative path
+                       string orignalCurrentDir = Directory.GetCurrentDirectory ();
+                       Directory.SetCurrentDirectory (TempFolder);
+                       try {
+                               stream = new FileStream ("thisfileshouldnotexists.test", FileMode.Open);
+                               Assert.Fail ("#B1");
+                       } catch (FileNotFoundException ex) {
+                               Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#B2");
+                               Assert.AreEqual (path, ex.FileName, "#B3");
+                               Assert.IsNull (ex.InnerException, "#B4");
+                               Assert.IsNotNull (ex.Message, "#B5");
+                               Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#B6");
+                       } finally {
+                               // restore original current directory
+                               Directory.SetCurrentDirectory (orignalCurrentDir);
                                if (stream != null)
                                        stream.Close ();
                                DeleteFile (path);
@@ -87,27 +116,54 @@ namespace MonoTests.System.IO
                }
 
                [Test]
-               [ExpectedException (typeof (FileNotFoundException))]
-               public void CtorFileNotFoundException2 ()
+               public void CtorFileNotFoundException_Mode_Truncate ()
                {
+                       // absolute path
                        string path = TempFolder + DSC + "thisfileshouldNOTexists.test";
                        DeleteFile (path);
                        FileStream stream = null;
+                       try {
+                               stream = new FileStream (path, FileMode.Truncate);
+                               Assert.Fail ("#A1");
+                       } catch (FileNotFoundException ex) {
+                               Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#A2");
+                               Assert.AreEqual (path, ex.FileName, "#A3");
+                               Assert.IsNull (ex.InnerException, "#A4");
+                               Assert.IsNotNull (ex.Message, "#A5");
+                               Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#A6");
+                       } finally {
+                               if (stream != null) {
+                                       stream.Close ();
+                                       stream = null;
+                               }
+                               DeleteFile (path);
+                       }
 
+                       // relative path
+                       string orignalCurrentDir = Directory.GetCurrentDirectory ();
+                       Directory.SetCurrentDirectory (TempFolder);
                        try {
-                               stream = new FileStream (TempFolder + DSC + "thisfileshouldNOTexists.test", FileMode.Truncate);
+                               stream = new FileStream ("thisfileshouldNOTexists.test", FileMode.Truncate);
+                               Assert.Fail ("#B1");
+                       } catch (FileNotFoundException ex) {
+                               Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#B2");
+                               Assert.AreEqual (path, ex.FileName, "#B3");
+                               Assert.IsNull (ex.InnerException, "#B4");
+                               Assert.IsNotNull (ex.Message, "#B5");
+                               Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#B6");
                        } finally {
+                               // restore original current directory
+                               Directory.SetCurrentDirectory (orignalCurrentDir);
                                if (stream != null)
                                        stream.Close ();
-
                                DeleteFile (path);
                        }
                }
 
                [Test]
-               [ExpectedException (typeof (IOException))]
                public void CtorIOException1 ()
                {
+                       // absolute path
                        string path = TempFolder + DSC + "thisfileshouldexists.test";
                        FileStream stream = null;
                        DeleteFile (path);
@@ -116,13 +172,41 @@ namespace MonoTests.System.IO
                                stream.Close ();
                                stream = null;
                                stream = new FileStream (path, FileMode.CreateNew);
+                               Assert.Fail ("#A1");
+                       } catch (IOException ex) {
+                               Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
+                               Assert.IsNull (ex.InnerException, "#A3");
+                               Assert.IsNotNull (ex.Message, "#A4");
+                               Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#A5");
                        } finally {
+                               if (stream != null) {
+                                       stream.Close ();
+                                       stream = null;
+                               }
+                               DeleteFile (path);
+                       }
 
+                       // relative path
+                       string orignalCurrentDir = Directory.GetCurrentDirectory ();
+                       Directory.SetCurrentDirectory (TempFolder);
+                       try {
+                               stream = new FileStream ("thisfileshouldexists.test", FileMode.CreateNew);
+                               stream.Close ();
+                               stream = null;
+                               stream = new FileStream ("thisfileshouldexists.test", FileMode.CreateNew);
+                               Assert.Fail ("#B1");
+                       } catch (IOException ex) {
+                               Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
+                               Assert.IsNull (ex.InnerException, "#B3");
+                               Assert.IsNotNull (ex.Message, "#B4");
+                               Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#B5");
+                       } finally {
+                               // restore original current directory
+                               Directory.SetCurrentDirectory (orignalCurrentDir);
                                if (stream != null)
                                        stream.Close ();
                                DeleteFile (path);
                        }
-
                }
 
                [Test]
@@ -218,6 +302,27 @@ namespace MonoTests.System.IO
                        CtorDirectoryNotFoundException (FileMode.Append);
                }
 
+               [Test]
+               public void CtorDirectoryNotFound_RelativePath ()
+               {
+                       string orignalCurrentDir = Directory.GetCurrentDirectory ();
+                       Directory.SetCurrentDirectory (TempFolder);
+                       string relativePath = "DirectoryDoesNotExist" + Path.DirectorySeparatorChar + "file.txt";
+                       string fullPath = Path.Combine (TempFolder, relativePath);
+                       try {
+                               new FileStream (relativePath, FileMode.Open);
+                               Assert.Fail ("#A1");
+                       } catch (DirectoryNotFoundException ex) {
+                               Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#A2");
+                               Assert.IsNull (ex.InnerException, "#A3");
+                               Assert.IsNotNull (ex.Message, "#A4");
+                               Assert.IsTrue (ex.Message.IndexOf (fullPath) != -1, "#A5");
+                       } finally {
+                               // restore original current directory
+                               Directory.SetCurrentDirectory (orignalCurrentDir);
+                       }
+               }
+
                [Test]
 #if NET_2_0
                // FileShare.Inheritable is ignored, but file does not exist
@@ -465,6 +570,10 @@ namespace MonoTests.System.IO
                        } catch (FileNotFoundException ex) {
                                // make sure it is exact this exception, and not a derived type
                                Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#E2");
+                               Assert.AreEqual (path, ex.FileName, "#E3");
+                               Assert.IsNull (ex.InnerException, "#E4");
+                               Assert.IsNotNull (ex.Message, "#E5");
+                               Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#E6");
                        } finally {
                                if (stream != null)
                                        stream.Close ();
@@ -479,6 +588,10 @@ namespace MonoTests.System.IO
                        } catch (FileNotFoundException ex) {
                                // make sure it is exact this exception, and not a derived type
                                Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#F2");
+                               Assert.AreEqual (path, ex.FileName, "#F3");
+                               Assert.IsNull (ex.InnerException, "#F4");
+                               Assert.IsNotNull (ex.Message, "#F5");
+                               Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#F6");
                        } finally {
                                if (stream != null)
                                        stream.Close ();
@@ -493,6 +606,10 @@ namespace MonoTests.System.IO
                        } catch (FileNotFoundException ex) {
                                // make sure it is exact this exception, and not a derived type
                                Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#G2");
+                               Assert.AreEqual (path, ex.FileName, "#G3");
+                               Assert.IsNull (ex.InnerException, "#G4");
+                               Assert.IsNotNull (ex.Message, "#G5");
+                               Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#G6");
                        } finally {
                                if (stream != null)
                                        stream.Close ();
@@ -551,6 +668,10 @@ namespace MonoTests.System.IO
                        } catch (FileNotFoundException ex) {
                                // make sure it is exact this exception, and not a derived type
                                Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#I2");
+                               Assert.AreEqual (path, ex.FileName, "#I3");
+                               Assert.IsNull (ex.InnerException, "#I4");
+                               Assert.IsNotNull (ex.Message, "#I5");
+                               Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#I6");
                        } finally {
                                if (stream != null)
                                        stream.Close ();
@@ -565,6 +686,10 @@ namespace MonoTests.System.IO
                        } catch (FileNotFoundException ex) {
                                // make sure it is exact this exception, and not a derived type
                                Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#J2");
+                               Assert.AreEqual (path, ex.FileName, "#J3");
+                               Assert.IsNull (ex.InnerException, "#J4");
+                               Assert.IsNotNull (ex.Message, "#J5");
+                               Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#J6");
                        } finally {
                                if (stream != null)
                                        stream.Close ();
@@ -573,6 +698,7 @@ namespace MonoTests.System.IO
                        }
                }
 
+#if !TARGET_JVM // No support IntPtr file handles under TARGET_JVM
                [Test, ExpectedException (typeof (IOException))]
                public void CtorIOException2 ()
                {
@@ -584,7 +710,9 @@ namespace MonoTests.System.IO
                                        stream.Close ();
                        }
                }
+#endif // TARGET_JVM
 
+               [Category("TargetJvmNotSupported")] // File sharing not supported for TARGET_JVM
                [Test, ExpectedException (typeof (IOException))]
                public void CtorIOException ()
                {
@@ -632,41 +760,49 @@ namespace MonoTests.System.IO
                }
 
                [Test]
+               [Category("TargetJvmNotSupported")] // File sharing not supported for TARGET_JVM
                [ExpectedException (typeof (IOException))]
                public void CtorAccess1Read2Write ()
                {
                        string fn = Path.Combine (TempFolder, "temp");
-                       FileStream fs = null;
+                       FileStream fs1 = null;
+                       FileStream fs2 = null;
                        try {
                                if (!File.Exists (fn)) {
                                        using (TextWriter tw = File.CreateText (fn)) {
                                                tw.Write ("FOO");
                                        }
                                }
-                               fs = new FileStream (fn, FileMode.Open, FileAccess.Read);
-                               fs = new FileStream (fn, FileMode.Create, FileAccess.Write);
+                               fs1 = new FileStream (fn, FileMode.Open, FileAccess.Read);
+                               fs2 = new FileStream (fn, FileMode.Create, FileAccess.Write);
                        } finally {
-                               if (fs != null)
-                                       fs.Close ();
+                               if (fs1 != null)
+                                       fs1.Close ();
+                               if (fs2 != null)
+                                       fs2.Close ();
                                if (File.Exists (fn))
                                        File.Delete (fn);
                        }
                }
 
                [Test]
+               [Category("TargetJvmNotSupported")] // File sharing not supported for TARGET_JVM
                [ExpectedException (typeof (IOException))]
                public void CtorAccess1Write2Write ()
                {
                        string fn = Path.Combine (TempFolder, "temp");
-                       FileStream fs = null;
+                       FileStream fs1 = null;
+                       FileStream fs2 = null;
                        try {
                                if (File.Exists (fn))
                                        File.Delete (fn);
-                               fs = new FileStream (fn, FileMode.Create, FileAccess.Write);
-                               fs = new FileStream (fn, FileMode.Create, FileAccess.Write);
+                               fs1 = new FileStream (fn, FileMode.Create, FileAccess.Write);
+                               fs2 = new FileStream (fn, FileMode.Create, FileAccess.Write);
                        } finally {
-                               if (fs != null)
-                                       fs.Close ();
+                               if (fs1 != null)
+                                       fs1.Close ();
+                               if (fs2 != null)
+                                       fs2.Close ();
                                if (File.Exists (fn))
                                        File.Delete (fn);
                        }
@@ -847,6 +983,9 @@ namespace MonoTests.System.IO
                        DeleteFile (path);
                }
 
+#if TARGET_JVM // File locking is not implemented.
+               [Category ("NotWorking")]
+#endif
                public void TestLock ()
                {
                        string path = TempFolder + Path.DirectorySeparatorChar + "TestLock";
@@ -1135,6 +1274,7 @@ namespace MonoTests.System.IO
                        }
                }
 
+#if !TARGET_JVM // No support IntPtr file handles under TARGET_JVM
                // Check that the stream is flushed even when it doesn't own the
                // handle
                [Test]
@@ -1154,6 +1294,7 @@ namespace MonoTests.System.IO
                        Assert.AreEqual ((int) '1', s.ReadByte ());
                        s.Close ();
                }
+#endif // TARGET_JVM
 
                private void DeleteFile (string path)
                {
@@ -1269,12 +1410,14 @@ namespace MonoTests.System.IO
                        }
                }
 
+#if !TARGET_JVM // No support IntPtr file handles under TARGET_JVM
                [Test]
                [ExpectedException (typeof (ArgumentException))]
                public void Constructor_InvalidFileHandle ()
                {
                        new FileStream ((IntPtr) (-1L), FileAccess.Read);
                }
+#endif // TARGET_JVM
 
                [Test]
                public void PositionAfterSetLength ()
@@ -1313,6 +1456,7 @@ namespace MonoTests.System.IO
                }
 
                [Test]
+               [Category("TargetJvmNotSupported")] // Async IO not supported for TARGET_JVM
                [ExpectedException (typeof (ObjectDisposedException))]
                public void BeginRead_Disposed ()
                {
@@ -1324,6 +1468,7 @@ namespace MonoTests.System.IO
                }
 
                [Test]
+               [Category("TargetJvmNotSupported")] // Async IO not supported for TARGET_JVM
                [ExpectedException (typeof (ObjectDisposedException))]
                public void BeginWrite_Disposed ()
                {
@@ -1335,6 +1480,7 @@ namespace MonoTests.System.IO
                }
 
                [Test]
+               [Category("TargetJvmNotSupported")] // File locking not supported for TARGET_JVM
                [ExpectedException (typeof (ObjectDisposedException))]
                public void Lock_Disposed ()
                {
@@ -1346,6 +1492,7 @@ namespace MonoTests.System.IO
                }
 
                [Test]
+               [Category("TargetJvmNotSupported")] // File locking not supported for TARGET_JVM
                [ExpectedException (typeof (ObjectDisposedException))]
                public void Unlock_Disposed ()
                {
@@ -1409,6 +1556,7 @@ namespace MonoTests.System.IO
                }
 
 #if NET_2_0
+               [Category("TargetJvmNotSupported")] // FileOptions.DeleteOnClose not supported for TARGET_JVM
                [Test] public void DeleteOnClose ()
                {
                        string path = TempFolder + DSC + "created.txt";