2 // ExceptionTest.cs - NUnit Test Cases for the System.Exception class
\r
5 // Linus Upson (linus@linus.com)
\r
6 // Duncan Mak (duncan@ximian.com)
\r
7 // Gert Driesen (drieseng@users.sourceforge.net)
\r
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
\r
13 using System.Collections;
\r
14 using System.Runtime.Serialization;
\r
16 using NUnit.Framework;
\r
18 namespace MonoTests.System
\r
21 public class ExceptionTest
\r
23 [Test] // .ctor (SerializationInfo, StreamingContext)
\r
24 public void Constructor3 ()
\r
26 SerializationInfo si;
\r
30 inner = new ArgumentException ();
\r
31 si = new SerializationInfo (typeof (Exception),
\r
32 new FormatterConverter ());
\r
33 si.AddValue ("ClassName", "CLASS");
\r
34 si.AddValue ("Message", "MSG");
\r
35 si.AddValue ("InnerException", inner, typeof (Exception));
\r
36 si.AddValue ("HelpURL", "URL");
\r
37 si.AddValue ("StackTraceString", null);
\r
38 si.AddValue ("RemoteStackTraceString", null);
\r
39 si.AddValue ("RemoteStackIndex", 0);
\r
40 si.AddValue ("HResult", 10);
\r
41 si.AddValue ("Source", "SRC");
\r
42 si.AddValue ("ExceptionMethod", null);
\r
43 Hashtable data = new Hashtable ();
\r
44 data.Add ("XX", "ZZ");
\r
45 si.AddValue ("Data", data, typeof (IDictionary));
\r
47 ex = new MyException (si, new StreamingContext ());
\r
48 Assert.AreEqual ("MSG", ex.Message, "#A1");
\r
49 Assert.AreSame (inner, ex.InnerException, "#A2");
\r
50 Assert.AreEqual ("URL", ex.HelpLink, "#A3");
\r
51 Assert.AreEqual (10, ex.HResult, "#A4");
\r
52 Assert.AreEqual ("SRC", ex.Source, "#A5");
\r
53 Assert.IsNotNull (ex.Data, "#A6");
\r
54 Assert.AreEqual (1, ex.Data.Keys.Count, "#A7");
\r
55 Assert.AreEqual ("ZZ", ex.Data ["XX"], "#A8");
\r
58 si = new SerializationInfo (typeof (Exception),
\r
59 new FormatterConverter ());
\r
60 si.AddValue ("ClassName", "CLASS");
\r
61 si.AddValue ("Message", null);
\r
62 si.AddValue ("InnerException", inner, typeof (Exception));
\r
63 si.AddValue ("HelpURL", "URL");
\r
64 si.AddValue ("StackTraceString", null);
\r
65 si.AddValue ("RemoteStackTraceString", null);
\r
66 si.AddValue ("RemoteStackIndex", 0);
\r
67 si.AddValue ("HResult", 10);
\r
68 si.AddValue ("Source", "SRC");
\r
69 si.AddValue ("ExceptionMethod", null);
\r
71 ex = new MyException (si, new StreamingContext ());
\r
72 Assert.IsNotNull (ex.Message, "#B1");
\r
73 Assert.IsTrue (ex.Message.IndexOf ("CLASS") != -1, "#B2");
\r
74 Assert.IsNull (ex.InnerException, "#B3");
\r
75 Assert.AreEqual ("URL", ex.HelpLink, "#B4");
\r
76 Assert.AreEqual (10, ex.HResult, "#B5");
\r
77 Assert.AreEqual ("SRC", ex.Source, "#B6");
\r
78 Assert.IsNotNull (ex.Data, "#B7");
\r
79 Assert.AreEqual (0, ex.Data.Keys.Count, "#B8");
\r
83 // This test makes sure that exceptions thrown on block boundaries are
\r
84 // handled in the correct block. The meaning of the 'caught' variable is
\r
85 // a little confusing since there are two catchers: the method being
\r
86 // tested the the method calling the test. There is probably a better
\r
87 // name, but I can't think of it right now.
\r
89 public void TestThrowOnBlockBoundaries ()
\r
99 Assert.IsTrue (caught, "Exceptions thrown before try blocks should not be caught");
\r
103 ThrowAtBeginOfTry();
\r
107 Assert.IsFalse (caught, "Exceptions thrown at begin of try blocks should be caught");
\r
115 Assert.IsFalse (caught, "Exceptions thrown at end of try blocks should be caught");
\r
119 ThrowAtBeginOfCatch();
\r
123 Assert.IsTrue (caught, "Exceptions thrown at begin of catch blocks should not be caught");
\r
127 ThrowAtEndOfCatch();
\r
131 Assert.IsTrue (caught, "Exceptions thrown at end of catch blocks should not be caught");
\r
135 ThrowAtBeginOfFinally();
\r
139 Assert.IsTrue (caught, "Exceptions thrown at begin of finally blocks should not be caught");
\r
143 ThrowAtEndOfFinally();
\r
147 Assert.IsTrue (caught, "Exceptions thrown at end of finally blocks should not be caught");
\r
151 ThrowAfterFinally();
\r
155 Assert.IsTrue (caught, "Exceptions thrown after finally blocks should not be caught");
\r
158 private static void DoNothing()
\r
162 private static void ThrowException()
\r
164 throw new Exception();
\r
167 private static void ThrowBeforeTry()
\r
172 } catch (Exception) {
\r
177 private static void ThrowAtBeginOfTry()
\r
183 } catch (Exception) {
\r
188 private static void ThrowAtEndOfTry()
\r
194 } catch (Exception) {
\r
199 private static void ThrowAtBeginOfCatch()
\r
205 } catch (Exception) {
\r
210 private static void ThrowAtEndOfCatch()
\r
216 } catch (Exception) {
\r
222 private static void ThrowAtBeginOfFinally()
\r
228 } catch (Exception) {
\r
236 private static void ThrowAtEndOfFinally()
\r
242 } catch (Exception) {
\r
250 private static void ThrowAfterFinally()
\r
256 } catch (Exception) {
\r
265 public void GetObjectData ()
\r
267 string msg = "MESSAGE";
\r
268 Exception inner = new ArgumentException ("whatever");
\r
269 SerializationInfo si;
\r
272 se = new Exception (msg, inner);
\r
273 si = new SerializationInfo (typeof (Exception),
\r
274 new FormatterConverter ());
\r
275 se.GetObjectData (si, new StreamingContext ());
\r
276 Assert.AreEqual (11, si.MemberCount, "#A1");
\r
277 Assert.AreEqual (typeof (Exception).FullName, si.GetString ("ClassName"), "#A2");
\r
278 Assert.IsNull (si.GetValue ("Data", typeof (IDictionary)), "#A3");
\r
279 Assert.AreSame (msg, si.GetString ("Message"), "#A4");
\r
280 Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#A5");
\r
281 Assert.AreSame (se.HelpLink, si.GetString ("HelpURL"), "#A6");
\r
282 Assert.IsNull (si.GetString ("StackTraceString"), "#A7");
\r
283 Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#A8");
\r
284 Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#A9");
\r
285 Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#A10");
\r
286 Assert.IsNull (si.GetString ("Source"), "#A11");
\r
287 Assert.IsNull (si.GetString ("ExceptionMethod"), "#A12");
\r
289 // attempt initialization of lazy init members
\r
290 Assert.IsNotNull (se.Data);
\r
291 Assert.IsNull (se.Source);
\r
292 Assert.IsNull (se.StackTrace);
\r
294 si = new SerializationInfo (typeof (Exception),
\r
295 new FormatterConverter ());
\r
296 se.GetObjectData (si, new StreamingContext ());
\r
297 Assert.AreEqual (11, si.MemberCount, "#B1");
\r
298 Assert.AreEqual (typeof (Exception).FullName, si.GetString ("ClassName"), "#B2");
\r
299 Assert.AreSame (se.Data, si.GetValue ("Data", typeof (IDictionary)), "#B3");
\r
300 Assert.AreSame (msg, si.GetString ("Message"), "#B4");
\r
301 Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#B5");
\r
302 Assert.AreSame (se.HelpLink, si.GetString ("HelpURL"), "#B6");
\r
303 Assert.IsNull (si.GetString ("StackTraceString"), "#B7");
\r
304 Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#B8");
\r
305 Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#B9");
\r
306 Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#B10");
\r
307 Assert.IsNull (si.GetString ("Source"), "#B11");
\r
308 Assert.IsNull (si.GetString ("ExceptionMethod"), "#B12");
\r
311 throw new Exception (msg, inner);
\r
312 } catch (Exception ex) {
\r
313 si = new SerializationInfo (typeof (Exception),
\r
314 new FormatterConverter ());
\r
315 ex.GetObjectData (si, new StreamingContext ());
\r
316 Assert.AreEqual (11, si.MemberCount, "#C1");
\r
317 Assert.AreEqual (typeof (Exception).FullName, si.GetString ("ClassName"), "#C2");
\r
318 Assert.IsNull (si.GetValue ("Data", typeof (IDictionary)), "#C3");
\r
319 Assert.AreSame (msg, si.GetString ("Message"), "#C4");
\r
320 Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#C5");
\r
321 Assert.AreSame (se.HelpLink, si.GetString ("HelpURL"), "#C6");
\r
322 Assert.IsNotNull (si.GetString ("StackTraceString"), "#C7");
\r
323 Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#C8");
\r
324 Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#C9");
\r
325 Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#C10");
\r
326 Assert.IsNotNull (si.GetString ("Source"), "#C11");
\r
327 //Assert.IsNotNull (si.GetString ("ExceptionMethod"), "#C12");
\r
331 throw new Exception (msg, inner);
\r
332 } catch (Exception ex) {
\r
333 // force initialization of lazy init members
\r
334 Assert.IsNotNull (ex.Data);
\r
335 Assert.IsNotNull (ex.StackTrace);
\r
337 si = new SerializationInfo (typeof (Exception),
\r
338 new FormatterConverter ());
\r
339 ex.GetObjectData (si, new StreamingContext ());
\r
340 Assert.AreEqual (11, si.MemberCount, "#D1");
\r
341 Assert.AreEqual (typeof (Exception).FullName, si.GetString ("ClassName"), "#D2");
\r
342 Assert.AreSame (ex.Data, si.GetValue ("Data", typeof (IDictionary)), "#D3");
\r
343 Assert.AreSame (msg, si.GetString ("Message"), "#D4");
\r
344 Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#D5");
\r
345 Assert.AreSame (ex.HelpLink, si.GetString ("HelpURL"), "#D6");
\r
346 Assert.IsNotNull (si.GetString ("StackTraceString"), "#D7");
\r
347 Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#D8");
\r
348 Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#D9");
\r
349 Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#D10");
\r
350 Assert.AreEqual (typeof (ExceptionTest).Assembly.GetName ().Name, si.GetString ("Source"), "#D11");
\r
351 //Assert.IsNotNull (si.GetString ("ExceptionMethod"), "#D12");
\r
356 public void GetObjectData_Info_Null ()
\r
358 Exception e = new Exception ();
\r
360 e.GetObjectData (null, new StreamingContext ());
\r
361 Assert.Fail ("#1");
\r
362 } catch (ArgumentNullException ex) {
\r
363 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
\r
364 Assert.IsNull (ex.InnerException, "#3");
\r
365 Assert.IsNotNull (ex.Message, "#4");
\r
366 Assert.AreEqual ("info", ex.ParamName, "#5");
\r
371 public void HResult ()
\r
373 MyException ex = new MyException ();
\r
374 Assert.AreEqual (-2146233088, ex.HResult, "#1");
\r
375 ex.HResult = int.MaxValue;
\r
376 Assert.AreEqual (int.MaxValue, ex.HResult, "#2");
\r
377 ex.HResult = int.MinValue;
\r
378 Assert.AreEqual (int.MinValue, ex.HResult, "#3");
\r
382 public void Source ()
\r
384 Exception ex1 = new Exception ("MSG");
\r
385 Assert.IsNull (ex1.Source, "#1");
\r
388 throw new Exception ("MSG");
\r
389 } catch (Exception ex2) {
\r
390 Assert.AreEqual (typeof (ExceptionTest).Assembly.GetName ().Name, ex2.Source, "#2");
\r
395 public void Source_InnerException ()
\r
397 Exception a = new Exception ("a", new ArgumentException ("b"));
\r
400 Assert.IsNull (a.InnerException.Source);
\r
404 public void StackTrace ()
\r
406 Exception ex1 = new Exception ("MSG");
\r
407 Assert.IsNull (ex1.StackTrace, "#1");
\r
410 throw new Exception ("MSG");
\r
411 } catch (Exception ex2) {
\r
412 Assert.IsNotNull (ex2.StackTrace, "#2");
\r
416 class MyException : Exception {
\r
417 public MyException ()
\r
421 public MyException (SerializationInfo info, StreamingContext context)
\r
422 : base (info, context)
\r
426 public new int HResult {
\r
427 get { return base.HResult; }
\r
428 set { base.HResult = value; }
\r