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