* ExceptonTest.cs: Added GetObjectData and deserialization ctor tests.
authorGert Driesen <drieseng@users.sourceforge.net>
Sat, 16 Aug 2008 10:49:12 +0000 (10:49 -0000)
committerGert Driesen <drieseng@users.sourceforge.net>
Sat, 16 Aug 2008 10:49:12 +0000 (10:49 -0000)
Added/improved tests for Source, HResult and StackTrace. Use Assert
instead of deriving from TestCase.
* Exception.cs: Fixed value of HResult to match MS. Added support for
(de)serializing Data. Use deserialized ClassName, if available, to
when name of type is used.

svn path=/trunk/mcs/; revision=110640

mcs/class/corlib/System/ChangeLog
mcs/class/corlib/System/Exception.cs
mcs/class/corlib/Test/System/ChangeLog
mcs/class/corlib/Test/System/ExceptionTest.cs

index 76f3321249339758cd22ad31640c201ef7879e1d..8354f45d5bfe4fb39d742887d2d17fd86b101f5f 100644 (file)
@@ -1,3 +1,9 @@
+2008-08-16  Gert Driesen  <drieseng@users.sourceforge.net>
+
+       * Exception.cs: Fixed value of HResult to match MS. Added support for
+       (de)serializing Data. Use deserialized ClassName, if available, to
+       when name of type is used.
+
 2008-08-10  Gert Driesen  <drieseng@users.sourceforge.net>
 
        * IServiceProvider.cs: Added ComVisible attribute (1.0 only).
index f76c8ff4a5b6ad22a62164e730fd18077f978949..74333cb6ae1cc4797afd9373528f43f575d692c3 100644 (file)
@@ -61,9 +61,9 @@ namespace System
                string stack_trace;
                string remote_stack_trace;
                int remote_stack_index;
-               internal int hresult = unchecked ((int)0x80004005);
+               internal int hresult = -2146233088;
                string source;
-               private IDictionary _data;
+               IDictionary _data;
                #endregion
 
                public Exception ()
@@ -89,6 +89,15 @@ namespace System
                        hresult             = info.GetInt32  ("HResult");
                        source              = info.GetString ("Source");
                        inner_exception     = (Exception) info.GetValue ("InnerException", typeof (Exception));
+
+#if NET_2_0
+                       try {
+                               _data = (IDictionary) info.GetValue ("Data", typeof (IDictionary));
+                       } catch (SerializationException) {
+                               // member did not exist in .NET 1.x
+                       }
+#endif
+
                }
 
                public Exception (string message, Exception innerException)
@@ -121,6 +130,14 @@ namespace System
                        stack_trace = s;
                }
 
+               string ClassName {
+                       get {
+                               if (class_name == null)
+                                       class_name = GetType ().ToString ();
+                               return class_name;
+                       }
+               }
+
                public virtual string Message {
                        get {
                                if (message == null)
@@ -129,7 +146,7 @@ namespace System
 #else
                                        message = string.Format (Locale.GetText ("Exception of type {0} was thrown."),
 #endif
-                                               GetType ().ToString());
+                                               ClassName);
 
                                return message;
                        }
@@ -258,10 +275,7 @@ namespace System
                        if (info == null)
                                throw new ArgumentNullException ("info");
 
-                       if (class_name == null)
-                               class_name = GetType ().FullName;
-
-                       info.AddValue ("ClassName", class_name);
+                       info.AddValue ("ClassName", ClassName);
                        info.AddValue ("Message", message);
                        info.AddValue ("InnerException", inner_exception);
                        info.AddValue ("HelpURL", help_link);
@@ -271,6 +285,9 @@ namespace System
                        info.AddValue ("HResult", hresult);
                        info.AddValue ("Source", Source);
                        info.AddValue ("ExceptionMethod", null);
+#if NET_2_0
+                       info.AddValue ("Data", _data, typeof (IDictionary));
+#endif
                }
 
 #if ONLY_1_1
