New test.
[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 //\r
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 //\r
10 \r
11 using System;
12 using System.Runtime.Serialization;
13 \r
14 using NUnit.Framework;\r
15 \r
16 namespace MonoTests.System\r
17 {\r
18         public class ExceptionTest : TestCase\r
19         {\r
20                 public ExceptionTest() {}\r
21                 \r
22                 // This test makes sure that exceptions thrown on block boundaries are\r
23                 // handled in the correct block. The meaning of the 'caught' variable is\r
24                 // a little confusing since there are two catchers: the method being\r
25                 // tested the the method calling the test. There is probably a better\r
26                 // name, but I can't think of it right now.\r
27 \r
28                 [Test]\r
29                 public void TestThrowOnBlockBoundaries()\r
30                 {\r
31                         bool caught;\r
32                         \r
33                         try {\r
34                                 caught = false;\r
35                                 ThrowBeforeTry();\r
36                         } catch {\r
37                                 caught = true;\r
38                         }\r
39                         Assert("Exceptions thrown before try blocks should not be caught", caught);\r
40                         \r
41                         try {\r
42                                 caught = false;\r
43                                 ThrowAtBeginOfTry();\r
44                         } catch {\r
45                                 caught = true;\r
46                         }\r
47                         Assert("Exceptions thrown at begin of try blocks should be caught", !caught);\r
48 \r
49                         try {\r
50                                 caught = false;\r
51                                 ThrowAtEndOfTry();\r
52                         } catch {\r
53                                 caught = true;\r
54                         }\r
55                         Assert("Exceptions thrown at end of try blocks should be caught", !caught);\r
56 \r
57                         try {\r
58                                 caught = false;\r
59                                 ThrowAtBeginOfCatch();\r
60                         } catch {\r
61                                 caught = true;\r
62                         }\r
63                         Assert("Exceptions thrown at begin of catch blocks should not be caught", caught);\r
64 \r
65                         try {\r
66                                 caught = false;\r
67                                 ThrowAtEndOfCatch();\r
68                         } catch {\r
69                                 caught = true;\r
70                         }\r
71                         Assert("Exceptions thrown at end of catch blocks should not be caught", caught);\r
72 \r
73                         try {\r
74                                 caught = false;\r
75                                 ThrowAtBeginOfFinally();\r
76                         } catch {\r
77                                 caught = true;\r
78                         }\r
79                         Assert("Exceptions thrown at begin of finally blocks should not be caught", caught);\r
80 \r
81                         try {\r
82                                 caught = false;\r
83                                 ThrowAtEndOfFinally();\r
84                         } catch {\r
85                                 caught = true;\r
86                         }\r
87                         Assert("Exceptions thrown at end of finally blocks should not be caught", caught);\r
88 \r
89                         try {\r
90                                 caught = false;\r
91                                 ThrowAfterFinally();\r
92                         } catch {\r
93                                 caught = true;\r
94                         }\r
95                         Assert("Exceptions thrown after finally blocks should not be caught", caught);\r
96                 }\r
97                 \r
98                 private static void DoNothing()\r
99                 {\r
100                 }\r
101 \r
102                 private static void ThrowException()\r
103                 {\r
104                         throw new Exception();\r
105                 }\r
106 \r
107                 private static void ThrowBeforeTry()\r
108                 {\r
109                         ThrowException();\r
110                         try {\r
111                                 DoNothing();\r
112                         } catch (Exception) {\r
113                                 DoNothing();\r
114                         }\r
115                 }\r
116 \r
117                 private static void ThrowAtBeginOfTry()\r
118                 {\r
119                         DoNothing();\r
120                         try {\r
121                                 ThrowException();\r
122                                 DoNothing();\r
123                         } catch (Exception) {\r
124                                 DoNothing();\r
125                         }\r
126                 }\r
127 \r
128                 private static void ThrowAtEndOfTry()\r
129                 {\r
130                         DoNothing();\r
131                         try {\r
132                                 DoNothing();\r
133                                 ThrowException();\r
134                         } catch (Exception) {\r
135                                 DoNothing();\r
136                         }\r
137                 }\r
138 \r
139                 private static void ThrowAtBeginOfCatch()\r
140                 {\r
141                         DoNothing();\r
142                         try {\r
143                                 DoNothing();\r
144                                 ThrowException();\r
145                         } catch (Exception) {\r
146                                 throw;\r
147                         }\r
148                 }\r
149 \r
150                 private static void ThrowAtEndOfCatch()\r
151                 {\r
152                         DoNothing();\r
153                         try {\r
154                                 DoNothing();\r
155                                 ThrowException();\r
156                         } catch (Exception) {\r
157                                 DoNothing();\r
158                                 throw;\r
159                         }\r
160                 }\r
161 \r
162                 private static void ThrowAtBeginOfFinally()\r
163                 {\r
164                         DoNothing();\r
165                         try {\r
166                                 DoNothing();\r
167                                 ThrowException();\r
168                         } catch (Exception) {\r
169                                 DoNothing();\r
170                         } finally {\r
171                                 ThrowException();\r
172                                 DoNothing();\r
173                         }\r
174                 }\r
175 \r
176                 private static void ThrowAtEndOfFinally()\r
177                 {\r
178                         DoNothing();\r
179                         try {\r
180                                 DoNothing();\r
181                                 ThrowException();\r
182                         } catch (Exception) {\r
183                                 DoNothing();\r
184                         } finally {\r
185                                 DoNothing();\r
186                                 ThrowException();\r
187                         }\r
188                 }\r
189 \r
190                 private static void ThrowAfterFinally()\r
191                 {\r
192                         DoNothing();\r
193                         try {\r
194                                 DoNothing();\r
195                                 ThrowException();\r
196                         } catch (Exception) {\r
197                                 DoNothing();\r
198                         } finally {\r
199                                 DoNothing();\r
200                         }\r
201                         ThrowException();\r
202                 }\r
203 \r
204                 [Test]\r
205                 public void InnerExceptionSource ()\r
206                 {\r
207                         Exception a = new Exception ("a", new ArgumentException ("b"));\r
208                         a.Source = "foo";\r
209 \r
210                         AssertEquals (null, a.InnerException.Source);\r
211                 }
212
213                 [Test]
214                 [ExpectedException (typeof (ArgumentNullException))]
215                 public void GetObjectData_Null ()
216                 {
217                         Exception e = new Exception ();
218                         e.GetObjectData (null, new StreamingContext (StreamingContextStates.All));\r
219                 }\r
220         }\r
221 }\r