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.Years;
19 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
20
21
22
23
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
30
31 private static final String NOT_A_YEARS = "Not a years!";
32
33
34
35
36 private static final String RESULT_NOT_AS_EXPECTED = "Result not as expected";
37
38
39
40
41 private static final String ARITHMETIC_EXCEPTION_EXPECTED = "Arithmetic exception expected";
42
43
44
45
46
47 YearsTests()
48 {
49 super();
50 }
51
52
53
54
55
56 @Test
57 void testFactory1()
58 {
59 assertEquals(0, Years.of("0").longValue(), YearsTests.NOT_A_YEARS);
60 }
61
62
63
64
65
66
67
68 @ParameterizedTest
69 @ValueSource(longs = {0, 20})
70 void testIsYears(final long years)
71 {
72 assertEquals(years, Years.of(years).longValue(), YearsTests.NOT_A_YEARS);
73 }
74
75
76
77
78
79
80
81 @ParameterizedTest
82 @ValueSource(longs = {-1})
83 void testIsNotAYears(final long years)
84 {
85 assertThrows(IndexOutOfBoundsException.class, () ->
86 {
87 Years.of(years);
88 }, "Index out of bounds exception expected"
89 );
90 }
91
92
93
94
95
96 @Test
97 void testLongValue()
98 {
99 assertEquals(10, Years.of(10).longValue(), YearsTests.NOT_A_YEARS);
100 }
101
102
103
104
105
106 @Test
107 void testStringValue()
108 {
109 assertEquals("10", Years.of(10).stringValue(), YearsTests.NOT_A_YEARS);
110 }
111
112
113
114
115
116 @Test
117 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",
123 () -> assertEquals(years1.hashCode(), years2.hashCode(), "hashCodes are not equal"),
124 () -> assertNotEquals(years1.hashCode(), years3.hashCode(), "hashCodes are equal")
125 );
126 }
127
128
129
130
131
132 @Test
133 @SuppressWarnings("java:S5785")
134 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",
141 () -> assertTrue(years1.equals(years1), "years11 is not equal"),
142 () -> assertTrue(years1.equals(years2), "years12 are not equal"),
143 () -> assertTrue(years2.equals(years1), "years21 are not equal"),
144 () -> assertTrue(years2.equals(years4), "years24 are not equal"),
145 () -> assertTrue(years1.equals(years4), "years14 are not equal"),
146 () -> assertFalse(years1.equals(years3), "years13 are equal"),
147 () -> assertFalse(years3.equals(years1), "years31 are equal"),
148 () -> assertFalse(years1.equals(null), "years10 is equal")
149 );
150 }
151
152
153
154
155
156 @Test
157 void testToString()
158 {
159 final Years years = Years.of(1);
160 assertEquals("Years[years=1]", years.toString(), "toString not equal");
161 }
162
163
164
165
166
167 @Test
168 @SuppressWarnings("java:S5785")
169 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",
177 () -> assertTrue(years1.compareTo(years2) == -years2.compareTo(years1), "reflexive1"),
178 () -> assertTrue(years1.compareTo(years3) == -years3.compareTo(years1), "reflexive2"),
179 () -> assertTrue((years4.compareTo(years3) > 0) && (years3.compareTo(years1) > 0) && (years4.compareTo(years1) > 0), "transitive1"),
180 () -> assertTrue((years1.compareTo(years2) == 0) && (Math.abs(years1.compareTo(years5)) == Math.abs(years2.compareTo(years5))), "sgn1"),
181 () -> assertTrue((years1.compareTo(years2) == 0) && years1.equals(years2), "equals")
182 );
183 }
184
185
186
187
188
189 @Test
190 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
201
202 @Test
203 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 years1.add(years2);
210 }, YearsTests.ARITHMETIC_EXCEPTION_EXPECTED
211 );
212 }
213
214
215
216
217
218 @Test
219 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
230
231 @Test
232 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
243
244 @Test
245 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
255
256 @Test
257 void testMultiply2()
258 {
259 final Years years1 = Years.of(Long.MAX_VALUE / 2);
260 assertThrows(ArithmeticException.class, () ->
261 {
262 years1.multiply(3);
263 }, YearsTests.ARITHMETIC_EXCEPTION_EXPECTED
264 );
265 }
266
267
268
269
270
271 @Test
272 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
282
283 @Test
284 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
294
295 @Test
296 void testDivide3()
297 {
298 final Years years1 = Years.of(10);
299 assertThrows(ArithmeticException.class, () ->
300 {
301 years1.divide(0);
302 }, YearsTests.ARITHMETIC_EXCEPTION_EXPECTED
303 );
304 }
305
306
307
308
309
310 @Test
311 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
321
322 @Test
323 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
333
334 @Test
335 void testModulo3()
336 {
337 final Years years1 = Years.of(10);
338 assertThrows(ArithmeticException.class, () ->
339 {
340 years1.modulo(0);
341 }, YearsTests.ARITHMETIC_EXCEPTION_EXPECTED
342 );
343 }
344
345 }