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.Hostname;
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"})
26 final class HostnameTests
27 {
28
29
30
31 private static final String PRIVATE_IP_192_168_1_1 = "192.168.1.1";
32
33
34
35
36 private static final String PRIVATE_IP_192_168_1_2 = "192.168.1.2";
37
38
39
40
41 private static final String FD00 = "fd00:0000:0000:0000:0000:0000:0000:0000";
42
43
44
45
46 private static final String WWW_POWERSTAT_DE = "www.powerstat.de";
47
48
49
50
51 private static final String NONEXISTANT_EXAMPLE_COM = "nonexistant.example.com";
52
53
54
55
56 private static final String HOSTNAME_NOT_AS_EXPECTED = "Hostname not as expected";
57
58
59
60
61 private static final String ILLEGAL_ARGUMENT = "Illegal argument exception expected";
62
63
64
65
66
67 HostnameTests()
68 {
69 super();
70 }
71
72
73
74
75
76
77
78 @ParameterizedTest
79 @ValueSource(strings = {HostnameTests.WWW_POWERSTAT_DE, "a.de", "www.powerstat012345678901234567890123456789012345678901234567890123.de", "abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdefghijklmnopqrstuvwxyz.abcdef.com", "192.168.0.1", "00fe:0080:0000:0000:0000:0000:0000:0000"})
80 void testHostnameOk0(final String hostname)
81 {
82 final Hostname cleanHostname = Hostname.of(hostname);
83 assertEquals(hostname, cleanHostname.stringValue(), HostnameTests.HOSTNAME_NOT_AS_EXPECTED);
84 }
85
86
87
88
89
90 @Test
91 void testHostnameOk1()
92 {
93 final Hostname cleanHostname = Hostname.of("::");
94 assertEquals("0000:0000:0000:0000:0000:0000:0000:0000", cleanHostname.stringValue(), HostnameTests.HOSTNAME_NOT_AS_EXPECTED);
95 }
96
97
98
99
100
101
102
103 @ParameterizedTest
104 @ValueSource(strings = {"p", "www..de", "www.powerstat0123456789012345678901234567890123456789012345678901234.de", "www.powerstat1234123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234.de"})
105 void testHostnameLength(final String hostname)
106 {
107 assertThrows(IllegalArgumentException.class, () ->
108 {
109 Hostname.of(hostname);
110 }, HostnameTests.ILLEGAL_ARGUMENT
111 );
112 }
113
114
115
116
117
118
119
120 @ParameterizedTest
121 @ValueSource(strings = {"www.power~stat.de", "www.powerstat.unknown", "ACCOUNTANT", "www.-powerstat.de", "www.powerstat-.de"})
122 void testHostnameIllegalParameters(final String hostname)
123 {
124 assertThrows(IllegalArgumentException.class, () ->
125 {
126 Hostname.of(hostname);
127 }, HostnameTests.ILLEGAL_ARGUMENT
128 );
129 }
130
131
132
133
134
135 @Test
136 void testStringValue()
137 {
138 final Hostname hostname = Hostname.of(HostnameTests.PRIVATE_IP_192_168_1_1);
139 assertEquals(HostnameTests.PRIVATE_IP_192_168_1_1, hostname.stringValue(), HostnameTests.HOSTNAME_NOT_AS_EXPECTED);
140 }
141
142
143
144
145
146 @Test
147 void testGetReverseHostname1()
148 {
149 final Hostname hostname = Hostname.of(HostnameTests.PRIVATE_IP_192_168_1_1);
150 assertEquals(HostnameTests.PRIVATE_IP_192_168_1_1, hostname.getReverseHostname(), HostnameTests.HOSTNAME_NOT_AS_EXPECTED);
151 }
152
153
154
155
156
157 @Test
158 void testGetReverseHostname2()
159 {
160 final Hostname hostname = Hostname.of(HostnameTests.FD00);
161 assertEquals(HostnameTests.FD00, hostname.getReverseHostname(), HostnameTests.HOSTNAME_NOT_AS_EXPECTED);
162 }
163
164
165
166
167
168 @Test
169 void testGetReverseHostname3()
170 {
171 final Hostname hostname = Hostname.of("www.example.com");
172 assertEquals("com.example.www", hostname.getReverseHostname(), HostnameTests.HOSTNAME_NOT_AS_EXPECTED);
173 }
174
175
176
177
178
179 @Test
180 void testExist()
181 {
182 assertTrue(Hostname.of(HostnameTests.WWW_POWERSTAT_DE).exist(), "Should be an existing hostname");
183 }
184
185
186
187
188
189 @Test
190 void testExistFalse()
191 {
192 assertFalse(Hostname.of(HostnameTests.NONEXISTANT_EXAMPLE_COM).exist(), "Should not be an existing hostname");
193 }
194
195
196
197
198
199 @Test
200 void testIsReachable()
201 {
202 assertTrue(Hostname.of(HostnameTests.WWW_POWERSTAT_DE).isReachable(1000), "Should be a reachable hostname");
203 }
204
205
206
207
208
209 @Test
210 void testIsReachableFalse()
211 {
212 assertFalse(Hostname.of(HostnameTests.NONEXISTANT_EXAMPLE_COM).isReachable(1000), "Should not be a reachable hostname");
213 }
214
215
216
217
218
219 @Test
220 void testHashCode()
221 {
222 final Hostname hostname1 = Hostname.of(HostnameTests.PRIVATE_IP_192_168_1_1);
223 final Hostname hostname2 = Hostname.of(HostnameTests.PRIVATE_IP_192_168_1_1);
224 final Hostname hostname3 = Hostname.of(HostnameTests.PRIVATE_IP_192_168_1_2);
225 assertAll("testHashCode",
226 () -> assertEquals(hostname1.hashCode(), hostname2.hashCode(), "hashCodes are not equal"),
227 () -> assertNotEquals(hostname1.hashCode(), hostname3.hashCode(), "hashCodes are equal")
228 );
229 }
230
231
232
233
234
235 @Test
236 @SuppressWarnings("java:S5785")
237 void testEquals()
238 {
239 final Hostname hostname1 = Hostname.of(HostnameTests.PRIVATE_IP_192_168_1_1);
240 final Hostname hostname2 = Hostname.of(HostnameTests.PRIVATE_IP_192_168_1_1);
241 final Hostname hostname3 = Hostname.of(HostnameTests.PRIVATE_IP_192_168_1_2);
242 final Hostname hostname4 = Hostname.of(HostnameTests.PRIVATE_IP_192_168_1_1);
243 assertAll("testEquals",
244 () -> assertTrue(hostname1.equals(hostname1), "hostname11 is not equal"),
245 () -> assertTrue(hostname1.equals(hostname2), "hostname12 are not equal"),
246 () -> assertTrue(hostname2.equals(hostname1), "hostname21 are not equal"),
247 () -> assertTrue(hostname2.equals(hostname4), "hostname24 are not equal"),
248 () -> assertTrue(hostname1.equals(hostname4), "hostname14 are not equal"),
249 () -> assertFalse(hostname1.equals(hostname3), "hostname13 are equal"),
250 () -> assertFalse(hostname3.equals(hostname1), "hostname31 are equal"),
251 () -> assertFalse(hostname1.equals(null), "hostname10 is equal")
252 );
253 }
254
255
256
257
258
259 @Test
260 void testToString()
261 {
262 final Hostname hostname = Hostname.of(HostnameTests.PRIVATE_IP_192_168_1_1);
263 assertEquals("Hostname[hostname=192.168.1.1]", hostname.toString(), "toString not equal");
264 }
265
266
267
268
269
270 @Test
271 @SuppressWarnings("java:S5785")
272 void testCompareTo()
273 {
274 final Hostname hostname1 = Hostname.of(HostnameTests.PRIVATE_IP_192_168_1_1);
275 final Hostname hostname2 = Hostname.of(HostnameTests.PRIVATE_IP_192_168_1_1);
276 final Hostname hostname3 = Hostname.of(HostnameTests.PRIVATE_IP_192_168_1_2);
277 final Hostname hostname4 = Hostname.of("192.168.1.3");
278 final Hostname hostname5 = Hostname.of(HostnameTests.PRIVATE_IP_192_168_1_1);
279 assertAll("testCompareTo",
280 () -> assertTrue(hostname1.compareTo(hostname2) == -hostname2.compareTo(hostname1), "reflexive1"),
281 () -> assertTrue(hostname1.compareTo(hostname3) == -hostname3.compareTo(hostname1), "reflexive2"),
282 () -> assertTrue((hostname4.compareTo(hostname3) > 0) && (hostname3.compareTo(hostname1) > 0) && (hostname4.compareTo(hostname1) > 0), "transitive1"),
283 () -> assertTrue((hostname1.compareTo(hostname2) == 0) && (Math.abs(hostname1.compareTo(hostname5)) == Math.abs(hostname2.compareTo(hostname5))), "sgn1"),
284 () -> assertTrue((hostname1.compareTo(hostname2) == 0) && hostname1.equals(hostname2), "equals")
285 );
286 }
287
288 }