@@ -278,7 +295,7 @@ namespace System
 #endif
                public override string ToString ()
                {
-                       System.Text.StringBuilder result = new System.Text.StringBuilder (this.GetType ().FullName);
+                       System.Text.StringBuilder result = new System.Text.StringBuilder (ClassName);
                        result.Append (": ").Append (Message);
 
                        if (null != remote_stack_trace)
index 0cd1b12fe8fb23586356a6720cd59e5499d73ee8..54e2396f029f514ddd785b169b46ed10b4268f54 100644 (file)
@@ -1,3 +1,9 @@
+2008-08-16  Gert Driesen  <drieseng@users.sourceforge.net>
+
+       * ExceptonTest.cs: Added GetObjectData and deserialization ctor tests.
+       Added/improved tests for Source, HResult and StackTrace. Use Assert
+       instead of deriving from TestCase.
+
 2008-08-09  Raja R Harinath  <harinath@hurrynot.org>
 
        * DateTimeTest.cs (Bug377042): Allow compilation on the 1.1 profile.
index affef3fa78258fb41222fa29cb76af1fef21426a..8e0d299dd2ea8ec4fbe4bd5fadb1ff92b0183d1d 100644 (file)
@@ -4,29 +4,93 @@
 // Authors:\r
 //     Linus Upson (linus@linus.com)\r
 //     Duncan Mak (duncan@ximian.com)\r
+//     Gert Driesen (drieseng@users.sourceforge.net)\r
 //\r
 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)\r
 //\r
 \r
 using System;\r
