Skip to content

Commit 6f96ff4

Browse files
committed
Refactor localhost check in PostgresUtils
1 parent d7226ad commit 6f96ff4

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

src/main/java/de/cronn/postgres/snapshot/util/PostgresUtils.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static ConnectionInformation parseConnectionInformation(String jdbcUrl, String u
2828
URI databaseUri = toUri(jdbcUrl);
2929

3030
int port = databaseUri.getPort();
31-
String host = resolveHost(databaseUri.getHost());
31+
String host = prepareHostname(databaseUri.getHost());
3232

3333
Properties connectionProperties = new Properties();
3434
connectionProperties.put("user", username);
@@ -52,21 +52,21 @@ private static URI toUri(String jdbcUrl) {
5252
}
5353
}
5454

55-
private static String resolveHost(String host) {
56-
try {
57-
InetAddress inetAddress = InetAddress.getByName(host);
58-
if (isLocalhost(inetAddress)) {
59-
return PostgresConstants.DOCKER_HOST_INTERNAL;
60-
} else {
61-
return inetAddress.getHostAddress();
62-
}
63-
} catch (UnknownHostException e) {
64-
throw new IllegalArgumentException("Failed to resolve host", e);
55+
private static String prepareHostname(String host) {
56+
if (isLocalhost(host)) {
57+
return PostgresConstants.DOCKER_HOST_INTERNAL;
58+
} else {
59+
return host;
6560
}
6661
}
6762

68-
private static boolean isLocalhost(InetAddress inetAddress) throws UnknownHostException {
69-
return InetAddress.getByName("localhost").equals(inetAddress);
63+
private static boolean isLocalhost(String host) {
64+
try {
65+
InetAddress localHost = InetAddress.getLocalHost();
66+
return localHost.getHostName().equals(host) || localHost.getHostAddress().equals(host);
67+
} catch (UnknownHostException e) {
68+
throw new RuntimeException("Failed resolve localhost", e);
69+
}
7070
}
7171

7272
static String deriveNetworkMode(ConnectionInformation connectionInformation) {

src/test/java/de/cronn/postgres/snapshot/util/PostgresUtilsTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import static org.assertj.core.api.Assertions.*;
44

55
import java.net.URISyntaxException;
6-
import java.net.UnknownHostException;
76

87
import org.junit.jupiter.api.Test;
98

@@ -19,10 +18,9 @@ void testParseConnectionInformation_illegalJdbcUrl() {
1918

2019
@Test
2120
void testParseConnectionInformation_unknownHost() {
22-
assertThatExceptionOfType(IllegalArgumentException.class)
23-
.isThrownBy(() -> PostgresUtils.parseConnectionInformation("jdbc:postgresql://unknown-host", "user", "password"))
24-
.withCauseExactlyInstanceOf(UnknownHostException.class)
25-
.withMessage("Failed to resolve host");
21+
assertThatExceptionOfType(RuntimeException.class)
22+
.isThrownBy(() -> PostgresUtils.parseConnectionInformation("jdbc:postgresql://unknown-host/test", "user", "password"))
23+
.withMessage("org.postgresql.util.PSQLException: The connection attempt failed.");
2624
}
2725

2826
@Test

0 commit comments

Comments
 (0)