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.CsvSource;
17 import org.junit.jupiter.params.provider.ValueSource;
18
19 import de.powerstat.validation.values.CalendarSystems;
20 import de.powerstat.validation.values.Days;
21 import de.powerstat.validation.values.Months;
22 import de.powerstat.validation.values.Year;
23 import de.powerstat.validation.values.Years;
24 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
25
26
27
28
29
30 @SuppressFBWarnings({"EC_NULL_ARG", "RV_NEGATING_RESULT_OF_COMPARETO", "RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT", "SPP_USE_ZERO_WITH_COMPARATOR", "PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS"})
31 final class YearTests
32 {
33
34
35
36 private static final String RESULT_NOT_AS_EXPECTED = "Result not as expected";
37
38
39
40
41 private static final String ARITHMETIC_EXCEPTION_EXPECTED = "Arithmetic exception expected";
42
43
44
45
46 private static final String TEN = "10";
47
48
49
50
51 private static final String NOT_A_YEAR = "Not a year!";
52
53
54
55
56
57 YearTests()
58 {
59 super();
60 }
61
62
63
64
65
66 @Test
67 void testFactory1()
68 {
69 assertEquals(10, Year.of(TEN).longValue(), NOT_A_YEAR);
70 }
71
72
73
74
75
76 @Test
77 void testLongValue()
78 {
79 assertEquals(10, Year.of(10).longValue(), NOT_A_YEAR);
80 }
81
82
83
84
85
86 @Test
87 void testStringValue()
88 {
89 assertEquals(TEN, Year.of(10).stringValue(), NOT_A_YEAR);
90 }
91
92
93
94
95
96
97
98 @ParameterizedTest
99 @ValueSource(longs = {-1, 1, 2020})
100 void testIsYear(final long year)
101 {
102 assertEquals(year, Year.of(year).longValue(), NOT_A_YEAR);
103 }
104
105
106
107
108
109
110
111 @ParameterizedTest
112 @ValueSource(longs = {0})
113 void testIsNotAYear(final long year)
114 {
115 assertThrows(IndexOutOfBoundsException.class, () ->
116 {
117 Year.of(year);
118 }, "Index out of bounds exception expected"
119 );
120 }
121
122
123
124
125
126 @Test
127 void testHashCode()
128 {
129 final Year year1 = Year.of(1);
130 final Year year2 = Year.of(1);
131 final Year year3 = Year.of(2);
132 assertAll("testHashCode",
133 () -> assertEquals(year1.hashCode(), year2.hashCode(), "hashCodes are not equal"),
134 () -> assertNotEquals(year1.hashCode(), year3.hashCode(), "hashCodes are equal")
135 );
136 }
137
138
139
140
141
142 @Test
143 @SuppressWarnings("java:S5785")
144 void testEquals()
145 {
146 final Year year1 = Year.of(1);
147 final Year year2 = Year.of(1);
148 final Year year3 = Year.of(2);
149 final Year year4 = Year.of(1);
150 assertAll("testEquals",
151 () -> assertTrue(year1.equals(year1), "year11 is not equal"),
152 () -> assertTrue(year1.equals(year2), "year12 are not equal"),
153 () -> assertTrue(year2.equals(year1), "year21 are not equal"),
154 () -> assertTrue(year2.equals(year4), "year24 are not equal"),
155 () -> assertTrue(year1.equals(year4), "year14 are not equal"),
156 () -> assertFalse(year1.equals(year3), "year13 are equal"),
157 () -> assertFalse(year3.equals(year1), "year31 are equal"),
158 () -> assertFalse(year1.equals(null), "year10 is equal")
159 );
160 }
161
162
163
164
165
166 @Test
167 void testToString()
168 {
169 final Year year = Year.of(1);
170 assertEquals("Year[calendarSystem=GREGORIAN, year=1]", year.toString(), "toString not equal");
171 }
172
173
174
175
176
177 @Test
178 @SuppressWarnings("java:S5785")
179 void testCompareTo1()
180 {
181 final Year year1 = Year.of(1);
182 final Year year2 = Year.of(1);
183 final Year year3 = Year.of(2);
184 final Year year4 = Year.of(3);
185 final Year year5 = Year.of(1);
186 assertAll("testCompareTo",
187 () -> assertTrue(year1.compareTo(year2) == -year2.compareTo(year1), "reflexive1"),
188 () -> assertTrue(year1.compareTo(year3) == -year3.compareTo(year1), "reflexive2"),
189 () -> assertTrue((year4.compareTo(year3) > 0) && (year3.compareTo(year1) > 0) && (year4.compareTo(year1) > 0), "transitive1"),
190 () -> assertTrue((year1.compareTo(year2) == 0) && (Math.abs(year1.compareTo(year5)) == Math.abs(year2.compareTo(year5))), "sgn1"),
191 () -> assertTrue((year1.compareTo(year2) == 0) && year1.equals(year2), "equals")
192 );
193 }
194
195
196
197
198
199 @Test
200 @SuppressWarnings("java:S5785")
201 void testCompareTo2()
202 {
203 final Year year1 = Year.of(CalendarSystems.JULIAN, 1582);
204 final Year year2 = Year.of(CalendarSystems.GREGORIAN, 1582);
205 assertThrows(IllegalStateException.class, () ->
206 {
207 year1.compareTo(year2);
208 }, "Illegal state exception"
209 );
210 }
211
212
213
214
215
216 @Test
217 void testAdd1()
218 {
219 final Year year = Year.of(2022);
220 final Years years = Years.of(1);
221 final Year yearResult = year.add(years);
222 assertEquals(2023, yearResult.longValue(), YearTests.RESULT_NOT_AS_EXPECTED);
223 }
224
225
226
227
228
229 @Test
230 void testAdd2()
231 {
232 final Year year = Year.of(-1);
233 final Years years = Years.of(1);
234 final Year yearResult = year.add(years);
235 assertEquals(1, yearResult.longValue(), YearTests.RESULT_NOT_AS_EXPECTED);
236 }
237
238
239
240
241
242 @Test
243 void testAdd3()
244 {
245 final Year year = Year.of(Long.MAX_VALUE);
246 final Years years = Years.of(1);
247 assertThrows(ArithmeticException.class, () ->
248 {
249 year.add(years);
250 }, YearTests.ARITHMETIC_EXCEPTION_EXPECTED
251 );
252 }
253
254
255
256
257
258 @Test
259 void testAdd4()
260 {
261 final Year year = Year.of(-2);
262 final Years years = Years.of(1);
263 final Year yearResult = year.add(years);
264 assertEquals(-1, yearResult.longValue(), YearTests.RESULT_NOT_AS_EXPECTED);
265 }
266
267
268
269
270
271 @Test
272 void testSubtract1()
273 {
274 final Year year = Year.of(2022);
275 final Years years = Years.of(1);
276 final Year yearResult = year.subtract(years);
277 assertEquals(2021, yearResult.longValue(), YearTests.RESULT_NOT_AS_EXPECTED);
278 }
279
280
281
282
283
284 @Test
285 void testSubtract2()
286 {
287 final Year year = Year.of(1);
288 final Years years = Years.of(1);
289 final Year yearResult = year.subtract(years);
290 assertEquals(-1, yearResult.longValue(), YearTests.RESULT_NOT_AS_EXPECTED);
291 }
292
293
294
295
296
297 @Test
298 void testSubtract3()
299 {
300 final Year year = Year.of(Long.MIN_VALUE);
301 final Years years = Years.of(1);
302 assertThrows(ArithmeticException.class, () ->
303 {
304 year.subtract(years);
305 }, YearTests.ARITHMETIC_EXCEPTION_EXPECTED
306 );
307 }
308
309
310
311
312
313 @Test
314 void testSubtract4()
315 {
316 final Year year = Year.of(-1);
317 final Years years = Years.of(1);
318 final Year yearResult = year.subtract(years);
319 assertEquals(-2, yearResult.longValue(), YearTests.RESULT_NOT_AS_EXPECTED);
320 }
321
322
323
324
325
326 @Test
327 void testIncrement1()
328 {
329 final Year year = Year.of(2022);
330 final Year yearResult = year.increment();
331 assertEquals(2023, yearResult.longValue(), YearTests.RESULT_NOT_AS_EXPECTED);
332 }
333
334
335
336
337
338 @Test
339 void testIncrement2()
340 {
341 final Year year = Year.of(-1);
342 final Year yearResult = year.increment();
343 assertEquals(1, yearResult.longValue(), YearTests.RESULT_NOT_AS_EXPECTED);
344 }
345
346
347
348
349
350 @Test
351 void testIncrement3()
352 {
353 final Year year = Year.of(Long.MAX_VALUE);
354 assertThrows(ArithmeticException.class, () ->
355 {
356 year.increment();
357 }, YearTests.ARITHMETIC_EXCEPTION_EXPECTED
358 );
359 }
360
361
362
363
364
365 @Test
366 void testDecrement1()
367 {
368 final Year year = Year.of(2022);
369 final Year yearResult = year.decrement();
370 assertEquals(2021, yearResult.longValue(), YearTests.RESULT_NOT_AS_EXPECTED);
371 }
372
373
374
375
376
377 @Test
378 void testDecrement2()
379 {
380 final Year year = Year.of(1);
381 final Year yearResult = year.decrement();
382 assertEquals(-1, yearResult.longValue(), YearTests.RESULT_NOT_AS_EXPECTED);
383 }
384
385
386
387
388
389 @Test
390 void testDecrement3()
391 {
392 final Year year = Year.of(Long.MIN_VALUE);
393 assertThrows(ArithmeticException.class, () ->
394 {
395 year.decrement();
396 }, YearTests.ARITHMETIC_EXCEPTION_EXPECTED
397 );
398 }
399
400
401
402
403
404 @Test
405 void testMonthWithin()
406 {
407 final Year year = Year.of(1);
408 final Months result = year.monthsWithin();
409 assertEquals(12, result.longValue(), YearTests.RESULT_NOT_AS_EXPECTED);
410 }
411
412
413
414
415
416 @Test
417 void testIsLeapYear1()
418 {
419 final Year year = Year.of(CalendarSystems.JULIAN, 4);
420 final boolean result = year.isLeapYear();
421 assertTrue(result, YearTests.RESULT_NOT_AS_EXPECTED);
422 }
423
424
425
426
427
428
429
430 @ParameterizedTest
431 @ValueSource(longs = {2000, 1500, 2004})
432 void testIsLeapYear2(final long pYear)
433 {
434 final Year year = Year.of(CalendarSystems.GREGORIAN, pYear);
435 final boolean result = year.isLeapYear();
436 assertTrue(result, YearTests.RESULT_NOT_AS_EXPECTED);
437 }
438
439
440
441
442
443 @Test
444 void testIsLeapYear5()
445 {
446 final Year year = Year.of(CalendarSystems.GREGORIAN, 2001);
447 final boolean result = year.isLeapYear();
448 assertFalse(result, YearTests.RESULT_NOT_AS_EXPECTED);
449 }
450
451
452
453
454
455 @Test
456 void testIsLeapYear7()
457 {
458 final Year year = Year.of(CalendarSystems.JULIAN, -1);
459 final boolean result = year.isLeapYear();
460 assertTrue(result, YearTests.RESULT_NOT_AS_EXPECTED);
461 }
462
463
464
465
466
467 @Test
468 void testIsLeapYear8()
469 {
470 final Year year = Year.of(CalendarSystems.JULIAN, -2);
471 final boolean result = year.isLeapYear();
472 assertFalse(result, YearTests.RESULT_NOT_AS_EXPECTED);
473 }
474
475
476
477
478
479
480
481
482 @ParameterizedTest
483 @CsvSource({"2000,366", "2001,365", "1582,355"})
484 void testDaysWithin1(final long pYear, final long pDays)
485 {
486 final Year year = Year.of(CalendarSystems.GREGORIAN, pYear);
487 final Days days = year.daysWithin();
488 assertEquals(pDays, days.longValue(), YearTests.RESULT_NOT_AS_EXPECTED);
489 }
490
491
492
493
494
495 @Test
496 void testDaysWithin4()
497 {
498 final Year year = Year.of(CalendarSystems.JULIAN, 2000);
499 final Days days = year.daysWithin();
500 assertEquals(366, days.longValue(), YearTests.RESULT_NOT_AS_EXPECTED);
501 }
502
503
504
505
506
507 @Test
508 void testDaysWithin5()
509 {
510 final Year year = Year.of(CalendarSystems.JULIAN, 2001);
511 final Days days = year.daysWithin();
512 assertEquals(365, days.longValue(), YearTests.RESULT_NOT_AS_EXPECTED);
513 }
514
515 }