[bcl] Remove more NET_2_0 checks from class libs
[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                         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
56 \r
57                         inner = null;\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
70 \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
80                 }\r
81 \r
82 \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
88                 [Test]\r
89                 public void TestThrowOnBlockBoundaries ()\r
90                 {\r
91                         bool caught;\r
92                         \r
93                         try {\r
94                                 caught = false;\r
95                                 ThrowBeforeTry();\r
96                         } catch {\r
97                                 caught = true;\r
98                         }\r
99                         Assert.IsTrue (caught, "Exceptions thrown before try blocks should not be caught");\r
100                         \r
101                         try {\r
102                                 caught = false;\r
103                                 ThrowAtBeginOfTry();\r
104                         } catch {\r
105                                 caught = true;\r
106                         }\r
107                         Assert.IsFalse (caught, "Exceptions thrown at begin of try blocks should be caught");\r
108 \r
109                         try {\r
110                                 caught = false;\r
111                                 ThrowAtEndOfTry();\r
112                         } catch {\r
113                                 caught = true;\r
114                         }\r
115                         Assert.IsFalse (caught, "Exceptions thrown at end of try blocks should be caught");\r
116 \r
117                         try {\r
118                                 caught = false;\r
119                                 ThrowAtBeginOfCatch();\r
120                         } catch {\r
121                                 caught = true;\r
122                         }\r
123                         Assert.IsTrue (caught, "Exceptions thrown at begin of catch blocks should not be caught");\r
124 \r
125                         try {\r
126                                 caught = false;\r
127                                 ThrowAtEndOfCatch();\r
128                         } catch {\r
129                                 caught = true;\r
130                         }\r
131                         Assert.IsTrue (caught, "Exceptions thrown at end of catch blocks should not be caught");\r
132 \r
133                         try {\r
134                                 caught = false;\r
135                                 ThrowAtBeginOfFinally();\r
136                         } catch {\r
137                                 caught = true;\r
138                         }\r
139                         Assert.IsTrue (caught, "Exceptions thrown at begin of finally blocks should not be caught");\r
140 \r
141                         try {\r
142                                 caught = false;\r
143                                 ThrowAtEndOfFinally();\r
144                         } catch {\r
145                                 caught = true;\r
146                         }\r
147                         Assert.IsTrue (caught, "Exceptions thrown at end of finally blocks should not be caught");\r
148 \r
149                         try {\r
150                                 caught = false;\r
151                                 ThrowAfterFinally();\r
152                         } catch {\r
153                                 caught = true;\r
154                         }\r
155                         Assert.IsTrue (caught, "Exceptions thrown after finally blocks should not be caught");\r
156                 }\r
157                 \r
158                 private static void DoNothing()\r
159                 {\r
160                 }\r
161 \r
162                 private static void ThrowException()\r
163                 {\r
164                         throw new Exception();\r
165                 }\r
166 \r
167                 private static void ThrowBeforeTry()\r
168                 {\r
169                         ThrowException();\r
170                         try {\r
171                                 DoNothing();\r
172                         } catch (Exception) {\r
173                                 DoNothing();\r
174                         }\r
175                 }\r
176 \r
177                 private static void ThrowAtBeginOfTry()\r
178                 {\r
179                         DoNothing();\r
180                         try {\r
181                                 ThrowException();\r
182                                 DoNothing();\r
183                         } catch (Exception) {\r
184                                 DoNothing();\r
185                         }\r
186                 }\r
187 \r
188                 private static void ThrowAtEndOfTry()\r
189                 {\r
190                         DoNothing();\r
191                         try {\r
192                                 DoNothing();\r
193                                 ThrowException();\r
194                         } catch (Exception) {\r
195                                 DoNothing();\r
196                         }\r
197                 }\r
198 \r
199                 private static void ThrowAtBeginOfCatch()\r
200                 {\r
201                         DoNothing();\r
202                         try {\r
203                                 DoNothing();\r
204                                 ThrowException();\r
205                         } catch (Exception) {\r
206                                 throw;\r
207                         }\r
208                 }\r
209 \r
210                 private static void ThrowAtEndOfCatch()\r
211                 {\r
212                         DoNothing();\r
213                         try {\r
214                                 DoNothing();\r
215                                 ThrowException();\r
216                         } catch (Exception) {\r
217                                 DoNothing();\r
218                                 throw;\r
219                         }\r
220                 }\r
221 \r
222                 private static void ThrowAtBeginOfFinally()\r
223                 {\r
224                         DoNothing();\r
225                         try {\r
226                                 DoNothing();\r
227                                 ThrowException();\r
228                         } catch (Exception) {\r
229                                 DoNothing();\r
230                         } finally {\r
231                                 ThrowException();\r
232                                 DoNothing();\r
233                         }\r
234                 }\r
235 \r
236                 private static void ThrowAtEndOfFinally()\r
237                 {\r
238                         DoNothing();\r
239                         try {\r
240                                 DoNothing();\r
241                                 ThrowException();\r
242                         } catch (Exception) {\r
243                                 DoNothing();\r
244                         } finally {\r
245                                 DoNothing();\r
246                                 ThrowException();\r
247                         }\r
248                 }\r
249 \r
250                 private static void ThrowAfterFinally()\r
251                 {\r
252                         DoNothing();\r
253                         try {\r
254                                 DoNothing();\r
255                                 ThrowException();\r
256                         } catch (Exception) {\r
257                                 DoNothing();\r
258                         } finally {\r
259                                 DoNothing();\r
260                         }\r
261                         ThrowException();\r
262                 }\r
263 \r
264                 [Test]\r
265                 public void GetObjectData ()\r
266                 {\r
267                         string msg = "MESSAGE";\r
268                         Exception inner = new ArgumentException ("whatever");\r
269                         SerializationInfo si;\r
270                         Exception se;\r
271 \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
288 \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
293 \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
309 \r
310                         try {\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
328                         }\r
329 \r
330                         try {\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
336 \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
352                         }\r
353                 }\r
354 \r
355                 [Test]\r
356                 public void GetObjectData_Info_Null ()\r
357                 {\r
358                         Exception e = new Exception ();\r
359                         try {\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
367                         }\r
368                 }\r
369 \r
370                 [Test]\r
371                 public void HResult ()\r
372                 {\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
379                 }\r
380 \r
381                 [Test]\r
382                 public void Source ()\r
383                 {\r
384                         Exception ex1 = new Exception ("MSG");\r
385                         Assert.IsNull (ex1.Source, "#1");\r
386 \r
387                         try {\r
388                                 throw new Exception ("MSG");\r
389                         } catch (Exception ex2) {\r
390                                 Assert.AreEqual (typeof (ExceptionTest).Assembly.GetName ().Name, ex2.Source, "#2");\r
391                         }\r
392                 }\r
393 \r
394                 [Test]\r
395                 public void Source_InnerException ()\r
396                 {\r
397                         Exception a = new Exception ("a", new ArgumentException ("b"));\r
398                         a.Source = "foo";\r
399 \r
400                         Assert.IsNull (a.InnerException.Source);\r
401                 }\r
402 \r
403                 [Test]\r
404                 public void StackTrace ()\r
405                 {\r
406                         Exception ex1 = new Exception ("MSG");\r
407                         Assert.IsNull (ex1.StackTrace, "#1");\r
408 \r
409                         try {\r
410                                 throw new Exception ("MSG");\r
411                         } catch (Exception ex2) {\r
412                                 Assert.IsNotNull (ex2.StackTrace, "#2");\r
413                         }\r
414                 }\r
415 \r
416                 class MyException : Exception {\r
417                         public MyException ()\r
418                         {\r
419                         }\r
420 \r
421                         public MyException (SerializationInfo info, StreamingContext context)\r
422                                 : base (info, context)\r
423                         {\r
424                         }\r
425 \r
426                         public new int HResult {\r
427                                 get { return base.HResult; }\r
428                                 set { base.HResult = value; }\r
429                         }\r
430                 }\r
431         }\r
432 }\r