19
19
import static org .junit .jupiter .api .Assumptions .assumeTrue ;
20
20
21
21
class EmailTest {
22
+ private static final NormalizationOptions MINIMAL_NORMALIZATION = NormalizationOptions
23
+ .builder ()
24
+ .adjustCase (CaseOption .NO_CHANGE )
25
+ .build ();
22
26
23
27
@ Test
24
28
void ensureEqualsContract () {
@@ -59,68 +63,73 @@ void ensureNormalizedIsCorrectForLongComment() {
59
63
void ensureNormalizedStripsQuotes (String address , String expected ) {
60
64
assertThat (Email .of (address ))
61
65
.isPresent ().get ()
62
- .returns (expected , email -> email .normalized (
63
- NormalizationOptions .builder ().stripQuotes ().build ()));
66
+ .returns (expected , email -> email .normalized (NormalizationOptions .builder ()
67
+ .adjustCase (CaseOption .NO_CHANGE )
68
+ .stripQuotes ()
69
+ .build ()));
64
70
65
71
// Check that nothing happens when stripQuotes is false
66
72
assertThat (Email .of (address ))
67
73
.isPresent ().get ()
68
- .returns (address , email -> email .normalized (
69
- NormalizationOptions .builder ().build ()));
74
+ .returns (address , email -> email .normalized (MINIMAL_NORMALIZATION ));
70
75
}
71
76
72
77
@ ParameterizedTest (name = "{0}" )
73
78
@ MethodSource ("provideValidForLowerCase" )
74
79
void ensureNormalizedConvertsToLowerCase (String address , String expected ) {
80
+ // Lowercase conversion is default
75
81
assertThat (Email .of (address ))
76
82
.isPresent ().get ()
77
83
.returns (expected , email -> email .normalized (
78
- NormalizationOptions .builder ().adjustCase ( CaseOption . LOWERCASE ). build ()));
84
+ NormalizationOptions .builder ().build ()));
79
85
80
86
// Check that nothing happens when adjustCase is NO_CHANGE
81
87
assertThat (Email .of (address ))
82
88
.isPresent ().get ()
83
- .returns (address , email -> email .normalized (
84
- NormalizationOptions .builder ().build ()));
89
+ .returns (address , email -> email .normalized (MINIMAL_NORMALIZATION ));
85
90
}
86
91
87
92
@ ParameterizedTest (name = "{0}" )
88
93
@ MethodSource ("provideValidForDots" )
89
94
void ensureNormalizedRemovesDots (String address , String expected ) {
90
95
assertThat (Email .of (address ))
91
96
.isPresent ().get ()
92
- .returns (expected , email -> email .normalized (
93
- NormalizationOptions .builder ().removeDots ().build ()));
97
+ .returns (expected , email -> email .normalized (NormalizationOptions .builder ()
98
+ .adjustCase (CaseOption .NO_CHANGE )
99
+ .removeDots ()
100
+ .build ()));
94
101
95
102
// Check that nothing happens when removeDots is false
96
103
assertThat (Email .of (address ))
97
104
.isPresent ().get ()
98
- .returns (address , email -> email .normalized (
99
- NormalizationOptions .builder ().build ()));
105
+ .returns (address , email -> email .normalized (MINIMAL_NORMALIZATION ));
100
106
}
101
107
102
108
@ ParameterizedTest (name = "{0}" )
103
109
@ MethodSource ("provideValidForSubAddress" )
104
110
void ensureNormalizedRemovesSubAddresses (String address , String expected ) {
105
111
assertThat (Email .of (address ))
106
112
.isPresent ().get ()
107
- .returns (expected , email -> email .normalized (
108
- NormalizationOptions .builder ().removeSubAddress ().build ()));
113
+ .returns (expected , email -> email .normalized (NormalizationOptions .builder ()
114
+ .adjustCase (CaseOption .NO_CHANGE )
115
+ .removeSubAddress ()
116
+ .build ()));
109
117
110
118
// Check that nothing happens when removeSubAddress is false
111
119
assertThat (Email .of (address ))
112
120
.isPresent ().get ()
113
- .returns (address , email -> email .normalized (
114
- NormalizationOptions .builder ().build ()));
121
+ .returns (address , email -> email .normalized (MINIMAL_NORMALIZATION ));
115
122
}
116
123
117
124
@ ParameterizedTest (name = "{0}" )
118
125
@ MethodSource ("provideValidForSubAddressSeparator" )
119
126
void ensureNormalizedRemovesSubAddressesWithCustomSeparator (String address , String expected ) {
120
127
assertThat (Email .of (address ))
121
128
.isPresent ().get ()
122
- .returns (expected , email -> email .normalized (
123
- NormalizationOptions .builder ().removeSubAddress ("%%" ).build ()));
129
+ .returns (expected , email -> email .normalized (NormalizationOptions .builder ()
130
+ .adjustCase (CaseOption .NO_CHANGE )
131
+ .removeSubAddress ("%%" )
132
+ .build ()));
124
133
}
125
134
126
135
@ Test
@@ -129,8 +138,11 @@ void ensureNormalizedPerformsUnicodeNormalization() {
129
138
130
139
assertThat (Email .of (address ))
131
140
.isPresent ().get ()
132
- .
returns (
"Äffintest1⁄[email protected] " ,
email ->
email .
normalized (
133
- NormalizationOptions .builder ().performUnicodeNormalization ().build ()));
141
+ .
returns (
"Äffintest1⁄[email protected] " ,
email ->
email .
normalized (
NormalizationOptions
142
+ .builder ()
143
+ .adjustCase (CaseOption .NO_CHANGE )
144
+ .performUnicodeNormalization ()
145
+ .build ()));
134
146
}
135
147
136
148
@ Test
@@ -139,10 +151,11 @@ void ensureNormalizedPerformsUnicodeNormalizationWithCustomForm() {
139
151
140
152
assertThat (Email .of (address ))
141
153
.isPresent ().get ()
142
- .returns ("Äffintest½@gmail.com" , email -> email .normalized (
143
- NormalizationOptions .builder ()
144
- .performUnicodeNormalization (Normalizer .Form .NFC )
145
- .build ()));
154
+ .returns ("Äffintest½@gmail.com" , email -> email .normalized (NormalizationOptions
155
+ .builder ()
156
+ .adjustCase (CaseOption .NO_CHANGE )
157
+ .performUnicodeNormalization (Normalizer .Form .NFC )
158
+ .build ()));
146
159
}
147
160
148
161
@ ParameterizedTest (name = "{0}" )
@@ -161,8 +174,32 @@ void ensureNormalizedStripsQuotesForAllValidAddresses(String address) {
161
174
162
175
assertThat (Email .of (quoted ))
163
176
.isPresent ().get ()
164
- .returns (validated .normalized (), email -> email .normalized (
165
- NormalizationOptions .builder ().stripQuotes ().build ()));
177
+ .returns (
178
+ validated .normalized (MINIMAL_NORMALIZATION ),
179
+ email -> email .normalized (NormalizationOptions .builder ()
180
+ .adjustCase (CaseOption .NO_CHANGE )
181
+ .stripQuotes ()
182
+ .build ()));
183
+ }
184
+
185
+ @ ParameterizedTest (name = "{0}" )
186
+ @ ValueSource (strings = {
187
+ "aaa@[123.123.123.123]" , "first.last@[IPv6:a1::11.22.33.44]" ,
188
+ "FIRST.LAST@[IPv6:a1::b2:11.22.33.44]" , "aAa@[123.123.123.123]" ,
189
+ "hElLo23@[1.2.3.4]"
190
+ })
191
+ void ensureNormalizedDoesNotAdjustCaseOfIPDomains (String address ) {
192
+ Email validated = Email .of (address ).get ();
193
+
194
+ // The test only works for IP domains
195
+ assumeTrue (validated .isIpAddress ());
196
+
197
+ String normalized = validated .normalized ();
198
+
199
+ assertThat (normalized .substring (normalized .indexOf ('@' ) + 1 ))
200
+ .isEqualTo ("[" + validated .domain () + "]" );
201
+ assertThat (normalized .substring (0 , normalized .indexOf ('@' )))
202
+ .isEqualTo (validated .localPart ().toLowerCase ());
166
203
}
167
204
168
205
@ ParameterizedTest (name = "{0}" )
@@ -173,8 +210,10 @@ void ensureNormalizedStripsQuotesForAllValidAddresses(String address) {
173
210
void ensureNormalizedDoesNotStripQuotesIfInvalid (String address ) {
174
211
assertThat (Email .of (address ))
175
212
.isPresent ().get ()
176
- .returns (address , email -> email .normalized (
177
- NormalizationOptions .builder ().stripQuotes ().build ()));
213
+ .returns (address , email -> email .normalized (NormalizationOptions .builder ()
214
+ .adjustCase (CaseOption .NO_CHANGE )
215
+ .stripQuotes ()
216
+ .build ()));
178
217
}
179
218
180
219
static Stream <Arguments > provideValidForStripQuotes () {
0 commit comments