[tests] Separate MONO_PATH directories by PLATFORM_PATH_SEPARATOR
[mono.git] / mcs / class / System / Test / System.Net.Mail / SmtpExceptionTest.cs
1 //
2 // SmtpExceptionTest.cs - NUnit Test Cases for System.Net.Mail.SmtpException
3 //
4 // Authors:
5 //      Gert Driesen (drieseng@users.sourceforge.net)
6 //
7 // (C) 2008 Gert Driesen
8 //
9
10
11 using System;
12 using System.Collections;
13 using System.Net.Mail;
14 using System.Reflection;
15 using System.Runtime.Serialization;
16 using System.Runtime.Serialization.Formatters.Binary;
17
18 using NUnit.Framework;
19
20 namespace MonoTests.System.Net.Mail {
21         [TestFixture]
22         public class SmtpExceptionTest {
23                 [Test] // .ctor ()
24                 public void Constructor1 ()
25                 {
26                         SmtpException se = new SmtpException ();
27                         Assert.IsNotNull (se.Data, "#1");
28                         Assert.AreEqual (0, se.Data.Keys.Count, "#2");
29                         Assert.IsNull (se.InnerException, "#3");
30                         Assert.IsNotNull (se.Message, "#4");
31                         Assert.AreEqual (-1, se.Message.IndexOf (typeof (SmtpException).FullName), "#5:" + se.Message);
32                         Assert.AreEqual (SmtpStatusCode.GeneralFailure, se.StatusCode, "#6");
33                 }
34
35                 [Test] // .ctor (SmtpStatusCode)
36                 public void Constructor2 ()
37                 {
38                         SmtpException se;
39
40                         se = new SmtpException (SmtpStatusCode.HelpMessage);
41                         Assert.IsNotNull (se.Data, "#A1");
42                         Assert.AreEqual (0, se.Data.Keys.Count, "#A2");
43                         Assert.IsNull (se.InnerException, "#A3");
44                         Assert.IsNotNull (se.Message, "#A4");
45                         Assert.AreEqual (-1, se.Message.IndexOf (typeof (SmtpException).FullName), "#A5:" + se.Message);
46                         Assert.AreEqual (SmtpStatusCode.HelpMessage, se.StatusCode, "#A6");
47
48                         se = new SmtpException ((SmtpStatusCode) 666);
49                         Assert.IsNotNull (se.Data, "#B1");
50                         Assert.AreEqual (0, se.Data.Keys.Count, "#B2");
51                         Assert.IsNull (se.InnerException, "#B3");
52                         Assert.IsNotNull (se.Message, "#B4");
53                         Assert.AreEqual (-1, se.Message.IndexOf (typeof (SmtpException).FullName), "#B5:" + se.Message);
54                         Assert.AreEqual ((SmtpStatusCode) 666, se.StatusCode, "#B6");
55                 }
56
57                 [Test] // .ctor (String)
58                 public void Constructor3 ()
59                 {
60                         string msg;
61                         SmtpException se;
62
63                         msg = "MESSAGE";
64                         se = new SmtpException (msg);
65                         Assert.IsNotNull (se.Data, "#A1");
66                         Assert.AreEqual (0, se.Data.Keys.Count, "#A2");
67                         Assert.IsNull (se.InnerException, "#A3");
68                         Assert.AreSame (msg, se.Message, "#A4");
69                         Assert.AreEqual (SmtpStatusCode.GeneralFailure, se.StatusCode, "#A5");
70
71                         msg = string.Empty;
72                         se = new SmtpException (msg);
73                         Assert.IsNotNull (se.Data, "#B1");
74                         Assert.AreEqual (0, se.Data.Keys.Count, "#B2");
75                         Assert.IsNull (se.InnerException, "#B3");
76                         Assert.AreSame (msg, se.Message, "#B4");
77                         Assert.AreEqual (SmtpStatusCode.GeneralFailure, se.StatusCode, "#B5");
78
79                         msg = null;
80                         se = new SmtpException (msg);
81                         Assert.IsNotNull (se.Data, "#C1");
82                         Assert.AreEqual (0, se.Data.Keys.Count, "#C2");
83                         Assert.IsNull (se.InnerException, "#C3");
84                         Assert.IsNotNull (se.Message, "#C4");
85                         Assert.IsTrue (se.Message.IndexOf ("'" + typeof (SmtpException).FullName + "'") != -1, "#C5:" + se.Message);
86                         Assert.AreEqual (SmtpStatusCode.GeneralFailure, se.StatusCode, "#C6");
87                 }
88
89                 [Test] // .ctor (SerializationInfo, StreamingContext)
90                 public void Constructor4 ()
91                 {
92                         string msg = "MESSAGE";
93                         Exception inner = new ArgumentException ("whatever");
94                         SerializationInfo si = new SerializationInfo (
95                                 typeof (SmtpException), new FormatterConverter ());
96
97                         new Exception (msg, inner).GetObjectData (si,
98                                 new StreamingContext ());
99                         si.AddValue ("Status", (int) SmtpStatusCode.ServiceReady);
100
101                         SmtpException se = new MySmtpException (si, new StreamingContext ());
102                         Assert.IsNotNull (se.Data, "#1");
103                         Assert.AreEqual (0, se.Data.Keys.Count, "#2");
104                         Assert.AreSame (inner, se.InnerException, "#3");
105                         Assert.AreSame (msg, se.Message, "#4");
106                         Assert.AreEqual (SmtpStatusCode.ServiceReady, se.StatusCode, "#5");
107                 }
108
109                 [Test]
110                 public void Constructor4_SerializationInfo_Null ()
111                 {
112                         try {
113                                 new MySmtpException ((SerializationInfo) null,
114                                         new StreamingContext ());
115                                 Assert.Fail ("#1");
116                         } catch (ArgumentNullException ex) {
117                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
118                                 Assert.IsNull (ex.InnerException, "#3");
119                                 Assert.IsNotNull (ex.Message, "#4");
120                                 Assert.AreEqual ("info", ex.ParamName, "#5");
121                         }
122                 }
123
124                 [Test] // .ctor (SmtpStatusCode, String)
125                 public void Constructor5 ()
126                 {
127                         string msg;
128                         SmtpException se;
129
130                         msg = "MESSAGE";
131                         se = new SmtpException (SmtpStatusCode.HelpMessage, msg);
132                         Assert.IsNotNull (se.Data, "#A1");
133                         Assert.AreEqual (0, se.Data.Keys.Count, "#A2");
134                         Assert.IsNull (se.InnerException, "#A3");
135                         Assert.AreSame (msg, se.Message, "#A4");
136                         Assert.AreEqual (SmtpStatusCode.HelpMessage, se.StatusCode, "#A5");
137
138                         msg = string.Empty;
139                         se = new SmtpException (SmtpStatusCode.ServiceReady, msg);
140                         Assert.IsNotNull (se.Data, "#B1");
141                         Assert.AreEqual (0, se.Data.Keys.Count, "#B2");
142                         Assert.IsNull (se.InnerException, "#B3");
143                         Assert.AreSame (msg, se.Message, "#B4");
144                         Assert.AreEqual (SmtpStatusCode.ServiceReady, se.StatusCode, "#B5");
145
146                         msg = null;
147                         se = new SmtpException ((SmtpStatusCode) 666, msg);
148                         Assert.IsNotNull (se.Data, "#C1");
149                         Assert.AreEqual (0, se.Data.Keys.Count, "#C2");
150                         Assert.IsNull (se.InnerException, "#C3");
151                         Assert.IsNotNull (se.Message, "#C4");
152                         Assert.IsTrue (se.Message.IndexOf ("'" + typeof (SmtpException).FullName + "'") != -1, "#C5:" + se.Message);
153                         Assert.AreEqual ((SmtpStatusCode) 666, se.StatusCode, "#C6");
154                 }
155
156                 [Test] // .ctor (String, Exception)
157                 public void Constructor6 ()
158                 {
159                         string msg = "MESSAGE";
160                         Exception inner = new Exception ();
161                         SmtpException se;
162
163                         se = new SmtpException (msg, inner);
164                         Assert.IsNotNull (se.Data, "#A1");
165                         Assert.AreEqual (0, se.Data.Keys.Count, "#A2");
166                         Assert.AreSame (inner, se.InnerException, "#A3");
167                         Assert.AreSame (msg, se.Message, "#A4");
168                         Assert.AreEqual (SmtpStatusCode.GeneralFailure, se.StatusCode, "#A5");
169
170                         se = new SmtpException (msg, (Exception) null);
171                         Assert.IsNotNull (se.Data, "#B1");
172                         Assert.AreEqual (0, se.Data.Keys.Count, "#B2");
173                         Assert.IsNull (se.InnerException, "#B3");
174                         Assert.AreSame (msg, se.Message, "#B4");
175                         Assert.AreEqual (SmtpStatusCode.GeneralFailure, se.StatusCode, "#B5");
176
177                         se = new SmtpException ((string) null, inner);
178                         Assert.IsNotNull (se.Data, "#C1");
179                         Assert.AreEqual (0, se.Data.Keys.Count, "#C2");
180                         Assert.AreSame (inner, se.InnerException, "#C3");
181                         Assert.IsNotNull (se.Message, "#C4");
182                         Assert.AreEqual (new SmtpException ((string) null).Message, se.Message, "#C5");
183                         Assert.AreEqual (SmtpStatusCode.GeneralFailure, se.StatusCode, "#C6");
184
185                         se = new SmtpException ((string) null, (Exception) null);
186                         Assert.IsNotNull (se.Data, "#D1");
187                         Assert.AreEqual (0, se.Data.Keys.Count, "#D2");
188                         Assert.IsNull (se.InnerException, "#D3");
189                         Assert.IsNotNull (se.Message, "#D4");
190                         Assert.AreEqual (new SmtpException ((string) null).Message, se.Message, "#D5");
191                         Assert.AreEqual (SmtpStatusCode.GeneralFailure, se.StatusCode, "#D6");
192                 }
193
194                 [Test]
195                 public void GetObjectData ()
196                 {
197                         string msg = "MESSAGE";
198                         Exception inner = new ArgumentException ("whatever");
199                         SerializationInfo si;
200                         SmtpException se;
201
202                         se = new SmtpException (msg, inner);
203                         si = new SerializationInfo (typeof (SmtpException),
204                                 new FormatterConverter ());
205                         se.GetObjectData (si, new StreamingContext ());
206                         Assert.AreEqual (12, si.MemberCount, "#A1");
207                         Assert.AreEqual (typeof (SmtpException).FullName, si.GetString ("ClassName"), "#A2");
208                         Assert.IsNull (si.GetValue ("Data", typeof (IDictionary)), "#A3");
209                         Assert.AreSame (msg, si.GetString ("Message"), "#A4");
210                         Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#A5");
211                         Assert.AreSame (se.HelpLink, si.GetString ("HelpURL"), "#A6");
212                         Assert.IsNull (si.GetString ("StackTraceString"), "#A7");
213                         Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#A8");
214                         Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#A9");
215                         Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#A10");
216                         Assert.IsNull (si.GetString ("Source"), "#A11");
217                         Assert.IsNull (si.GetString ("ExceptionMethod"), "#A12");
218                         Assert.AreEqual ((int) SmtpStatusCode.GeneralFailure,
219                                 si.GetInt32 ("Status"), "#A13");
220
221                         // attempt initialization of lazy init members
222                         Assert.IsNotNull (se.Data);
223                         Assert.IsNull (se.Source);
224                         Assert.IsNull (se.StackTrace);
225
226                         si = new SerializationInfo (typeof (SmtpException),
227                                 new FormatterConverter ());
228                         se.GetObjectData (si, new StreamingContext ());
229                         Assert.AreEqual (12, si.MemberCount, "#B1");
230                         Assert.AreEqual (typeof (SmtpException).FullName, si.GetString ("ClassName"), "#B2");
231                         Assert.AreSame (se.Data, si.GetValue ("Data", typeof (IDictionary)), "#B3");
232                         Assert.AreSame (msg, si.GetString ("Message"), "#B4");
233                         Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#B5");
234                         Assert.AreSame (se.HelpLink, si.GetString ("HelpURL"), "#B6");
235                         Assert.IsNull (si.GetString ("StackTraceString"), "#B7");
236                         Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#B8");
237                         Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#B9");
238                         Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#B10");
239                         Assert.IsNull (si.GetString ("Source"), "#B11");
240                         Assert.IsNull (si.GetString ("ExceptionMethod"), "#B12");
241                         Assert.AreEqual ((int) SmtpStatusCode.GeneralFailure,
242                                 si.GetInt32 ("Status"), "#B13");
243
244                         try {
245                                 throw new SmtpException (msg, inner);
246                         } catch (SmtpException ex) {
247                                 si = new SerializationInfo (typeof (SmtpException),
248                                         new FormatterConverter ());
249                                 ex.GetObjectData (si, new StreamingContext ());
250                                 Assert.AreEqual (12, si.MemberCount, "#C1");
251                                 Assert.AreEqual (typeof (SmtpException).FullName, si.GetString ("ClassName"), "#C2");
252                                 Assert.IsNull (si.GetValue ("Data", typeof (IDictionary)), "#C3");
253                                 Assert.AreSame (msg, si.GetString ("Message"), "#C4");
254                                 Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#C5");
255                                 Assert.AreSame (se.HelpLink, si.GetString ("HelpURL"), "#C6");
256                                 Assert.IsNotNull (si.GetString ("StackTraceString"), "#C7");
257                                 Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#C8");
258                                 Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#C9");
259                                 Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#C10");
260                                 Assert.IsNotNull (si.GetString ("Source"), "#C11");
261                                 //Assert.IsNotNull (si.GetString ("ExceptionMethod"), "#C12");
262                                 Assert.AreEqual ((int) SmtpStatusCode.GeneralFailure,
263                                         si.GetInt32 ("Status"), "#C13");
264                         }
265
266                         try {
267                                 throw new SmtpException (msg, inner);
268                         } catch (SmtpException ex) {
269                                 // force initialization of lazy init members
270                                 Assert.IsNotNull (ex.Data);
271                                 Assert.IsNotNull (ex.StackTrace);
272
273                                 si = new SerializationInfo (typeof (SmtpException),
274                                         new FormatterConverter ());
275                                 ex.GetObjectData (si, new StreamingContext ());
276                                 Assert.AreEqual (12, si.MemberCount, "#D1");
277                                 Assert.AreEqual (typeof (SmtpException).FullName, si.GetString ("ClassName"), "#D2");
278                                 Assert.AreSame (ex.Data, si.GetValue ("Data", typeof (IDictionary)), "#D3");
279                                 Assert.AreSame (msg, si.GetString ("Message"), "#D4");
280                                 Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#D5");
281                                 Assert.AreSame (ex.HelpLink, si.GetString ("HelpURL"), "#D6");
282                                 Assert.IsNotNull (si.GetString ("StackTraceString"), "#D7");
283                                 Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#D8");
284                                 Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#D9");
285                                 Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#D10");
286                                 Assert.AreEqual (typeof (SmtpExceptionTest).Assembly.GetName ().Name, si.GetString ("Source"), "#D11");
287                                 //Assert.IsNotNull (si.GetString ("ExceptionMethod"), "#D12");
288                                 Assert.AreEqual ((int) SmtpStatusCode.GeneralFailure,
289                                         si.GetInt32 ("Status"), "#D13");
290                         }
291                 }
292
293                 [Test]
294                 public void GetObjectData_SerializationInfo_Null ()
295                 {
296                         SmtpException se = new SmtpException ();
297                         try {
298                                 se.GetObjectData ((SerializationInfo) null,
299                                         new StreamingContext ());
300                                 Assert.Fail ("#1");
301                         } catch (ArgumentNullException ex) {
302                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
303                                 Assert.IsNull (ex.InnerException, "#3");
304                                 Assert.IsNotNull (ex.Message, "#4");
305                                 Assert.AreEqual ("info", ex.ParamName, "#5");
306                         }
307                 }
308         }
309
310         class MySmtpException : SmtpException {
311                 public MySmtpException (SerializationInfo info, StreamingContext context)
312                         : base (info, context)
313                 {
314                 }
315         }
316 }
317