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.Years;
19  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
20  
21  
22  /**
23   * Years tests.
24   */
25  @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"})
26  final class YearsTests
27   {
28    /**
29     * Not a years constant.
30     */
31    private static final String NOT_A_YEARS = "Not a years!"; //$NON-NLS-1$
32  
33    /**
34     * Result not as expected 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    /**
45     * Default constructor.
46     */
47    /* default */ YearsTests()
48     {
49      super();
50     }
51  
52  
53    /**
54     * Factory string test.
55     */
56    @Test
57    /* default */ void testFactory1()
58     {
59      assertEquals(0, Years.of("0").longValue(), YearsTests.NOT_A_YEARS);
60     }
61  
62  
63    /**
64     * Is years.
65     *
66     * @param years Years
67     */
68    @ParameterizedTest
69    @ValueSource(longs = {0, 20})
70    /* default */ void testIsYears(final long years)
71     {
72      assertEquals(years, Years.of(years).longValue(), YearsTests.NOT_A_YEARS);
73     }
74  
75  
76    /**
77     * Is not a years.
78     *
79     * @param years Years
80     */
81    @ParameterizedTest
82    @ValueSource(longs = {-1})
83    /* default */ void testIsNotAYears(final long years)
84     {
85      assertThrows(IndexOutOfBoundsException.class, () ->
86       {
87        /* final Years years = */ Years.of(years);
88       }, "Index out of bounds exception expected" //$NON-NLS-1$
89      );
90     }
91  
92  
93    /**
94     * longValue.
95     */
96    @Test
97    /* default */ void testLongValue()
98     {
99      assertEquals(10, Years.of(10).longValue(), YearsTests.NOT_A_YEARS);
100    }
101 
102 
103   /**
104    * stringValue.
105    */
106   @Test
107   /* default */ void testStringValue()
108    {
109     assertEquals("10", Years.of(10).stringValue(), YearsTests.NOT_A_YEARS);
110    }
111 
112 
113   /**
114    * Test hash code.
115    */
116   @Test
117   /* default */ void testHashCode()
118    {
119     final Years years1 = Years.of(1);
120     final Years years2 = Years.of(1);
121     final Years years3 = Years.of(2);
122     assertAll("testHashCode", //$NON-NLS-1$
123       () -> assertEquals(years1.hashCode(), years2.hashCode(), "hashCodes are not equal"), //$NON-NLS-1$
124       () -> assertNotEquals(years1.hashCode(), years3.hashCode(), "hashCodes are equal") //$NON-NLS-1$
125     );
126    }
127 
128 
129   /**
130    * Test equals.
131    */
132   @Test
133   @SuppressWarnings("java:S5785")
134   /* default */ void testEquals()
135    {
136     final Years years1 = Years.of(1);
137     final Years years2 = Years.of(1);
138     final Years years3 = Years.of(2);
139     final Years years4 = Years.of(1);
140     assertAll("testEquals", //$NON-NLS-1$
141       () -> assertTrue(years1.equals(years1), "years11 is not equal"), //$NON-NLS-1$
142       () -> assertTrue(years1.equals(years2), "years12 are not equal"), //$NON-NLS-1$
143       () -> assertTrue(years2.equals(years1), "years21 are not equal"), //$NON-NLS-1$
144       () -> assertTrue(years2.equals(years4), "years24 are not equal"), //$NON-NLS-1$
145       () -> assertTrue(years1.equals(years4), "years14 are not equal"), //$NON-NLS-1$
146       () -> assertFalse(years1.equals(years3), "years13 are equal"), //$NON-NLS-1$
147       () -> assertFalse(years3.equals(years1), "years31 are equal"), //$NON-NLS-1$
148       () -> assertFalse(years1.equals(null), "years10 is equal") //$NON-NLS-1$
149     );
150    }
151 
152 
153   /**
154    * Test toString.
155    */
156   @Test
157   /* default */ void testToString()
158    {
159     final Years years = Years.of(1);
160     assertEquals("Years[years=1]", years.toString(), "toString not equal"); //$NON-NLS-1$ //$NON-NLS-2$
161    }
162 
163 
164   /**
165    * Test compareTo.
166    */
167   @Test
168   @SuppressWarnings("java:S5785")
169   /* default */ void testCompareTo()
170    {
171     final Years years1 = Years.of(1);
172     final Years years2 = Years.of(1);
173     final Years years3 = Years.of(2);
174     final Years years4 = Years.of(3);
175     final Years years5 = Years.of(1);
176     assertAll("testCompareTo", //$NON-NLS-1$
177       () -> assertTrue(years1.compareTo(years2) == -years2.compareTo(years1), "reflexive1"), //$NON-NLS-1$
178       () -> assertTrue(years1.compareTo(years3) == -years3.compareTo(years1), "reflexive2"), //$NON-NLS-1$
179       () -> assertTrue((years4.compareTo(years3) > 0) && (years3.compareTo(years1) > 0) && (years4.compareTo(years1) > 0), "transitive1"), //$NON-NLS-1$
180       () -> assertTrue((years1.compareTo(years2) == 0) && (Math.abs(years1.compareTo(years5)) == Math.abs(years2.compareTo(years5))), "sgn1"), //$NON-NLS-1$
181       () -> assertTrue((years1.compareTo(years2) == 0) && years1.equals(years2), "equals") //$NON-NLS-1$
182     );
183    }
184 
185 
186   /**
187    * Test add.
188    */
189   @Test
190   /* default */ void testAdd1()
191    {
192     final Years years1 = Years.of(1);
193     final Years years2 = Years.of(1);
194     final Years yearsResult = years1.add(years2);
195     assertEquals(2, yearsResult.longValue(), YearsTests.RESULT_NOT_AS_EXPECTED);
196    }
197 
198 
199   /**
200    * Test add.
201    */
202   @Test
203   /* default */ void testAdd2()
204    {
205     final Years years1 = Years.of(Long.MAX_VALUE);
206     final Years years2 = Years.of(1);
207     assertThrows(ArithmeticException.class, () ->
208      {
209       /* final Years yearsResult = */ years1.add(years2);
210      }, YearsTests.ARITHMETIC_EXCEPTION_EXPECTED
211     );
212    }
213 
214 
215   /**
216    * Test substract.
217    */
218   @Test
219   /* default */ void testSubstract1()
220    {
221     final Years years1 = Years.of(6);
222     final Years years2 = Years.of(3);
223     final Years yearsResult = years1.subtract(years2);
224     assertEquals(3, yearsResult.longValue(), YearsTests.RESULT_NOT_AS_EXPECTED);
225    }
226 
227 
228   /**
229    * Test substract.
230    */
231   @Test
232   /* default */ void testSubstract2()
233    {
234     final Years years1 = Years.of(3);
235     final Years years2 = Years.of(6);
236     final Years yearsResult = years1.subtract(years2);
237     assertEquals(3, yearsResult.longValue(), YearsTests.RESULT_NOT_AS_EXPECTED);
238    }
239 
240 
241   /**
242    * Test multiply.
243    */
244   @Test
245   /* default */ void testMultiply1()
246    {
247     final Years years1 = Years.of(7);
248     final Years yearsResult = years1.multiply(3);
249     assertEquals(21, yearsResult.longValue(), YearsTests.RESULT_NOT_AS_EXPECTED);
250    }
251 
252 
253   /**
254    * Test multiply.
255    */
256   @Test
257   /* default */ void testMultiply2()
258    {
259     final Years years1 = Years.of(Long.MAX_VALUE / 2);
260     assertThrows(ArithmeticException.class, () ->
261      {
262       /* final Years yearsResult = */ years1.multiply(3);
263      }, YearsTests.ARITHMETIC_EXCEPTION_EXPECTED
264     );
265    }
266 
267 
268   /**
269    * Test divide.
270    */
271   @Test
272   /* default */ void testDivide1()
273    {
274     final Years years1 = Years.of(10);
275     final Years yearsResult = years1.divide(2);
276     assertEquals(5, yearsResult.longValue(), YearsTests.RESULT_NOT_AS_EXPECTED);
277    }
278 
279 
280   /**
281    * Test divide.
282    */
283   @Test
284   /* default */ void testDivide2()
285    {
286     final Years years1 = Years.of(10);
287     final Years yearsResult = years1.divide(3);
288     assertEquals(3, yearsResult.longValue(), YearsTests.RESULT_NOT_AS_EXPECTED);
289    }
290 
291 
292   /**
293    * Test divide.
294    */
295   @Test
296   /* default */ void testDivide3()
297    {
298     final Years years1 = Years.of(10);
299     assertThrows(ArithmeticException.class, () ->
300      {
301       /* final Years yearsResult = */ years1.divide(0);
302      }, YearsTests.ARITHMETIC_EXCEPTION_EXPECTED
303     );
304    }
305 
306 
307   /**
308    * Test divide.
309    */
310   @Test
311   /* default */ void testModulo1()
312    {
313     final Years years1 = Years.of(10);
314     final Years yearsResult = years1.modulo(2);
315     assertEquals(0, yearsResult.longValue(), YearsTests.RESULT_NOT_AS_EXPECTED);
316    }
317 
318 
319   /**
320    * Test divide.
321    */
322   @Test
323   /* default */ void testModulo2()
324    {
325     final Years years1 = Years.of(10);
326     final Years yearsResult = years1.modulo(3);
327     assertEquals(1, yearsResult.longValue(), YearsTests.RESULT_NOT_AS_EXPECTED);
328    }
329 
330 
331   /**
332    * Test divide.
333    */
334   @Test
335   /* default */ void testModulo3()
336    {
337     final Years years1 = Years.of(10);
338     assertThrows(ArithmeticException.class, () ->
339      {
340       /* final Years yearsResult = */ years1.modulo(0);
341      }, YearsTests.ARITHMETIC_EXCEPTION_EXPECTED
342     );
343    }
344 
345  }