File: SmtpExceptionTest.cs

package info (click to toggle)
mono 2.6.7-5.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 327,344 kB
  • ctags: 413,649
  • sloc: cs: 2,471,883; xml: 1,768,594; ansic: 350,665; sh: 13,644; makefile: 8,640; perl: 1,784; asm: 717; cpp: 209; python: 146; sql: 81; sed: 16
file content (319 lines) | stat: -rw-r--r-- 12,432 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//
// SmtpExceptionTest.cs - NUnit Test Cases for System.Net.Mail.SmtpException
//
// Authors:
//	Gert Driesen (drieseng@users.sourceforge.net)
//
// (C) 2008 Gert Driesen
//

#if NET_2_0

using System;
using System.Collections;
using System.Net.Mail;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

using NUnit.Framework;

namespace MonoTests.System.Net.Mail {
	[TestFixture]
	public class SmtpExceptionTest {
		[Test] // .ctor ()
		public void Constructor1 ()
		{
			SmtpException se = new SmtpException ();
			Assert.IsNotNull (se.Data, "#1");
			Assert.AreEqual (0, se.Data.Keys.Count, "#2");
			Assert.IsNull (se.InnerException, "#3");
			Assert.IsNotNull (se.Message, "#4");
			Assert.AreEqual (-1, se.Message.IndexOf (typeof (SmtpException).FullName), "#5:" + se.Message);
			Assert.AreEqual (SmtpStatusCode.GeneralFailure, se.StatusCode, "#6");
		}

		[Test] // .ctor (SmtpStatusCode)
		public void Constructor2 ()
		{
			SmtpException se;

			se = new SmtpException (SmtpStatusCode.HelpMessage);
			Assert.IsNotNull (se.Data, "#A1");
			Assert.AreEqual (0, se.Data.Keys.Count, "#A2");
			Assert.IsNull (se.InnerException, "#A3");
			Assert.IsNotNull (se.Message, "#A4");
			Assert.AreEqual (-1, se.Message.IndexOf (typeof (SmtpException).FullName), "#A5:" + se.Message);
			Assert.AreEqual (SmtpStatusCode.HelpMessage, se.StatusCode, "#A6");

			se = new SmtpException ((SmtpStatusCode) 666);
			Assert.IsNotNull (se.Data, "#B1");
			Assert.AreEqual (0, se.Data.Keys.Count, "#B2");
			Assert.IsNull (se.InnerException, "#B3");
			Assert.IsNotNull (se.Message, "#B4");
			Assert.AreEqual (-1, se.Message.IndexOf (typeof (SmtpException).FullName), "#B5:" + se.Message);
			Assert.AreEqual ((SmtpStatusCode) 666, se.StatusCode, "#B6");
		}

		[Test] // .ctor (String)
		public void Constructor3 ()
		{
			string msg;
			SmtpException se;

			msg = "MESSAGE";
			se = new SmtpException (msg);
			Assert.IsNotNull (se.Data, "#A1");
			Assert.AreEqual (0, se.Data.Keys.Count, "#A2");
			Assert.IsNull (se.InnerException, "#A3");
			Assert.AreSame (msg, se.Message, "#A4");
			Assert.AreEqual (SmtpStatusCode.GeneralFailure, se.StatusCode, "#A5");

			msg = string.Empty;
			se = new SmtpException (msg);
			Assert.IsNotNull (se.Data, "#B1");
			Assert.AreEqual (0, se.Data.Keys.Count, "#B2");
			Assert.IsNull (se.InnerException, "#B3");
			Assert.AreSame (msg, se.Message, "#B4");
			Assert.AreEqual (SmtpStatusCode.GeneralFailure, se.StatusCode, "#B5");

			msg = null;
			se = new SmtpException (msg);
			Assert.IsNotNull (se.Data, "#C1");
			Assert.AreEqual (0, se.Data.Keys.Count, "#C2");
			Assert.IsNull (se.InnerException, "#C3");
			Assert.IsNotNull (se.Message, "#C4");
			Assert.IsTrue (se.Message.IndexOf ("'" + typeof (SmtpException).FullName + "'") != -1, "#C5:" + se.Message);
			Assert.AreEqual (SmtpStatusCode.GeneralFailure, se.StatusCode, "#C6");
		}

		[Test] // .ctor (SerializationInfo, StreamingContext)
		public void Constructor4 ()
		{
			string msg = "MESSAGE";
			Exception inner = new ArgumentException ("whatever");
			SerializationInfo si = new SerializationInfo (
				typeof (SmtpException), new FormatterConverter ());

			new Exception (msg, inner).GetObjectData (si,
				new StreamingContext ());
			si.AddValue ("Status", (int) SmtpStatusCode.ServiceReady);

			SmtpException se = new MySmtpException (si, new StreamingContext ());
			Assert.IsNotNull (se.Data, "#1");
			Assert.AreEqual (0, se.Data.Keys.Count, "#2");
			Assert.AreSame (inner, se.InnerException, "#3");
			Assert.AreSame (msg, se.Message, "#4");
			Assert.AreEqual (SmtpStatusCode.ServiceReady, se.StatusCode, "#5");
		}

