1
2
3
4 package de.powerstat.validation.values.test;
5
6
7 import static org.junit.jupiter.api.Assertions.assertAll;
8 import static org.junit.jupiter.api.Assertions.assertEquals;
9 import static org.junit.jupiter.api.Assertions.assertFalse;
10 import static org.junit.jupiter.api.Assertions.assertNotEquals;
11 import static org.junit.jupiter.api.Assertions.assertThrows;
12 import static org.junit.jupiter.api.Assertions.assertTrue;
13
14 import org.junit.jupiter.api.Test;
15 import org.junit.jupiter.params.ParameterizedTest;
16 import org.junit.jupiter.params.provider.ValueSource;
17
18 import de.powerstat.validation.values.EMail;
19 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
20
21
22
23
24
25 @SuppressFBWarnings({"EC_NULL_ARG", "RV_NEGATING_RESULT_OF_COMPARETO", "RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT", "SPP_USE_ZERO_WITH_COMPARATOR"})
26 final class EMailTests
27 {
28
29
30
31 private static final String EMAIL_USER1_AT_EXAMPLE_COM = "user1@example.com";
32
33
34
35
36 private static final String USER_EXAMPLE_COM = "user@example.com";
37
38
39
40
41 private static final String USER2_EXAMPLE_COM = "user2@example.com";
42
43
44
45
46 private static final String EXAMPLE_COM = "example.com";
47
48
49
50
51 private static final String EMAIL_NOT_AS_EXPECTED = "EMail not as expected";
52
53
54
55
56 private static final String ILLEGAL_ARGUMENT_EXCEPTION = "Illegal argument exception expected";
57
58
59
60
61
62 EMailTests()
63 {
64 super();
65 }
66
67
68
69
70
71
72
73 @ParameterizedTest
74 @ValueSource(strings = {"u@example.com", "a@a.de", "user@[192.168.1.1]", "user@[ipv6:00fd:0000:0000:0000:0000:0000:0000:0000]", "1234567890123456789012345678901234567890123456789012345678901234@example.com", "a@abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcde.com"})
75 void testEmailOk0(final String email)
76 {
77 final EMail cleanEMail = EMail.of(email);
78 assertEquals(email, cleanEMail.stringValue(), EMailTests.EMAIL_NOT_AS_EXPECTED);
79 }
80
81
82
83
84
85
86
87 @ParameterizedTest
88 @ValueSource(strings = {"a@a.d", "12345678901234567890123456789012345678901234567890123456789012345@example.com", "a@abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdef.com"})
89 void testEmailLength(final String email)
90 {
91 assertThrows(IllegalArgumentException.class, () ->
92 {
93 EMail.of(email);
94 }, EMailTests.ILLEGAL_ARGUMENT_EXCEPTION
95 );
96 }
97
98
99
100
101
102 @Test
103 void testEmailWithMissingAt()
104 {
105 assertThrows(IllegalArgumentException.class, () ->
106 {
107 EMail.of(EMailTests.EXAMPLE_COM);
108 }, EMailTests.ILLEGAL_ARGUMENT_EXCEPTION
109 );
110 }
111
112
113
114
115
116 @Test
117 void testEmailWithIpAndMissingCloseSquareBracket()
118 {
119 assertThrows(IllegalArgumentException.class, () ->
120 {
121 EMail.of("user@[192.168.1.1");
122 }, EMailTests.ILLEGAL_ARGUMENT_EXCEPTION
123 );
124 }
125
126
127
128
129
130 @Test
131 void testEmailWithComment0()
132 {
133 assertThrows(IllegalArgumentException.class, () ->
134 {
135 EMail.of("(comment)user@example.com");
136 }, EMailTests.ILLEGAL_ARGUMENT_EXCEPTION
137 );
138 }
139
140
141
142
143
144 @Test
145 void testEmailWithComment1()
146 {
147 assertThrows(IllegalArgumentException.class, () ->
148 {
149 EMail.of("user(comment)@example.com");
150 }, EMailTests.ILLEGAL_ARGUMENT_EXCEPTION
151 );
152 }
153
154
155
156
157
158 @Test
159 void testEmailWithDoubleQuotes()
160 {
161 assertThrows(IllegalArgumentException.class, () ->
162 {
163 EMail.of("\"user\"@example.com");
164 }, EMailTests.ILLEGAL_ARGUMENT_EXCEPTION
165 );
166 }
167
168
169
170
171
172 @Test
173 void testEmailWithDotAtStart()
174 {
175 assertThrows(IllegalArgumentException.class, () ->
176 {
177 EMail.of(".user@example.com");
178 }, EMailTests.ILLEGAL_ARGUMENT_EXCEPTION
179 );
180 }
181
182
183
184
185
186 @Test
187 void testEmailWithDotAtEnd()
188 {
189 assertThrows(IllegalArgumentException.class, () ->
190 {
191 EMail.of("user.@example.com");
192 }, EMailTests.ILLEGAL_ARGUMENT_EXCEPTION
193 );
194 }
195
196
197
198
199
200 @Test
201 void testEmailWithDoubleDot()
202 {
203 assertThrows(IllegalArgumentException.class, () ->
204 {
205 EMail.of("user..name@example.com");
206 }, EMailTests.ILLEGAL_ARGUMENT_EXCEPTION
207 );
208 }
209
210
211
212
213
214 @Test
215 void testEmailWithIllegalCharacters0()
216 {
217 assertThrows(IllegalArgumentException.class, () ->
218 {
219 EMail.of("üöäÖÄÜß§;,:@example.com");
220 }, EMailTests.ILLEGAL_ARGUMENT_EXCEPTION
221 );
222 }
223
224
225
226
227
228 @Test
229 void testStringValue()
230 {
231 final EMail email = EMail.of(EMailTests.USER_EXAMPLE_COM);
232 assertEquals(EMailTests.USER_EXAMPLE_COM, email.stringValue(), EMailTests.EMAIL_NOT_AS_EXPECTED);
233 }
234
235
236
237
238
239 @Test
240 void testGetDomainPart()
241 {
242 final EMail email = EMail.of(EMailTests.USER_EXAMPLE_COM);
243 assertEquals(EMailTests.EXAMPLE_COM, email.getDomainPart(), EMailTests.EMAIL_NOT_AS_EXPECTED);
244 }
245
246
247
248
249
250 @Test
251 void testGetReverseDomainPart()
252 {
253 final EMail email = EMail.of(EMailTests.USER_EXAMPLE_COM);
254 assertEquals("com.example", email.getReverseDomainPart(), EMailTests.EMAIL_NOT_AS_EXPECTED);
255 }
256
257
258
259
260
261 @Test
262 void testGetLocalPart()
263 {
264 final EMail email = EMail.of(EMailTests.USER_EXAMPLE_COM);
265 assertEquals("user", email.getLocalPart(), EMailTests.EMAIL_NOT_AS_EXPECTED);
266 }
267
268
269
270
271
272 @Test
273 void testHashCode()
274 {
275 final EMail email1 = EMail.of(EMailTests.EMAIL_USER1_AT_EXAMPLE_COM);
276 final EMail email2 = EMail.of(EMailTests.EMAIL_USER1_AT_EXAMPLE_COM);
277 final EMail email3 = EMail.of(EMailTests.USER2_EXAMPLE_COM);
278 assertAll("testHashCode",
279 () -> assertEquals(email1.hashCode(), email2.hashCode(), "hashCodes are not equal"),
280 () -> assertNotEquals(email1.hashCode(), email3.hashCode(), "hashCodes are equal")
281 );
282 }
283
284
285
286
287
288 @Test
289 @SuppressWarnings("java:S5785")
290 void testEquals()
291 {
292 final EMail email1 = EMail.of(EMailTests.EMAIL_USER1_AT_EXAMPLE_COM);
293 final EMail email2 = EMail.of(EMailTests.EMAIL_USER1_AT_EXAMPLE_COM);
294 final EMail email3 = EMail.of(EMailTests.USER2_EXAMPLE_COM);
295 final EMail email4 = EMail.of(EMailTests.EMAIL_USER1_AT_EXAMPLE_COM);
296 assertAll("testEquals",
297 () -> assertTrue(email1.equals(email1), "email11 is not equal"),
298 () -> assertTrue(email1.equals(email2), "email12 are not equal"),
299 () -> assertTrue(email2.equals(email1), "email21 are not equal"),
300 () -> assertTrue(email2.equals(email4), "email24 are not equal"),
301 () -> assertTrue(email1.equals(email4), "email14 are not equal"),
302 () -> assertFalse(email1.equals(email3), "email13 are equal"),
303 () -> assertFalse(email3.equals(email1), "email31 are equal"),
304 () -> assertFalse(email1.equals(null), "email10 is equal")
305 );
306 }
307
308
309
310
311
312 @Test
313 void testToString()
314 {
315 final EMail email = EMail.of(EMailTests.EMAIL_USER1_AT_EXAMPLE_COM);
316 assertEquals("EMail[email=user1@example.com]", email.toString(), "toString not equal");
317 }
318
319
320
321
322
323 @Test
324 @SuppressWarnings("java:S5785")
325 void testCompareTo()
326 {
327 final EMail email1 = EMail.of(EMailTests.EMAIL_USER1_AT_EXAMPLE_COM);
328 final EMail email2 = EMail.of(EMailTests.EMAIL_USER1_AT_EXAMPLE_COM);
329 final EMail email3 = EMail.of(EMailTests.USER2_EXAMPLE_COM);
330 final EMail email4 = EMail.of("user3@example.com");
331 final EMail email5 = EMail.of(EMailTests.EMAIL_USER1_AT_EXAMPLE_COM);
332 assertAll("testCompareTo",
333 () -> assertTrue(email1.compareTo(email2) == -email2.compareTo(email1), "reflexive1"),
334 () -> assertTrue(email1.compareTo(email3) == -email3.compareTo(email1), "reflexive2"),
335 () -> assertTrue((email4.compareTo(email3) > 0) && (email3.compareTo(email1) > 0) && (email4.compareTo(email1) > 0), "transitive1"),
336 () -> assertTrue((email1.compareTo(email2) == 0) && (Math.abs(email1.compareTo(email5)) == Math.abs(email2.compareTo(email5))), "sgn1"),
337 () -> assertTrue((email1.compareTo(email2) == 0) && email1.equals(email2), "equals")
338 );
339 }
340
341 }