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.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   * Year tests.
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     * Result nor as expecte constant.
35     */
36    private static final String RESULT_NOT_AS_EXPECTED = "Result not as expected"; //$NON-NLS-1$
37  
38    /**
39     * Arithmetic exception expected constant.
40     */
41    private static final String ARITHMETIC_EXCEPTION_EXPECTED = "Arithmetic exception expected"; //$NON-NLS-1$
42  
43    /**
44     * 10 constant.
45     */
46    private static final String TEN = "10"; //$NON-NLS-1$
47  
48    /**
49     * Not a year constant.
50     */
51    private static final String NOT_A_YEAR = "Not a year!"; //$NON-NLS-1$
52  
53  
54    /**
55     * Default constructor.
56     */
57    /* default */ YearTests()
58     {
59      super();
60     }
61  
62  
63    /**
64     * Factory string test.
65     */
66    @Test
67    /* default */ void testFactory1()
68     {
69      assertEquals(10, Year.of(TEN).longValue(), NOT_A_YEAR);
70     }
71  
72  
73    /**
74     * longValue.
75     */
76    @Test
77    /* default */ void testLongValue()
78     {
79      assertEquals(10, Year.of(10).longValue(), NOT_A_YEAR);
80     }
81  
82  
83    /**
84     * stringValue.
85     */
86    @Test
87    /* default */ void testStringValue()
88     {
89      assertEquals(TEN, Year.of(10).stringValue(), NOT_A_YEAR);
90     }
91  
92  
93    /**
94     * Is year.
95     *
96     * @param year Year
97     */
98    @ParameterizedTest
99    @ValueSource(longs = {-1, 1, 2020})
100   /* default */ void testIsYear(final long year)
101    {
102     assertEquals(year, Year.of(year).longValue(), NOT_A_YEAR);
103    }
104 
105 
106   /**
107    * Is not a year.
108    *
109    * @param year Year
110    */
111   @ParameterizedTest
112   @ValueSource(longs = {0})
113   /* default */ void testIsNotAYear(final long year)
114    {
115     assertThrows(IndexOutOfBoundsException.class, () ->
116      {
117       /* final Year year = */ Year.of(year);
118      }, "Index out of bounds exception expected" //$NON-NLS-1$
119     );
120    }
121 
122 
123   /**
124    * Test hash code.
125    */
126   @Test
127   /* default */ 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", //$NON-NLS-1$
133       () -> assertEquals(year1.hashCode(), year2.hashCode(), "hashCodes are not equal"), //$NON-NLS-1$
134       () -> assertNotEquals(year1.hashCode(), year3.hashCode(), "hashCodes are equal") //$NON-NLS-1$
135     );
136    }
137 
138 
139   /**
140    * Test equals.
141    */
142   @Test
143   @SuppressWarnings("java:S5785")
144   /* default */ 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", //$NON-NLS-1$
151       () -> assertTrue(year1.equals(year1), "year11 is not equal"), //$NON-NLS-1$
152       () -> assertTrue(year1.equals(year2), "year12 are not equal"), //$NON-NLS-1$
153       () -> assertTrue(year2.equals(year1), "year21 are not equal"), //$NON-NLS-1$
154       () -> assertTrue(year2.equals(year4), "year24 are not equal"), //$NON-NLS-1$
155       () -> assertTrue(year1.equals(year4), "year14 are not equal"), //$NON-NLS-1$
156       () -> assertFalse(year1.equals(year3), "year13 are equal"), //$NON-NLS-1$
157       () -> assertFalse(year3.equals(year1), "year31 are equal"), //$NON-NLS-1$
158       () -> assertFalse(year1.equals(null), "year10 is equal") //$NON-NLS-1$
159     );
160    }
161 
162 
163   /**
164    * Test toString.
165    */
166   @Test
167   /* default */ void testToString()
168    {
169     final Year year = Year.of(1);
170     assertEquals("Year[calendarSystem=GREGORIAN, year=1]", year.toString(), "toString not equal"); //$NON-NLS-1$ //$NON-NLS-2$
171    }
172 
173 
174   /**
175    * Test compareTo.
176    */
177   @Test
178   @SuppressWarnings("java:S5785")
179   /* default */ 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", //$NON-NLS-1$
187       () -> assertTrue(year1.compareTo(year2) == -year2.compareTo(year1), "reflexive1"), //$NON-NLS-1$
188       () -> assertTrue(year1.compareTo(year3) == -year3.compareTo(year1), "reflexive2"), //$NON-NLS-1$
189       () -> assertTrue((year4.compareTo(year3) > 0) && (year3.compareTo(year1) > 0) && (year4.compareTo(year1) > 0), "transitive1"), //$NON-NLS-1$
190       () -> assertTrue((year1.compareTo(year2) == 0) && (Math.abs(year1.compareTo(year5)) == Math.abs(year2.compareTo(year5))), "sgn1"), //$NON-NLS-1$
191       () -> assertTrue((year1.compareTo(year2) == 0) && year1.equals(year2), "equals") //$NON-NLS-1$
192     );
193    }
194 
195 
196   /**
197    * Test compareTo.
198    */
199   @Test
200   @SuppressWarnings("java:S5785")
201   /* default */ 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    * Test add.
215    */
216   @Test
217   /* default */ 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    * Test add.
228    */
229   @Test
230   /* default */ 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    * Test add.
241    */
242   @Test
243   /* default */ 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       /* final Year yearResult = */ year.add(years);
250      }, YearTests.ARITHMETIC_EXCEPTION_EXPECTED
251     );
252    }
253 
254 
255   /**
256    * Test add.
257    */
258   @Test
259   /* default */ 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    * Test subtract.
270    */
271   @Test
272   /* default */ 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    * Test subtract.
283    */
284   @Test
285   /* default */ 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    * Test subtract.
296    */
297   @Test
298   /* default */ 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       /* final Year yearResult = */ year.subtract(years);
305      }, YearTests.ARITHMETIC_EXCEPTION_EXPECTED
306     );
307    }
308 
309 
310   /**
311    * Test subtract.
312    */
313   @Test
314   /* default */ 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    * Test incfrement.
325    */
326   @Test
327   /* default */ 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    * Test increment.
337    */
338   @Test
339   /* default */ 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    * Test increment.
349    */
350   @Test
351   /* default */ void testIncrement3()
352    {
353     final Year year = Year.of(Long.MAX_VALUE);
354     assertThrows(ArithmeticException.class, () ->
355      {
356       /* final Year yearResult = */ year.increment();
357      }, YearTests.ARITHMETIC_EXCEPTION_EXPECTED
358     );
359    }
360 
361 
362   /**
363    * Test decrement.
364    */
365   @Test
366   /* default */ 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    * Test decrement.
376    */
377   @Test
378   /* default */ 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    * Test decrement.
388    */
389   @Test
390   /* default */ void testDecrement3()
391    {
392     final Year year = Year.of(Long.MIN_VALUE);
393     assertThrows(ArithmeticException.class, () ->
394      {
395       /* final Year yearResult = */ year.decrement();
396      }, YearTests.ARITHMETIC_EXCEPTION_EXPECTED
397     );
398    }
399 
400 
401   /**
402    * Test monthsWithin.
403    */
404   @Test
405   /* default */ 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    * Test isLepaYear.
415    */
416   @Test
417   /* default */ 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    * Test isLepaYear.
427    *
428    * @param pYear Year
429    */
430   @ParameterizedTest
431   @ValueSource(longs = {2000, 1500, 2004})
432   /* default */ 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    * Test isLepaYear.
442    */
443   @Test
444   /* default */ 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    * Test isLepaYear.
454    */
455   @Test
456   /* default */ 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    * Test isLepaYear.
466    */
467   @Test
468   /* default */ 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    * Test daysWithin.
478    *
479    * @param pYear Year
480    * @param pDays Number of days in year
481    */
482   @ParameterizedTest
483   @CsvSource({"2000,366", "2001,365", "1582,355"})
484   /* default */ 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    * Test daysWithin.
494    */
495   @Test
496   /* default */ 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    * Test daysWithin.
506    */
507   @Test
508   /* default */ 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  }