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
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Xunit;
namespace System.ComponentModel.DataAnnotations
{
public class EmailAddressAttributeTests
{
private static readonly ValidationContext s_testValidationContext = new ValidationContext(new object());
[Fact]
public static void EmailAddressAttribute_creation_DataType_and_CustomDataType()
{
var attribute = new EmailAddressAttribute();
Assert.Equal(DataType.EmailAddress, attribute.DataType);
Assert.Null(attribute.CustomDataType);
}
[Fact]
public static void Validate_successful_for_null_address()
{
var attribute = new EmailAddressAttribute();
AssertEx.DoesNotThrow(() => attribute.Validate(null, s_testValidationContext)); // Null is valid
}
[Fact]
public static void Validate_successful_for_valid_local_part()
{
var attribute = new EmailAddressAttribute();
AssertEx.DoesNotThrow(() => attribute.Validate("someName@someDomain.com", s_testValidationContext)); // Simple valid value
AssertEx.DoesNotThrow(() => attribute.Validate("1234@someDomain.com", s_testValidationContext)); // numbers are valid
AssertEx.DoesNotThrow(() => attribute.Validate("firstName.lastName@someDomain.com", s_testValidationContext)); // With dot in name
AssertEx.DoesNotThrow(() => attribute.Validate("\u00A0@someDomain.com", s_testValidationContext)); // With valid \u character
AssertEx.DoesNotThrow(() => attribute.Validate("!#$%&'*+-/=?^_`|~@someDomain.com", s_testValidationContext)); // With valid (but unusual) characters
AssertEx.DoesNotThrow(() => attribute.Validate("\"firstName.lastName\"@someDomain.com", s_testValidationContext)); // quotes around whole local part
}
[Fact]
public static void Validate_successful_for_valid_domain_part()
{
var attribute = new EmailAddressAttribute();
AssertEx.DoesNotThrow(() => attribute.Validate("someName@someDomain.com", s_testValidationContext)); // Simple valid value
AssertEx.DoesNotThrow(() => attribute.Validate("someName@some~domain.com", s_testValidationContext)); // With tilde
AssertEx.DoesNotThrow(() => attribute.Validate("someName@some_domain.com", s_testValidationContext)); // With underscore
AssertEx.DoesNotThrow(() => attribute.Validate("someName@1234.com", s_testValidationContext)); // numbers are valid
AssertEx.DoesNotThrow(() => attribute.Validate("someName@someDomain\uFFEF.com", s_testValidationContext)); // With valid \u character
}
[Fact]
public static void Validate_throws_for_invalid_local_part()
{
var attribute = new EmailAddressAttribute();
Assert.Throws<ValidationException>(() => attribute.Validate("@someDomain.com", s_testValidationContext)); // no local part
Assert.Throws<ValidationException>(() => attribute.Validate("@someDomain@abc.com", s_testValidationContext)); // multiple @'s
}
[Fact]
public static void Validate_throws_for_invalid_domain_name()
{
var attribute = new EmailAddressAttribute();
Assert.Throws<ValidationException>(() => attribute.Validate("someName", s_testValidationContext)); // no domain
Assert.Throws<ValidationException>(() => attribute.Validate("someName@", s_testValidationContext)); // no domain
Assert.Throws<ValidationException>(() => attribute.Validate("someName@a@b.com", s_testValidationContext)); // multiple @'s
}
[Fact]
public static void Validate_throws_InvalidOperationException_if_ErrorMessage_is_null()
{
var attribute = new EmailAddressAttribute();
attribute.ErrorMessage = null; // note: this overrides the default value
Assert.Throws<InvalidOperationException>(() => attribute.Validate("InvalidEmailAddress", s_testValidationContext));
}
[Fact]
public static void Validate_throws_InvalidOperationException_if_ErrorMessage_and_ErrorMessageResourceName_are_set()
{
var attribute = new EmailAddressAttribute();
attribute.ErrorMessage = "SomeErrorMessage";
attribute.ErrorMessageResourceName = "SomeErrorMessageResourceName";
Assert.Throws<InvalidOperationException>(() => attribute.Validate("InvalidEmailAddress", s_testValidationContext));
}
[Fact]
public static void Validate_throws_InvalidOperationException_if_ErrorMessageResourceName_set_but_ErrorMessageResourceType_not_set()
{
var attribute = new EmailAddressAttribute();
attribute.ErrorMessageResourceName = "SomeErrorMessageResourceName";
attribute.ErrorMessageResourceType = null;
Assert.Throws<InvalidOperationException>(() => attribute.Validate("InvalidEmailAddress", s_testValidationContext));
}
[Fact]
public static void Validate_throws_InvalidOperationException_if_ErrorMessageResourceType_set_but_ErrorMessageResourceName_not_set()
{
var attribute = new EmailAddressAttribute();
attribute.ErrorMessageResourceName = null;
attribute.ErrorMessageResourceType = typeof(ErrorMessageResources);
Assert.Throws<InvalidOperationException>(() => attribute.Validate("InvalidEmailAddress", s_testValidationContext));
}
[Fact]
public static void GetValidationResult_returns_ErrorMessage_if_ErrorMessage_overrides_default()
{
var attribute = new EmailAddressAttribute();
attribute.ErrorMessage = "SomeErrorMessage";
var toBeTested = new EmailClassToBeTested();
var validationContext = new ValidationContext(toBeTested);
validationContext.MemberName = "EmailPropertyToBeTested";
var validationResult = attribute.GetValidationResult(toBeTested, validationContext);
Assert.Equal("SomeErrorMessage", validationResult.ErrorMessage);
}
[Fact]
public static void GetValidationResult_returns_DefaultErrorMessage_if_ErrorMessage_is_not_set()
{
var attribute = new EmailAddressAttribute();
var toBeTested = new EmailClassToBeTested();
var validationContext = new ValidationContext(toBeTested);
validationContext.MemberName = "EmailPropertyToBeTested";
AssertEx.DoesNotThrow(() => attribute.GetValidationResult(toBeTested, validationContext));
}
[Fact]
public static void GetValidationResult_returns_ErrorMessage_from_resource_if_ErrorMessageResourceName_and_ErrorMessageResourceType_both_set()
{
var attribute = new EmailAddressAttribute();
attribute.ErrorMessageResourceName = "InternalErrorMessageTestProperty";
attribute.ErrorMessageResourceType = typeof(ErrorMessageResources);
var toBeTested = new EmailClassToBeTested();
var validationContext = new ValidationContext(toBeTested);
validationContext.MemberName = "EmailPropertyToBeTested";
var validationResult = attribute.GetValidationResult(toBeTested, validationContext);
Assert.Equal(
"Error Message from ErrorMessageResources.InternalErrorMessageTestProperty",
validationResult.ErrorMessage);
}
}
public class EmailClassToBeTested
{
public string EmailPropertyToBeTested
{
get { return "InvalidEmailAddress"; }
}
}
}
|