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.Second;
19  import de.powerstat.validation.values.Seconds;
20  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
21  
22  
23  /**
24   * Second 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 SecondTests
28   {
29    /**
30     * Not a second constant.
31     */
32    private static final String NOT_A_SECOND = "Not a second!"; //$NON-NLS-1$
33  
34    /**
35     * Result not 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 */ SecondTests()
49     {
50      super();
51     }
52  
53  
54    /**
55     * Factory string test.
56     */
57    @Test
58    /* default */ void testFactory1()
59     {
60      assertEquals(0, Second.of("0").intValue(), SecondTests.NOT_A_SECOND);
61     }
62  
63  
64    /**
65     * Is second.
66     *
67     * @param second Second
68     */
69    @ParameterizedTest
70    @ValueSource(ints = {0, 59, 60})
71    /* default */ void testIsSecond(final int second)
72     {
73      assertEquals(second, Second.of(second).intValue(), SecondTests.NOT_A_SECOND);
74     }
75  
76  
77    /**
78     * Is not a second.
79     *
80     * @param second Second
81     */
82    @ParameterizedTest
83    @ValueSource(ints = {-1, 61})
84    /* default */ void testIsNotASecond(final int second)
85     {
86      assertThrows(IndexOutOfBoundsException.class, () ->
87       {
88        /* final Second second = */ Second.of(second);
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, Second.of(10).intValue(), SecondTests.NOT_A_SECOND);
101    }
102 
103 
104   /**
105    * stringValue.
106    */
107   @Test
108   /* default */ void testStringValue()
109    {
110     assertEquals("10", Second.of(10).stringValue(), SecondTests.NOT_A_SECOND);
111    }
112 
113 
114   /**
115    * Test hash code.
116    */
117   @Test
118   /* default */ void testHashCode()
119    {
120     final Second second1 = Second.of(1);
121     final Second second2 = Second.of(1);
122     final Second second3 = Second.of(2);
123     assertAll("testHashCode", //$NON-NLS-1$
124       () -> assertEquals(second1.hashCode(), second2.hashCode(), "hashCodes are not equal"), //$NON-NLS-1$
125       () -> assertNotEquals(second1.hashCode(), second3.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 Second second1 = Second.of(1);
138     final Second second2 = Second.of(1);
139     final Second second3 = Second.of(2);
140     final Second second4 = Second.of(1);
141     assertAll("testEquals", //$NON-NLS-1$
142       () -> assertTrue(second1.equals(second1), "second11 is not equal"), //$NON-NLS-1$
143       () -> assertTrue(second1.equals(second2), "second12 are not equal"), //$NON-NLS-1$
144       () -> assertTrue(second2.equals(second1), "second21 are not equal"), //$NON-NLS-1$
145       () -> assertTrue(second2.equals(second4), "second24 are not equal"), //$NON-NLS-1$
146       () -> assertTrue(second1.equals(second4), "second14 are not equal"), //$NON-NLS-1$
147       () -> assertFalse(second1.equals(second3), "second13 are equal"), //$NON-NLS-1$
148       () -> assertFalse(second3.equals(second1), "second31 are equal"), //$NON-NLS-1$
149       () -> assertFalse(second1.equals(null), "second10 is equal") //$NON-NLS-1$
150     );
151    }
152 
153 
154   /**
155    * Test toString.
156    */
157   @Test
158   /* default */ void testToString()
159    {
160     final Second second = Second.of(1);
161     assertEquals("Second[second=1]", second.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 Second second1 = Second.of(1);
173     final Second second2 = Second.of(1);
174     final Second second3 = Second.of(2);
175     final Second second4 = Second.of(3);
176     final Second second5 = Second.of(1);
177     assertAll("testCompareTo", //$NON-NLS-1$
178       () -> assertTrue(second1.compareTo(second2) == -second2.compareTo(second1), "reflexive1"), //$NON-NLS-1$
179       () -> assertTrue(second1.compareTo(second3) == -second3.compareTo(second1), "reflexive2"), //$NON-NLS-1$
180       () -> assertTrue((second4.compareTo(second3) > 0) && (second3.compareTo(second1) > 0) && (second4.compareTo(second1) > 0), "transitive1"), //$NON-NLS-1$
181       () -> assertTrue((second1.compareTo(second2) == 0) && (Math.abs(second1.compareTo(second5)) == Math.abs(second2.compareTo(second5))), "sgn1"), //$NON-NLS-1$
182       () -> assertTrue((second1.compareTo(second2) == 0) && second1.equals(second2), "equals") //$NON-NLS-1$
183     );
184    }
185 
186 
187   /**
188    * Test add.
189    */
190   @Test
191   /* default */ void testAdd1()
192    {
193     final Second second = Second.of(1);
194     final Seconds seconds = Seconds.of(1);
195     final Second secondResult = second.add(seconds);
196     assertEquals(2, secondResult.intValue(), SecondTests.RESULT_NOT_AS_EXPECTED);
197    }
198 
199 
200   /**
201    * Test add.
202    */
203   @Test
204   /* default */ void testAdd2()
205    {
206     final Second second = Second.of(59);
207     final Seconds seconds = Seconds.of(1);
208     assertThrows(ArithmeticException.class, () ->
209      {
210       /* final Second secondResult = */ second.add(seconds);
211      }, SecondTests.ARITHMETIC_EXCEPTION_EXPECTED
212     );
213    }
214 
215 
216   /**
217    * Test subtract.
218    */
219   @Test
220   /* default */ void testSubtract1()
221    {
222     final Second second = Second.of(2);
223     final Seconds seconds = Seconds.of(1);
224     final Second secondResult = second.subtract(seconds);
225     assertEquals(1, secondResult.intValue(), SecondTests.RESULT_NOT_AS_EXPECTED);
226    }
227 
228 
229   /**
230    * Test subtract.
231    */
232   @Test
233   /* default */ void testSubtract2()
234    {
235     final Second second = Second.of(0);
236     final Seconds seconds = Seconds.of(1);
237     assertThrows(ArithmeticException.class, () ->
238      {
239       /* final Second secondResult = */ second.subtract(seconds);
240      }, SecondTests.ARITHMETIC_EXCEPTION_EXPECTED
241     );
242    }
243 
244 
245   /**
246    * Test increment.
247    */
248   @Test
249   /* default */ void testIncrement1()
250    {
251     final Second second = Second.of(1);
252     final Second secondResult = second.increment();
253     assertEquals(2, secondResult.intValue(), SecondTests.RESULT_NOT_AS_EXPECTED);
254    }
255 
256 
257   /**
258    * Test increment.
259    */
260   @Test
261   /* default */ void testIncrement2()
262    {
263     final Second second = Second.of(59);
264     assertThrows(ArithmeticException.class, () ->
265      {
266       /* final Second secondResult = */ second.increment();
267      }, SecondTests.ARITHMETIC_EXCEPTION_EXPECTED
268     );
269    }
270 
271 
272   /**
273    * Test decrement.
274    */
275   @Test
276   /* default */ void testDecrement1()
277    {
278     final Second second = Second.of(2);
279     final Second secondResult = second.decrement();
280     assertEquals(1, secondResult.intValue(), SecondTests.RESULT_NOT_AS_EXPECTED);
281    }
282 
283 
284   /**
285    * Test decrement.
286    */
287   @Test
288   /* default */ void testDecrement2()
289    {
290     final Second second = Second.of(0);
291     assertThrows(ArithmeticException.class, () ->
292      {
293       /* final Second secondResult = */ second.decrement();
294      }, SecondTests.ARITHMETIC_EXCEPTION_EXPECTED
295     );
296    }
297 
298  }