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  
16  import de.powerstat.validation.values.Day;
17  import de.powerstat.validation.values.Month;
18  import de.powerstat.validation.values.MonthDay;
19  import de.powerstat.validation.values.Months;
20  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
21  
22  
23  /**
24   * Month day tests.
25   */
26  @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"})
27  final class MonthDayTests
28   {
29    /**
30     * Month not as expected constant.
31     */
32    private static final String MONTH_NOT_AS_EXPECTED = "Month not as expected";
33  
34    /**
35     * Day not as expected constant.
36     */
37    private static final String DAY_NOT_AS_EXPECTED = "Day not as expected";
38  
39    /**
40     * Day month constant.
41     */
42    private static final String DAYMONTH_10_13 = "10-13";
43  
44    /**
45     * Result not as expected constant.
46     */
47    private static final String RESULT_NOT_AS_EXPECTED = "result not as expected";
48  
49    /**
50     * Test of constant.
51     */
52    private static final String TEST_OF = "testOf";
53  
54  
55    /**
56     * Default constructor.
57     */
58    /* default */ MonthDayTests()
59     {
60      super();
61     }
62  
63  
64    /**
65     * Factory test.
66     */
67    @Test
68    /* default */ void testOf1()
69     {
70      final MonthDay test = MonthDay.of(Month.of(10), Day.of(13));
71      assertAll(TEST_OF,
72        () -> assertEquals(10, test.monthValue().intValue(), MONTH_NOT_AS_EXPECTED),
73        () -> assertEquals(13, test.dayValue().intValue(), DAY_NOT_AS_EXPECTED)
74      );
75     }
76  
77  
78    /**
79     * Factory test.
80     */
81    @Test
82    /* default */ void testOf2()
83     {
84      final MonthDay test = MonthDay.of(DAYMONTH_10_13);
85      assertAll(TEST_OF,
86        () -> assertEquals(10, test.monthValue().intValue(), MONTH_NOT_AS_EXPECTED),
87        () -> assertEquals(13, test.dayValue().intValue(), DAY_NOT_AS_EXPECTED)
88      );
89     }
90  
91  
92    /**
93     * Factory test.
94     */
95    @Test
96    /* default */ void testOf3()
97     {
98      assertThrows(IllegalArgumentException.class, () ->
99       {
100       /* final MonthDay test = */ MonthDay.of("10");
101      }, "Illegal argument exception"
102     );
103    }
104 
105 
106   /**
107    * Factory test.
108    */
109   @Test
110   /* default */ void testOf4()
111    {
112     final MonthDay test = MonthDay.of(Month.of(2), Day.of(1));
113     assertAll(TEST_OF,
114       () -> assertEquals(2, test.monthValue().intValue(), MONTH_NOT_AS_EXPECTED),
115       () -> assertEquals(1, test.dayValue().intValue(), DAY_NOT_AS_EXPECTED)
116     );
117    }
118 
119 
120   /**
121    * Factory test.
122    */
123   @Test
124   /* default */ void testOf5()
125    {
126     final Month month = Month.of(2);
127     final Day day = Day.of(30);
128     assertThrows(IndexOutOfBoundsException.class, () ->
129      {
130       /* final MonthDay test = */ MonthDay.of(month, day);
131      }, "Index out of bounds exception"
132     );
133    }
134 
135 
136   /**
137    * stringValue test.
138    */
139   @Test
140   /* default */ void testStringValue()
141    {
142     final MonthDay test = MonthDay.of(DAYMONTH_10_13);
143     assertEquals(DAYMONTH_10_13, test.stringValue(), MONTH_NOT_AS_EXPECTED);
144    }
145 
146 
147   /**
148    * Test hash code.
149    */
150   @Test
151   /* default */ void testHashCode()
152    {
153     final MonthDay test1 = MonthDay.of(Month.of(10), Day.of(13));
154     final MonthDay test2 = MonthDay.of(Month.of(10), Day.of(13));
155     final MonthDay test3 = MonthDay.of(Month.of(10), Day.of(14));
156     assertAll("testHashCode", //$NON-NLS-1$
157       () -> assertEquals(test1.hashCode(), test2.hashCode(), "hashCodes are not equal"), //$NON-NLS-1$
158       () -> assertNotEquals(test1.hashCode(), test3.hashCode(), "hashCodes are equal") //$NON-NLS-1$
159     );
160    }
161 
162 
163   /**
164    * Test equals.
165    */
166   @Test
167   @SuppressWarnings("java:S5785")
168   /* default */ void testEquals()
169    {
170     final MonthDay test1 = MonthDay.of(Month.of(10), Day.of(13));
171     final MonthDay test2 = MonthDay.of(Month.of(10), Day.of(13));
172     final MonthDay test3 = MonthDay.of(Month.of(10), Day.of(14));
173     final MonthDay test4 = MonthDay.of(Month.of(10), Day.of(13));
174     assertAll("testEquals", //$NON-NLS-1$
175       () -> assertTrue(test1.equals(test1), "test11 is not equal"), //$NON-NLS-1$
176       () -> assertTrue(test1.equals(test2), "test12 are not equal"), //$NON-NLS-1$
177       () -> assertTrue(test2.equals(test1), "test21 are not equal"), //$NON-NLS-1$
178       () -> assertTrue(test2.equals(test4), "test24 are not equal"), //$NON-NLS-1$
179       () -> assertTrue(test1.equals(test4), "test14 are not equal"), //$NON-NLS-1$
180       () -> assertFalse(test1.equals(test3), "test13 are equal"), //$NON-NLS-1$
181       () -> assertFalse(test3.equals(test1), "test31 are equal"), //$NON-NLS-1$
182       () -> assertFalse(test1.equals(null), "test10 is equal") //$NON-NLS-1$
183     );
184    }
185 
186 
187   /**
188    * Test equals.
189    */
190   @Test
191   @SuppressWarnings("java:S5785")
192   /* default */ void testEquals2()
193    {
194     final MonthDay test1 = MonthDay.of(Month.of(10), Day.of(13));
195     final MonthDay test2 = MonthDay.of(Month.of(9), Day.of(13));
196     assertAll("testEquals", //$NON-NLS-1$
197       () -> assertFalse(test1.equals(test2), "test12 is not equal") //$NON-NLS-1$
198     );
199    }
200 
201 
202   /**
203    * toString test.
204    */
205   @Test
206   /* default */ void testToString()
207    {
208     final MonthDay test = MonthDay.of(DAYMONTH_10_13);
209     assertEquals("MonthDay[month=Month[month=10], day=Day[day=13]]", test.toString(), "toString not as expected");
210    }
211 
212 
213   /**
214    * Test compareTo.
215    */
216   @Test
217   @SuppressWarnings("java:S5785")
218   /* default */ void testCompareTo()
219    {
220     final MonthDay test1 = MonthDay.of(Month.of(10), Day.of(13));
221     final MonthDay test2 = MonthDay.of(Month.of(10), Day.of(13));
222     final MonthDay test3 = MonthDay.of(Month.of(10), Day.of(14));
223     final MonthDay test4 = MonthDay.of(Month.of(11), Day.of(13));
224     final MonthDay test5 = MonthDay.of(Month.of(10), Day.of(13));
225     assertAll("testCompareTo", //$NON-NLS-1$
226       () -> assertTrue(test1.compareTo(test2) == -test2.compareTo(test1), "reflexive1"), //$NON-NLS-1$
227       () -> assertTrue(test1.compareTo(test3) == -test3.compareTo(test1), "reflexive2"), //$NON-NLS-1$
228       () -> assertTrue((test4.compareTo(test3) > 0) && (test3.compareTo(test1) > 0) && (test4.compareTo(test1) > 0), "transitive1"), //$NON-NLS-1$
229       () -> assertTrue((test1.compareTo(test2) == 0) && (Math.abs(test1.compareTo(test5)) == Math.abs(test2.compareTo(test5))), "sgn1"), //$NON-NLS-1$
230       () -> assertTrue((test1.compareTo(test2) == 0) && test1.equals(test2), "equals") //$NON-NLS-1$
231     );
232    }
233 
234 
235   /**
236    * Test add.
237    */
238   @Test
239   /* default */ void testAdd1()
240    {
241     final MonthDay test1 = MonthDay.of(Month.of(10), Day.of(13));
242     final MonthDay result = test1.add(Months.of(1));
243     assertAll("add", //$NON-NLS-1$
244       () -> assertEquals(11, result.monthValue().intValue(), RESULT_NOT_AS_EXPECTED),
245       () -> assertEquals(13, result.dayValue().intValue(), RESULT_NOT_AS_EXPECTED)
246     );
247    }
248 
249 
250   /**
251    * Test add.
252    */
253   @Test
254   /* default */ void testAdd2()
255    {
256     final MonthDay test1 = MonthDay.of(Month.of(10), Day.of(13));
257     final Months months = Months.of(3);
258     assertThrows(ArithmeticException.class, () ->
259      {
260       /* final MonthDay result = */ test1.add(months);
261      }, "Arithmetic exception"
262     );
263    }
264 
265 
266   /**
267    * Test sutract.
268    */
269   @Test
270   /* default */ void testSubtract1()
271    {
272     final MonthDay test1 = MonthDay.of(Month.of(10), Day.of(13));
273     final MonthDay result = test1.subtract(Months.of(1));
274     assertAll("subtract", //$NON-NLS-1$
275       () -> assertEquals(9, result.monthValue().intValue(), RESULT_NOT_AS_EXPECTED),
276       () -> assertEquals(13, result.dayValue().intValue(), RESULT_NOT_AS_EXPECTED)
277     );
278    }
279 
280 
281   /**
282    * Test subtract.
283    */
284   @Test
285   /* default */ void testSubtract2()
286    {
287     final MonthDay test1 = MonthDay.of(Month.of(2), Day.of(13));
288     final Months months = Months.of(2);
289     assertThrows(ArithmeticException.class, () ->
290      {
291       /* final MonthDay result = */ test1.subtract(months);
292      }, "Arithmetic exception"
293     );
294    }
295 
296 
297   /**
298    * Test increment.
299    */
300   @Test
301   /* default */ void testIncrement1()
302    {
303     final MonthDay test1 = MonthDay.of(Month.of(10), Day.of(13));
304     final MonthDay result = test1.incrementMonth();
305     assertAll("add", //$NON-NLS-1$
306       () -> assertEquals(11, result.monthValue().intValue(), RESULT_NOT_AS_EXPECTED),
307       () -> assertEquals(13, result.dayValue().intValue(), RESULT_NOT_AS_EXPECTED)
308     );
309    }
310 
311 
312   /**
313    * Test increment.
314    */
315   @Test
316   /* default */ void testIncrement2()
317    {
318     final MonthDay test1 = MonthDay.of(Month.of(12), Day.of(13));
319     assertThrows(ArithmeticException.class, () ->
320      {
321       /* final MonthDay result = */ test1.incrementMonth();
322      }, "Arithmetic exception"
323     );
324    }
325 
326 
327   /**
328    * Test decrement.
329    */
330   @Test
331   /* default */ void testDecrement1()
332    {
333     final MonthDay test1 = MonthDay.of(Month.of(10), Day.of(13));
334     final MonthDay result = test1.decrementMonth();
335     assertAll("add", //$NON-NLS-1$
336       () -> assertEquals(9, result.monthValue().intValue(), RESULT_NOT_AS_EXPECTED),
337       () -> assertEquals(13, result.dayValue().intValue(), RESULT_NOT_AS_EXPECTED)
338     );
339    }
340 
341 
342   /**
343    * Test decrement.
344    */
345   @Test
346   /* default */ void testDecrement2()
347    {
348     final MonthDay test1 = MonthDay.of(Month.of(1), Day.of(13));
349     assertThrows(ArithmeticException.class, () ->
350      {
351       /* final MonthDay result = */ test1.decrementMonth();
352      }, "Arithmetic exception"
353     );
354    }
355 
356  }