From ab56dccc67ec8206da9323e23d501ad2467d2128 Mon Sep 17 00:00:00 2001 From: Gert Driesen Date: Thu, 21 Sep 2006 19:00:28 +0000 Subject: [PATCH] * corlib_test.dll.sources: Added FileNotFoundExceptionTest.cs. * Exception.cs: Marked message internal to allow derived classes to access the raw message (without changing the public API). * FileNotFoundException.cs: Changed message for default ctor to match MS. Use internal message field of Exception to check whether Message is null. On 2.0 profile, use file/assembly load failure message when no message is set and a filename was specified. On 1.0 profile, always use file/assembly load failure message when no message is set (regardless of whether a filename was specified or not). Made some cosmetic changes to ToString to have it match MS. * FileNotFoundExceptionTest.cs: Added ctor tests. svn path=/trunk/mcs/; revision=65792 --- mcs/class/corlib/ChangeLog | 4 + mcs/class/corlib/System.IO/ChangeLog | 10 + .../corlib/System.IO/FileNotFoundException.cs | 35 +- mcs/class/corlib/System/ChangeLog | 7 +- mcs/class/corlib/System/Exception.cs | 2 +- mcs/class/corlib/Test/System.IO/ChangeLog | 4 + .../Test/System.IO/FileNotFoundExceptionTest.cs | 388 +++++++++++++++++++++ mcs/class/corlib/corlib_test.dll.sources | 1 + 8 files changed, 442 insertions(+), 9 deletions(-) create mode 100644 mcs/class/corlib/Test/System.IO/FileNotFoundExceptionTest.cs diff --git a/mcs/class/corlib/ChangeLog b/mcs/class/corlib/ChangeLog index ac5b17221f7..e72fc08e3e5 100644 --- a/mcs/class/corlib/ChangeLog +++ b/mcs/class/corlib/ChangeLog @@ -1,3 +1,7 @@ +2006-09-21 Gert Driesen + + * corlib_test.dll.sources: Added FileNotFoundExceptionTest.cs. + 2006-08-20 Gert Driesen * corlib.dll.sources: Added RegistryValueOptions.cs. diff --git a/mcs/class/corlib/System.IO/ChangeLog b/mcs/class/corlib/System.IO/ChangeLog index edffd5a10c4..6d818ac427d 100644 --- a/mcs/class/corlib/System.IO/ChangeLog +++ b/mcs/class/corlib/System.IO/ChangeLog @@ -1,3 +1,13 @@ +2006-09-19 Gert Driesen + + * FileNotFoundException.cs: Changed message for default ctor to match + MS. Use internal message field of Exception to check whether Message + is null. On 2.0 profile, use file/assembly load failure message when + no message is set and a filename was specified. On 1.0 profile, + always use file/assembly load failure message when no message is set + (regardless of whether a filename was specified or not). Made some + cosmetic changes to ToString to have it match MS. + 2006-09-02 Zoltan Varga * BinaryReader.cs (Read): Avoid allocating memory when reading a char. diff --git a/mcs/class/corlib/System.IO/FileNotFoundException.cs b/mcs/class/corlib/System.IO/FileNotFoundException.cs index 8e240a61ae6..d7ce404dcbd 100644 --- a/mcs/class/corlib/System.IO/FileNotFoundException.cs +++ b/mcs/class/corlib/System.IO/FileNotFoundException.cs @@ -45,7 +45,7 @@ namespace System.IO { // Constructors public FileNotFoundException () - : base (Locale.GetText ("File not found")) + : base (Locale.GetText ("Unable to find the specified file.")) { HResult = Result; } @@ -98,9 +98,24 @@ namespace System.IO { public override string Message { get { - if (base.Message == null) - return "File not found"; - return base.Message; + if (base.message == null) { +#if NET_2_0 + if (fileName != null) { + string message = string.Format (CultureInfo.CurrentCulture, + "Could not load file or assembly '{0}' or one of" + + " its dependencies. The system cannot find the" + + " file specified.", fileName); + return message; + } +#else + string file = fileName == null ? "(null)" : fileName; + string message = string.Format (CultureInfo.CurrentCulture, + "File or assembly name {0}, or one of its dependencies," + + " was not found.", file); + return message; +#endif + } + return base.message; } } @@ -116,11 +131,17 @@ namespace System.IO { StringBuilder sb = new StringBuilder (GetType ().FullName); sb.AppendFormat (": {0}", Message); - if (fileName != null) - sb.AppendFormat (" : {0}", fileName); + if (fileName != null && fileName.Length > 0) { + sb.Append (Environment.NewLine); +#if NET_2_0 + sb.AppendFormat ("File name: '{0}'", fileName); +#else + sb.AppendFormat ("File name: \"{0}\"", fileName); +#endif + } if (this.InnerException != null) - sb.AppendFormat (" ----> {0}", InnerException); + sb.AppendFormat (" ---> {0}", InnerException); if (this.StackTrace != null) { sb.Append (Environment.NewLine); diff --git a/mcs/class/corlib/System/ChangeLog b/mcs/class/corlib/System/ChangeLog index 71e1f63d650..fb1c7203409 100644 --- a/mcs/class/corlib/System/ChangeLog +++ b/mcs/class/corlib/System/ChangeLog @@ -1,4 +1,9 @@ -2006-09-19 Gonzalo Paniagua Javier +2006-09-21 Gert Driesen + + * Exception.cs: Marked message internal to allow derived classes to + access the raw message (without changing the public API). + +2006-09-19 Gonzalo Paniagua Javier * ConsoleDriver.cs: * TermInfoDriver.cs: diff --git a/mcs/class/corlib/System/Exception.cs b/mcs/class/corlib/System/Exception.cs index c2633b6e335..15adaed837a 100644 --- a/mcs/class/corlib/System/Exception.cs +++ b/mcs/class/corlib/System/Exception.cs @@ -48,7 +48,7 @@ namespace System { IntPtr [] trace_ips; Exception inner_exception; - string message; + internal string message; string help_link; string class_name; string stack_trace; diff --git a/mcs/class/corlib/Test/System.IO/ChangeLog b/mcs/class/corlib/Test/System.IO/ChangeLog index ee76060cd4c..4756e1a4146 100644 --- a/mcs/class/corlib/Test/System.IO/ChangeLog +++ b/mcs/class/corlib/Test/System.IO/ChangeLog @@ -1,3 +1,7 @@ +2006-09-21 Gert Driesen + + * FileNotFoundExceptionTest.cs: Added ctor tests. + 2006-07-06 Dick Porter * DirectoryTest.cs: Test creating a directory when a file or diff --git a/mcs/class/corlib/Test/System.IO/FileNotFoundExceptionTest.cs b/mcs/class/corlib/Test/System.IO/FileNotFoundExceptionTest.cs new file mode 100644 index 00000000000..5e601d7e6ba --- /dev/null +++ b/mcs/class/corlib/Test/System.IO/FileNotFoundExceptionTest.cs @@ -0,0 +1,388 @@ +// +// FileNotFoundExceptionTest.cs - Unit tests for +// System.IO.FileNotFoundException +// +// Author: +// Gert Driesen +// +// Copyright (C) 2006 Novell, Inc (http://www.novell.com) +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + +using System; +using System.IO; + +using NUnit.Framework; + +namespace MonoTests.System.IO { + [TestFixture] + public class FileNotFoundExceptionTest { + [Test] + public void Constructor1 () + { + FileNotFoundException fnf = new FileNotFoundException (); + +#if NET_2_0 + Assert.IsNotNull (fnf.Data, "#1"); +#endif + Assert.IsNull (fnf.FileName, "#2"); + Assert.IsNull (fnf.InnerException, "#3"); + Assert.IsNotNull (fnf.Message, "#4"); // Unable to find the specified file + Assert.IsNull (fnf.FusionLog, "#5"); + Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType().FullName), "#6"); + } + + [Test] + public void Constructor2 () + { + FileNotFoundException fnf = new FileNotFoundException ("message"); + +#if NET_2_0 + Assert.IsNotNull (fnf.Data, "#1"); +#endif + Assert.IsNull (fnf.FileName, "#2"); + Assert.IsNull (fnf.InnerException, "#3"); + Assert.IsNotNull (fnf.Message, "#4"); + Assert.AreEqual ("message", fnf.Message, "#5"); + Assert.IsNull (fnf.FusionLog, "#6"); + Assert.AreEqual (fnf.GetType ().FullName + ": message", + fnf.ToString (), "#7"); + } + + [Test] + public void Constructor2_Message_Empty () + { + FileNotFoundException fnf = new FileNotFoundException (string.Empty); + +#if NET_2_0 + Assert.IsNotNull (fnf.Data, "#1"); +#endif + Assert.IsNull (fnf.FileName, "#2"); + Assert.IsNull (fnf.InnerException, "#3"); + Assert.IsNotNull (fnf.Message, "#4"); + Assert.AreEqual (string.Empty, fnf.Message, "#5"); + Assert.IsNull (fnf.FusionLog, "#6"); + Assert.AreEqual (fnf.GetType ().FullName + ": ", + fnf.ToString (), "#7"); + } + + [Test] + public void Constructor2_Message_Null () + { + FileNotFoundException fnf = new FileNotFoundException ((string) null); + +#if NET_2_0 + Assert.IsNotNull (fnf.Data, "#1"); +#endif + Assert.IsNull (fnf.FileName, "#2"); + Assert.IsNull (fnf.InnerException, "#3"); +#if NET_2_0 + Assert.IsNull (fnf.Message, "#4"); +#else + Assert.IsNotNull (fnf.Message, "#4"); // File or assembly name (null), or ... +#endif + Assert.IsNull (fnf.FusionLog, "#5"); +#if NET_2_0 + Assert.AreEqual (fnf.GetType ().FullName + ": ", + fnf.ToString (), "#6"); +#else + Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType ().FullName), "#6"); +#endif + } + + [Test] + public void Constructor3 () + { + ArithmeticException ame = new ArithmeticException ("something"); + FileNotFoundException fnf = new FileNotFoundException ("message", + ame); + +#if NET_2_0 + Assert.IsNotNull (fnf.Data, "#1"); +#endif + Assert.IsNull (fnf.FileName, "#2"); + Assert.IsNotNull (fnf.InnerException, "#3"); + Assert.AreSame (ame, fnf.InnerException, "#4"); + Assert.IsNotNull (fnf.Message, "#5"); + Assert.AreEqual ("message", fnf.Message, "#6"); + Assert.IsNull (fnf.FusionLog, "#7"); + Assert.AreEqual (fnf.GetType ().FullName + ": message ---> " + + ame.GetType ().FullName + ": something", fnf.ToString (), "#8"); + } + + [Test] + public void Constructor3_Message_Empty () + { + ArithmeticException ame = new ArithmeticException ("something"); + FileNotFoundException fnf = new FileNotFoundException (string.Empty, ame); + +#if NET_2_0 + Assert.IsNotNull (fnf.Data, "#1"); +#endif + Assert.IsNull (fnf.FileName, "#2"); + Assert.IsNotNull (fnf.InnerException, "#3"); + Assert.AreSame (ame, fnf.InnerException, "#4"); + Assert.IsNotNull (fnf.Message, "#5"); + Assert.AreEqual (string.Empty, fnf.Message, "#6"); + Assert.IsNull (fnf.FusionLog, "#7"); + Assert.AreEqual (fnf.GetType ().FullName + ": ---> " + + ame.GetType ().FullName + ": something", fnf.ToString (), "#8"); + } + + [Test] + public void Constructor3_Message_Null () + { + ArithmeticException ame = new ArithmeticException ("something"); + FileNotFoundException fnf = new FileNotFoundException ((string) null, ame); + +#if NET_2_0 + Assert.IsNotNull (fnf.Data, "#1"); +#endif + Assert.IsNull (fnf.FileName, "#2"); + Assert.IsNotNull (fnf.InnerException, "#3"); + Assert.AreSame (ame, fnf.InnerException, "#4"); +#if NET_2_0 + Assert.IsNull (fnf.Message, "#5"); +#else + Assert.IsNotNull (fnf.Message, "#5"); // File or assembly name (null), or ... +#endif + Assert.IsNull (fnf.FusionLog, "#6"); +#if NET_2_0 + Assert.AreEqual (fnf.GetType ().FullName + ": ---> " + + ame.GetType ().FullName + ": something", fnf.ToString (), "#7"); +#else + Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType ().FullName), "#7"); + Assert.IsFalse (fnf.ToString ().IndexOf (Environment.NewLine) != -1, "#9"); +#endif + } + + [Test] + public void Constructor3_InnerException_Null () + { + FileNotFoundException fnf = new FileNotFoundException ("message", + (Exception) null); + +#if NET_2_0 + Assert.IsNotNull (fnf.Data, "#1"); +#endif + Assert.IsNull (fnf.FileName, "#2"); + Assert.IsNull (fnf.InnerException, "#3"); + Assert.IsNotNull (fnf.Message, "#4"); + Assert.AreEqual ("message", fnf.Message, "#5"); + Assert.IsNull (fnf.FusionLog, "#6"); + Assert.AreEqual (fnf.GetType ().FullName + ": message", + fnf.ToString (), "#7"); + } + + [Test] + public void Constructor4 () + { + FileNotFoundException fnf = new FileNotFoundException ("message", + "file.txt"); + +#if NET_2_0 + Assert.IsNotNull (fnf.Data, "#1"); +#endif + Assert.IsNotNull (fnf.FileName, "#2"); + Assert.AreEqual ("file.txt", fnf.FileName, "#3"); + Assert.IsNull (fnf.InnerException, "#4"); + Assert.IsNotNull (fnf.Message, "#5"); + Assert.AreEqual ("message", fnf.Message, "#6"); + Assert.IsNull (fnf.FusionLog, "#7"); + Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType ().FullName + + ": message" + Environment.NewLine), "#8"); +#if NET_2_0 + Assert.IsTrue (fnf.ToString ().IndexOf ("'file.txt'") != -1, "#9"); + Assert.IsFalse (fnf.ToString ().IndexOf ("\"file.txt\"") != -1, "#9"); +#else + Assert.IsFalse (fnf.ToString ().IndexOf ("'file.txt'") != -1, "#9"); + Assert.IsTrue (fnf.ToString ().IndexOf ("\"file.txt\"") != -1, "#10"); +#endif + } + + [Test] + public void Constructor4_FileName_Empty () + { + FileNotFoundException fnf = new FileNotFoundException ("message", + string.Empty); + +#if NET_2_0 + Assert.IsNotNull (fnf.Data, "#1"); +#endif + Assert.IsNotNull (fnf.FileName, "#2"); + Assert.AreEqual (string.Empty, fnf.FileName, "#3"); + Assert.IsNull (fnf.InnerException, "#4"); + Assert.IsNotNull (fnf.Message, "#5"); + Assert.AreEqual ("message", fnf.Message, "#6"); + Assert.IsNull (fnf.FusionLog, "#7"); + Assert.AreEqual (fnf.GetType ().FullName + ": message", + fnf.ToString (), "#8"); + } + + [Test] + public void Constructor4_FileName_Null () + { + FileNotFoundException fnf = new FileNotFoundException ("message", + (string) null); + +#if NET_2_0 + Assert.IsNotNull (fnf.Data, "#A1"); +#endif + Assert.IsNull (fnf.FileName, "#A2"); + Assert.IsNull (fnf.InnerException, "#A3"); + Assert.IsNotNull (fnf.Message, "#A4"); + Assert.AreEqual ("message", fnf.Message, "#A5"); + Assert.IsNull (fnf.FusionLog, "#A6"); + Assert.AreEqual (fnf.GetType ().FullName + ": message", + fnf.ToString (), "#A7"); + + fnf = new FileNotFoundException (string.Empty, (string) null); + +#if NET_2_0 + Assert.IsNotNull (fnf.Data, "#B1"); +#endif + Assert.IsNull (fnf.FileName, "#B2"); + Assert.IsNull (fnf.InnerException, "#B3"); + Assert.IsNotNull (fnf.Message, "#B4"); + Assert.AreEqual (string.Empty, fnf.Message, "#B5"); + Assert.IsNull (fnf.FusionLog, "#B6"); + Assert.AreEqual (fnf.GetType ().FullName + ": ", + fnf.ToString (), "#B7"); + } + + [Test] + public void Constructor4_FileNameAndMessage_Empty () + { + FileNotFoundException fnf = new FileNotFoundException (string.Empty, + string.Empty); + +#if NET_2_0 + Assert.IsNotNull (fnf.Data, "#1"); +#endif + Assert.IsNotNull (fnf.FileName, "#2"); + Assert.AreEqual (string.Empty, fnf.FileName, "#3"); + Assert.IsNull (fnf.InnerException, "#4"); + Assert.IsNotNull (fnf.Message, "#5"); + Assert.AreEqual (string.Empty, fnf.Message, "#6"); + Assert.IsNull (fnf.FusionLog, "#7"); + Assert.AreEqual (fnf.GetType ().FullName + ": ", fnf.ToString (), "#8"); + } + + [Test] + public void Constructor4_FileNameAndMessage_Null () + { + FileNotFoundException fnf = new FileNotFoundException ((string) null, + (string) null); + +#if NET_2_0 + Assert.IsNotNull (fnf.Data, "#1"); +#endif + Assert.IsNull (fnf.FileName, "#2"); + Assert.IsNull (fnf.InnerException, "#3"); +#if NET_2_0 + Assert.IsNull (fnf.Message, "#4"); +#else + Assert.IsNotNull (fnf.Message, "#4"); +#endif + Assert.IsNull (fnf.FusionLog, "#5"); + Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType ().FullName + + ": "), "#6"); + Assert.IsFalse (fnf.ToString ().IndexOf (Environment.NewLine) != -1, "#7"); + Assert.IsFalse (fnf.ToString ().IndexOf ("''") != -1, "#8"); + } + + [Test] + public void Constructor4_Message_Empty () + { + FileNotFoundException fnf = null; + + fnf = new FileNotFoundException (string.Empty, "file.txt"); + +#if NET_2_0 + Assert.IsNotNull (fnf.Data, "#1"); +#endif + Assert.IsNotNull (fnf.FileName, "#2"); + Assert.AreEqual ("file.txt", fnf.FileName, "#3"); + Assert.IsNull (fnf.InnerException, "#4"); + Assert.IsNotNull (fnf.Message, "#5"); + Assert.AreEqual (string.Empty, fnf.Message, "#6"); + Assert.IsNull (fnf.FusionLog, "#7"); + Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType ().FullName + + ": " + Environment.NewLine), "#8"); +#if NET_2_0 + Assert.IsTrue (fnf.ToString ().IndexOf ("'file.txt'") != -1, "#9"); + Assert.IsFalse (fnf.ToString ().IndexOf ("\"file.txt\"") != -1, "#10"); +#else + Assert.IsFalse (fnf.ToString ().IndexOf ("'file.txt'") != -1, "#9"); + Assert.IsTrue (fnf.ToString ().IndexOf ("\"file.txt\"") != -1, "#10"); +#endif + } + + [Test] + public void Constructor4_Message_Null () + { + FileNotFoundException fnf = null; + + fnf = new FileNotFoundException ((string) null, "file.txt"); + +#if NET_2_0 + Assert.IsNotNull (fnf.Data, "#A1"); +#endif + Assert.IsNotNull (fnf.FileName, "#A2"); + Assert.AreEqual ("file.txt", fnf.FileName, "#A3"); + Assert.IsNull (fnf.InnerException, "#A4"); + Assert.IsNotNull (fnf.Message, "#A5"); + Assert.IsNull (fnf.FusionLog, "#A6"); + Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType ().FullName + + ": "), "#A7"); + Assert.IsTrue (fnf.ToString ().IndexOf (Environment.NewLine) != -1, "#A8"); +#if NET_2_0 + Assert.IsTrue (fnf.ToString ().IndexOf ("'file.txt'") != -1, "#A9"); + Assert.IsFalse (fnf.ToString ().IndexOf ("\"file.txt\"") != -1, "#A10"); +#else + Assert.IsFalse (fnf.ToString ().IndexOf ("'file.txt'") != -1, "#A9"); + Assert.IsTrue (fnf.ToString ().IndexOf ("\"file.txt\"") != -1, "#A10"); +#endif + + fnf = new FileNotFoundException ((string) null, string.Empty); + +#if NET_2_0 + Assert.IsNotNull (fnf.Data, "#B1"); +#endif + Assert.IsNotNull (fnf.FileName, "#B2"); + Assert.AreEqual (string.Empty, fnf.FileName, "#B3"); + Assert.IsNull (fnf.InnerException, "#B4"); + // .NET 1.1: File or assembly name , or one of its dependencies ... + // .NET 2.0: Could not load file or assembly '' or one of its ... + Assert.IsNotNull (fnf.Message, "#B5"); + Assert.IsNull (fnf.FusionLog, "#B6"); + Assert.IsTrue (fnf.ToString ().StartsWith (fnf.GetType ().FullName + + ": "), "#B7"); + Assert.IsFalse (fnf.ToString ().IndexOf (Environment.NewLine) != -1, "#B8"); +#if NET_2_0 + Assert.IsTrue (fnf.ToString ().IndexOf ("''") != -1, "#B9"); +#else + Assert.IsFalse (fnf.ToString ().IndexOf ("''") != -1, "#B9"); + Assert.IsFalse (fnf.ToString ().IndexOf ("\"\"") != -1, "#B10"); +#endif + } + } +} diff --git a/mcs/class/corlib/corlib_test.dll.sources b/mcs/class/corlib/corlib_test.dll.sources index f47368e4753..51f24bfe94f 100644 --- a/mcs/class/corlib/corlib_test.dll.sources +++ b/mcs/class/corlib/corlib_test.dll.sources @@ -67,6 +67,7 @@ System.IO/BufferedStreamTest.cs System.IO/DirectoryInfoTest.cs System.IO/DirectoryTest.cs System.IO/FileInfoTest.cs +System.IO/FileNotFoundExceptionTest.cs System.IO/FileStreamTest.cs System.IO/FileSystemInfoTest.cs System.IO/FileTest.cs -- 2.11.4.GIT