// 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
} 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
} 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
} 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
} 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
} 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
} 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
} 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
} 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
}\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