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.Month;
19  import de.powerstat.validation.values.Months;
20  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
21  
22  
23  /**
24   * Month 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 MonthTests
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 constant.
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 month constant.
46     */
47    private static final String NOT_A_MONTH = "Not a month!"; //$NON-NLS-1$
48  
49  
50    /**
51     * Default constructor.
52     */
53    /* default */ MonthTests()
54     {
55      super();
56     }
57  
58  
59    /**
60     * Factory string test.
61     */
62    @Test
63    /* default */ void testFactory1()
64     {
65      assertEquals(10, Month.of(TEN).intValue(), NOT_A_MONTH);
66     }
67  
68  
69    /**
70     * intValue.
71     */
72    @Test
73    /* default */ void testIntValue()
74     {
75      assertEquals(10, Month.of(10).intValue(), NOT_A_MONTH);
76     }
77  
78  
79    /**
80     * stringValue.
81     */
82    @Test
83    /* default */ void testStringValue()
84     {
85      assertEquals(TEN, Month.of(10).stringValue(), NOT_A_MONTH);
86     }
87  
88  
89    /**
90     * Is month.
91     *
92     * @param month Month
93     */
94    @ParameterizedTest
95    @ValueSource(ints = {1, 12})
96    /* default */ void testIsMonth(final int month)
97     {
98      assertEquals(month, Month.of(month).intValue(), NOT_A_MONTH);
99     }
100 
101 
102   /**
103    * Is not a month.
104    *
105    * @param month Month
106    */
107   @ParameterizedTest
108   @ValueSource(ints = {0, 13})
109   /* default */ void testIsNotAMonth(final int month)
110    {
111     assertThrows(IndexOutOfBoundsException.class, () ->
112      {
113       /* final Month month = */ Month.of(month);
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 Month month1 = Month.of(1);
126     final Month month2 = Month.of(1);
127     final Month month3 = Month.of(2);
128     assertAll("testHashCode", //$NON-NLS-1$
129       () -> assertEquals(month1.hashCode(), month2.hashCode(), "hashCodes are not equal"), //$NON-NLS-1$
130       () -> assertNotEquals(month1.hashCode(), month3.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 Month month1 = Month.of(1);
143     final Month month2 = Month.of(1);
144     final Month month3 = Month.of(2);
145     final Month month4 = Month.of(1);
146     assertAll("testEquals", //$NON-NLS-1$
147       () -> assertTrue(month1.equals(month1), "month11 is not equal"), //$NON-NLS-1$
148       () -> assertTrue(month1.equals(month2), "month12 are not equal"), //$NON-NLS-1$
149       () -> assertTrue(month2.equals(month1), "month21 are not equal"), //$NON-NLS-1$
150       () -> assertTrue(month2.equals(month4), "month24 are not equal"), //$NON-NLS-1$
151       () -> assertTrue(month1.equals(month4), "month14 are not equal"), //$NON-NLS-1$
152       () -> assertFalse(month1.equals(month3), "month13 are equal"), //$NON-NLS-1$
153       () -> assertFalse(month3.equals(month1), "month31 are equal"), //$NON-NLS-1$
154       () -> assertFalse(month1.equals(null), "month10 is equal") //$NON-NLS-1$
155     );
156    }
157 
158 
159   /**
160    * Test toString.
161    */
162   @Test
163   /* default */ void testToString()
164    {
165     final Month month = Month.of(1);
166     assertEquals("Month[month=1]", month.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 Month month1 = Month.of(1);
178     final Month month2 = Month.of(1);
179     final Month month3 = Month.of(2);
180     final Month month4 = Month.of(3);
181     final Month month5 = Month.of(1);
182     assertAll("testCompareTo", //$NON-NLS-1$
183       () -> assertTrue(month1.compareTo(month2) == -month2.compareTo(month1), "reflexive1"), //$NON-NLS-1$
184       () -> assertTrue(month1.compareTo(month3) == -month3.compareTo(month1), "reflexive2"), //$NON-NLS-1$
185       () -> assertTrue((month4.compareTo(month3) > 0) && (month3.compareTo(month1) > 0) && (month4.compareTo(month1) > 0), "transitive1"), //$NON-NLS-1$
186       () -> assertTrue((month1.compareTo(month2) == 0) && (Math.abs(month1.compareTo(month5)) == Math.abs(month2.compareTo(month5))), "sgn1"), //$NON-NLS-1$
187       () -> assertTrue((month1.compareTo(month2) == 0) && month1.equals(month2), "equals") //$NON-NLS-1$
188     );
189    }
190 
191 
192   /**
193    * Test add.
194    */
195   @Test
196   /* default */ void testAdd1()
197    {
198     final Month month = Month.of(1);
199     final Months months = Months.of(1);
200     final Month monthResult = month.add(months);
201     assertEquals(2, monthResult.intValue(), MonthTests.RESULT_NOT_AS_EXPECTED);
202    }
203 
204 
205   /**
206    * Test add.
207    */
208   @Test
209   /* default */ void testAdd2()
210    {
211     final Month month = Month.of(12);
212     final Months months = Months.of(1);
213     assertThrows(ArithmeticException.class, () ->
214      {
215       /* final Month monthResult = */ month.add(months);
216      }, MonthTests.ARITHMETIC_EXCEPTION_EXPECTED
217     );
218    }
219 
220 
221   /**
222    * Test subtract.
223    */
224   @Test
225   /* default */ void testSubtract1()
226    {
227     final Month month = Month.of(2);
228     final Months months = Months.of(1);
229     final Month monthResult = month.subtract(months);
230     assertEquals(1, monthResult.intValue(), MonthTests.RESULT_NOT_AS_EXPECTED);
231    }
232 
233 
234   /**
235    * Test subtract.
236    */
237   @Test
238   /* default */ void testSubtract2()
239    {
240     final Month month = Month.of(1);
241     final Months months = Months.of(1);
242     assertThrows(ArithmeticException.class, () ->
243      {
244       /* final Month monthResult = */ month.subtract(months);
245      }, MonthTests.ARITHMETIC_EXCEPTION_EXPECTED
246     );
247    }
248 
249 
250   /**
251    * Test increment.
252    */
253   @Test
254   /* default */ void testIncrement1()
255    {
256     final Month month = Month.of(1);
257     final Month monthResult = month.increment();
258     assertEquals(2, monthResult.intValue(), MonthTests.RESULT_NOT_AS_EXPECTED);
259    }
260 
261 
262   /**
263    * Test increment.
264    */
265   @Test
266   /* default */ void testIncrement2()
267    {
268     final Month month = Month.of(12);
269     assertThrows(ArithmeticException.class, () ->
270      {
271       /* final Month monthResult = */ month.increment();
272      }, MonthTests.ARITHMETIC_EXCEPTION_EXPECTED
273     );
274    }
275 
276 
277   /**
278    * Test decrement.
279    */
280   @Test
281   /* default */ void testDecrement1()
282    {
283     final Month month = Month.of(2);
284     final Month monthResult = month.decrement();
285     assertEquals(1, monthResult.intValue(), MonthTests.RESULT_NOT_AS_EXPECTED);
286    }
287 
288 
289   /**
290    * Test decrement.
291    */
292   @Test
293   /* default */ void testDecrement2()
294    {
295     final Month month = Month.of(1);
296     assertThrows(ArithmeticException.class, () ->
297      {
298       /* final Month monthResult = */ month.decrement();
299      }, MonthTests.ARITHMETIC_EXCEPTION_EXPECTED
300     );
301    }
302 
303  }