EmailAddressTest.cs
上传用户:wdhx888
上传日期:2017-06-08
资源大小:112k
文件大小:5k
源码类别:

WEB邮件程序

开发平台:

C#

  1. namespace EricDaugherty.CSES.Common
  2. {
  3. using System;
  4. using NUnit.Framework;
  5. /// <summary>
  6. /// Test cases for the EmailAddress class.  These tests
  7. /// also covers the InvalidEmailAddressException class.
  8. /// </summary>
  9. /// <remarks>
  10. /// Created By: Eric Daugherty
  11. /// </remarks>
  12. [TestFixture]
  13. public class EmailAddressTest
  14. {
  15. #region Username Tests
  16. [Test]
  17. [ExpectedException(typeof(InvalidEmailAddressException))]
  18. public void TestNullUsername()
  19. {
  20. new EmailAddress( null, "mydomain.com" );
  21. }
  22. [Test]
  23. [ExpectedException(typeof(InvalidEmailAddressException))]
  24. public void TestEmptyUsername()
  25. {
  26. new EmailAddress( "", "mydomain.com" );
  27. }
  28. [Test]
  29. [ExpectedException(typeof(InvalidEmailAddressException))]
  30. public void TestInvalidUsername1()
  31. {
  32. new EmailAddress( "m@e", "mydomain.com" );
  33. }
  34. [Test]
  35. [ExpectedException(typeof(InvalidEmailAddressException))]
  36. public void TestInvalidUsername2()
  37. {
  38. new EmailAddress( "m<e", "mydomain.com" );
  39. }
  40. [Test]
  41. [ExpectedException(typeof(InvalidEmailAddressException))]
  42. public void TestInvalidUsername3()
  43. {
  44. new EmailAddress( "m:e", "mydomain.com" );
  45. }
  46. #endregion
  47. #region Domain Tests
  48. [Test]
  49. [ExpectedException(typeof(InvalidEmailAddressException))]
  50. public void TestNullDomain()
  51. {
  52. new EmailAddress( "user", null );
  53. }
  54. [Test]
  55. [ExpectedException(typeof(InvalidEmailAddressException))]
  56. public void TestEmptyDomain()
  57. {
  58. new EmailAddress( "user", "" );
  59. }
  60. [Test]
  61. [ExpectedException(typeof(InvalidEmailAddressException))]
  62. public void TestInvalidDomain1()
  63. {
  64. new EmailAddress( "user", "mydom]in.com" );
  65. }
  66. [Test]
  67. [ExpectedException(typeof(InvalidEmailAddressException))]
  68. public void TestInvalidDomain2()
  69. {
  70. new EmailAddress( "user", "mydo;main.com" );
  71. }
  72. [Test]
  73. [ExpectedException(typeof(InvalidEmailAddressException))]
  74. public void TestInvalidDomain3()
  75. {
  76. new EmailAddress( "user", "mydo"main.com" );
  77. }
  78. #endregion
  79. #region Valid Username/Domain Tests
  80. [Test]
  81. public void TestValidUsername()
  82. {
  83. EmailAddress address = new EmailAddress( "us er", "mydomain.com" );
  84. Assertion.Assert( "Username incorrect", "us er".Equals( address.Username ) );
  85. Assertion.Assert( "Domain incorrect", "mydomain.com".Equals( address.Domain ) );
  86. Assertion.Assert( "Address incorrect", "us er@mydomain.com".Equals( address.Address ) );
  87. Assertion.Assert( "ToString incorrect", "us er@mydomain.com".Equals( address.ToString() ) );
  88. address = new EmailAddress( "my_name", "my_domain.com" );
  89. address = new EmailAddress( "my_name100@mydomain.com" );
  90. }
  91. #endregion
  92. #region Address tests
  93. [Test]
  94. [ExpectedException(typeof(InvalidEmailAddressException))]
  95. public void TestAddressNoUsername()
  96. {
  97. new EmailAddress( "@mydomain.com" );
  98. }
  99. [Test]
  100. [ExpectedException(typeof(InvalidEmailAddressException))]
  101. public void TestAddressNoDomain()
  102. {
  103. new EmailAddress( "user@" );
  104. }
  105. [Test]
  106. [ExpectedException(typeof(InvalidEmailAddressException))]
  107. public void TestAddressNoAt()
  108. {
  109. new EmailAddress( "usermydomain.com" );
  110. }
  111. [Test]
  112. [ExpectedException(typeof(InvalidEmailAddressException))]
  113. public void TestAddressInvalid1()
  114. {
  115. new EmailAddress( "us@er@mydomain.com" );
  116. }
  117. [Test]
  118. [ExpectedException(typeof(InvalidEmailAddressException))]
  119. public void TestAddressInvalid2()
  120. {
  121. new EmailAddress( "user@mydo:main.com" );
  122. }
  123. #endregion
  124. #region Valid Address Tests
  125. [Test]
  126. public void TestAddress()
  127. {
  128. string addressString = "user@mydomain.com";
  129. EmailAddress address = new EmailAddress( addressString );
  130. Assertion.AssertEquals( "Username incorrect", "user", address.Username );
  131. Assertion.AssertEquals( "Domain incorrect", "mydomain.com", address.Domain );
  132. Assertion.AssertEquals( "Address incorrect", addressString, address.Address );
  133. Assertion.AssertEquals( "ToString incorrect", addressString, address.ToString() );
  134. }
  135. [Test]
  136. public void TestAddress1()
  137. {
  138. string addressString = "eric@ericdomain.com";
  139. EmailAddress address = new EmailAddress( addressString );
  140. Assertion.AssertEquals( "Username incorrect", "eric", address.Username );
  141. Assertion.AssertEquals( "Domain incorrect", "ericdomain.com", address.Domain );
  142. Assertion.AssertEquals( "Address incorrect", addressString, address.Address );
  143. Assertion.AssertEquals( "ToString incorrect", addressString, address.ToString() );
  144. }
  145. #endregion
  146. }
  147. }