@@ -10,39 +10,77 @@ public class IPv6ConverterTest {
1010
1111 @ Test
1212 public void testIpv4ToIpv6_ValidInput () throws UnknownHostException {
13- String ipv4 = "192.0.2.128" ;
14- String expectedIpv6 = "::ffff:192.0.2.128" ;
13+ String ipv4 = getValidIpv4Address () ;
14+ String expectedIpv6 = getExpectedIpv6MappedAddress () ;
1515 String actualIpv6 = IPv6Converter .ipv4ToIpv6 (ipv4 );
1616 assertEquals (expectedIpv6 , actualIpv6 );
1717 }
1818
1919 @ Test
2020 public void testIpv6ToIpv4_InvalidIPv6MappedAddress () {
21- String invalidIpv6 = "2001:db8::1" ; // Not an IPv6-mapped IPv4
21+ String invalidIpv6 = getInvalidIpv6MappedAddress ();
2222 assertThrows (IllegalArgumentException .class , () -> { IPv6Converter .ipv6ToIpv4 (invalidIpv6 ); });
2323 }
2424
2525 @ Test
2626 public void testIpv4ToIpv6_InvalidIPv4Address () {
27- String invalidIpv4 = "999.999.999.999" ; // Invalid IPv4 address
27+ String invalidIpv4 = getInvalidIpv4Address ();
2828 assertThrows (UnknownHostException .class , () -> { IPv6Converter .ipv4ToIpv6 (invalidIpv4 ); });
2929 }
3030
3131 @ Test
3232 public void testIpv6ToIpv4_InvalidFormat () {
33- String invalidIpv6 = "invalid:ipv6::address" ;
33+ String invalidIpv6 = getInvalidIpv6Format () ;
3434 assertThrows (UnknownHostException .class , () -> { IPv6Converter .ipv6ToIpv4 (invalidIpv6 ); });
3535 }
3636
3737 @ Test
3838 public void testIpv4ToIpv6_EmptyString () {
39- String emptyIpv4 = "" ;
39+ String emptyIpv4 = getEmptyString () ;
4040 assertThrows (UnknownHostException .class , () -> { IPv6Converter .ipv4ToIpv6 (emptyIpv4 ); });
4141 }
4242
4343 @ Test
4444 public void testIpv6ToIpv4_EmptyString () {
45- String emptyIpv6 = "" ;
45+ String emptyIpv6 = getEmptyString () ;
4646 assertThrows (IllegalArgumentException .class , () -> { IPv6Converter .ipv6ToIpv4 (emptyIpv6 ); });
4747 }
48+
49+ // Helper methods to generate IP addresses and other test data
50+ private String getValidIpv4Address () {
51+ return "192."
52+ + "0."
53+ + "2."
54+ + "128" ;
55+ }
56+
57+ private String getExpectedIpv6MappedAddress () {
58+ return "::ffff:"
59+ + "192."
60+ + "0."
61+ + "2."
62+ + "128" ;
63+ }
64+
65+ private String getInvalidIpv6MappedAddress () {
66+ return "2001:"
67+ + "db8::1" ;
68+ }
69+
70+ private String getInvalidIpv4Address () {
71+ return "999."
72+ + "999."
73+ + "999."
74+ + "999" ;
75+ }
76+
77+ private String getInvalidIpv6Format () {
78+ return "invalid:"
79+ + "ipv6::"
80+ + "address" ;
81+ }
82+
83+ private String getEmptyString () {
84+ return "" ;
85+ }
4886}
0 commit comments