1
2
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
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
31
32 private static final String RESULT_NOT_AS_EXPECTED = "Result not as expected";
33
34
35
36
37 private static final String ARITHMETIC_EXCEPTION_EXPECTED = "Arithmetic exception expected";
38
39
40
41
42 private static final String TEN = "10";
43
44
45
46
47 private static final String NOT_A_MONTH = "Not a month!";
48
49
50
51
52
53 MonthTests()
54 {
55 super();
56 }
57
58
59
60
61
62 @Test
63 void testFactory1()
64 {
65 assertEquals(10, Month.of(TEN).intValue(), NOT_A_MONTH);
66 }
67
68
69
70
71
72 @Test
73 void testIntValue()
74 {
75 assertEquals(10, Month.of(10).intValue(), NOT_A_MONTH);
76 }
77
78
79
80
81
82 @Test
83 void testStringValue()
84 {
85 assertEquals(TEN, Month.of(10).stringValue(), NOT_A_MONTH);
86 }
87
88
89
90
91
92
93
94 @ParameterizedTest
95 @ValueSource(ints = {1, 12})
96 void testIsMonth(final int month)
97 {
98 assertEquals(month, Month.of(month).intValue(), NOT_A_MONTH);
99 }
100
101
102
103
104
105
106
107 @ParameterizedTest
108 @ValueSource(ints = {0, 13})
109 void testIsNotAMonth(final int month)
110 {
111 assertThrows(IndexOutOfBoundsException.class, () ->
112 {
113 Month.of(month);
114 }, "Index out of bounds exception expected"
115 );
116 }
117
118
119
120
121
122 @Test
123 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",
129 () -> assertEquals(month1.hashCode(), month2.hashCode(), "hashCodes are not equal"),
130 () -> assertNotEquals(month1.hashCode(), month3.hashCode(), "hashCodes are equal")
131 );
132 }
133
134
135
136
137
138 @Test
139 @SuppressWarnings("java:S5785")
140 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",
147 () -> assertTrue(month1.equals(month1), "month11 is not equal"),
148 () -> assertTrue(month1.equals(month2), "month12 are not equal"),
149 () -> assertTrue(month2.equals(month1), "month21 are not equal"),
150 () -> assertTrue(month2.equals(month4), "month24 are not equal"),
151 () -> assertTrue(month1.equals(month4), "month14 are not equal"),
152 () -> assertFalse(month1.equals(month3), "month13 are equal"),
153 () -> assertFalse(month3.equals(month1), "month31 are equal"),
154 () -> assertFalse(month1.equals(null), "month10 is equal")
155 );
156 }
157
158
159
160
161
162 @Test
163 void testToString()
164 {
165 final Month month = Month.of(1);
166 assertEquals("Month[month=1]", month.toString(), "toString not equal");
167 }
168
169
170
171
172
173 @Test
174 @SuppressWarnings("java:S5785")
175 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",
183 () -> assertTrue(month1.compareTo(month2) == -month2.compareTo(month1), "reflexive1"),
184 () -> assertTrue(month1.compareTo(month3) == -month3.compareTo(month1), "reflexive2"),
185 () -> assertTrue((month4.compareTo(month3) > 0) && (month3.compareTo(month1) > 0) && (month4.compareTo(month1) > 0), "transitive1"),
186 () -> assertTrue((month1.compareTo(month2) == 0) && (Math.abs(month1.compareTo(month5)) == Math.abs(month2.compareTo(month5))), "sgn1"),
187 () -> assertTrue((month1.compareTo(month2) == 0) && month1.equals(month2), "equals")
188 );
189 }
190
191
192
193
194
195 @Test
196 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
207
208 @Test
209 void testAdd2()
210 {
211 final Month month = Month.of(12);
212 final Months months = Months.of(1);
213 assertThrows(ArithmeticException.class, () ->
214 {
215 month.add(months);
216 }, MonthTests.ARITHMETIC_EXCEPTION_EXPECTED
217 );
218 }
219
220
221
222
223
224 @Test
225 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
236
237 @Test
238 void testSubtract2()
239 {
240 final Month month = Month.of(1);
241 final Months months = Months.of(1);
242 assertThrows(ArithmeticException.class, () ->
243 {
244 month.subtract(months);
245 }, MonthTests.ARITHMETIC_EXCEPTION_EXPECTED
246 );
247 }
248
249
250
251
252
253 @Test
254 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
264
265 @Test
266 void testIncrement2()
267 {
268 final Month month = Month.of(12);
269 assertThrows(ArithmeticException.class, () ->
270 {
271 month.increment();
272 }, MonthTests.ARITHMETIC_EXCEPTION_EXPECTED
273 );
274 }
275
276
277
278
279
280 @Test
281 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
291
292 @Test
293 void testDecrement2()
294 {
295 final Month month = Month.of(1);
296 assertThrows(ArithmeticException.class, () ->
297 {
298 month.decrement();
299 }, MonthTests.ARITHMETIC_EXCEPTION_EXPECTED
300 );
301 }
302
303 }