+using System.Collections;\r
 using System.Runtime.Serialization;\r
 \r
 using NUnit.Framework;\r
 \r
 namespace MonoTests.System\r
 {\r
-       public class ExceptionTest : TestCase\r
+       [TestFixture]\r
+       public class ExceptionTest\r
        {\r
-               public ExceptionTest() {}\r
-               \r
+               [Test] // .ctor (SerializationInfo, StreamingContext)\r
+               public void Constructor3 ()\r
+               {\r
+                       SerializationInfo si;\r
+                       MyException ex;\r
+                       Exception inner;\r
+\r
+                       inner = new ArgumentException ();\r
+                       si = new SerializationInfo (typeof (Exception),\r
+                               new FormatterConverter ());\r
+                       si.AddValue ("ClassName", "CLASS");\r
+                       si.AddValue ("Message", "MSG");\r
+                       si.AddValue ("InnerException", inner, typeof (Exception));\r
+                       si.AddValue ("HelpURL", "URL");\r
+                       si.AddValue ("StackTraceString", null);\r
+                       si.AddValue ("RemoteStackTraceString", null);\r
+                       si.AddValue ("RemoteStackIndex", 0);\r
+                       si.AddValue ("HResult", 10);\r
+                       si.AddValue ("Source", "SRC");\r
+                       si.AddValue ("ExceptionMethod", null);\r
+                       Hashtable data = new Hashtable ();\r
+                       data.Add ("XX", "ZZ");\r
+                       si.AddValue ("Data", data, typeof (IDictionary));\r
+\r
+                       ex = new MyException (si, new StreamingContext ());\r
+                       Assert.AreEqual ("MSG", ex.Message, "#A1");\r
+                       Assert.AreSame (inner, ex.InnerException, "#A2");\r
+                       Assert.AreEqual ("URL", ex.HelpLink, "#A3");\r
+                       Assert.AreEqual (10, ex.HResult, "#A4");\r
+                       Assert.AreEqual ("SRC", ex.Source, "#A5");\r
+#if NET_2_0\r
+                       Assert.IsNotNull (ex.Data, "#A6");\r
+                       Assert.AreEqual (1, ex.Data.Keys.Count, "#A7");\r
+                       Assert.AreEqual ("ZZ", ex.Data ["XX"], "#A8");\r
+#endif\r
+\r
+                       inner = null;\r
+                       si = new SerializationInfo (typeof (Exception),\r
+                               new FormatterConverter ());\r
+                       si.AddValue ("ClassName", "CLASS");\r
+                       si.AddValue ("Message", null);\r
+                       si.AddValue ("InnerException", inner, typeof (Exception));\r
+                       si.AddValue ("HelpURL", "URL");\r
+                       si.AddValue ("StackTraceString", null);\r
+                       si.AddValue ("RemoteStackTraceString", null);\r
+                       si.AddValue ("RemoteStackIndex", 0);\r
+                       si.AddValue ("HResult", 10);\r
+                       si.AddValue ("Source", "SRC");\r
+                       si.AddValue ("ExceptionMethod", null);\r
+\r
+                       ex = new MyException (si, new StreamingContext ());\r
+                       Assert.IsNotNull (ex.Message, "#B1");\r
+                       Assert.IsTrue (ex.Message.IndexOf ("CLASS") != -1, "#B2");\r
+                       Assert.IsNull (ex.InnerException, "#B3");\r
+                       Assert.AreEqual ("URL", ex.HelpLink, "#B4");\r
+                       Assert.AreEqual (10, ex.HResult, "#B5");\r
+                       Assert.AreEqual ("SRC", ex.Source, "#B6");\r
+#if NET_2_0\r
+                       Assert.IsNotNull (ex.Data, "#B7");\r
+                       Assert.AreEqual (0, ex.Data.Keys.Count, "#B8");\r
+#endif\r
+               }\r
+\r
+\r
                // This test makes sure that exceptions thrown on block boundaries are\r
                // handled in the correct block. The meaning of the 'caught' variable is\r
                // a little confusing since there are two catchers: the method being\r
                // tested the the method calling the test. There is probably a better\r
                // name, but I can't think of it right now.\r
-\r
                [Test]\r
-               public void TestThrowOnBlockBoundaries()\r
+               public void TestThrowOnBlockBoundaries ()\r
                {\r
                        bool caught;\r
                        \r
@@ -36,7 +100,7 @@ namespace MonoTests.System
                        } catch {\r
                                caught = true;\r
                        }\r
-                       Assert("Exceptions thrown before try blocks should not be caught", caught);\r
+                       Assert.IsTrue (caught, "Exceptions thrown before try blocks should not be caught");\r
                        \r
                        try {\r
                                caught = false;\r
@@ -44,7 +108,7 @@ namespace MonoTests.System
                        } catch {\r
                                caught = true;\r
                        }\r
-                       Assert("Exceptions thrown at begin of try blocks should be caught", !caught);\r
+                       Assert.IsFalse (caught, "Exceptions thrown at begin of try blocks should be caught");\r
 \r
                        try {\r
                                caught = false;\r
@@ -52,7 +116,7 @@ namespace MonoTests.System
                        } catch {\r
                                caught = true;\r
                        }\r
-                       Assert("Exceptions thrown at end of try blocks should be caught", !caught);\r
+                       Assert.IsFalse (caught, "Exceptions thrown at end of try blocks should be caught");\r
 \r
                        try {\r
                                caught = false;\r
@@ -60,7 +124,7 @@ namespace MonoTests.System
                        } catch {\r
                                caught = true;\r
                        }\r
-                       Assert("Exceptions thrown at begin of catch blocks should not be caught", caught);\r
+                       Assert.IsTrue (caught, "Exceptions thrown at begin of catch blocks should not be caught");\r
 \r
                        try {\r
                                caught = false;\r
@@ -68,7 +132,7 @@ namespace MonoTests.System
                        } catch {\r
                                caught = true;\r
                        }\r
-                       Assert("Exceptions thrown at end of catch blocks should not be caught", caught);\r
+                       Assert.IsTrue (caught, "Exceptions thrown at end of catch blocks should not be caught");\r
 \r
                        try {\r
                                caught = false;\r
@@ -76,7 +140,7 @@ namespace MonoTests.System
                        } catch {\r
                                caught = true;\r
                        }\r
-                       Assert("Exceptions thrown at begin of finally blocks should not be caught", caught);\r
+                       Assert.IsTrue (caught, "Exceptions thrown at begin of finally blocks should not be caught");\r
 \r
                        try {\r
                                caught = false;\r
@@ -84,7 +148,7 @@ namespace MonoTests.System
                        } catch {\r
                                caught = true;\r
                        }\r
-                       Assert("Exceptions thrown at end of finally blocks should not be caught", caught);\r
+                       Assert.IsTrue (caught, "Exceptions thrown at end of finally blocks should not be caught");\r
 \r
                        try {\r
                                caught = false;\r
@@ -92,7 +156,7 @@ namespace MonoTests.System
                        } catch {\r
                                caught = true;\r
                        }\r
-                       Assert("Exceptions thrown after finally blocks should not be caught", caught);\r
+                       Assert.IsTrue (caught, "Exceptions thrown after finally blocks should not be caught");\r
                }\r
                \r
                private static void DoNothing()\r
@@ -202,20 +266,199 @@ namespace MonoTests.System
                }\r
 \r
                [Test]\r
