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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
//
// MailAddressTest.cs - NUnit Test Cases for System.Net.MailAddress.MailAddress
//
// Authors:
// John Luke (john.luke@gmail.com)
//
// (C) 2005 John Luke
//
using NUnit.Framework;
using System;
using System.Net.Mail;
namespace MonoTests.System.Net.Mail
{
[TestFixture]
public class MailAddressTest
{
MailAddress address;
[SetUp]
public void GetReady ()
{
address = new MailAddress ("foo@example.com", "Mr. Foo Bar");
}
[Test]
public void Constructor0 ()
{
address = new MailAddress (" foo@example.com ");
Assert.AreEqual ("foo@example.com", address.Address, "#A1");
Assert.AreEqual (string.Empty, address.DisplayName, "#A2");
Assert.AreEqual ("example.com", address.Host, "#A3");
Assert.AreEqual ("foo@example.com", address.ToString (), "#A4");
Assert.AreEqual ("foo", address.User, "#A5");
address = new MailAddress ("Mr. Foo Bar <foo@example.com>");
Assert.AreEqual ("foo@example.com", address.Address, "#B1");
Assert.AreEqual ("Mr. Foo Bar", address.DisplayName, "#B2");
Assert.AreEqual ("example.com", address.Host, "#B3");
Assert.AreEqual ("\"Mr. Foo Bar\" <foo@example.com>", address.ToString (), "#B4");
Assert.AreEqual ("foo", address.User, "#B5");
address = new MailAddress ("Mr. F@@ Bar <foo@example.com> Whatever@You@Want");
Assert.AreEqual ("foo@example.com", address.Address, "#C1");
Assert.AreEqual ("Mr. F@@ Bar", address.DisplayName, "#C2");
Assert.AreEqual ("example.com", address.Host, "#C3");
Assert.AreEqual ("\"Mr. F@@ Bar\" <foo@example.com>", address.ToString (), "#C4");
Assert.AreEqual ("foo", address.User, "#C5");
address = new MailAddress ("\"Mr. F@@ Bar\" <foo@example.com> Whatever@You@Want");
Assert.AreEqual ("foo@example.com", address.Address, "#D1");
Assert.AreEqual ("Mr. F@@ Bar", address.DisplayName, "#D2");
Assert.AreEqual ("example.com", address.Host, "#D3");
Assert.AreEqual ("\"Mr. F@@ Bar\" <foo@example.com>", address.ToString (), "#D4");
Assert.AreEqual ("foo", address.User, "#D5");
address = new MailAddress ("FooBar <foo@example.com>");
Assert.AreEqual ("foo@example.com", address.Address, "#E1");
Assert.AreEqual ("FooBar", address.DisplayName, "#E2");
Assert.AreEqual ("example.com", address.Host, "#E3");
Assert.AreEqual ("\"FooBar\" <foo@example.com>", address.ToString (), "#E4");
Assert.AreEqual ("foo", address.User, "#E5");
address = new MailAddress ("\"FooBar\"foo@example.com ");
Assert.AreEqual ("foo@example.com", address.Address, "#F1");
Assert.AreEqual ("FooBar", address.DisplayName, "#F2");
Assert.AreEqual ("example.com", address.Host, "#F3");
Assert.AreEqual ("\"FooBar\" <foo@example.com>", address.ToString (), "#F4");
Assert.AreEqual ("foo", address.User, "#F5");
address = new MailAddress ("\" FooBar \"< foo@example.com >");
Assert.AreEqual ("foo@example.com", address.Address, "#G1");
Assert.AreEqual ("FooBar", address.DisplayName, "#G2");
Assert.AreEqual ("example.com", address.Host, "#G3");
Assert.AreEqual ("\"FooBar\" <foo@example.com>", address.ToString (), "#G4");
Assert.AreEqual ("foo", address.User, "#G5");
address = new MailAddress ("<foo@example.com>");
Assert.AreEqual ("foo@example.com", address.Address, "#H1");
Assert.AreEqual (string.Empty, address.DisplayName, "#H2");
Assert.AreEqual ("example.com", address.Host, "#H3");
Assert.AreEqual ("foo@example.com", address.ToString (), "#H4");
Assert.AreEqual ("foo", address.User, "#H5");
address = new MailAddress (" < foo@example.com >");
Assert.AreEqual ("foo@example.com", address.Address, "#H1");
Assert.AreEqual (string.Empty, address.DisplayName, "#H2");
Assert.AreEqual ("example.com", address.Host, "#H3");
Assert.AreEqual ("foo@example.com", address.ToString (), "#H4");
Assert.AreEqual ("foo", address.User, "#H5");
}
[Test]
public void Constructor0_Address_Null ()
{
try {
new MailAddress ((string) null);
Assert.Fail ("#1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.IsNotNull (ex.ParamName, "#5");
Assert.AreEqual ("address", ex.ParamName, "#6");
}
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void Constructor_Address_Empty ()
{
new MailAddress ("");
}
[Test]
public void Constructor0_Address_Invalid ()
{
try {
new MailAddress ("Mr. Foo Bar");
Assert.Fail ("#A1");
} catch (FormatException ex) {
// The specified string is not in the form required for an
// e-mail address
Assert.AreEqual (typeof (FormatException), ex.GetType (), "#A2");
Assert.IsNull (ex.InnerException, "#A3");
Assert.IsNotNull (ex.Message, "#A4");
}
try {
new MailAddress ("foo@b@ar");
Assert.Fail ("#B1");
} catch (FormatException ex) {
// The specified string is not in the form required for an
// e-mail address
Assert.AreEqual (typeof (FormatException), ex.GetType (), "#B2");
Assert.IsNull (ex.InnerException, "#B3");
Assert.IsNotNull (ex.Message, "#B4");
}
try {
new MailAddress ("Mr. Foo Bar <foo@exa<mple.com");
Assert.Fail ("#C1");
} catch (FormatException ex) {
// The specified string is not in the form required for an
// e-mail address
Assert.AreEqual (typeof (FormatException), ex.GetType (), "#C2");
Assert.IsNull (ex.InnerException, "#C3");
Assert.IsNotNull (ex.Message, "#C4");
}
try {
new MailAddress ("Mr. Foo Bar <foo@example.com");
Assert.Fail ("#D1");
} catch (FormatException ex) {
// The specified string is not in the form required for an
// e-mail address
Assert.AreEqual (typeof (FormatException), ex.GetType (), "#D2");
Assert.IsNull (ex.InnerException, "#D3");
Assert.IsNotNull (ex.Message, "#D4");
}
try {
new MailAddress ("Mr. \"F@@ Bar\" <foo@example.com> Whatever@You@Want");
Assert.Fail ("#E1");
} catch (FormatException ex) {
// The specified string is not in the form required for an
// e-mail address
Assert.AreEqual (typeof (FormatException), ex.GetType (), "#E2");
Assert.IsNull (ex.InnerException, "#E3");
Assert.IsNotNull (ex.Message, "#E4");
}
try {
new MailAddress ("Mr. F@@ Bar <foo@example.com> What\"ever@You@Want");
Assert.Fail ("#F1");
} catch (FormatException ex) {
// The specified string is not in the form required for an
// e-mail address
Assert.AreEqual (typeof (FormatException), ex.GetType (), "#F2");
Assert.IsNull (ex.InnerException, "#F3");
Assert.IsNotNull (ex.Message, "#F4");
}
try {
new MailAddress ("\"MrFo@Bar\"");
Assert.Fail ("#G1");
} catch (FormatException ex) {
// The specified string is not in the form required for an
// e-mail address
Assert.AreEqual (typeof (FormatException), ex.GetType (), "#G2");
Assert.IsNull (ex.InnerException, "#G3");
Assert.IsNotNull (ex.Message, "#G4");
}
try {
new MailAddress ("\"MrFo@Bar\"<>");
Assert.Fail ("#H1");
} catch (FormatException ex) {
// The specified string is not in the form required for an
// e-mail address
Assert.AreEqual (typeof (FormatException), ex.GetType (), "#H2");
Assert.IsNull (ex.InnerException, "#H3");
Assert.IsNotNull (ex.Message, "#H4");
}
try {
new MailAddress (" ");
Assert.Fail ("#I1");
} catch (FormatException ex) {
// The specified string is not in the form required for an
// e-mail address
Assert.AreEqual (typeof (FormatException), ex.GetType (), "#I2");
Assert.IsNull (ex.InnerException, "#I3");
Assert.IsNotNull (ex.Message, "#I4");
}
}
[Test]
public void Constructor1 ()
{
address = new MailAddress (" foo@example.com ", (string) null);
Assert.AreEqual ("foo@example.com", address.Address, "#A1");
Assert.AreEqual (string.Empty, address.DisplayName, "#A2");
Assert.AreEqual ("example.com", address.Host, "#A3");
Assert.AreEqual ("foo@example.com", address.ToString (), "#A4");
Assert.AreEqual ("foo", address.User, "#A5");
address = new MailAddress ("<foo@example.com> WhatEver", " Mr. Foo Bar ");
Assert.AreEqual ("foo@example.com", address.Address, "#B1");
Assert.AreEqual ("Mr. Foo Bar", address.DisplayName, "#B2");
Assert.AreEqual ("example.com", address.Host, "#B3");
Assert.AreEqual ("\"Mr. Foo Bar\" <foo@example.com>", address.ToString (), "#B4");
Assert.AreEqual ("foo", address.User, "#B5");
address = new MailAddress ("Mr. F@@ Bar <foo@example.com> Whatever", "BarFoo");
Assert.AreEqual ("foo@example.com", address.Address, "#C1");
Assert.AreEqual ("BarFoo", address.DisplayName, "#C2");
Assert.AreEqual ("example.com", address.Host, "#C3");
Assert.AreEqual ("\"BarFoo\" <foo@example.com>", address.ToString (), "#C4");
Assert.AreEqual ("foo", address.User, "#C5");
address = new MailAddress ("Mr. F@@ Bar <foo@example.com> Whatever", string.Empty);
Assert.AreEqual ("foo@example.com", address.Address, "#D1");
Assert.AreEqual (string.Empty, address.DisplayName, "#D2");
Assert.AreEqual ("example.com", address.Host, "#D3");
Assert.AreEqual ("foo@example.com", address.ToString (), "#D4");
Assert.AreEqual ("foo", address.User, "#D5");
address = new MailAddress ("Mr. F@@ Bar <foo@example.com> Whatever", (string) null);
Assert.AreEqual ("foo@example.com", address.Address, "#E1");
Assert.AreEqual ("Mr. F@@ Bar", address.DisplayName, "#E2");
Assert.AreEqual ("example.com", address.Host, "#E3");
Assert.AreEqual ("\"Mr. F@@ Bar\" <foo@example.com>", address.ToString (), "#E4");
Assert.AreEqual ("foo", address.User, "#E5");
address = new MailAddress ("Mr. F@@ Bar <foo@example.com> Whatever", " ");
Assert.AreEqual ("foo@example.com", address.Address, "#F1");
Assert.AreEqual (string.Empty, address.DisplayName, "#F2");
Assert.AreEqual ("example.com", address.Host, "#F3");
Assert.AreEqual ("foo@example.com", address.ToString (), "#F4");
Assert.AreEqual ("foo", address.User, "#F5");
}
[Test]
public void DisplayName_Precedence ()
{
var ma = new MailAddress ("Hola <foo@bar.com>");
Assert.AreEqual (ma.DisplayName, "Hola");
ma = new MailAddress ("Hola <foo@bar.com>", "Adios");
Assert.AreEqual (ma.DisplayName, "Adios");
ma = new MailAddress ("Hola <foo@bar.com>", "");
Assert.AreEqual (ma.DisplayName, "");
}
[Test]
[ExpectedException (typeof (FormatException))]
public void Address_Invalid ()
{
new MailAddress ("foobar");
}
[Test]
public void Address_QuoteFirst ()
{
new MailAddress ("\"Hola\" <foo@bar.com>");
}
[Test]
[ExpectedException (typeof (FormatException))]
public void Address_QuoteNotFirst ()
{
new MailAddress ("H\"ola\" <foo@bar.com>");
}
[Test]
[ExpectedException (typeof (FormatException))]
public void Address_NoClosingQuote ()
{
new MailAddress ("\"Hola <foo@bar.com>");
}
[Test]
[ExpectedException (typeof (FormatException))]
public void Address_NoUser ()
{
new MailAddress ("Hola <@bar.com>");
}
[Test]
[ExpectedException (typeof (FormatException))]
public void Address_NoUserNoHost ()
{
new MailAddress ("Hola <@>");
}
[Test]
public void Address ()
{
Assert.AreEqual ("foo@example.com", address.Address);
}
[Test]
public void DisplayName ()
{
Assert.AreEqual ("Mr. Foo Bar", address.DisplayName);
}
[Test]
public void Host ()
{
Assert.AreEqual ("example.com", address.Host);
}
[Test]
public void User ()
{
Assert.AreEqual ("foo", address.User);
}
[Test]
public void ToStringTest ()
{
Assert.AreEqual ("\"Mr. Foo Bar\" <foo@example.com>", address.ToString ());
}
[Test]
public void EqualsTest ()
{
var n = new MailAddress ("Mr. Bar <a@example.com>");
var n2 = new MailAddress ("a@example.com", "Mr. Bar");
Assert.AreEqual (n, n2);
}
[Test]
public void EqualsTest2 ()
{
var n = new MailAddress ("Mr. Bar <a@example.com>");
var n2 = new MailAddress ("MR. BAR <a@EXAMPLE.com>");
Assert.AreEqual (n, n2);
}
}
}
|