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.Day;
19  import de.powerstat.validation.values.Days;
20  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
21  
22  
23  /**
24   * 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 DayTests
28   {
29    /**
30     * Result not as expected constant.
31     */
32    private static final String RESULT_NOT_AS_EXPECTED = "Result not as expected"; //$NON-NLS-1$
33  
34    /**
35     * Arithmetic exception expected contant.
36     */
37    private static final String ARITHMETIC_EXCEPTION_EXPECTED = "Arithmetic exception expected"; //$NON-NLS-1$
38  
39    /**
40     * 10 constant.
41     */
42    private static final String TEN = "10"; //$NON-NLS-1$
43  
44    /**
45     * Not a day constant.
46     */
47    private static final String NOT_A_DAY = "Not a day!"; //$NON-NLS-1$
48  
49  
50    /**
51     * Default constructor.
52     */
53    /* default */ DayTests()
54     {
55      super();
56     }
57  
58  
59    /**
60     * Factory string gtest.
61     */
62    @Test
63    /* default */ void testFactory1()
64     {
65      assertEquals(10, Day.of(TEN).intValue(), NOT_A_DAY);
66     }
67  
68  
69    /**
70     * intValue.
71     */
72    @Test
73    /* default */ void testIntValue()
74     {
75      assertEquals(10, Day.of(10).intValue(), NOT_A_DAY);
76     }
77  
78  
79    /**
80     * stringValue.
81     */
82    @Test
83    /* default */ void testStringValue()
84     {
85      assertEquals(TEN, Day.of(10).stringValue(), NOT_A_DAY);
86     }
87  
88  
89    /**
90     * Is day.
91     *
92     * @param day Day
93     */
94    @ParameterizedTest
95    @ValueSource(ints = {1, 31})
96    /* default */ void testIsDay(final int day)
97     {
98      assertEquals(day, Day.of(day).intValue(), NOT_A_DAY);
99     }
100 
101 
102   /**
103    * Is not a day.
104    *
105    * @param day Day
106    */
107   @ParameterizedTest
108   @ValueSource(ints = {0, 32})
109   /* default */ void testIsNotADay(final int day)
110    {
111     assertThrows(IndexOutOfBoundsException.class, () ->
112      {
113       /* final Day day = */ Day.of(day);
114      }, "Index out of bounds exception expected" //$NON-NLS-1$
115     );
116    }
117 
118 
119   /**
120    * Test hash code.
121    */
122   @Test
123   /* default */ void testHashCode()
124    {
125     final Day day1 = Day.of(1);
126     final Day day2 = Day.of(1);
127     final Day day3 = Day.of(2);
128     assertAll("testHashCode", //$NON-NLS-1$
129       () -> assertEquals(day1.hashCode(), day2.hashCode(), "hashCodes are not equal"), //$NON-NLS-1$
130       () -> assertNotEquals(day1.hashCode(), day3.hashCode(), "hashCodes are equal") //$NON-NLS-1$
131     );
132    }
133 
134 
135   /**
136    * Test equals.
137    */
138   @Test
139   @SuppressWarnings("java:S5785")
140   /* default */ void testEquals()
141    {
142     final Day day1 = Day.of(1);
143     final Day day2 = Day.of(1);
144     final Day day3 = Day.of(2);
145     final Day day4 = Day.of(1);
146     assertAll("testEquals", //$NON-NLS-1$
147       () -> assertTrue(day1.equals(day1), "day11 is not equal"), //$NON-NLS-1$
148       () -> assertTrue(day1.equals(day2), "day12 are not equal"), //$NON-NLS-1$
149       () -> assertTrue(day2.equals(day1), "day21 are not equal"), //$NON-NLS-1$
150       () -> assertTrue(day2.equals(day4), "day24 are not equal"), //$NON-NLS-1$
151       () -> assertTrue(day1.equals(day4), "day14 are not equal"), //$NON-NLS-1$
152       () -> assertFalse(day1.equals(day3), "day13 are equal"), //$NON-NLS-1$
153       () -> assertFalse(day3.equals(day1), "day31 are equal"), //$NON-NLS-1$
154       () -> assertFalse(day1.equals(null), "day10 is equal") //$NON-NLS-1$
155     );
156    }
157 
158 
159   /**
160    * Test toString.
161    */
162   @Test
163   /* default */ void testToString()
164    {
165     final Day day = Day.of(1);
166     assertEquals("Day[day=1]", day.toString(), "toString not equal"); //$NON-NLS-1$ //$NON-NLS-2$
167    }
168 
169 
170   /**
171    * Test compareTo.
172    */
173   @Test
174   @SuppressWarnings("java:S5785")
175   /* default */ void testCompareTo()
176    {
177     final Day day1 = Day.of(1);
178     final Day day2 = Day.of(1);
179     final Day day3 = Day.of(2);
180     final Day day4 = Day.of(3);
181     final Day day5 = Day.of(1);
182     assertAll("testCompareTo", //$NON-NLS-1$
183       () -> assertTrue(day1.compareTo(day2) == -day2.compareTo(day1), "reflexive1"), //$NON-NLS-1$
184       () -> assertTrue(day1.compareTo(day3) == -day3.compareTo(day1), "reflexive2"), //$NON-NLS-1$
185       () -> assertTrue((day4.compareTo(day3) > 0) && (day3.compareTo(day1) > 0) && (day4.compareTo(day1) > 0), "transitive1"), //$NON-NLS-1$
186       () -> assertTrue((day1.compareTo(day2) == 0) && (Math.abs(day1.compareTo(day5)) == Math.abs(day2.compareTo(day5))), "sgn1"), //$NON-NLS-1$
187       () -> assertTrue((day1.compareTo(day2) == 0) && day1.equals(day2), "equals") //$NON-NLS-1$
188     );
189    }
190 
191 
192   /**
193    * Test add.
194    */
195   @Test
196   /* default */ void testAdd1()
197    {
198     final Day day = Day.of(1);
199     final Days days = Days.of(1);
200     final Day dayResult = day.add(days);
201     assertEquals(2, dayResult.intValue(), DayTests.RESULT_NOT_AS_EXPECTED);
202    }
203 
204 
205   /**
206    * Test add.
207    */
208   @Test
209   /* default */ void testAdd2()
210    {
211     final Day day = Day.of(31);
212     final Days days = Days.of(1);
213     assertThrows(ArithmeticException.class, () ->
214      {
215       /* final Day dayResult = */ day.add(days);
216      }, DayTests.ARITHMETIC_EXCEPTION_EXPECTED
217     );
218    }
219 
220 
221   /**
222    * Test subtract.
223    */
224   @Test
225   /* default */ void testSubtract1()
226    {
227     final Day day = Day.of(2);
228     final Days days = Days.of(1);
229     final Day dayResult = day.subtract(days);
230     assertEquals(1, dayResult.intValue(), DayTests.RESULT_NOT_AS_EXPECTED);
231    }
232 
233 
234   /**
235    * Test subtract.
236    */
237   @Test
238   /* default */ void testSubtract2()
239    {
240     final Day day = Day.of(1);
241     final Days days = Days.of(1);
242     assertThrows(ArithmeticException.class, () ->
243      {
244       /* final Day dayResult = */ day.subtract(days);
245      }, DayTests.ARITHMETIC_EXCEPTION_EXPECTED
246     );
247    }
248 
249 
250   /**
251    * Test increment.
252    */
253   @Test
254   /* default */ void testIncrement1()
255    {
256     final Day day = Day.of(1);
257     final Day dayResult = day.increment();
258     assertEquals(2, dayResult.intValue(), DayTests.RESULT_NOT_AS_EXPECTED);
259    }
260 
261 
262   /**
263    * Test increment.
264    */
265   @Test
266   /* default */ void testIncrement2()
267    {
268     final Day day = Day.of(31);
269     assertThrows(ArithmeticException.class, () ->
270      {
271       /* final Day dayResult = */ day.increment();
272      }, DayTests.ARITHMETIC_EXCEPTION_EXPECTED
273     );
274    }
275 
276 
277   /**
278    * Test decrement.
279    */
280   @Test
281   /* default */ void testDecrement1()
282    {
283     final Day day = Day.of(2);
284     final Day dayResult = day.decrement();
285     assertEquals(1, dayResult.intValue(), DayTests.RESULT_NOT_AS_EXPECTED);
286    }
287 
288 
289   /**
290    * Test decrement.
291    */
292   @Test
293   /* default */ void testDecrement2()
294    {
295     final Day day = Day.of(1);
296     assertThrows(ArithmeticException.class, () ->
297      {
298       /* final Day dayResult = */ day.decrement();
299      }, DayTests.ARITHMETIC_EXCEPTION_EXPECTED
300     );
301    }
302 
303  }