1. Project Clover database Mon Dec 22 2025 14:59:38 CST
  2. Package org.europa.together.utils

File ValidatorTest.java

 

Code metrics

0
90
55
1
437
366
55
0.61
1.64
55
1

Classes

Class Line # Actions
ValidatorTest 22 90 0% 55 3
0.9793103397.9%
 

Contributing tests

This file is covered by 238 tests. .

Source view

1    package org.europa.together.utils;
2   
3    import java.lang.reflect.Constructor;
4    import java.time.ZoneId;
5    import org.europa.together.application.LogbackLogger;
6    import org.europa.together.business.Logger;
7    import static org.junit.jupiter.api.Assertions.*;
8    import org.junit.jupiter.api.Test;
9    import java.time.ZonedDateTime;
10    import org.europa.together.JUnit5Preperator;
11    import org.junit.jupiter.api.DisplayName;
12    import org.junit.jupiter.api.extension.ExtendWith;
13    import org.junit.jupiter.params.ParameterizedTest;
14    import org.junit.jupiter.params.provider.ValueSource;
15    import org.springframework.test.context.ContextConfiguration;
16    import org.springframework.test.context.junit.jupiter.SpringExtension;
17   
18    @SuppressWarnings("unchecked")
19    @ExtendWith({JUnit5Preperator.class})
20    @ExtendWith(SpringExtension.class)
21    @ContextConfiguration(locations = {"/applicationContext.xml"})
 
