22
33import static org .assertj .core .api .Assertions .*;
44
5+ import java .net .InetAddress ;
56import java .net .URISyntaxException ;
67
78import org .junit .jupiter .api .Test ;
9+ import org .junit .jupiter .params .ParameterizedTest ;
10+ import org .junit .jupiter .params .provider .ValueSource ;
811
912class PostgresUtilsTest {
1013
@@ -23,6 +26,13 @@ void testParseConnectionInformation_unknownHost() {
2326 .withMessage ("org.postgresql.util.PSQLException: The connection attempt failed." );
2427 }
2528
29+ @ Test
30+ void testParseConnectionInformation_failsToConnect () {
31+ assertThatExceptionOfType (RuntimeException .class )
32+ .isThrownBy (() -> PostgresUtils .parseConnectionInformation ("jdbc:postgresql://10.0.0.1/test?connectTimeout=1" , "user" , "password" ))
33+ .withMessage ("org.postgresql.util.PSQLException: The connection attempt failed." );
34+ }
35+
2636 @ Test
2737 void testParseConnectionInformation_driverFailsToParseJdbcUrl () {
2838 assertThatExceptionOfType (RuntimeException .class )
@@ -37,4 +47,18 @@ void testParseConnectionInformation_failToConnectToPostgresDatabase() {
3747 .withMessage ("org.postgresql.util.PSQLException: Connection to localhost:5432 refused." +
3848 " Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections." );
3949 }
50+
51+ @ ParameterizedTest
52+ @ ValueSource (strings = { "localhost" , "127.0.0.1" })
53+ void testIsLocalHost_positive (String host ) {
54+ assertThat (PostgresUtils .isLocalhost (host )).isTrue ();
55+ }
56+
57+ @ Test
58+ void testIsLocalHost_negative () throws Exception {
59+ assertThat (PostgresUtils .isLocalhost ("some.host.local" )).isFalse ();
60+ assertThat (PostgresUtils .isLocalhost ("1.2.3.4" )).isFalse ();
61+ InetAddress localHost = InetAddress .getLocalHost ();
62+ assertThat (PostgresUtils .isLocalhost (localHost .getHostName ())).isFalse ();
63+ }
4064}
0 commit comments