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.Port;
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 PortTests
27 {
28
29
30
31 private static final String PORT_SHOULD_BE_49152 = "Port should be 49152!";
32
33
34
35
36 private static final String PORT_49152 = "49152";
37
38
39
40
41
42 PortTests()
43 {
44 super();
45 }
46
47
48
49
50
51 @Test
52 void testFactory1()
53 {
54 assertEquals(49152, Port.of(PORT_49152).intValue(), PortTests.PORT_SHOULD_BE_49152);
55 }
56
57
58
59
60
61 @Test
62 void testIsPort()
63 {
64 final Port port = Port.of(49152);
65 assertEquals(49152, port.intValue(), PortTests.PORT_SHOULD_BE_49152);
66 }
67
68
69
70
71
72
73
74 @ParameterizedTest
75 @ValueSource(ints = {65536, -1})
76 void testIsPortFalse(final int port)
77 {
78 assertThrows(IndexOutOfBoundsException.class, () ->
79 {
80 Port.of(port);
81 }, "Index out of bounds exception expected"
82 );
83 }
84
85
86
87
88
89 @Test
90 void testIntValue()
91 {
92 final Port port = Port.of(49152);
93 assertEquals(49152, port.intValue(), PortTests.PORT_SHOULD_BE_49152);
94 }
95
96
97
98
99
100 @Test
101 void testStringValue()
102 {
103 final Port port = Port.of(49152);
104 assertEquals(PORT_49152, port.stringValue(), PortTests.PORT_SHOULD_BE_49152);
105 }
106
107
108
109
110
111
112
113 @ParameterizedTest
114 @ValueSource(ints = {0, 1023})
115 void testIsSystem(final int port)
116 {
117 assertTrue(Port.of(port).isSystem(), "Should be a system port!");
118 }
119
120
121
122
123
124 @Test
125 void testIsSystemFalse()
126 {
127 assertFalse(Port.of(49152).isSystem(), "Should not be a system port!");
128 }
129
130
131
132
133
134
135
136 @ParameterizedTest
137 @ValueSource(ints = {1024, 49151})
138 void testIsRegistered(final int port)
139 {
140 assertTrue(Port.of(port).isRegistered(), "Should be a registered port!");
141 }
142
143
144
145
146
147
148
149 @ParameterizedTest
150 @ValueSource(ints = {1023, 49152})
151 void testIsRegisteredFalse(final int port)
152 {
153 assertFalse(Port.of(port).isRegistered(), "Should not be a registered port!");
154 }
155
156
157
158
159
160
161
162 @ParameterizedTest
163 @ValueSource(ints = {49152, 65535})
164 void testIsDynamic(final int port)
165 {
166 assertTrue(Port.of(port).isDynamic(), "Should be a dynamic port!");
167 }
168
169
170
171
172
173 @Test
174 void testIsDynamicFalse()
175 {
176 assertFalse(Port.of(1023).isDynamic(), "Should not be a dynamic port!");
177 }
178
179
180
181
182
183 @Test
184 void testHashCode()
185 {
186 final Port port1 = Port.of(1024);
187 final Port port2 = Port.of(1024);
188 final Port port3 = Port.of(1025);
189 assertAll("testHashCode",
190 () -> assertEquals(port1.hashCode(), port2.hashCode(), "hashCodes are not equal"),
191 () -> assertNotEquals(port1.hashCode(), port3.hashCode(), "hashCodes are equal")
192 );
193 }
194
195
196
197
198
199 @Test
200 @SuppressWarnings("java:S5785")
201 void testEquals()
202 {
203 final Port port1 = Port.of(1024);
204 final Port port2 = Port.of(1024);
205 final Port port3 = Port.of(1025);
206 final Port port4 = Port.of(1024);
207 assertAll("testEquals",
208 () -> assertTrue(port1.equals(port1), "port11 is not equal"),
209 () -> assertTrue(port1.equals(port2), "port12 are not equal"),
210 () -> assertTrue(port2.equals(port1), "port21 are not equal"),
211 () -> assertTrue(port2.equals(port4), "port24 are not equal"),
212 () -> assertTrue(port1.equals(port4), "port14 are not equal"),
213 () -> assertFalse(port1.equals(port3), "port13 are equal"),
214 () -> assertFalse(port3.equals(port1), "port31 are equal"),
215 () -> assertFalse(port1.equals(null), "port10 is equal")
216 );
217 }
218
219
220
221
222
223 @Test
224 void testToString()
225 {
226 final Port port = Port.of(49152);
227 assertEquals("Port[port=49152]", port.toString(), "toString not equal");
228 }
229
230
231
232
233
234 @Test
235 @SuppressWarnings("java:S5785")
236 void testCompareTo()
237 {
238 final Port port1 = Port.of(1024);
239 final Port port2 = Port.of(1024);
240 final Port port3 = Port.of(1025);
241 final Port port4 = Port.of(1026);
242 final Port port5 = Port.of(1024);
243 assertAll("testCompareTo",
244 () -> assertTrue(port1.compareTo(port2) == -port2.compareTo(port1), "reflexive1"),
245 () -> assertTrue(port1.compareTo(port3) == -port3.compareTo(port1), "reflexive2"),
246 () -> assertTrue((port4.compareTo(port3) > 0) && (port3.compareTo(port1) > 0) && (port4.compareTo(port1) > 0), "transitive1"),
247 () -> assertTrue((port1.compareTo(port2) == 0) && (Math.abs(port1.compareTo(port5)) == Math.abs(port2.compareTo(port5))), "sgn1"),
248 () -> assertTrue((port1.compareTo(port2) == 0) && port1.equals(port2), "equals")
249 );
250 }
251
252 }