|
1 | 1 | package codes.thischwa.dyndrest.model; |
2 | 2 |
|
3 | | -import org.junit.jupiter.api.Test; |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertNotEquals; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 8 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
4 | 9 |
|
5 | 10 | import java.net.Inet4Address; |
6 | 11 | import java.net.Inet6Address; |
7 | 12 | import java.net.InetAddress; |
8 | 13 | import java.net.UnknownHostException; |
9 | | - |
10 | | -import static org.junit.jupiter.api.Assertions.*; |
| 14 | +import org.junit.jupiter.api.Test; |
11 | 15 |
|
12 | 16 | class IpSettingTest { |
13 | 17 |
|
14 | | - @Test |
15 | | - void defaultConstructor_isNotSet() { |
16 | | - IpSetting s = new IpSetting(); |
17 | | - assertTrue(s.isNotSet()); |
18 | | - assertNull(s.getIpv4()); |
19 | | - assertNull(s.getIpv6()); |
20 | | - assertNull(s.ipv4ToString()); |
21 | | - assertNull(s.ipv6ToString()); |
22 | | - } |
| 18 | + @Test |
| 19 | + void defaultConstructor_isNotSet() { |
| 20 | + IpSetting s = new IpSetting(); |
| 21 | + assertTrue(s.isNotSet()); |
| 22 | + assertNull(s.getIpv4()); |
| 23 | + assertNull(s.getIpv6()); |
| 24 | + assertNull(s.ipv4ToString()); |
| 25 | + assertNull(s.ipv6ToString()); |
| 26 | + } |
23 | 27 |
|
24 | | - @Test |
25 | | - void stringConstructor_setsIpv4() throws UnknownHostException { |
26 | | - IpSetting s = new IpSetting("192.168.1.10"); |
27 | | - assertFalse(s.isNotSet()); |
28 | | - assertNotNull(s.getIpv4()); |
29 | | - assertNull(s.getIpv6()); |
30 | | - assertEquals("192.168.1.10", s.ipv4ToString()); |
31 | | - } |
| 28 | + @Test |
| 29 | + void stringConstructor_setsIpv4() throws UnknownHostException { |
| 30 | + IpSetting s = new IpSetting("192.168.1.10"); |
| 31 | + assertFalse(s.isNotSet()); |
| 32 | + assertNotNull(s.getIpv4()); |
| 33 | + assertNull(s.getIpv6()); |
| 34 | + assertEquals("192.168.1.10", s.ipv4ToString()); |
| 35 | + } |
32 | 36 |
|
33 | | - @Test |
34 | | - void stringConstructor_setsIpv6() throws UnknownHostException { |
35 | | - IpSetting s = new IpSetting("2a03:4000:41:32::2"); |
36 | | - assertFalse(s.isNotSet()); |
37 | | - assertNull(s.getIpv4()); |
38 | | - assertNotNull(s.getIpv6()); |
39 | | - assertEquals("2a03:4000:41:32:0:0:0:2", s.ipv6ToString()); |
40 | | - } |
| 37 | + @Test |
| 38 | + void stringConstructor_setsIpv6() throws UnknownHostException { |
| 39 | + IpSetting s = new IpSetting("2a03:4000:41:32::2"); |
| 40 | + assertFalse(s.isNotSet()); |
| 41 | + assertNull(s.getIpv4()); |
| 42 | + assertNotNull(s.getIpv6()); |
| 43 | + assertEquals("2a03:4000:41:32:0:0:0:2", s.ipv6ToString()); |
| 44 | + } |
41 | 45 |
|
42 | | - @Test |
43 | | - void dualStringConstructor_setsBoth_whenValid() throws UnknownHostException { |
44 | | - IpSetting s = new IpSetting("10.0.0.1", "2a03:4000:41:32::20"); |
45 | | - assertFalse(s.isNotSet()); |
46 | | - assertEquals("10.0.0.1", s.ipv4ToString()); |
47 | | - assertEquals("2a03:4000:41:32:0:0:0:20", s.ipv6ToString()); |
48 | | - } |
| 46 | + @Test |
| 47 | + void dualStringConstructor_setsBoth_whenValid() throws UnknownHostException { |
| 48 | + IpSetting s = new IpSetting("10.0.0.1", "2a03:4000:41:32::20"); |
| 49 | + assertFalse(s.isNotSet()); |
| 50 | + assertEquals("10.0.0.1", s.ipv4ToString()); |
| 51 | + assertEquals("2a03:4000:41:32:0:0:0:20", s.ipv6ToString()); |
| 52 | + } |
49 | 53 |
|
50 | | - @Test |
51 | | - void dualStringConstructor_handlesNullsIndividually() throws UnknownHostException { |
52 | | - IpSetting onlyV4 = new IpSetting("10.0.0.2", null); |
53 | | - assertEquals("10.0.0.2", onlyV4.ipv4ToString()); |
54 | | - assertNull(onlyV4.ipv6ToString()); |
| 54 | + @Test |
| 55 | + void dualStringConstructor_handlesNullsIndividually() throws UnknownHostException { |
| 56 | + IpSetting onlyV4 = new IpSetting("10.0.0.2", null); |
| 57 | + assertEquals("10.0.0.2", onlyV4.ipv4ToString()); |
| 58 | + assertNull(onlyV4.ipv6ToString()); |
55 | 59 |
|
56 | | - IpSetting onlyV6 = new IpSetting(null, "2a03:4000:41:32::21"); |
57 | | - assertNull(onlyV6.ipv4ToString()); |
58 | | - assertEquals("2a03:4000:41:32:0:0:0:21", onlyV6.ipv6ToString()); |
59 | | - } |
| 60 | + IpSetting onlyV6 = new IpSetting(null, "2a03:4000:41:32::21"); |
| 61 | + assertNull(onlyV6.ipv4ToString()); |
| 62 | + assertEquals("2a03:4000:41:32:0:0:0:21", onlyV6.ipv6ToString()); |
| 63 | + } |
60 | 64 |
|
61 | | - @Test |
62 | | - void inetAddressConstructor_setsOnlyMatchingTypes() throws Exception { |
63 | | - InetAddress v4 = InetAddress.getByName("172.16.0.3"); |
64 | | - InetAddress v6 = InetAddress.getByName("2a03:4000:41:32::22"); |
65 | | - IpSetting s = new IpSetting(v4, v6); |
66 | | - assertEquals("172.16.0.3", s.ipv4ToString()); |
67 | | - assertEquals("2a03:4000:41:32:0:0:0:22", s.ipv6ToString()); |
| 65 | + @Test |
| 66 | + void inetAddressConstructor_setsOnlyMatchingTypes() throws Exception { |
| 67 | + InetAddress v4 = InetAddress.getByName("172.16.0.3"); |
| 68 | + InetAddress v6 = InetAddress.getByName("2a03:4000:41:32::22"); |
| 69 | + IpSetting s = new IpSetting(v4, v6); |
| 70 | + assertEquals("172.16.0.3", s.ipv4ToString()); |
| 71 | + assertEquals("2a03:4000:41:32:0:0:0:22", s.ipv6ToString()); |
68 | 72 |
|
69 | | - // Pass swapped types to ensure non-matching are ignored |
70 | | - Inet4Address onlyV4 = (Inet4Address) v4; |
71 | | - Inet6Address onlyV6 = (Inet6Address) v6; |
72 | | - IpSetting s2 = new IpSetting(onlyV6, onlyV4); // wrong order on purpose |
73 | | - // constructor should ignore mismatched types, leaving nulls |
74 | | - assertNull(s2.getIpv4()); |
75 | | - assertNull(s2.getIpv6()); |
76 | | - } |
| 73 | + // Pass swapped types to ensure non-matching are ignored |
| 74 | + Inet4Address onlyV4 = (Inet4Address) v4; |
| 75 | + Inet6Address onlyV6 = (Inet6Address) v6; |
| 76 | + IpSetting s2 = new IpSetting(onlyV6, onlyV4); // wrong order on purpose |
| 77 | + // constructor should ignore mismatched types, leaving nulls |
| 78 | + assertNull(s2.getIpv4()); |
| 79 | + assertNull(s2.getIpv6()); |
| 80 | + } |
77 | 81 |
|
78 | | - @Test |
79 | | - void equalsAndHashCode_sameIps_areEqual() throws UnknownHostException { |
80 | | - IpSetting a = new IpSetting("10.0.0.1", "2a03:4000:41:32::23"); |
81 | | - IpSetting b = new IpSetting("10.0.0.1", "2a03:4000:41:32::23"); |
82 | | - assertEquals(a, b); |
83 | | - assertEquals(a.hashCode(), b.hashCode()); |
84 | | - } |
| 82 | + @Test |
| 83 | + void equalsAndHashCode_sameIps_areEqual() throws UnknownHostException { |
| 84 | + IpSetting a = new IpSetting("10.0.0.1", "2a03:4000:41:32::23"); |
| 85 | + IpSetting b = new IpSetting("10.0.0.1", "2a03:4000:41:32::23"); |
| 86 | + assertEquals(a, b); |
| 87 | + assertEquals(a.hashCode(), b.hashCode()); |
| 88 | + } |
85 | 89 |
|
86 | | - @Test |
87 | | - void equalsAndHashCode_differentIps_notEqual() throws UnknownHostException { |
88 | | - IpSetting a = new IpSetting("10.0.0.1", "2a03:4000:41:32::23"); |
89 | | - IpSetting b = new IpSetting("10.0.0.2", "2a03:4000:41:32::23"); |
90 | | - assertNotEquals(a, b); |
91 | | - } |
| 90 | + @Test |
| 91 | + void equalsAndHashCode_differentIps_notEqual() throws UnknownHostException { |
| 92 | + IpSetting a = new IpSetting("10.0.0.1", "2a03:4000:41:32::23"); |
| 93 | + IpSetting b = new IpSetting("10.0.0.2", "2a03:4000:41:32::23"); |
| 94 | + assertNotEquals(a, b); |
| 95 | + } |
92 | 96 |
|
93 | | - @Test |
94 | | - void toString_containsAddresses_whenSet() throws UnknownHostException { |
95 | | - IpSetting s = new IpSetting("10.0.0.5", "2a03:4000:41:32::24"); |
96 | | - String txt = s.toString(); |
97 | | - assertTrue(txt.contains("10.0.0.5")); |
98 | | - assertTrue(txt.contains("2a03:4000:41:32")); |
99 | | - } |
| 97 | + @Test |
| 98 | + void toString_containsAddresses_whenSet() throws UnknownHostException { |
| 99 | + IpSetting s = new IpSetting("10.0.0.5", "2a03:4000:41:32::24"); |
| 100 | + String txt = s.toString(); |
| 101 | + assertTrue(txt.contains("10.0.0.5")); |
| 102 | + assertTrue(txt.contains("2a03:4000:41:32")); |
| 103 | + } |
100 | 104 | } |
0 commit comments