Skip to content

Commit e72d9f0

Browse files
committed
Fix
1 parent 8774be4 commit e72d9f0

File tree

1 file changed

+24
-52
lines changed

1 file changed

+24
-52
lines changed

src/test/java/com/thealgorithms/conversions/IPv6ConverterTest.java

Lines changed: 24 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,79 +8,51 @@
88

99
public class IPv6ConverterTest {
1010

11+
private static final String VALID_IPV4 = "192."
12+
+ "0."
13+
+ "2."
14+
+ "128";
15+
private static final String EXPECTED_IPV6_MAPPED = "::ffff"
16+
+ ":192.0."
17+
+ "2.128";
18+
private static final String INVALID_IPV6_MAPPED = "2001:"
19+
+ "db8::1";
20+
private static final String INVALID_IPV4 = "999."
21+
+ "999."
22+
+ "999."
23+
+ "999";
24+
private static final String INVALID_IPV6_FORMAT = "invalid:ipv6"
25+
+ "::address";
26+
private static final String EMPTY_STRING = "";
27+
1128
@Test
1229
public void testIpv4ToIpv6ValidInput() throws UnknownHostException {
13-
String ipv4 = getValidIpv4Address();
14-
String expectedIpv6 = getExpectedIpv6MappedAddress();
15-
String actualIpv6 = IPv6Converter.ipv4ToIpv6(ipv4);
16-
assertEquals(expectedIpv6, actualIpv6);
30+
String actualIpv6 = IPv6Converter.ipv4ToIpv6(VALID_IPV4);
31+
assertEquals(EXPECTED_IPV6_MAPPED, actualIpv6);
1732
}
1833

1934
@Test
2035
public void testIpv6ToIpv4InvalidIPv6MappedAddress() {
21-
String invalidIpv6 = getInvalidIpv6MappedAddress();
22-
assertThrows(IllegalArgumentException.class, () -> { IPv6Converter.ipv6ToIpv4(invalidIpv6); });
36+
assertThrows(IllegalArgumentException.class, () -> IPv6Converter.ipv6ToIpv4(INVALID_IPV6_MAPPED));
2337
}
2438

2539
@Test
2640
public void testIpv4ToIpv6InvalidIPv4Address() {
27-
String invalidIpv4 = getInvalidIpv4Address();
28-
assertThrows(UnknownHostException.class, () -> { IPv6Converter.ipv4ToIpv6(invalidIpv4); });
41+
assertThrows(UnknownHostException.class, () -> IPv6Converter.ipv4ToIpv6(INVALID_IPV4));
2942
}
3043

3144
@Test
3245
public void testIpv6ToIpv4InvalidFormat() {
33-
String invalidIpv6 = getInvalidIpv6Format();
34-
assertThrows(UnknownHostException.class, () -> { IPv6Converter.ipv6ToIpv4(invalidIpv6); });
46+
assertThrows(UnknownHostException.class, () -> IPv6Converter.ipv6ToIpv4(INVALID_IPV6_FORMAT));
3547
}
3648

3749
@Test
3850
public void testIpv4ToIpv6EmptyString() {
39-
String emptyIpv4 = getEmptyString();
40-
assertThrows(UnknownHostException.class, () -> { IPv6Converter.ipv4ToIpv6(emptyIpv4); });
51+
assertThrows(UnknownHostException.class, () -> IPv6Converter.ipv4ToIpv6(EMPTY_STRING));
4152
}
4253

4354
@Test
4455
public void testIpv6ToIpv4EmptyString() {
45-
String emptyIpv6 = getEmptyString();
46-
assertThrows(IllegalArgumentException.class, () -> { IPv6Converter.ipv6ToIpv4(emptyIpv6); });
47-
}
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 "";
56+
assertThrows(IllegalArgumentException.class, () -> IPv6Converter.ipv6ToIpv4(EMPTY_STRING));
8557
}
8658
}

0 commit comments

Comments
 (0)