2
0

SmtpExceptionTest.cs 12 KB

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