		[Test]
		public void Constructor4_SerializationInfo_Null ()
		{
			try {
				new MySmtpException ((SerializationInfo) null,
					new StreamingContext ());
				Assert.Fail ("#1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.AreEqual ("info", ex.ParamName, "#5");
			}
		}

		[Test] // .ctor (SmtpStatusCode, String)
		public void Constructor5 ()
		{
			string msg;
			SmtpException se;

			msg = "MESSAGE";
			se = new SmtpException (SmtpStatusCode.HelpMessage, msg);
			Assert.IsNotNull (se.Data, "#A1");
			Assert.AreEqual (0, se.Data.Keys.Count, "#A2");
			Assert.IsNull (se.InnerException, "#A3");
			Assert.AreSame (msg, se.Message, "#A4");
			Assert.AreEqual (SmtpStatusCode.HelpMessage, se.StatusCode, "#A5");

			msg = string.Empty;
			se = new SmtpException (SmtpStatusCode.ServiceReady, msg);
			Assert.IsNotNull (se.Data, "#B1");
			Assert.AreEqual (0, se.Data.Keys.Count, "#B2");
			Assert.IsNull (se.InnerException, "#B3");
			Assert.AreSame (msg, se.Message, "#B4");
			Assert.AreEqual (SmtpStatusCode.ServiceReady, se.StatusCode, "#B5");

			msg = null;
			se = new SmtpException ((SmtpStatusCode) 666, msg);
			Assert.IsNotNull (se.Data, "#C1");
			Assert.AreEqual (0, se.Data.Keys.Count, "#C2");
			Assert.IsNull (se.InnerException, "#C3");
			Assert.IsNotNull (se.Message, "#C4");
			Assert.IsTrue (se.Message.IndexOf ("'" + typeof (SmtpException).FullName + "'") != -1, "#C5:" + se.Message);
			Assert.AreEqual ((SmtpStatusCode) 666, se.StatusCode, "#C6");
		}

		[Test] // .ctor (String, Exception)
		public void Constructor6 ()
		{
			string msg = "MESSAGE";
			Exception inner = new Exception ();
			SmtpException se;

			se = new SmtpException (msg, inner);
			Assert.IsNotNull (se.Data, "#A1");
			Assert.AreEqual (0, se.Data.Keys.Count, "#A2");
			Assert.AreSame (inner, se.InnerException, "#A3");
			Assert.AreSame (msg, se.Message, "#A4");
			Assert.AreEqual (SmtpStatusCode.GeneralFailure, se.StatusCode, "#A5");

			se = new SmtpException (msg, (Exception) null);
			Assert.IsNotNull (se.Data, "#B1");
			Assert.AreEqual (0, se.Data.Keys.Count, "#B2");
			Assert.IsNull (se.InnerException, "#B3");
			Assert.AreSame (msg, se.Message, "#B4");
			Assert.AreEqual (SmtpStatusCode.GeneralFailure, se.StatusCode, "#B5");

			se = new SmtpException ((string) null, inner);
			Assert.IsNotNull (se.Data, "#C1");
			Assert.AreEqual (0, se.Data.Keys.Count, "#C2");
			Assert.AreSame (inner, se.InnerException, "#C3");
			Assert.IsNotNull (se.Message, "#C4");
			Assert.AreEqual (new SmtpException ((string) null).Message, se.Message, "#C5");
			Assert.AreEqual (SmtpStatusCode.GeneralFailure, se.StatusCode, "#C6");

			se = new SmtpException ((string) null, (Exception) null);
			Assert.IsNotNull (se.Data, "#D1");
			Assert.AreEqual (0, se.Data.Keys.Count, "#D2");
			Assert.IsNull (se.InnerException, "#D3");
			Assert.IsNotNull (se.Message, "#D4");
			Assert.AreEqual (new SmtpException ((string) null).Message, se.Message, "#D5");
			Assert.AreEqual (SmtpStatusCode.GeneralFailure, se.StatusCode, "#D6");
		}

		[Test]
		public void GetObjectData ()
		{
			string msg = "MESSAGE";
			Exception inner = new ArgumentException ("whatever");
			SerializationInfo si;
			SmtpException se;

			se = new SmtpException (msg, inner);
			si = new SerializationInfo (typeof (SmtpException),
				new FormatterConverter ());
			se.GetObjectData (si, new StreamingContext ());
			Assert.AreEqual (12, si.MemberCount, "#A1");
			Assert.AreEqual (typeof (SmtpException).FullName, si.GetString ("ClassName"), "#A2");
			Assert.IsNull (si.GetValue ("Data", typeof (IDictionary)), "#A3");
			Assert.AreSame (msg, si.GetString ("Message"), "#A4");
			Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#A5");
			Assert.AreSame (se.HelpLink, si.GetString ("HelpURL"), "#A6");
			Assert.IsNull (si.GetString ("StackTraceString"), "#A7");
			Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#A8");
			Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#A9");
			Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#A10");
			Assert.IsNull (si.GetString ("Source"), "#A11");
			Assert.IsNull (si.GetString ("ExceptionMethod"), "#A12");
			Assert.AreEqual ((int) SmtpStatusCode.GeneralFailure,
				si.GetInt32 ("Status"), "#A13");

			// attempt initialization of lazy init members
			Assert.IsNotNull (se.Data);
			Assert.IsNull (se.Source);
			Assert.IsNull (se.StackTrace);

			si = new SerializationInfo (typeof (SmtpException),
				new FormatterConverter ());
			se.GetObjectData (si, new StreamingContext ());
			Assert.AreEqual (12, si.MemberCount, "#B1");
			Assert.AreEqual (typeof (SmtpException).FullName, si.GetString ("ClassName"), "#B2");
			Assert.AreSame (se.Data, si.GetValue ("Data", typeof (IDictionary)), "#B3");
			Assert.AreSame (msg, si.GetString ("Message"), "#B4");
			Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#B5");
			Assert.AreSame (se.HelpLink, si.GetString ("HelpURL"), "#B6");
			Assert.IsNull (si.GetString ("StackTraceString"), "#B7");
			Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#B8");
			Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#B9");
			Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#B10");
			Assert.IsNull (si.GetString ("Source"), "#B11");
			Assert.IsNull (si.GetString ("ExceptionMethod"), "#B12");
			Assert.AreEqual ((int) SmtpStatusCode.GeneralFailure,
				si.GetInt32 ("Status"), "#B13");

			try {
				throw new SmtpException (msg, inner);
			} catch (SmtpException ex) {
				si = new SerializationInfo (typeof (SmtpException),
					new FormatterConverter ());
				ex.GetObjectData (si, new StreamingContext ());
				Assert.AreEqual (12, si.MemberCount, "#C1");
				Assert.AreEqual (typeof (SmtpException).FullName, si.GetString ("ClassName"), "#C2");
				Assert.IsNull (si.GetValue ("Data", typeof (IDictionary)), "#C3");
				Assert.AreSame (msg, si.GetString ("Message"), "#C4");
				Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#C5");
				Assert.AreSame (se.HelpLink, si.GetString ("HelpURL"), "#C6");
				Assert.IsNotNull (si.GetString ("StackTraceString"), "#C7");
				Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#C8");
				Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#C9");
				Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#C10");
				Assert.IsNotNull (si.GetString ("Source"), "#C11");
				//Assert.IsNotNull (si.GetString ("ExceptionMethod"), "#C12");
				Assert.AreEqual ((int) SmtpStatusCode.GeneralFailure,
					si.GetInt32 ("Status"), "#C13");
			}

			try {
				throw new SmtpException (msg, inner);
			} catch (SmtpException ex) {
				// force initialization of lazy init members
				Assert.IsNotNull (ex.Data);
				Assert.IsNotNull (ex.StackTrace);

				si = new SerializationInfo (typeof (SmtpException),
					new FormatterConverter ());
				ex.GetObjectData (si, new StreamingContext ());
				Assert.AreEqual (12, si.MemberCount, "#D1");
				Assert.AreEqual (typeof (SmtpException).FullName, si.GetString ("ClassName"), "#D2");
				Assert.AreSame (ex.Data, si.GetValue ("Data", typeof (IDictionary)), "#D3");
				Assert.AreSame (msg, si.GetString ("Message"), "#D4");
				Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#D5");
				Assert.AreSame (ex.HelpLink, si.GetString ("HelpURL"), "#D6");
				Assert.IsNotNull (si.GetString ("StackTraceString"), "#D7");
				Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#D8");
				Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#D9");
				Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#D10");
				Assert.AreEqual (typeof (SmtpExceptionTest).Assembly.GetName ().Name, si.GetString ("Source"), "#D11");
				//Assert.IsNotNull (si.GetString ("ExceptionMethod"), "#D12");
				Assert.AreEqual ((int) SmtpStatusCode.GeneralFailure,
					si.GetInt32 ("Status"), "#D13");
			}
		}

		[Test]
		public void GetObjectData_SerializationInfo_Null ()
		{
			SmtpException se = new SmtpException ();
			try {
				se.GetObjectData ((SerializationInfo) null,
					new StreamingContext ());
				Assert.Fail ("#1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.AreEqual ("info", ex.ParamName, "#5");
			}
		}
	}

	class MySmtpException : SmtpException {
		public MySmtpException (SerializationInfo info, StreamingContext context)
			: base (info, context)
		{
		}
	}
}

#endif