Skip to content

Commit

Permalink
Add unit tests and use SnowflakeSQLException instead of generic SQLEx…
Browse files Browse the repository at this point in the history
…ception
  • Loading branch information
fabiencelier committed Feb 3, 2025
1 parent 14a9f0e commit 10a3c88
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public Connection getConnection(String username, String password) throws SQLExce
if (!AUTHENTICATOR_OAUTH.equalsIgnoreCase(
authenticator)) { // For OAuth, no username is required
if (username == null) {
throw new SQLException(
throw new SnowflakeSQLException(
"Cannot create connection because username is missing in DataSource properties.");
}
properties.put(SFSessionProperty.USER.getPropertyKey(), username);
Expand All @@ -105,7 +105,7 @@ public Connection getConnection(String username, String password) throws SQLExce
if (!AUTHENTICATOR_SNOWFLAKE_JWT.equalsIgnoreCase(authenticator)
&& !AUTHENTICATOR_EXTERNAL_BROWSER.equalsIgnoreCase(authenticator)) {
if (password == null) {
throw new SQLException(
throw new SnowflakeSQLException(
"Cannot create connection because password is missing in DataSource properties.");
}
properties.put(SFSessionProperty.PASSWORD.getPropertyKey(), password);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.sql.SQLException;
import java.util.Properties;
Expand Down Expand Up @@ -112,4 +113,22 @@ public void testDataSourceSetters() {
assertEquals("pwd", props.get(SFSessionProperty.PRIVATE_KEY_PWD.getPropertyKey()));
assertEquals("SNOWFLAKE_JWT", props.get(SFSessionProperty.AUTHENTICATOR.getPropertyKey()));
}

@Test
public void testDataSourceWithoutUsernameOrPasswordThrowsExplicitException() {
SnowflakeBasicDataSource ds = new SnowflakeBasicDataSource();

ds.setAccount("testaccount");
ds.setAuthenticator("snowflake");
assertThrows(
SnowflakeSQLException.class,
ds::getConnection,
"Cannot create connection because username is missing in DataSource properties.");

ds.setUser("testuser");
assertThrows(
SnowflakeSQLException.class,
ds::getConnection,
"Cannot create connection because password is missing in DataSource properties.");
}
}

0 comments on commit 10a3c88

Please sign in to comment.