Merge pull request #200 from ch5oh/master
[mono.git] / mcs / class / corlib / Test / System / ExceptionTest.cs
1 //\r
2 // ExceptionTest.cs - NUnit Test Cases for the System.Exception class\r
3 //\r
4 // Authors:\r
5 //      Linus Upson (linus@linus.com)\r
6 //      Duncan Mak (duncan@ximian.com)\r
7 //      Gert Driesen (drieseng@users.sourceforge.net)\r
8 //\r
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)\r
10 //\r
11 \r
12 using System;\r
13 using System.Collections;\r
14 using System.Runtime.Serialization;\r
15 \r
16 using NUnit.Framework;\r
17 \r
18 namespace MonoTests.System\r
19 {\r
20         [TestFixture]\r
21         public class ExceptionTest\r
22         {\r
23                 [Test] // .ctor (SerializationInfo, StreamingContext)\r
24                 public void Constructor3 ()\r
25                 {\r
26                         SerializationInfo si;\r
27                         MyException ex;\r
28                         Exception inner;\r
29 \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
46 \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 #if NET_2_0\r
54                         Assert.IsNotNull (ex.Data, "#A6");\r
55                         Assert.AreEqual (1, ex.Data.Keys.Count, "#A7");\r
56                         Assert.AreEqual ("ZZ", ex.Data ["XX"], "#A8");\r
57 #endif\r
58 \r
59                         inner = null;\r
60                         si = new SerializationInfo (typeof (Exception),\r
61                                 new FormatterConverter ());\r
62                         si.AddValue ("ClassName", "CLASS");\r
63                         si.AddValue ("Message", null);\r
64                         si.AddValue ("InnerException", inner, typeof (Exception));\r
65                         si.AddValue ("HelpURL", "URL");\r
66                         si.AddValue ("StackTraceString", null);\r
67                         si.AddValue ("RemoteStackTraceString", null);\r
68                         si.AddValue ("RemoteStackIndex", 0);\r
69                         si.AddValue ("HResult", 10);\r
70                         si.AddValue ("Source", "SRC");\r
71                         si.AddValue ("ExceptionMethod", null);\r
72 \r
73                         ex = new MyException (si, new StreamingContext ());\r
74                         Assert.IsNotNull (ex.Message, "#B1");\r
75                         Assert.IsTrue (ex.Message.IndexOf ("CLASS") != -1, "#B2");\r
76                         Assert.IsNull (ex.InnerException, "#B3");\r
77                         Assert.AreEqual ("URL", ex.HelpLink, "#B4");\r
78                         Assert.AreEqual (10, ex.HResult, "#B5");\r
79                         Assert.AreEqual ("SRC", ex.Source, "#B6");\r
80 #if NET_2_0\r
81                         Assert.IsNotNull (ex.Data, "#B7");\r
82                         Assert.AreEqual (0, ex.Data.Keys.Count, "#B8");\r
83 #endif\r
84                 }\r
85 \r
86 \r
87                 // This test makes sure that exceptions thrown on block boundaries are\r
88                 // handled in the correct block. The meaning of the 'caught' variable is\r
89                 // a little confusing since there are two catchers: the method being\r
90                 // tested the the method calling the test. There is probably a better\r
91                 // name, but I can't think of it right now.\r
92                 [Test]\r
93                 public void TestThrowOnBlockBoundaries ()\r
94                 {\r
95                         bool caught;\r
96                         \r
97                         try {\r
98                                 caught = false;\r
99                                 ThrowBeforeTry();\r
100                         } catch {\r
101                                 caught = true;\r
102                         }\r
103                         Assert.IsTrue (caught, "Exceptions thrown before try blocks should not be caught");\r
104                         \r
105                         try {\r
106                                 caught = false;\r
107                                 ThrowAtBeginOfTry();\r
108                         } catch {\r
109                                 caught = true;\r
110                         }\r
111                         Assert.IsFalse (caught, "Exceptions thrown at begin of try blocks should be caught");\r
112 \r
113                         try {\r
114                                 caught = false;\r
115                                 ThrowAtEndOfTry();\r
116                         } catch {\r
117                                 caught = true;\r
118                         }\r
119                         Assert.IsFalse (caught, "Exceptions thrown at end of try blocks should be caught");\r
120 \r
121                         try {\r
122                                 caught = false;\r
123                                 ThrowAtBeginOfCatch();\r
124                         } catch {\r
125                                 caught = true;\r
126                         }\r
127                         Assert.IsTrue (caught, "Exceptions thrown at begin of catch blocks should not be caught");\r
128 \r
129                         try {\r
130                                 caught = false;\r
131                                 ThrowAtEndOfCatch();\r
132                         } catch {\r
133                                 caught = true;\r
134                         }\r
135                         Assert.IsTrue (caught, "Exceptions thrown at end of catch blocks should not be caught");\r
136 \r
137                         try {\r
138                                 caught = false;\r
139                                 ThrowAtBeginOfFinally();\r
140                         } catch {\r
141                                 caught = true;\r
142                         }\r
143                         Assert.IsTrue (caught, "Exceptions thrown at begin of finally blocks should not be caught");\r
144 \r
145                         try {\r
146                                 caught = false;\r
147                                 ThrowAtEndOfFinally();\r
148                         } catch {\r
149                                 caught = true;\r
150                         }\r
151                         Assert.IsTrue (caught, "Exceptions thrown at end of finally blocks should not be caught");\r
152 \r
153                         try {\r
154                                 caught = false;\r
155                                 ThrowAfterFinally();\r
156                         } catch {\r
157                                 caught = true;\r
158                         }\r
159                         Assert.IsTrue (caught, "Exceptions thrown after finally blocks should not be caught");\r
160                 }\r
161                 \r
162                 private static void DoNothing()\r
163                 {\r
164                 }\r
165 \r
166                 private static void ThrowException()\r
167                 {\r
168                         throw new Exception();\r
169                 }\r
170 \r
171                 private static void ThrowBeforeTry()\r
172                 {\r
173                         ThrowException();\r
174                         try {\r
175                                 DoNothing();\r
176                         } catch (Exception) {\r
177                                 DoNothing();\r
178                         }\r
179                 }\r
180 \r
181                 private static void ThrowAtBeginOfTry()\r
182                 {\r
183                         DoNothing();\r
184                         try {\r
185                                 ThrowException();\r
186                                 DoNothing();\r
187                         } catch (Exception) {\r
188                                 DoNothing();\r
189                         }\r
190                 }\r
191 \r
192                 private static void ThrowAtEndOfTry()\r
193                 {\r
194                         DoNothing();\r
195                         try {\r
196                                 DoNothing();\r
197                                 ThrowException();\r
198                         } catch (Exception) {\r
199                                 DoNothing();\r
200                         }\r
201                 }\r
202 \r
203                 private static void ThrowAtBeginOfCatch()\r
204                 {\r
205                         DoNothing();\r
206                         try {\r
207                                 DoNothing();\r
208                                 ThrowException();\r
209                         } catch (Exception) {\r
210                                 throw;\r
211                         }\r
212                 }\r
213 \r
214                 private static void ThrowAtEndOfCatch()\r
215                 {\r
216                         DoNothing();\r
217                         try {\r
218                                 DoNothing();\r
219                                 ThrowException();\r
220                         } catch (Exception) {\r
221                                 DoNothing();\r
222                                 throw;\r
223                         }\r
224                 }\r
225 \r
226                 private static void ThrowAtBeginOfFinally()\r
227                 {\r
228                         DoNothing();\r
229                         try {\r
230                                 DoNothing();\r
231                                 ThrowException();\r
232                         } catch (Exception) {\r
233                                 DoNothing();\r
234                         } finally {\r
235                                 ThrowException();\r
236                                 DoNothing();\r
237                         }\r
238                 }\r
239 \r
240                 private static void ThrowAtEndOfFinally()\r
241                 {\r
242                         DoNothing();\r
243                         try {\r
244                                 DoNothing();\r
245                                 ThrowException();\r
246                         } catch (Exception) {\r
247                                 DoNothing();\r
248                         } finally {\r
249                                 DoNothing();\r
250                                 ThrowException();\r
251                         }\r
252                 }\r
253 \r
254                 private static void ThrowAfterFinally()\r
255                 {\r
256                         DoNothing();\r
257                         try {\r
258                                 DoNothing();\r
259                                 ThrowException();\r
260                         } catch (Exception) {\r
261                                 DoNothing();\r
262                         } finally {\r
263                                 DoNothing();\r
264                         }\r
265                         ThrowException();\r
266                 }\r
267 \r
268                 [Test]\r
269                 public void GetObjectData ()\r
270                 {\r
271                         string msg = "MESSAGE";\r
272                         Exception inner = new ArgumentException ("whatever");\r
273                         SerializationInfo si;\r
274                         Exception se;\r
275 \r
276                         se = new Exception (msg, inner);\r
277                         si = new SerializationInfo (typeof (Exception),\r
278                                 new FormatterConverter ());\r
279                         se.GetObjectData (si, new StreamingContext ());\r
280 #if NET_2_0\r
281                         Assert.AreEqual (11, si.MemberCount, "#A1");\r
282 #else\r
283                         Assert.AreEqual (10, si.MemberCount, "#A1");\r
284 #endif\r
285                         Assert.AreEqual (typeof (Exception).FullName, si.GetString ("ClassName"), "#A2");\r
286 #if NET_2_0\r
287                         Assert.IsNull (si.GetValue ("Data", typeof (IDictionary)), "#A3");\r
288 #endif\r
289                         Assert.AreSame (msg, si.GetString ("Message"), "#A4");\r
290                         Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#A5");\r
291                         Assert.AreSame (se.HelpLink, si.GetString ("HelpURL"), "#A6");\r
292                         Assert.IsNull (si.GetString ("StackTraceString"), "#A7");\r
293                         Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#A8");\r
294                         Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#A9");\r
295                         Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#A10");\r
296                         Assert.IsNull (si.GetString ("Source"), "#A11");\r
297                         Assert.IsNull (si.GetString ("ExceptionMethod"), "#A12");\r
298 \r
299                         // attempt initialization of lazy init members\r
300 #if NET_2_0\r
301                         Assert.IsNotNull (se.Data);\r
302 #endif\r
303                         Assert.IsNull (se.Source);\r
304                         Assert.IsNull (se.StackTrace);\r
305 \r
306                         si = new SerializationInfo (typeof (Exception),\r
307                                 new FormatterConverter ());\r
308                         se.GetObjectData (si, new StreamingContext ());\r
309 #if NET_2_0\r
310                         Assert.AreEqual (11, si.MemberCount, "#B1");\r
311 #else\r
312                         Assert.AreEqual (10, si.MemberCount, "#B1");\r
313 #endif\r
314                         Assert.AreEqual (typeof (Exception).FullName, si.GetString ("ClassName"), "#B2");\r
315 #if NET_2_0\r
316                         Assert.AreSame (se.Data, si.GetValue ("Data", typeof (IDictionary)), "#B3");\r
317 #endif\r
318                         Assert.AreSame (msg, si.GetString ("Message"), "#B4");\r
319                         Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#B5");\r
320                         Assert.AreSame (se.HelpLink, si.GetString ("HelpURL"), "#B6");\r
321                         Assert.IsNull (si.GetString ("StackTraceString"), "#B7");\r
322                         Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#B8");\r
323                         Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#B9");\r
324                         Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#B10");\r
325                         Assert.IsNull (si.GetString ("Source"), "#B11");\r
326                         Assert.IsNull (si.GetString ("ExceptionMethod"), "#B12");\r
327 \r
328                         try {\r
329                                 throw new Exception (msg, inner);\r
330                         } catch (Exception ex) {\r
331                                 si = new SerializationInfo (typeof (Exception),\r
332                                         new FormatterConverter ());\r
333                                 ex.GetObjectData (si, new StreamingContext ());\r
334 #if NET_2_0\r
335                                 Assert.AreEqual (11, si.MemberCount, "#C1");\r
336 #else\r
337                                 Assert.AreEqual (10, si.MemberCount, "#C1");\r
338 #endif\r
339                                 Assert.AreEqual (typeof (Exception).FullName, si.GetString ("ClassName"), "#C2");\r
340 #if NET_2_0\r
341                                 Assert.IsNull (si.GetValue ("Data", typeof (IDictionary)), "#C3");\r
342 #endif\r
343                                 Assert.AreSame (msg, si.GetString ("Message"), "#C4");\r
344                                 Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#C5");\r
345                                 Assert.AreSame (se.HelpLink, si.GetString ("HelpURL"), "#C6");\r
346                                 Assert.IsNotNull (si.GetString ("StackTraceString"), "#C7");\r
347                                 Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#C8");\r
348                                 Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#C9");\r
349                                 Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#C10");\r
350                                 Assert.IsNotNull (si.GetString ("Source"), "#C11");\r
351                                 //Assert.IsNotNull (si.GetString ("ExceptionMethod"), "#C12");\r
352                         }\r
353 \r
354                         try {\r
355                                 throw new Exception (msg, inner);\r
356                         } catch (Exception ex) {\r
357                                 // force initialization of lazy init members\r
358 #if NET_2_0\r
359                                 Assert.IsNotNull (ex.Data);\r
360 #endif\r
361                                 Assert.IsNotNull (ex.StackTrace);\r
362 \r
363                                 si = new SerializationInfo (typeof (Exception),\r
364                                         new FormatterConverter ());\r
365                                 ex.GetObjectData (si, new StreamingContext ());\r
366 #if NET_2_0\r
367                                 Assert.AreEqual (11, si.MemberCount, "#D1");\r
368 #else\r
369                                 Assert.AreEqual (10, si.MemberCount, "#D1");\r
370 #endif\r
371                                 Assert.AreEqual (typeof (Exception).FullName, si.GetString ("ClassName"), "#D2");\r
372 #if NET_2_0\r
373                                 Assert.AreSame (ex.Data, si.GetValue ("Data", typeof (IDictionary)), "#D3");\r
374 #endif\r
375                                 Assert.AreSame (msg, si.GetString ("Message"), "#D4");\r
376                                 Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#D5");\r
377                                 Assert.AreSame (ex.HelpLink, si.GetString ("HelpURL"), "#D6");\r
378                                 Assert.IsNotNull (si.GetString ("StackTraceString"), "#D7");\r
379                                 Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#D8");\r
380                                 Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#D9");\r
381                                 Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#D10");\r
382                                 Assert.AreEqual (typeof (ExceptionTest).Assembly.GetName ().Name, si.GetString ("Source"), "#D11");\r
383                                 //Assert.IsNotNull (si.GetString ("ExceptionMethod"), "#D12");\r
384                         }\r
385                 }\r
386 \r
387                 [Test]\r
388                 public void GetObjectData_Info_Null ()\r
389                 {\r
390                         Exception e = new Exception ();\r
391                         try {\r
392                                 e.GetObjectData (null, new StreamingContext ());\r
393                                 Assert.Fail ("#1");\r
394                         } catch (ArgumentNullException ex) {\r
395                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");\r
396                                 Assert.IsNull (ex.InnerException, "#3");\r
397                                 Assert.IsNotNull (ex.Message, "#4");\r
398                                 Assert.AreEqual ("info", ex.ParamName, "#5");\r
399                         }\r
400                 }\r
401 \r
402                 [Test]\r
403                 public void HResult ()\r
404                 {\r
405                         MyException ex = new MyException ();\r
406                         Assert.AreEqual (-2146233088, ex.HResult, "#1");\r
407                         ex.HResult = int.MaxValue;\r
408                         Assert.AreEqual (int.MaxValue, ex.HResult, "#2");\r
409                         ex.HResult = int.MinValue;\r
410                         Assert.AreEqual (int.MinValue, ex.HResult, "#3");\r
411                 }\r
412 \r
413                 [Test]\r
414                 public void Source ()\r
415                 {\r
416                         Exception ex1 = new Exception ("MSG");\r
417                         Assert.IsNull (ex1.Source, "#1");\r
418 \r
419                         try {\r
420                                 throw new Exception ("MSG");\r
421                         } catch (Exception ex2) {\r
422                                 Assert.AreEqual (typeof (ExceptionTest).Assembly.GetName ().Name, ex2.Source, "#2");\r
423                         }\r
424                 }\r
425 \r
426                 [Test]\r
427                 public void Source_InnerException ()\r
428                 {\r
429                         Exception a = new Exception ("a", new ArgumentException ("b"));\r
430                         a.Source = "foo";\r
431 \r
432                         Assert.IsNull (a.InnerException.Source);\r
433                 }\r
434 \r
435                 [Test]\r
436                 public void StackTrace ()\r
437                 {\r
438                         Exception ex1 = new Exception ("MSG");\r
439                         Assert.IsNull (ex1.StackTrace, "#1");\r
440 \r
441                         try {\r
442                                 throw new Exception ("MSG");\r
443                         } catch (Exception ex2) {\r
444                                 Assert.IsNotNull (ex2.StackTrace, "#2");\r
445                         }\r
446                 }\r
447 \r
448                 class MyException : Exception {\r
449                         public MyException ()\r
450                         {\r
451                         }\r
452 \r
453                         public MyException (SerializationInfo info, StreamingContext context)\r
454                                 : base (info, context)\r
455                         {\r
456                         }\r
457 \r
458                         public new int HResult {\r
459                                 get { return base.HResult; }\r
460                                 set { base.HResult = value; }\r
461                         }\r
462                 }\r
463         }\r
464 }\r