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
16 import de.powerstat.validation.values.Day;
17 import de.powerstat.validation.values.Month;
18 import de.powerstat.validation.values.MonthDay;
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 MonthDayTests
28 {
29
30
31
32 private static final String MONTH_NOT_AS_EXPECTED = "Month not as expected";
33
34
35
36
37 private static final String DAY_NOT_AS_EXPECTED = "Day not as expected";
38
39
40
41
42 private static final String DAYMONTH_10_13 = "10-13";
43
44
45
46
47 private static final String RESULT_NOT_AS_EXPECTED = "result not as expected";
48
49
50
51
52 private static final String TEST_OF = "testOf";
53
54
55
56
57
58 MonthDayTests()
59 {
60 super();
61 }
62
63
64
65
66
67 @Test
68 void testOf1()
69 {
70 final MonthDay test = MonthDay.of(Month.of(10), Day.of(13));
71 assertAll(TEST_OF,
72 () -> assertEquals(10, test.monthValue().intValue(), MONTH_NOT_AS_EXPECTED),
73 () -> assertEquals(13, test.dayValue().intValue(), DAY_NOT_AS_EXPECTED)
74 );
75 }
76
77
78
79
80
81 @Test
82 void testOf2()
83 {
84 final MonthDay test = MonthDay.of(DAYMONTH_10_13);
85 assertAll(TEST_OF,
86 () -> assertEquals(10, test.monthValue().intValue(), MONTH_NOT_AS_EXPECTED),
87 () -> assertEquals(13, test.dayValue().intValue(), DAY_NOT_AS_EXPECTED)
88 );
89 }
90
91
92
93
94
95 @Test
96 void testOf3()
97 {
98 assertThrows(IllegalArgumentException.class, () ->
99 {
100 MonthDay.of("10");
101 }, "Illegal argument exception"
102 );
103 }
104
105
106
107
108
109 @Test
110 void testOf4()
111 {
112 final MonthDay test = MonthDay.of(Month.of(2), Day.of(1));
113 assertAll(TEST_OF,
114 () -> assertEquals(2, test.monthValue().intValue(), MONTH_NOT_AS_EXPECTED),
115 () -> assertEquals(1, test.dayValue().intValue(), DAY_NOT_AS_EXPECTED)
116 );
117 }
118
119
120
121
122
123 @Test
124 void testOf5()
125 {
126 final Month month = Month.of(2);
127 final Day day = Day.of(30);
128 assertThrows(IndexOutOfBoundsException.class, () ->
129 {
130 MonthDay.of(month, day);
131 }, "Index out of bounds exception"
132 );
133 }
134
135
136
137
138
139 @Test
140 void testStringValue()
141 {
142 final MonthDay test = MonthDay.of(DAYMONTH_10_13);
143 assertEquals(DAYMONTH_10_13, test.stringValue(), MONTH_NOT_AS_EXPECTED);
144 }
145
146
147
148
149
150 @Test
151 void testHashCode()
152 {
153 final MonthDay test1 = MonthDay.of(Month.of(10), Day.of(13));
154 final MonthDay test2 = MonthDay.of(Month.of(10), Day.of(13));
155 final MonthDay test3 = MonthDay.of(Month.of(10), Day.of(14));
156 assertAll("testHashCode",
157 () -> assertEquals(test1.hashCode(), test2.hashCode(), "hashCodes are not equal"),
158 () -> assertNotEquals(test1.hashCode(), test3.hashCode(), "hashCodes are equal")
159 );
160 }
161
162
163
164
165
166 @Test
167 @SuppressWarnings("java:S5785")
168 void testEquals()
169 {
170 final MonthDay test1 = MonthDay.of(Month.of(10), Day.of(13));
171 final MonthDay test2 = MonthDay.of(Month.of(10), Day.of(13));
172 final MonthDay test3 = MonthDay.of(Month.of(10), Day.of(14));
173 final MonthDay test4 = MonthDay.of(Month.of(10), Day.of(13));
174 assertAll("testEquals",
175 () -> assertTrue(test1.equals(test1), "test11 is not equal"),
176 () -> assertTrue(test1.equals(test2), "test12 are not equal"),
177 () -> assertTrue(test2.equals(test1), "test21 are not equal"),
178 () -> assertTrue(test2.equals(test4), "test24 are not equal"),
179 () -> assertTrue(test1.equals(test4), "test14 are not equal"),
180 () -> assertFalse(test1.equals(test3), "test13 are equal"),
181 () -> assertFalse(test3.equals(test1), "test31 are equal"),
182 () -> assertFalse(test1.equals(null), "test10 is equal")
183 );
184 }
185
186
187
188
189
190 @Test
191 @SuppressWarnings("java:S5785")
192 void testEquals2()
193 {
194 final MonthDay test1 = MonthDay.of(Month.of(10), Day.of(13));
195 final MonthDay test2 = MonthDay.of(Month.of(9), Day.of(13));
196 assertAll("testEquals",
197 () -> assertFalse(test1.equals(test2), "test12 is not equal")
198 );
199 }
200
201
202
203
204
205 @Test
206 void testToString()
207 {
208 final MonthDay test = MonthDay.of(DAYMONTH_10_13);
209 assertEquals("MonthDay[month=Month[month=10], day=Day[day=13]]", test.toString(), "toString not as expected");
210 }
211
212
213
214
215
216 @Test
217 @SuppressWarnings("java:S5785")
218 void testCompareTo()
219 {
220 final MonthDay test1 = MonthDay.of(Month.of(10), Day.of(13));
221 final MonthDay test2 = MonthDay.of(Month.of(10), Day.of(13));
222 final MonthDay test3 = MonthDay.of(Month.of(10), Day.of(14));
223 final MonthDay test4 = MonthDay.of(Month.of(11), Day.of(13));
224 final MonthDay test5 = MonthDay.of(Month.of(10), Day.of(13));
225 assertAll("testCompareTo",
226 () -> assertTrue(test1.compareTo(test2) == -test2.compareTo(test1), "reflexive1"),
227 () -> assertTrue(test1.compareTo(test3) == -test3.compareTo(test1), "reflexive2"),
228 () -> assertTrue((test4.compareTo(test3) > 0) && (test3.compareTo(test1) > 0) && (test4.compareTo(test1) > 0), "transitive1"),
229 () -> assertTrue((test1.compareTo(test2) == 0) && (Math.abs(test1.compareTo(test5)) == Math.abs(test2.compareTo(test5))), "sgn1"),
230 () -> assertTrue((test1.compareTo(test2) == 0) && test1.equals(test2), "equals")
231 );
232 }
233
234
235
236
237
238 @Test
239 void testAdd1()
240 {
241 final MonthDay test1 = MonthDay.of(Month.of(10), Day.of(13));
242 final MonthDay result = test1.add(Months.of(1));
243 assertAll("add",
244 () -> assertEquals(11, result.monthValue().intValue(), RESULT_NOT_AS_EXPECTED),
245 () -> assertEquals(13, result.dayValue().intValue(), RESULT_NOT_AS_EXPECTED)
246 );
247 }
248
249
250
251
252
253 @Test
254 void testAdd2()
255 {
256 final MonthDay test1 = MonthDay.of(Month.of(10), Day.of(13));
257 final Months months = Months.of(3);
258 assertThrows(ArithmeticException.class, () ->
259 {
260 test1.add(months);
261 }, "Arithmetic exception"
262 );
263 }
264
265
266
267
268
269 @Test
270 void testSubtract1()
271 {
272 final MonthDay test1 = MonthDay.of(Month.of(10), Day.of(13));
273 final MonthDay result = test1.subtract(Months.of(1));
274 assertAll("subtract",
275 () -> assertEquals(9, result.monthValue().intValue(), RESULT_NOT_AS_EXPECTED),
276 () -> assertEquals(13, result.dayValue().intValue(), RESULT_NOT_AS_EXPECTED)
277 );
278 }
279
280
281
282
283
284 @Test
285 void testSubtract2()
286 {
287 final MonthDay test1 = MonthDay.of(Month.of(2), Day.of(13));
288 final Months months = Months.of(2);
289 assertThrows(ArithmeticException.class, () ->
290 {
291 test1.subtract(months);
292 }, "Arithmetic exception"
293 );
294 }
295
296
297
298
299
300 @Test
301 void testIncrement1()
302 {
303 final MonthDay test1 = MonthDay.of(Month.of(10), Day.of(13));
304 final MonthDay result = test1.incrementMonth();
305 assertAll("add",
306 () -> assertEquals(11, result.monthValue().intValue(), RESULT_NOT_AS_EXPECTED),
307 () -> assertEquals(13, result.dayValue().intValue(), RESULT_NOT_AS_EXPECTED)
308 );
309 }
310
311
312
313
314
315 @Test
316 void testIncrement2()
317 {
318 final MonthDay test1 = MonthDay.of(Month.of(12), Day.of(13));
319 assertThrows(ArithmeticException.class, () ->
320 {
321 test1.incrementMonth();
322 }, "Arithmetic exception"
323 );
324 }
325
326
327
328
329
330 @Test
331 void testDecrement1()
332 {
333 final MonthDay test1 = MonthDay.of(Month.of(10), Day.of(13));
334 final MonthDay result = test1.decrementMonth();
335 assertAll("add",
336 () -> assertEquals(9, result.monthValue().intValue(), RESULT_NOT_AS_EXPECTED),
337 () -> assertEquals(13, result.dayValue().intValue(), RESULT_NOT_AS_EXPECTED)
338 );
339 }
340
341
342
343
344
345 @Test
346 void testDecrement2()
347 {
348 final MonthDay test1 = MonthDay.of(Month.of(1), Day.of(13));
349 assertThrows(ArithmeticException.class, () ->
350 {
351 test1.decrementMonth();
352 }, "Arithmetic exception"
353 );
354 }
355
356 }