From 73ce0fd62316b3458cb972fc5c38e2a443df597a Mon Sep 17 00:00:00 2001 From: sfc-gh-astachowski Date: Tue, 5 Nov 2024 05:58:10 +0100 Subject: [PATCH] Refactored callable statement tests to avoid running a test twice --- .../client/jdbc/CallableStatementIT.java | 52 ++----------------- .../client/jdbc/CallableStatementITBase.java | 48 +++++++++++++++++ .../jdbc/CallableStatementLatestIT.java | 16 +++--- 3 files changed, 61 insertions(+), 55 deletions(-) create mode 100644 src/test/java/net/snowflake/client/jdbc/CallableStatementITBase.java diff --git a/src/test/java/net/snowflake/client/jdbc/CallableStatementIT.java b/src/test/java/net/snowflake/client/jdbc/CallableStatementIT.java index f6df67104..a21961063 100644 --- a/src/test/java/net/snowflake/client/jdbc/CallableStatementIT.java +++ b/src/test/java/net/snowflake/client/jdbc/CallableStatementIT.java @@ -15,7 +15,6 @@ import java.sql.Date; import java.sql.ResultSet; import java.sql.SQLException; -import java.sql.Statement; import java.sql.Time; import java.sql.Timestamp; import java.sql.Types; @@ -23,54 +22,14 @@ import java.util.HashMap; import net.snowflake.client.category.TestTags; import net.snowflake.client.providers.SimpleFormatProvider; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ArgumentsSource; // @Category(TestCategoryStatement.class) @Tag(TestTags.STATEMENT) -public class CallableStatementIT extends BaseJDBCTest { - - public static Connection getConnection() throws SQLException { - return BaseJDBCTest.getConnection(); - } - - public static Connection getConnection(String queryResultFormat) throws SQLException { - Connection conn = BaseJDBCTest.getConnection(); - try (Statement stmt = conn.createStatement()) { - stmt.execute("alter session set jdbc_query_result_format = '" + queryResultFormat + "'"); - } - return conn; - } - - private final String createStoredProcedure = - "create or replace procedure square_it(num FLOAT) returns float not " - + "null language javascript as $$ return NUM * NUM; $$"; - private final String createSecondStoredProcedure = - "create or replace procedure add_nums(x DOUBLE, y DOUBLE) " - + "returns double not null language javascript as $$ return X + Y; $$"; - private final String deleteStoredProcedure = "drop procedure if exists square_it(FLOAT)"; - private final String deleteSecondStoredProcedure = "drop procedure if exists add_nums(INT, INT)"; - - @BeforeEach - public void setUp() throws SQLException { - try (Connection con = getConnection(); - Statement statement = con.createStatement()) { - statement.execute(createStoredProcedure); - statement.execute(createSecondStoredProcedure); - } - } - - @AfterEach - public void tearDown() throws SQLException { - try (Connection con = getConnection(); - Statement statement = con.createStatement()) { - statement.execute(deleteStoredProcedure); - statement.execute(deleteSecondStoredProcedure); - } - } +public class CallableStatementIT extends CallableStatementITBase { @ParameterizedTest @ArgumentsSource(SimpleFormatProvider.class) @@ -106,10 +65,9 @@ public void testPrepareCall(String queryResultFormat) throws SQLException { } } - @ParameterizedTest - @ArgumentsSource(SimpleFormatProvider.class) - public void testFeatureNotSupportedException(String queryResultFormat) throws Throwable { - try (Connection connection = getConnection(queryResultFormat); ) { + @Test + public void testFeatureNotSupportedException() throws Throwable { + try (Connection connection = getConnection()) { CallableStatement callableStatement = connection.prepareCall("select ?"); expectFeatureNotSupportedException( () -> callableStatement.registerOutParameter(1, Types.INTEGER)); diff --git a/src/test/java/net/snowflake/client/jdbc/CallableStatementITBase.java b/src/test/java/net/snowflake/client/jdbc/CallableStatementITBase.java new file mode 100644 index 000000000..8635d4246 --- /dev/null +++ b/src/test/java/net/snowflake/client/jdbc/CallableStatementITBase.java @@ -0,0 +1,48 @@ +package net.snowflake.client.jdbc; + +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; + +public class CallableStatementITBase extends BaseJDBCTest { + public static Connection getConnection() throws SQLException { + return BaseJDBCTest.getConnection(); + } + + public static Connection getConnection(String queryResultFormat) throws SQLException { + Connection conn = BaseJDBCTest.getConnection(); + try (Statement stmt = conn.createStatement()) { + stmt.execute("alter session set jdbc_query_result_format = '" + queryResultFormat + "'"); + } + return conn; + } + + private final String createStoredProcedure = + "create or replace procedure square_it(num FLOAT) returns float not " + + "null language javascript as $$ return NUM * NUM; $$"; + private final String createSecondStoredProcedure = + "create or replace procedure add_nums(x DOUBLE, y DOUBLE) " + + "returns double not null language javascript as $$ return X + Y; $$"; + private final String deleteStoredProcedure = "drop procedure if exists square_it(FLOAT)"; + private final String deleteSecondStoredProcedure = "drop procedure if exists add_nums(INT, INT)"; + + @BeforeEach + public void setUp() throws SQLException { + try (Connection con = getConnection(); + Statement statement = con.createStatement()) { + statement.execute(createStoredProcedure); + statement.execute(createSecondStoredProcedure); + } + } + + @AfterEach + public void tearDown() throws SQLException { + try (Connection con = getConnection(); + Statement statement = con.createStatement()) { + statement.execute(deleteStoredProcedure); + statement.execute(deleteSecondStoredProcedure); + } + } +} diff --git a/src/test/java/net/snowflake/client/jdbc/CallableStatementLatestIT.java b/src/test/java/net/snowflake/client/jdbc/CallableStatementLatestIT.java index d2ab3bc9a..74ca5ec81 100644 --- a/src/test/java/net/snowflake/client/jdbc/CallableStatementLatestIT.java +++ b/src/test/java/net/snowflake/client/jdbc/CallableStatementLatestIT.java @@ -11,16 +11,15 @@ import java.sql.SQLException; import java.sql.Types; import net.snowflake.client.category.TestTags; +import net.snowflake.client.providers.SimpleFormatProvider; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ArgumentsSource; // @Category(TestCategoryStatement.class) @Tag(TestTags.STATEMENT) -public class CallableStatementLatestIT extends CallableStatementIT { - - public CallableStatementLatestIT(String format) { - super(); - } +public class CallableStatementLatestIT extends CallableStatementITBase { /** * Test that function that removes curly brackets from outside of call statements works properly @@ -45,10 +44,11 @@ public void testParseSqlEscapeSyntaxFunction() { * * @throws SQLException */ - @Test - public void testPrepareCallWithCurlyBracketSyntax() throws SQLException { + @ParameterizedTest + @ArgumentsSource(SimpleFormatProvider.class) + public void testPrepareCallWithCurlyBracketSyntax(String queryResultFormat) throws SQLException { // test CallableStatement with no binding parameters - try (Connection connection = getConnection()) { + try (Connection connection = getConnection(queryResultFormat)) { try (CallableStatement callableStatement = connection.prepareCall("{call square_it(5)}")) { assertThat(callableStatement.getParameterMetaData().getParameterCount(), is(0)); }