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.Hour;
19 import de.powerstat.validation.values.Hours;
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 HourTests
28 {
29
30
31
32 private static final String NOT_A_HOUR = "Not a hour!";
33
34
35
36
37 private static final String RESULT_NOT_AS_EXPECTED = "Result not as expected";
38
39
40
41
42 private static final String ARITHMETIC_EXCEPTION_EXPECTED = "Arithmetic exception expected";
43
44
45
46
47
48 HourTests()
49 {
50 super();
51 }
52
53
54
55
56
57 @Test
58 void testFactory1()
59 {
60 assertEquals(0, Hour.of("0").intValue(), HourTests.NOT_A_HOUR);
61 }
62
63
64
65
66
67
68
69 @ParameterizedTest
70 @ValueSource(ints = {0, 23})
71 void testIsHour(final int hour)
72 {
73 assertEquals(hour, Hour.of(hour).intValue(), HourTests.NOT_A_HOUR);
74 }
75
76
77
78
79
80
81
82 @ParameterizedTest
83 @ValueSource(ints = {-1, 24})
84 void testIsNotAHour(final int hour)
85 {
86 assertThrows(IndexOutOfBoundsException.class, () ->
87 {
88 Hour.of(hour);
89 }, "Index out of bounds exception expected"
90 );
91 }
92
93
94
95
96
97 @Test
98 void testIntValue()
99 {
100 assertEquals(10, Hour.of(10).intValue(), HourTests.NOT_A_HOUR);
101 }
102
103
104
105
106
107 @Test
108 void testStringValue()
109 {
110 assertEquals("10", Hour.of(10).stringValue(), HourTests.NOT_A_HOUR);
111 }
112
113
114
115
116
117 @Test
118 void testHashCode()
119 {
120 final Hour hour1 = Hour.of(1);
121 final Hour hour2 = Hour.of(1);
122 final Hour hour3 = Hour.of(2);
123 assertAll("testHashCode",
124 () -> assertEquals(hour1.hashCode(), hour2.hashCode(), "hashCodes are not equal"),
125 () -> assertNotEquals(hour1.hashCode(), hour3.hashCode(), "hashCodes are equal")
126 );
127 }
128
129
130
131
132
133 @Test
134 @SuppressWarnings("java:S5785")
135 void testEquals()
136 {
137 final Hour hour1 = Hour.of(1);
138 final Hour hour2 = Hour.of(1);
139 final Hour hour3 = Hour.of(2);
140 final Hour hour4 = Hour.of(1);
141 assertAll("testEquals",
142 () -> assertTrue(hour1.equals(hour1), "hour11 is not equal"),
143 () -> assertTrue(hour1.equals(hour2), "hour12 are not equal"),
144 () -> assertTrue(hour2.equals(hour1), "hour21 are not equal"),
145 () -> assertTrue(hour2.equals(hour4), "hour24 are not equal"),
146 () -> assertTrue(hour1.equals(hour4), "hour14 are not equal"),
147 () -> assertFalse(hour1.equals(hour3), "hour13 are equal"),
148 () -> assertFalse(hour3.equals(hour1), "hour31 are equal"),
149 () -> assertFalse(hour1.equals(null), "hour10 is equal")
150 );
151 }
152
153
154
155
156
157 @Test
158 void testToString()
159 {
160 final Hour hour = Hour.of(1);
161 assertEquals("Hour[hour=1]", hour.toString(), "toString not equal");
162 }
163
164
165
166
167
168 @Test
169 @SuppressWarnings("java:S5785")
170 void testCompareTo()
171 {
172 final Hour hour1 = Hour.of(1);
173 final Hour hour2 = Hour.of(1);
174 final Hour hour3 = Hour.of(2);
175 final Hour hour4 = Hour.of(3);
176 final Hour hour5 = Hour.of(1);
177 assertAll("testCompareTo",
178 () -> assertTrue(hour1.compareTo(hour2) == -hour2.compareTo(hour1), "reflexive1"),
179 () -> assertTrue(hour1.compareTo(hour3) == -hour3.compareTo(hour1), "reflexive2"),
180 () -> assertTrue((hour4.compareTo(hour3) > 0) && (hour3.compareTo(hour1) > 0) && (hour4.compareTo(hour1) > 0), "transitive1"),
181 () -> assertTrue((hour1.compareTo(hour2) == 0) && (Math.abs(hour1.compareTo(hour5)) == Math.abs(hour2.compareTo(hour5))), "sgn1"),
182 () -> assertTrue((hour1.compareTo(hour2) == 0) && hour1.equals(hour2), "equals")
183 );
184 }
185
186
187
188
189
190 @Test
191 void testAdd1()
192 {
193 final Hour hour = Hour.of(0);
194 final Hours hours = Hours.of(1);
195 final Hour hourResult = hour.add(hours);
196 assertEquals(1, hourResult.intValue(), HourTests.RESULT_NOT_AS_EXPECTED);
197 }
198
199
200
201
202
203 @Test
204 void testAdd2()
205 {
206 final Hour hour = Hour.of(23);
207 final Hours hours = Hours.of(1);
208 assertThrows(ArithmeticException.class, () ->
209 {
210 hour.add(hours);
211 }, HourTests.ARITHMETIC_EXCEPTION_EXPECTED
212 );
213 }
214
215
216
217
218
219 @Test
220 void testSubtract1()
221 {
222 final Hour hour = Hour.of(1);
223 final Hours hours = Hours.of(1);
224 final Hour hourResult = hour.subtract(hours);
225 assertEquals(0, hourResult.intValue(), HourTests.RESULT_NOT_AS_EXPECTED);
226 }
227
228
229
230
231
232 @Test
233 void testSubtract2()
234 {
235 final Hour hour = Hour.of(0);
236 final Hours hours = Hours.of(1);
237 assertThrows(ArithmeticException.class, () ->
238 {
239 hour.subtract(hours);
240 }, HourTests.ARITHMETIC_EXCEPTION_EXPECTED
241 );
242 }
243
244
245
246
247
248 @Test
249 void testIncrement1()
250 {
251 final Hour hour = Hour.of(0);
252 final Hour hourResult = hour.increment();
253 assertEquals(1, hourResult.intValue(), HourTests.RESULT_NOT_AS_EXPECTED);
254 }
255
256
257
258
259
260 @Test
261 void testIncrement2()
262 {
263 final Hour hour = Hour.of(23);
264 assertThrows(ArithmeticException.class, () ->
265 {
266 hour.increment();
267 }, HourTests.ARITHMETIC_EXCEPTION_EXPECTED
268 );
269 }
270
271
272
273
274
275 @Test
276 void testDecrement1()
277 {
278 final Hour hour = Hour.of(1);
279 final Hour hourResult = hour.decrement();
280 assertEquals(0, hourResult.intValue(), HourTests.RESULT_NOT_AS_EXPECTED);
281 }
282
283
284
285
286
287 @Test
288 void testDecrement2()
289 {
290 final Hour hour = Hour.of(0);
291 assertThrows(ArithmeticException.class, () ->
292 {
293 hour.decrement();
294 }, HourTests.ARITHMETIC_EXCEPTION_EXPECTED
295 );
296 }
297
298 }