-               public void InnerExceptionSource ()\r
+               public void GetObjectData ()\r
+               {\r
+                       string msg = "MESSAGE";\r
+                       Exception inner = new ArgumentException ("whatever");\r
+                       SerializationInfo si;\r
+                       Exception se;\r
+\r
+                       se = new Exception (msg, inner);\r
+                       si = new SerializationInfo (typeof (Exception),\r
+                               new FormatterConverter ());\r
+                       se.GetObjectData (si, new StreamingContext ());\r
+#if NET_2_0\r
+                       Assert.AreEqual (11, si.MemberCount, "#A1");\r
+#else\r
+                       Assert.AreEqual (10, si.MemberCount, "#A1");\r
+#endif\r
+                       Assert.AreEqual (typeof (Exception).FullName, si.GetString ("ClassName"), "#A2");\r
+#if NET_2_0\r
+                       Assert.IsNull (si.GetValue ("Data", typeof (IDictionary)), "#A3");\r
+#endif\r
+                       Assert.AreSame (msg, si.GetString ("Message"), "#A4");\r
+                       Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#A5");\r
+                       Assert.AreSame (se.HelpLink, si.GetString ("HelpURL"), "#A6");\r
+                       Assert.IsNull (si.GetString ("StackTraceString"), "#A7");\r
+                       Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#A8");\r
+                       Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#A9");\r
+                       Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#A10");\r
+                       Assert.IsNull (si.GetString ("Source"), "#A11");\r
+                       Assert.IsNull (si.GetString ("ExceptionMethod"), "#A12");\r
+\r
+                       // attempt initialization of lazy init members\r
+#if NET_2_0\r
+                       Assert.IsNotNull (se.Data);\r
+#endif\r
+                       Assert.IsNull (se.Source);\r
+                       Assert.IsNull (se.StackTrace);\r
+\r
+                       si = new SerializationInfo (typeof (Exception),\r
+                               new FormatterConverter ());\r
+                       se.GetObjectData (si, new StreamingContext ());\r
+#if NET_2_0\r
+                       Assert.AreEqual (11, si.MemberCount, "#B1");\r
+#else\r
+                       Assert.AreEqual (10, si.MemberCount, "#B1");\r
+#endif\r
+                       Assert.AreEqual (typeof (Exception).FullName, si.GetString ("ClassName"), "#B2");\r
+#if NET_2_0\r
+                       Assert.AreSame (se.Data, si.GetValue ("Data", typeof (IDictionary)), "#B3");\r
+#endif\r
+                       Assert.AreSame (msg, si.GetString ("Message"), "#B4");\r
+                       Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#B5");\r
+                       Assert.AreSame (se.HelpLink, si.GetString ("HelpURL"), "#B6");\r
+                       Assert.IsNull (si.GetString ("StackTraceString"), "#B7");\r
+                       Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#B8");\r
+                       Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#B9");\r
+                       Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#B10");\r
+                       Assert.IsNull (si.GetString ("Source"), "#B11");\r
+                       Assert.IsNull (si.GetString ("ExceptionMethod"), "#B12");\r
+\r
+                       try {\r
+                               throw new Exception (msg, inner);\r
+                       } catch (Exception ex) {\r
+                               si = new SerializationInfo (typeof (Exception),\r
+                                       new FormatterConverter ());\r
+                               ex.GetObjectData (si, new StreamingContext ());\r
+#if NET_2_0\r
+                               Assert.AreEqual (11, si.MemberCount, "#C1");\r
+#else\r
+                               Assert.AreEqual (10, si.MemberCount, "#C1");\r
+#endif\r
+                               Assert.AreEqual (typeof (Exception).FullName, si.GetString ("ClassName"), "#C2");\r
+#if NET_2_0\r
+                               Assert.IsNull (si.GetValue ("Data", typeof (IDictionary)), "#C3");\r
+#endif\r
+                               Assert.AreSame (msg, si.GetString ("Message"), "#C4");\r
+                               Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#C5");\r
+                               Assert.AreSame (se.HelpLink, si.GetString ("HelpURL"), "#C6");\r
+                               Assert.IsNotNull (si.GetString ("StackTraceString"), "#C7");\r
+                               Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#C8");\r
+                               Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#C9");\r
+                               Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#C10");\r
+                               Assert.IsNotNull (si.GetString ("Source"), "#C11");\r
+                               //Assert.IsNotNull (si.GetString ("ExceptionMethod"), "#C12");\r
+                       }\r
+\r
+                       try {\r
+                               throw new Exception (msg, inner);\r
+                       } catch (Exception ex) {\r
+                               // force initialization of lazy init members\r
+#if NET_2_0\r
+                               Assert.IsNotNull (ex.Data);\r
+#endif\r
+                               Assert.IsNotNull (ex.StackTrace);\r
+\r
+                               si = new SerializationInfo (typeof (Exception),\r
+                                       new FormatterConverter ());\r
+                               ex.GetObjectData (si, new StreamingContext ());\r
+#if NET_2_0\r
+                               Assert.AreEqual (11, si.MemberCount, "#D1");\r
+#else\r
+                               Assert.AreEqual (10, si.MemberCount, "#D1");\r
+#endif\r
+                               Assert.AreEqual (typeof (Exception).FullName, si.GetString ("ClassName"), "#D2");\r
+#if NET_2_0\r
+                               Assert.AreSame (ex.Data, si.GetValue ("Data", typeof (IDictionary)), "#D3");\r
+#endif\r
+                               Assert.AreSame (msg, si.GetString ("Message"), "#D4");\r
+                               Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#D5");\r
+                               Assert.AreSame (ex.HelpLink, si.GetString ("HelpURL"), "#D6");\r
+                               Assert.IsNotNull (si.GetString ("StackTraceString"), "#D7");\r
+                               Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#D8");\r
+                               Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#D9");\r
+                               Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#D10");\r
+                               Assert.AreEqual (typeof (ExceptionTest).Assembly.GetName ().Name, si.GetString ("Source"), "#D11");\r
+                               //Assert.IsNotNull (si.GetString ("ExceptionMethod"), "#D12");\r
+                       }\r
+               }\r
+\r
+               [Test]\r
+               public void GetObjectData_Info_Null ()\r
+               {\r
+                       Exception e = new Exception ();\r
+                       try {\r
+                               e.GetObjectData (null, new StreamingContext ());\r
+                               Assert.Fail ("#1");\r
+                       } catch (ArgumentNullException ex) {\r
+                               Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
+                               Assert.IsNull (ex.InnerException, "#3");\r
+                               Assert.IsNotNull (ex.Message, "#4");\r
+                               Assert.AreEqual ("info", ex.ParamName, "#5");\r
+                       }\r
+               }\r
+\r
+               [Test]\r
+               public void HResult ()\r
+               {\r
+                       MyException ex = new MyException ();\r
+                       Assert.AreEqual (-2146233088, ex.HResult, "#1");\r
+                       ex.HResult = int.MaxValue;\r
+                       Assert.AreEqual (int.MaxValue, ex.HResult, "#2");\r
+                       ex.HResult = int.MinValue;\r
+                       Assert.AreEqual (int.MinValue, ex.HResult, "#3");\r
+               }\r
+\r
+               [Test]\r
+               public void Source ()\r
+               {\r
+                       Exception ex1 = new Exception ("MSG");\r
+                       Assert.IsNull (ex1.Source, "#1");\r
+\r
+                       try {\r
+                               throw new Exception ("MSG");\r
+                       } catch (Exception ex2) {\r
+                               Assert.AreEqual (typeof (ExceptionTest).Assembly.GetName ().Name, ex2.Source, "#2");\r
+                       }\r
+               }\r
+\r
+               [Test]\r
+               public void Source_InnerException ()\r
                {\r
                        Exception a = new Exception ("a", new ArgumentException ("b"));\r
                        a.Source = "foo";\r
 \r
-                       AssertEquals (null, a.InnerException.Source);\r
+                       Assert.IsNull (a.InnerException.Source);\r
                }\r
 \r
                [Test]\r
-               [ExpectedException (typeof (ArgumentNullException))]\r
-               public void GetObjectData_Null ()\r
+               public void StackTrace ()\r
                {\r
-                       Exception e = new Exception ();\r
-                       e.GetObjectData (null, new StreamingContext (StreamingContextStates.All));\r
+                       Exception ex1 = new Exception ("MSG");\r
+                       Assert.IsNull (ex1.StackTrace, "#1");\r
+\r
+                       try {\r
+                               throw new Exception ("MSG");\r
+                       } catch (Exception ex2) {\r
+                               Assert.IsNotNull (ex2.StackTrace, "#2");\r
+                       }\r
+               }\r
+\r
+               class MyException : Exception {\r
+                       public MyException ()\r
+                       {\r
+                       }\r
+\r
+                       public MyException (SerializationInfo info, StreamingContext context)\r
+                               : base (info, context)\r
+                       {\r
+                       }\r
+\r
+                       public new int HResult {\r
+                               get { return base.HResult; }\r
+                               set { base.HResult = value; }\r
+                       }\r
                }\r
        }\r
 }\r