22    public class ValidatorTest {
23   
24    private static final Logger LOGGER = new LogbackLogger(ValidatorTest.class);
25   
 
26  1 toggle @Test
27    void privateConstructor() throws Exception {
28  1 Constructor<Validator> clazz
29    = Validator.class.getDeclaredConstructor();
30  1 clazz.setAccessible(true);
31  1 assertThrows(Exception.class, () -> {
32  1 Validator call = clazz.newInstance();
33    });
34    }
35   
 
36  8 toggle @ParameterizedTest
37    @ValueSource(strings = {"true", "false", "TRUE", "FALSE", "TrUe",
38    "fAlSe", "1", "0"})
39    void isBooleanRegex(String input) {
40  8 assertTrue(Validator.validate(input, Validator.BOOLEAN));
41    }
42   
 
43  2 toggle @ParameterizedTest
44    @ValueSource(strings = {"-1", "5"})
45    void isNotBooleanRegex(String input) {
46  2 assertFalse(Validator.validate(input, Validator.BOOLEAN));
47    }
48   
 
49  4 toggle @ParameterizedTest
50    @ValueSource(strings = {"1", "0000", "1234567890", "1 2 345 6 789 0"})
51    void isDigitRegex(String input) {
52  4 assertTrue(Validator.validate(input, Validator.DIGIT));
53    }
54   
 
55  1 toggle @ParameterizedTest
56    @ValueSource(strings = {"ABC.01"})
57    void isNotDigitRegex(String input) {
58  1 assertFalse(Validator.validate(input, Validator.DIGIT));
59    }
60   
 
61  3 toggle @ParameterizedTest
62    @ValueSource(strings = {"abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
63    "abc XYZ"})
64    void isCharacterRegex(String input) {
65  3 assertTrue(Validator.validate(input, Validator.ASCII_CHARACTER));
66    }
67   
 
68  2 toggle @ParameterizedTest
69    @ValueSource(strings = {"äöü ÄÖÜ ß", "abc 123 XYZ"})
70    void isNotCharacterRegex(String input) {
71  2 assertFalse(Validator.validate(input, Validator.ASCII_CHARACTER));
72    }
73   
 
74  7 toggle @ParameterizedTest
75    @ValueSource(strings = {"A fool with a tool is still a fool.", "EN/00001.III-D",
76    "Remember, remember the 1 st. of November!", "Math: 1 + ~1 : 12 * 4/3 = -10%",
77    "Brackets: <>(){}[]", "Symbols 01: ?!.,;_", "Symbols 02: ¬&§$@€#"})
78    void isTextRegex(String input) {
79  7 assertTrue(Validator.validate(input, Validator.TEXT));
80    }
81   
 
82  1 toggle @ParameterizedTest
83    @ValueSource(strings = {"äöüÄÖÜß"})
84    void isNotTextRegex(String input) {
85  1 assertFalse(Validator.validate(input, Validator.TEXT));
86    }
87   
 
88  4 toggle @ParameterizedTest
89    @ValueSource(strings = {"abcdefXYZ", "a", "A", "ABCxyz"})
90    void isRegexLetters(String input) {
91  4 assertTrue(Validator.validate(input, Validator.LETTERS));
92    }
93   
 
94  3 toggle @ParameterizedTest
95    @ValueSource(strings = {"äöüÄÖÜß", "12345678890", "!§$%"})
96    void isNotRegexLetters(String input) {
97  3 assertFalse(Validator.validate(input, Validator.LETTERS));
98    }
99   
 
100  10 toggle @ParameterizedTest
101    @ValueSource(strings = {"0000", "1", "1234567890", "1 2 345 6 789 0", "0.0",
102    "-0.0", "0.123456789", "-0.123456789", "0123456789.0987654321", "-0123456789.0987654321"})
103    void isFloatingPointRegex(String input) {
104  10 assertTrue(Validator.validate(input, Validator.FLOATING_POINT));
105    }
106   
 
107  7 toggle @ParameterizedTest
108    @ValueSource(strings = {"0.", "-0.", ".0", "-.0", ".", "-.", "ABC.01"})
109    void isNotFloatingPointRegex(String input) {
110  7 assertFalse(Validator.validate(input, Validator.FLOATING_POINT));
111    }
112   
 
113  37 toggle @ParameterizedTest
114    @ValueSource(strings = {"00:00", "00:01", "00:09", "00:10", "01:11", "01:19",
115    "02:20", "02:21", "02:29", "03:30", "03:31", "03:39", "04:40", "04:41",
116    "04:49", "05:50", "05:51", "05:59", "06:00", "07:00", "08:00", "09:00",
117    "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00",
118    "18:00", "19:00", "20:00", "21:00", "22:00", "23:00", "23:59"})
119    void isTime24Regex(String input) {
120  37 assertTrue(Validator.validate(input, Validator.TIME_24H));
121    }
122   
 
123  9 toggle @ParameterizedTest
124    @ValueSource(strings = {"24:00", "24:01", "25:00", "0:0", "1:20", "01:2",
125    "10:60", "10:61", "10:69"})
126    void isNotTime24Regex(String input) {
127  9 assertFalse(Validator.validate(input, Validator.TIME_24H));
128    }
129   
 
130  13 toggle @ParameterizedTest
131    @ValueSource(strings = {"#000", "#fff", "#f0A", "#000000", "#123451", "#999999",
132    "#aAAAaa", "#BBbbbb", "#cCcccC", "#dDdDdd", "#eEeeEE", "#fFFfff", "#996789"})
133    void isRGBColor(String input) {
134  13 assertTrue(Validator.validate(input, Validator.RGB_COLOR));
135    }
136   
 
137  8 toggle @ParameterizedTest
138    @ValueSource(strings = {"#0", "#11af990", "#9999", "#09090", "#a", "#ffff",
139    "#ggg", "#AFaFA"})
140    void isNotRGBColor(String input) {
141  8 assertFalse(Validator.validate(input, Validator.RGB_COLOR));
142    }
143   
 
144  1 toggle @ParameterizedTest
145    @ValueSource(strings = {"localhost"})
146    void isValidIP4ForLocalhost(String input) {
147  1 assertTrue(Validator.validate(input, Validator.IP4_ADDRESS));
148    }
149   
 
150  1 toggle @ParameterizedTest
151    @ValueSource(strings = {"localhost:80"})
152    void isValidIP4ForLocalhostWithPort(String input) {
153  1 assertTrue(Validator.validate(input, Validator.IP4_ADDRESS));
154    }
155   
 
156  10 toggle @ParameterizedTest
157    @ValueSource(strings = {"0.0.0.0", "1.0.0.0", "0.1.0.0", "0.0.1.0", "0.0.0.1",
158    "127.0.0.1", "20.20.20.20", "99.100.200.10", "140.145.240.245", "255.255.255.255"})
159    void isValidIP4(String input) {
160  10 assertTrue(Validator.validate(input, Validator.IP4_ADDRESS));
161    }
162   
 
163  25 toggle @ParameterizedTest
164    @ValueSource(strings = {"127.0.0.1:80", "127.0.0.1:0", "127.0.0.1:1", "127.0.0.1:10",
165    "127.0.0.1:100", "127.0.0.1:1000", "127.0.0.1:9999", "127.0.0.1:10000", "127.0.0.1:19999",
166    "127.0.0.1:29999", "127.0.0.1:39999", "127.0.0.1:49999", "127.0.0.1:59999",
167    "127.0.0.1:60000", "127.0.0.1:61000", "127.0.0.1:62000", "127.0.0.1:63000",
168    "127.0.0.1:64000", "127.0.0.1:64999", "127.0.0.1:65000", "127.0.0.1:65499",
169    "127.0.0.1:65500", "127.0.0.1:65529", "127.0.0.1:65530", "127.0.0.1:65535"})
170    void isValidIP4_WithPort(String input) {
171  25 assertTrue(Validator.validate(input, Validator.IP4_ADDRESS));
172    }
173   
 
174  2 toggle @ParameterizedTest
175    @ValueSource(strings = {"127.0.0.1:65536", "127.0.0.1:555555"})
176    void isIP4_WithInvalidPort(String input) {
177  2 assertFalse(Validator.validate(input, Validator.IP4_ADDRESS));
178    }
179   
 
180  3 toggle @ParameterizedTest
181    @ValueSource(strings = {"255.255.255", "255.255", "255"})
182    void isInvalidIP4_Format(String input) {
183  3 assertFalse(Validator.validate(input, Validator.IP4_ADDRESS));
184    }
185   
 
186  8 toggle @ParameterizedTest
187    @ValueSource(strings = {"1.1.1.01", "1.1.1.001", "1.1.01.1", "1.1.001.1",
188    "1.01.1.1", "1.001.1.1", "01.1.1.1", "001.1.1.1"})
189    void isInvalidIP4_LedingZeros(String input) {
190  8 assertFalse(Validator.validate(input, Validator.IP4_ADDRESS));
191    }
192   
 
193  5 toggle @ParameterizedTest
194    @ValueSource(strings = {"256.255.255.255", "255.256.255.255", "255.255.256.255",
195    "255.255.255.256", "355.255.255.255"})
196    void isInvalidIP4_Ending(String input) {
197  5 assertFalse(Validator.validate(input, Validator.IP4_ADDRESS));
198    }
199   
 
200  14 toggle @ParameterizedTest
201    @ValueSource(strings = {"1", "9", "1.0", "1.0.0", "1.10", "1.0.10", "10.100.1000",
202    "123.1234.12345", "1234", "12.1-SNAPSHOT", "1.109.1234567890", "1-xYz",
203    "1.2-SNAPSHOT", "1.2.3-xyzXYZxyzX"})
204    void isVersionNumber(String input) {
205  14 assertTrue(Validator.validate(input, Validator.SEMANTIC_VERSION_NUMBER));
206    }
207   
 
208  4 toggle @ParameterizedTest
209    @ValueSource(strings = {"0", "01.02", "1.0.03", "0.0.0-LABEL"})
210    void isNotVersionNumber_StartWithZero(String input) {
211  4 assertFalse(Validator.validate(input, Validator.SEMANTIC_VERSION_NUMBER));
212    }
213   
 
214  8 toggle @ParameterizedTest
215    @ValueSource(strings = {"1.0.0.0", "A", ".1", "1.", "1.0.", "1.0.0.", "1.0.0-",
216    "1.0.0-abcabcabcab"})
217    void isNotVersionNumber_Format(String input) {
218  8 assertFalse(Validator.validate(input, Validator.SEMANTIC_VERSION_NUMBER));
219    }
220   
 
221  9 toggle @ParameterizedTest
222    @ValueSource(strings = {"john@sample.eu", "john.doe@sample.org", "john_doe@sample.org",
223    "john-doe@sample.org", "john_doe@sample.org", "_john-doe@sample.org", "123@sample.org",
224    "_1@sample.org", "a@sample.org"})
225    void isValidEMail(String input) {
226  9 assertTrue(Validator.validate(input, Validator.E_MAIL_ADDRESS));
227    }
228   
 
229  4 toggle @ParameterizedTest
230    @ValueSource(strings = {"john@sample", "john@", "@sample.o", "-john.sample.o",})
231    void isInvalidEMail(String input) {
232  4 assertFalse(Validator.validate(input, Validator.E_MAIL_ADDRESS));
233    }
234   
 
235  1 toggle @Test
236    void isIntegerInRange() {
237  1 assertTrue(Validator.isIntegerInRange(1, 1, 5));
238  1 assertTrue(Validator.isIntegerInRange(3, 1, 5));
239  1 assertTrue(Validator.isIntegerInRange(5, 1, 5));
240    }
241   
 
242  1 toggle @Test
243    void isIntegerNotInRange() {
244  1 assertFalse(Validator.isIntegerInRange(0, 1, 5));
245  1 assertFalse(Validator.isIntegerInRange(6, 1, 5));
246    }
247   
 
248  1 toggle @Test
249    void isDateAfter() {
250    //now() is after 2015
251  1 assertTrue(Validator.isDateAfter(ZonedDateTime.now(),
252    ZonedDateTime.of(2015, 12, 31, 23, 59, 0, 0, ZoneId.of("UTC"))));
253    //after
254  1 assertTrue(Validator.isDateAfter(ZonedDateTime.of(2016, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC")),
255    ZonedDateTime.of(2015, 12, 31, 23, 59, 0, 0, ZoneId.of("UTC"))));
256    }
257   
 
258  1 toggle @Test
259    void isDateNotAfter() {
260    //equal
261  1 assertFalse(Validator.isDateAfter(ZonedDateTime.of(2015, 12, 31, 23, 59, 0, 0, ZoneId.of("UTC")),
262    ZonedDateTime.of(2015, 12, 31, 23, 59, 0, 0, ZoneId.of("UTC"))));
263   
264  1 assertFalse(Validator.isDateAfter(null, null));
265  1 assertFalse(Validator.isDateAfter(ZonedDateTime.now(), null));
266  1 assertFalse(Validator.isDateAfter(null, ZonedDateTime.now()));
267   
268  1 assertFalse(Validator.isDateAfter(ZonedDateTime.of(2015, 12, 31, 23, 59, 0, 0, ZoneId.of("UTC")), ZonedDateTime.now()));
269  1 assertFalse(Validator.isDateAfter(ZonedDateTime.of(2015, 12, 31, 23, 59, 0, 0, ZoneId.of("UTC")),
270    ZonedDateTime.of(2016, 12, 31, 23, 59, 0, 0, ZoneId.of("UTC"))));
271    }
272   
 
273  1 toggle @Test
274    void isDateBefore() {
275    //2015 is before now() => TRUE
276  1 assertTrue(Validator.isDateBefore(ZonedDateTime.of(2015, 12, 31, 23, 59, 0, 0, ZoneId.of("UTC")),
277    ZonedDateTime.now()));
278    //before
279  1 assertTrue(Validator.isDateBefore(ZonedDateTime.of(2015, 12, 31, 23, 59, 0, 0, ZoneId.of("UTC")),
280    ZonedDateTime.of(2016, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC"))));
281    }
282   
 
283  1 toggle @Test
284    void isDateNotBefore() {
285    //equal
286  1 assertFalse(Validator.isDateAfter(ZonedDateTime.of(2015, 12, 31, 23, 59, 0, 0, ZoneId.of("UTC")),
287    ZonedDateTime.of(2015, 12, 31, 23, 59, 0, 0, ZoneId.of("UTC"))));
288   
289  1 assertFalse(Validator.isDateBefore(null, null));
290  1 assertFalse(Validator.isDateBefore(ZonedDateTime.now(), null));
291  1 assertFalse(Validator.isDateBefore(null, ZonedDateTime.now()));
292    //now() is before 2015 => FALSE
293  1 assertFalse(Validator.isDateBefore(ZonedDateTime.now(), ZonedDateTime.of(2015, 12, 31, 23, 59, 0, 0, ZoneId.of("UTC"))));
294  1 assertFalse(Validator.isDateBefore(ZonedDateTime.of(2015, 12, 31, 23, 59, 0, 0, ZoneId.of("UTC")),
295    ZonedDateTime.of(2015, 12, 31, 23, 49, 0, 0, ZoneId.of("UTC"))));
296    }
297   
 
298  1 toggle @Test
299    void isDateInRange() {
300  1 assertTrue(Validator.isDateInRange(
301    ZonedDateTime.of(2015, 6, 15, 12, 0, 0, 0, ZoneId.of("UTC")),
302    ZonedDateTime.of(2014, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC")),
303    ZonedDateTime.of(2016, 12, 31, 23, 59, 0, 0, ZoneId.of("UTC"))
304    ));
305    }
306   
 
307  1 toggle @Test
308    void isDateNotInRange() {
309  1 ZonedDateTime check = ZonedDateTime.now();
310  1 assertFalse(Validator.isDateInRange(check, check, check));
311    }
312   
 
313  1 toggle @Test
314    void isDateNotInRange_LowerBoundry() {
315  1 assertFalse(Validator.isDateInRange(
316    ZonedDateTime.of(2014, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC")),
317    ZonedDateTime.of(2014, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC")),
318    ZonedDateTime.of(2016, 12, 31, 23, 59, 0, 0, ZoneId.of("UTC"))
319    ));
320    }
321   
 
322  1 toggle @Test
323    void isDateNotInRange_UpperBoundry() {
324  1 assertFalse(Validator.isDateInRange(
325    ZonedDateTime.of(2016, 12, 31, 23, 59, 0, 0, ZoneId.of("UTC")),
326    ZonedDateTime.of(2014, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC")),
327    ZonedDateTime.of(2016, 12, 31, 23, 59, 0, 0, ZoneId.of("UTC"))
328    ));
329    }
330   
 
331  1 toggle @Test
332    void isDateNotInRange_After() {
333  1 assertFalse(Validator.isDateInRange(
334    ZonedDateTime.of(2017, 1, 1, 12, 0, 0, 0, ZoneId.of("UTC")),
335    ZonedDateTime.of(2014, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC")),
336    ZonedDateTime.of(2016, 12, 31, 23, 59, 0, 0, ZoneId.of("UTC"))
337    ));
338    }
339   
 
340  1 toggle @Test
341    void isDateNotInRange_Before() {
342  1 assertFalse(Validator.isDateInRange(
343    ZonedDateTime.of(2013, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC")),
344    ZonedDateTime.of(2014, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC")),
345    ZonedDateTime.of(2016, 12, 31, 23, 59, 0, 0, ZoneId.of("UTC"))
346    ));
347    }
348   
 
349  1 toggle @Test
350    void isDateNotInRange_NullPointer() {
351  1 assertFalse(Validator.isDateInRange(null, ZonedDateTime.now(), ZonedDateTime.now()));
352  1 assertFalse(Validator.isDateInRange(ZonedDateTime.now(), null, ZonedDateTime.now()));
353  1 assertFalse(Validator.isDateInRange(ZonedDateTime.now(), ZonedDateTime.now(), null));
354   
355  1 assertFalse(Validator.isDateInRange(ZonedDateTime.now(), null, null));
356  1 assertFalse(Validator.isDateInRange(null, ZonedDateTime.now(), null));
357  1 assertFalse(Validator.isDateInRange(null, null, ZonedDateTime.now()));
358   
359  1 assertFalse(Validator.isDateInRange(null, null, null));
360    }
361   
 
362  1 toggle @Test
363    void isValidIsbn10() {
364  1 assertTrue(Validator.isIsbn("3836278340"));
365  1 assertTrue(Validator.isIsbn("38-3627-834-0"));
366  1 assertTrue(Validator.isIsbn("0-9752298-0-X"));
367    }
368   
 
369  1 toggle @Test
370    void isInvalidIsbn10() {
371  1 assertFalse(Validator.isIsbn("3836278344"));
372  1 assertFalse(Validator.isIsbn("9836278340"));
373    }
374   
 
375  1 toggle @Test
376    void isValidIsbn13() {
377  1 assertTrue(Validator.isIsbn("9783836278348"));
378  1 assertTrue(Validator.isIsbn("978-3-8362-7834-8"));
379    }
380   
 
381  1 toggle @Test
382    void isInvalidIsbn13() {
383  1 assertFalse(Validator.isIsbn("9783846278348"));
384    }
385   
 
386  1 toggle @Test
387    void isInvalidIsbn_NotInterger() throws Exception {
388  1 assertThrows(Exception.class, () -> {
389  1 Validator.isIsbn("d836278340");
390    });
391    }
392   
 
393  1 toggle @Test
394    void isInvalidIsbn_empty() throws Exception {
395  1 assertThrows(Exception.class, () -> {
396  1 Validator.isIsbn("");
397    });
398    }
399   
 
400  1 toggle @Test
401    void isInvalidIsbn10_toShort() throws Exception {
402  1 assertThrows(Exception.class, () -> {
403  1 Validator.isIsbn("383627834");
404    });
405    }
406   
 
407  0 toggle void isInvalidIsbn10_tolong() throws Exception {
408  0 assertThrows(Exception.class, () -> {
409  0 Validator.isIsbn("38362783400");
410    });
411    }
412   
 
413  1 toggle @Test
414    void isInvalidIsbn13_toShort() throws Exception {
415  1 assertThrows(Exception.class, () -> {
416  1 Validator.isIsbn("978383627834");
417    });
418    }
419   
 
420  1 toggle @Test
421    void isInvalidIsbn13_tolong() throws Exception {
422  1 assertThrows(Exception.class, () -> {
423  1 Validator.isIsbn("97838362783481");
424    });
425    }
426   
 
427  1 toggle @Test
428    void luhnNumberChecksumTest() {
429  1 assertTrue(Validator.luhnNumberChecksum("5374343040363120"));
430    }
431   
 
432  1 toggle @Test
433    void failLuhnNumberChecksumTest() {
434  1 assertFalse(Validator.luhnNumberChecksum("79927398713"));
435    }
436   
437    }