View Javadoc
1   /*
2    * Copyright (C) 2020-2023 Dipl.-Inform. Kai Hofmann. All rights reserved!
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   * EMail tests.
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     * EMail address for testing user1@example.com.
30     */
31    private static final String EMAIL_USER1_AT_EXAMPLE_COM = "user1@example.com"; //$NON-NLS-1$
32  
33    /**
34     * user@example.com email constant.
35     */
36    private static final String USER_EXAMPLE_COM = "user@example.com"; //$NON-NLS-1$
37  
38    /**
39     * user2@example.com.
40     */
41    private static final String USER2_EXAMPLE_COM = "user2@example.com"; //$NON-NLS-1$
42  
43    /**
44     * example.com.
45     */
46    private static final String EXAMPLE_COM = "example.com"; //$NON-NLS-1$
47  
48    /**
49     * EMail not as expected constant.
50     */
51    private static final String EMAIL_NOT_AS_EXPECTED = "EMail not as expected"; //$NON-NLS-1$
52  
53    /**
54     * Illegal argument exception expected constant.
55     */
56    private static final String ILLEGAL_ARGUMENT_EXCEPTION = "Illegal argument exception expected"; //$NON-NLS-1$
57  
58  
59    /**
60     * Default constructor.
61     */
62    /* default */ EMailTests()
63     {
64      super();
65     }
66  
67  
68    /**
69     * Test EMail with valid email addresses.
70     *
71     * @param email Email
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    /* default */ 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     * Test EMail with email to short or long.
84     *
85     * @param email EMail
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    /* default */ void testEmailLength(final String email)
90     {
91      assertThrows(IllegalArgumentException.class, () ->
92       {
93        /* final EMail cleanEMail = */ EMail.of(email);
94       }, EMailTests.ILLEGAL_ARGUMENT_EXCEPTION
95      );
96     }
97  
98  
99    /**
100    * Test EMail with missing @.
101    */
102   @Test
103   /* default */ void testEmailWithMissingAt()
104    {
105     assertThrows(IllegalArgumentException.class, () ->
106      {
107       /* final EMail cleanEMail = */ EMail.of(EMailTests.EXAMPLE_COM);
108      }, EMailTests.ILLEGAL_ARGUMENT_EXCEPTION
109     );
110    }
111 
112 
113   /**
114    * Test EMail with ip and missing close square bracket.
115    */
116   @Test
117   /* default */ void testEmailWithIpAndMissingCloseSquareBracket()
118    {
119     assertThrows(IllegalArgumentException.class, () ->
120      {
121       /* final EMail cleanEMail = */ EMail.of("user@[192.168.1.1"); //$NON-NLS-1$
122      }, EMailTests.ILLEGAL_ARGUMENT_EXCEPTION
123     );
124    }
125 
126 
127   /**
128    * Test EMail with comments.
129    */
130   @Test
131   /* default */ void testEmailWithComment0()
132    {
133     assertThrows(IllegalArgumentException.class, () ->
134      {
135       /* final EMail cleanEMail = */ EMail.of("(comment)user@example.com"); //$NON-NLS-1$
136      }, EMailTests.ILLEGAL_ARGUMENT_EXCEPTION
137     );
138    }
139 
140 
141   /**
142    * Test EMail with comments.
143    */
144   @Test
145   /* default */ void testEmailWithComment1()
146    {
147     assertThrows(IllegalArgumentException.class, () ->
148      {
149       /* final EMail cleanEMail = */ EMail.of("user(comment)@example.com"); //$NON-NLS-1$
150      }, EMailTests.ILLEGAL_ARGUMENT_EXCEPTION
151     );
152    }
153 
154 
155   /**
156    * Test EMail with double quotes.
157    */
158   @Test
159   /* default */ void testEmailWithDoubleQuotes()
160    {
161     assertThrows(IllegalArgumentException.class, () ->
162      {
163       /* final EMail cleanEMail = */ EMail.of("\"user\"@example.com"); //$NON-NLS-1$
164      }, EMailTests.ILLEGAL_ARGUMENT_EXCEPTION
165     );
166    }
167 
168 
169   /**
170    * Test EMail with dot at start.
171    */
172   @Test
173   /* default */ void testEmailWithDotAtStart()
174    {
175     assertThrows(IllegalArgumentException.class, () ->
176      {
177       /* final EMail cleanEMail = */ EMail.of(".user@example.com"); //$NON-NLS-1$
178      }, EMailTests.ILLEGAL_ARGUMENT_EXCEPTION
179     );
180    }
181 
182 
183   /**
184    * Test EMail with dot at end.
185    */
186   @Test
187   /* default */ void testEmailWithDotAtEnd()
188    {
189     assertThrows(IllegalArgumentException.class, () ->
190      {
191       /* final EMail cleanEMail = */ EMail.of("user.@example.com"); //$NON-NLS-1$
192      }, EMailTests.ILLEGAL_ARGUMENT_EXCEPTION
193     );
194    }
195 
196 
197   /**
198    * Test EMail with double dot.
199    */
200   @Test
201   /* default */ void testEmailWithDoubleDot()
202    {
203     assertThrows(IllegalArgumentException.class, () ->
204      {
205       /* final EMail cleanEMail = */ EMail.of("user..name@example.com"); //$NON-NLS-1$
206      }, EMailTests.ILLEGAL_ARGUMENT_EXCEPTION
207     );
208    }
209 
210 
211   /**
212    * Test EMail with illegal characters.
213    */
214   @Test
215   /* default */ void testEmailWithIllegalCharacters0()
216    {
217     assertThrows(IllegalArgumentException.class, () ->
218      {
219       /* final EMail cleanEMail = */ EMail.of("üöäÖÄÜß§;,:@example.com"); //$NON-NLS-1$
220      }, EMailTests.ILLEGAL_ARGUMENT_EXCEPTION
221     );
222    }
223 
224 
225   /**
226    * Test get email.
227    */
228   @Test
229   /* default */ 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    * Test get domain part.
238    */
239   @Test
240   /* default */ 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    * Test get reverse domain part.
249    */
250   @Test
251   /* default */ void testGetReverseDomainPart()
252    {
253     final EMail email = EMail.of(EMailTests.USER_EXAMPLE_COM);
254     assertEquals("com.example", email.getReverseDomainPart(), EMailTests.EMAIL_NOT_AS_EXPECTED); //$NON-NLS-1$
255    }
256 
257 
258   /**
259    * Test get local part.
260    */
261   @Test
262   /* default */ void testGetLocalPart()
263    {
264     final EMail email = EMail.of(EMailTests.USER_EXAMPLE_COM);
265     assertEquals("user", email.getLocalPart(), EMailTests.EMAIL_NOT_AS_EXPECTED); //$NON-NLS-1$
266    }
267 
268 
269   /**
270    * Test hash code.
271    */
272   @Test
273   /* default */ 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", //$NON-NLS-1$
279       () -> assertEquals(email1.hashCode(), email2.hashCode(), "hashCodes are not equal"), //$NON-NLS-1$
280       () -> assertNotEquals(email1.hashCode(), email3.hashCode(), "hashCodes are equal") //$NON-NLS-1$
281     );
282    }
283 
284 
285   /**
286    * Test equals.
287    */
288   @Test
289   @SuppressWarnings("java:S5785")
290   /* default */ 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", //$NON-NLS-1$
297       () -> assertTrue(email1.equals(email1), "email11 is not equal"), //$NON-NLS-1$
298       () -> assertTrue(email1.equals(email2), "email12 are not equal"), //$NON-NLS-1$
299       () -> assertTrue(email2.equals(email1), "email21 are not equal"), //$NON-NLS-1$
300       () -> assertTrue(email2.equals(email4), "email24 are not equal"), //$NON-NLS-1$
301       () -> assertTrue(email1.equals(email4), "email14 are not equal"), //$NON-NLS-1$
302       () -> assertFalse(email1.equals(email3), "email13 are equal"), //$NON-NLS-1$
303       () -> assertFalse(email3.equals(email1), "email31 are equal"), //$NON-NLS-1$
304       () -> assertFalse(email1.equals(null), "email10 is equal") //$NON-NLS-1$
305     );
306    }
307 
308 
309   /**
310    * Test toString.
311    */
312   @Test
313   /* default */ 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"); //$NON-NLS-1$ //$NON-NLS-2$
317    }
318 
319 
320   /**
321    * Test compareTo.
322    */
323   @Test
324   @SuppressWarnings("java:S5785")
325   /* default */ 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"); //$NON-NLS-1$
331     final EMail email5 = EMail.of(EMailTests.EMAIL_USER1_AT_EXAMPLE_COM);
332     assertAll("testCompareTo", //$NON-NLS-1$
333       () -> assertTrue(email1.compareTo(email2) == -email2.compareTo(email1), "reflexive1"), //$NON-NLS-1$
334       () -> assertTrue(email1.compareTo(email3) == -email3.compareTo(email1), "reflexive2"), //$NON-NLS-1$
335       () -> assertTrue((email4.compareTo(email3) > 0) && (email3.compareTo(email1) > 0) && (email4.compareTo(email1) > 0), "transitive1"), //$NON-NLS-1$
336       () -> assertTrue((email1.compareTo(email2) == 0) && (Math.abs(email1.compareTo(email5)) == Math.abs(email2.compareTo(email5))), "sgn1"), //$NON-NLS-1$
337       () -> assertTrue((email1.compareTo(email2) == 0) && email1.equals(email2), "equals") //$NON-NLS-1$
338     );
339    }
340 
341  }