diff --git a/src/test/java/net/snowflake/client/jdbc/BaseJDBCWithSharedConnectionIT.java b/src/test/java/net/snowflake/client/jdbc/BaseJDBCWithSharedConnectionIT.java new file mode 100644 index 000000000..5602bffca --- /dev/null +++ b/src/test/java/net/snowflake/client/jdbc/BaseJDBCWithSharedConnectionIT.java @@ -0,0 +1,23 @@ +package net.snowflake.client.jdbc; + +import java.sql.Connection; +import java.sql.SQLException; +import org.junit.AfterClass; +import org.junit.BeforeClass; + +public class BaseJDBCWithSharedConnectionIT extends BaseJDBCTest { + + protected static Connection connection; + + @BeforeClass + public static void setUpConnection() throws SQLException { + connection = getConnection(); + } + + @AfterClass + public static void closeConnection() throws SQLException { + if (connection != null && !connection.isClosed()) { + connection.close(); + } + } +} diff --git a/src/test/java/net/snowflake/client/jdbc/ResultSet0IT.java b/src/test/java/net/snowflake/client/jdbc/ResultSet0IT.java index b6832632b..62148327d 100644 --- a/src/test/java/net/snowflake/client/jdbc/ResultSet0IT.java +++ b/src/test/java/net/snowflake/client/jdbc/ResultSet0IT.java @@ -17,24 +17,10 @@ /** Result set test base class. */ @Category(TestCategoryResultSet.class) -public class ResultSet0IT extends BaseJDBCTest { +public class ResultSet0IT extends BaseJDBCWithSharedConnectionIT { private final String queryResultFormat; - public Connection init(int injectSocketTimeout) throws SQLException { - Connection connection = BaseJDBCTest.getConnection(injectSocketTimeout); - try (Statement statement = connection.createStatement()) { - statement.execute( - "alter session set " - + "TIMEZONE='America/Los_Angeles'," - + "TIMESTAMP_TYPE_MAPPING='TIMESTAMP_LTZ'," - + "TIMESTAMP_OUTPUT_FORMAT='DY, DD MON YYYY HH24:MI:SS TZHTZM'," - + "TIMESTAMP_TZ_OUTPUT_FORMAT='DY, DD MON YYYY HH24:MI:SS TZHTZM'," - + "TIMESTAMP_LTZ_OUTPUT_FORMAT='DY, DD MON YYYY HH24:MI:SS TZHTZM'," - + "TIMESTAMP_NTZ_OUTPUT_FORMAT='DY, DD MON YYYY HH24:MI:SS TZHTZM'"); - } - return connection; - } - + // TODO: Clean up init() methods when updating other test classes to use a common connection. public Connection init() throws SQLException { Connection conn = BaseJDBCTest.getConnection(BaseJDBCTest.DONT_INJECT_SOCKET_TIMEOUT); try (Statement stmt = conn.createStatement()) { @@ -54,8 +40,7 @@ public Connection init(Properties paramProperties) throws SQLException { @Before public void setUp() throws SQLException { - try (Connection con = init(); - Statement statement = con.createStatement()) { + try (Statement statement = connection.createStatement()) { // TEST_RS statement.execute("create or replace table test_rs (colA string)"); @@ -88,9 +73,7 @@ public void setUp() throws SQLException { } ResultSet numberCrossTesting() throws SQLException { - Connection con = init(); - Statement statement = con.createStatement(); - + Statement statement = connection.createStatement(); statement.execute( "create or replace table test_types(c1 number, c2 integer, c3 float, c4 boolean," + "c5 char, c6 varchar, c7 date, c8 datetime, c9 time, c10 timestamp_ltz, " diff --git a/src/test/java/net/snowflake/client/jdbc/ResultSetMultiTimeZoneLatestIT.java b/src/test/java/net/snowflake/client/jdbc/ResultSetMultiTimeZoneLatestIT.java index 06a253b95..e03dc35df 100644 --- a/src/test/java/net/snowflake/client/jdbc/ResultSetMultiTimeZoneLatestIT.java +++ b/src/test/java/net/snowflake/client/jdbc/ResultSetMultiTimeZoneLatestIT.java @@ -3,7 +3,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import java.sql.Connection; import java.sql.Date; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -19,6 +18,7 @@ import net.snowflake.client.ConditionalIgnoreRule; import net.snowflake.client.RunningOnGithubAction; import net.snowflake.client.category.TestCategoryResultSet; +import org.junit.Before; import org.junit.Test; import org.junit.experimental.categories.Category; import org.junit.runner.RunWith; @@ -29,7 +29,7 @@ */ @RunWith(Parameterized.class) @Category(TestCategoryResultSet.class) -public class ResultSetMultiTimeZoneLatestIT extends BaseJDBCTest { +public class ResultSetMultiTimeZoneLatestIT extends BaseJDBCWithSharedConnectionIT { @Parameterized.Parameters(name = "format={0}, tz={1}") public static Collection data() { // all tests in this class need to run for both query result formats json and arrow @@ -53,9 +53,8 @@ public ResultSetMultiTimeZoneLatestIT(String queryResultFormat, String timeZone) System.setProperty("user.timezone", timeZone); } - public Connection init() throws SQLException { - Connection connection = BaseJDBCTest.getConnection(); - + @Before + public void init() throws SQLException { try (Statement statement = connection.createStatement()) { statement.execute( "alter session set " @@ -67,7 +66,6 @@ public Connection init() throws SQLException { + "TIMESTAMP_NTZ_OUTPUT_FORMAT='DY, DD MON YYYY HH24:MI:SS TZHTZM'"); statement.execute("alter session set jdbc_query_result_format = '" + queryResultFormat + "'"); } - return connection; } /** @@ -78,8 +76,7 @@ public Connection init() throws SQLException { */ @Test public void testTimesWithGetTimestamp() throws SQLException { - try (Connection connection = init(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { String timeStringValue = "10:30:50.123456789"; String timestampStringValue = "1970-01-01 " + timeStringValue; int length = timestampStringValue.length(); @@ -113,8 +110,7 @@ public void testTimesWithGetTimestamp() throws SQLException { */ @Test public void testTimestampNTZWithDaylightSavings() throws SQLException { - try (Connection connection = init(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { statement.execute( "alter session set TIMESTAMP_TYPE_MAPPING='TIMESTAMP_NTZ'," + "TIMEZONE='Europe/London'"); try (ResultSet rs = statement.executeQuery("select TIMESTAMP '2011-09-04 00:00:00'")) { @@ -135,8 +131,7 @@ public void testDateAndTimestampWithTimezone() throws SQLException { Calendar cal = null; SimpleDateFormat sdf = null; - try (Connection connection = init(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { statement.execute("alter session set JDBC_FORMAT_DATE_WITH_TIMEZONE=true"); try (ResultSet rs = statement.executeQuery( @@ -232,8 +227,7 @@ public void testUseSessionTimeZoneOverrides() throws SQLException { * @throws SQLException */ private void testUseSessionTimeZoneHelper(boolean useDefaultParamSettings) throws SQLException { - try (Connection connection = init(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { try { // create table with all timestamp types, time, and date statement.execute( diff --git a/src/test/java/net/snowflake/client/jdbc/ResultSetVectorLatestIT.java b/src/test/java/net/snowflake/client/jdbc/ResultSetVectorLatestIT.java index bbc145516..bc553b2f6 100644 --- a/src/test/java/net/snowflake/client/jdbc/ResultSetVectorLatestIT.java +++ b/src/test/java/net/snowflake/client/jdbc/ResultSetVectorLatestIT.java @@ -5,7 +5,6 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -import java.sql.Connection; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; @@ -44,8 +43,7 @@ public static List queryResultFormats() { @Test public void testGetIntVectorAsIntArray() throws SQLException { - try (Connection con = BaseJDBCTest.getConnection(); - Statement stmt = con.createStatement()) { + try (Statement stmt = connection.createStatement()) { enforceQueryResultFormat(stmt); Integer[] vector = {-1, 5}; try (ResultSet resultSet = stmt.executeQuery("select " + vectorToString(vector, "int"))) { @@ -60,8 +58,7 @@ public void testGetIntVectorAsIntArray() throws SQLException { @Test public void testGetIntVectorAsLongArray() throws SQLException { - try (Connection con = BaseJDBCTest.getConnection(); - Statement stmt = con.createStatement()) { + try (Statement stmt = connection.createStatement()) { enforceQueryResultFormat(stmt); Long[] vector = {-1L, 5L}; try (ResultSet resultSet = stmt.executeQuery("select " + vectorToString(vector, "int"))) { @@ -75,8 +72,7 @@ public void testGetIntVectorAsLongArray() throws SQLException { @Test public void testGetFloatVectorAsFloatArray() throws SQLException { - try (Connection con = BaseJDBCTest.getConnection(); - Statement stmt = con.createStatement()) { + try (Statement stmt = connection.createStatement()) { enforceQueryResultFormat(stmt); Float[] vector = {-1.2f, 5.1f, 15.87f}; try (ResultSet resultSet = stmt.executeQuery("select " + vectorToString(vector, "float"))) { @@ -90,8 +86,7 @@ public void testGetFloatVectorAsFloatArray() throws SQLException { @Test public void testGetNullAsIntVector() throws SQLException { - try (Connection con = BaseJDBCTest.getConnection(); - Statement stmt = con.createStatement()) { + try (Statement stmt = connection.createStatement()) { enforceQueryResultFormat(stmt); try (ResultSet resultSet = stmt.executeQuery("select null::vector(int, 2)")) { assertTrue(resultSet.next()); @@ -105,8 +100,7 @@ public void testGetNullAsIntVector() throws SQLException { @Test public void testGetNullAsFloatVector() throws SQLException { - try (Connection con = BaseJDBCTest.getConnection(); - Statement stmt = con.createStatement()) { + try (Statement stmt = connection.createStatement()) { enforceQueryResultFormat(stmt); try (ResultSet resultSet = stmt.executeQuery("select null::vector(float, 2)")) { assertTrue(resultSet.next()); @@ -120,8 +114,7 @@ public void testGetNullAsFloatVector() throws SQLException { @Test public void testGetIntVectorFromTable() throws SQLException { - try (Connection con = BaseJDBCTest.getConnection(); - Statement stmt = con.createStatement()) { + try (Statement stmt = connection.createStatement()) { enforceQueryResultFormat(stmt); stmt.execute("create or replace table test_vector_int(x vector(int, 2), y int)"); stmt.execute("insert into test_vector_int select [3, 7]::vector(int, 2), 15"); @@ -137,8 +130,7 @@ public void testGetIntVectorFromTable() throws SQLException { @Test public void testGetFloatVectorFromTable() throws SQLException { - try (Connection con = BaseJDBCTest.getConnection(); - Statement stmt = con.createStatement()) { + try (Statement stmt = connection.createStatement()) { enforceQueryResultFormat(stmt); stmt.execute("create or replace table test_vector_float(x vector(float, 2), y float)"); stmt.execute("insert into test_vector_float select [-3, 7.1]::vector(float, 2), 20.3"); @@ -154,8 +146,7 @@ public void testGetFloatVectorFromTable() throws SQLException { /** Added in > 3.16.1 */ @Test public void testGetVectorViaGetStringIsEqualToTheGetObject() throws SQLException { - try (Connection con = BaseJDBCTest.getConnection(); - Statement stmt = con.createStatement()) { + try (Statement stmt = connection.createStatement()) { enforceQueryResultFormat(stmt); Integer[] intVector = {-1, 5}; Float[] floatVector = {-1.2f, 5.1f, 15.87f}; diff --git a/src/test/java/net/snowflake/client/jdbc/StatementIT.java b/src/test/java/net/snowflake/client/jdbc/StatementIT.java index be5f65a56..eb3411213 100644 --- a/src/test/java/net/snowflake/client/jdbc/StatementIT.java +++ b/src/test/java/net/snowflake/client/jdbc/StatementIT.java @@ -36,7 +36,7 @@ /** Statement tests */ @Category(TestCategoryStatement.class) -public class StatementIT extends BaseJDBCTest { +public class StatementIT extends BaseJDBCWithSharedConnectionIT { protected static String queryResultFormat = "json"; public static Connection getConnection() throws SQLException { @@ -51,8 +51,7 @@ public static Connection getConnection() throws SQLException { @Test public void testFetchDirection() throws SQLException { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { assertEquals(ResultSet.FETCH_FORWARD, statement.getFetchDirection()); try { statement.setFetchDirection(ResultSet.FETCH_REVERSE); @@ -65,8 +64,7 @@ public void testFetchDirection() throws SQLException { @Ignore("Not working for setFetchSize") @Test public void testFetchSize() throws SQLException { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { assertEquals(50, statement.getFetchSize()); statement.setFetchSize(1); ResultSet rs = statement.executeQuery("select * from JDBC_STATEMENT"); @@ -76,8 +74,7 @@ public void testFetchSize() throws SQLException { @Test public void testMaxRows() throws SQLException { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { String sqlSelect = "select seq4() from table(generator(rowcount=>3))"; assertEquals(0, statement.getMaxRows()); @@ -100,8 +97,7 @@ public void testMaxRows() throws SQLException { @Test public void testQueryTimeOut() throws SQLException { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { assertEquals(0, statement.getQueryTimeout()); statement.setQueryTimeout(5); assertEquals(5, statement.getQueryTimeout()); @@ -117,8 +113,7 @@ public void testQueryTimeOut() throws SQLException { @Test public void testStatementClose() throws SQLException { - try (Connection connection = getConnection()) { - Statement statement = connection.createStatement(); + try (Statement statement = connection.createStatement(); ) { assertEquals(connection, statement.getConnection()); assertTrue(!statement.isClosed()); statement.close(); @@ -128,8 +123,7 @@ public void testStatementClose() throws SQLException { @Test public void testExecuteSelect() throws SQLException { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { String sqlSelect = "select seq4() from table(generator(rowcount=>3))"; boolean success = statement.execute(sqlSelect); @@ -154,8 +148,7 @@ public void testExecuteSelect() throws SQLException { @Test public void testExecuteInsert() throws SQLException { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { try { statement.execute("create or replace table test_insert(cola number)"); @@ -189,8 +182,7 @@ public void testExecuteInsert() throws SQLException { @Test public void testExecuteUpdateAndDelete() throws SQLException { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { try { statement.execute( "create or replace table test_update(cola number, colb string) " @@ -226,8 +218,7 @@ public void testExecuteUpdateAndDelete() throws SQLException { @Test public void testExecuteMerge() throws SQLException { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { String mergeSQL = "merge into target using source on target.id = source.id " + "when matched and source.sb =22 then update set ta = 'newStr' " @@ -259,8 +250,7 @@ public void testExecuteMerge() throws SQLException { */ @Test public void testAutogenerateKey() throws Throwable { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { statement.execute("create or replace table t(c1 int)"); statement.execute("insert into t values(1)", Statement.NO_GENERATED_KEYS); try { @@ -278,8 +268,7 @@ public void testAutogenerateKey() throws Throwable { @Test public void testExecuteMultiInsert() throws SQLException { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { String multiInsertionSQL = " insert all " + "into foo " @@ -314,8 +303,7 @@ public void testExecuteMultiInsert() throws SQLException { @Test public void testExecuteBatch() throws Exception { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { try { connection.setAutoCommit(false); // mixed of ddl/dml in batch @@ -388,8 +376,7 @@ public void testExecuteBatch() throws Exception { @Test public void testExecuteLargeBatch() throws SQLException { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { /** * Generate a table with several rows and 1 column named test_large_batch Note: to truly test * that executeLargeBatch works with a number of rows greater than MAX_INT, replace rowcount @@ -477,8 +464,7 @@ public void testExecuteUpdateZeroCount() throws SQLException { @Test public void testExecuteUpdateFail() throws Exception { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { String[] testCommands = { "list @~", "ls @~", @@ -506,8 +492,7 @@ public void testExecuteUpdateFail() throws Exception { @Test public void testTelemetryBatch() throws SQLException { Telemetry telemetryClient = null; - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { String sqlSelect = "select seq4() from table(generator(rowcount=>3))"; statement.execute(sqlSelect); @@ -539,8 +524,7 @@ public void testTelemetryBatch() throws SQLException { @Test public void testMultiStmtNotEnabled() throws SQLException { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { String multiStmtQuery = "create or replace temporary table test_multi (cola int);\n" + "insert into test_multi VALUES (1), (2);\n" @@ -557,8 +541,7 @@ public void testMultiStmtNotEnabled() throws SQLException { @Test public void testCallStoredProcedure() throws SQLException { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { statement.execute( "create or replace procedure SP()\n" + "returns string not null\n" @@ -609,8 +592,7 @@ public void testCreateStatementWithParameters() throws Throwable { @Test public void testUnwrapper() throws Throwable { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { if (statement.isWrapperFor(SnowflakeStatementV1.class)) { statement.execute("select 1"); SnowflakeStatement sfstatement = statement.unwrap(SnowflakeStatement.class); @@ -629,8 +611,7 @@ public void testUnwrapper() throws Throwable { @Test public void testQueryIdIsNullOnFreshStatement() throws SQLException { - try (Connection con = getConnection(); - Statement stmt = con.createStatement()) { + try (Statement stmt = connection.createStatement()) { assertNull(stmt.unwrap(SnowflakeStatement.class).getQueryID()); } } diff --git a/src/test/java/net/snowflake/client/jdbc/StatementLatestIT.java b/src/test/java/net/snowflake/client/jdbc/StatementLatestIT.java index d37d88118..9d96f44ea 100644 --- a/src/test/java/net/snowflake/client/jdbc/StatementLatestIT.java +++ b/src/test/java/net/snowflake/client/jdbc/StatementLatestIT.java @@ -41,7 +41,7 @@ * the latest and oldest supported driver run the tests. */ @Category(TestCategoryStatement.class) -public class StatementLatestIT extends BaseJDBCTest { +public class StatementLatestIT extends BaseJDBCWithSharedConnectionIT { protected static String queryResultFormat = "json"; public static Connection getConnection() throws SQLException { @@ -56,8 +56,7 @@ public static Connection getConnection() throws SQLException { @Test public void testExecuteCreateAndDrop() throws SQLException { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { boolean success = statement.execute("create or replace table test_create(colA integer)"); assertFalse(success); @@ -178,25 +177,23 @@ public void testCopyAndUpload() throws Exception { */ @Test public void testExecuteOpenResultSets() throws SQLException { - try (Connection con = getConnection()) { - try (Statement statement = con.createStatement()) { - for (int i = 0; i < 10; i++) { - statement.execute("select 1"); - statement.getResultSet(); - } - - assertEquals(9, statement.unwrap(SnowflakeStatementV1.class).getOpenResultSets().size()); + try (Statement statement = connection.createStatement()) { + for (int i = 0; i < 10; i++) { + statement.execute("select 1"); + statement.getResultSet(); } - try (Statement statement = con.createStatement()) { - for (int i = 0; i < 10; i++) { - statement.execute("select 1"); - ResultSet resultSet = statement.getResultSet(); - resultSet.close(); - } + assertEquals(9, statement.unwrap(SnowflakeStatementV1.class).getOpenResultSets().size()); + } - assertEquals(0, statement.unwrap(SnowflakeStatementV1.class).getOpenResultSets().size()); + try (Statement statement = connection.createStatement()) { + for (int i = 0; i < 10; i++) { + statement.execute("select 1"); + ResultSet resultSet = statement.getResultSet(); + resultSet.close(); } + + assertEquals(0, statement.unwrap(SnowflakeStatementV1.class).getOpenResultSets().size()); } } @@ -246,8 +243,7 @@ public void testSchemaWith255CharactersDoesNotCauseException() throws SQLExcepti String schemaName = TestUtil.GENERATED_SCHEMA_PREFIX + SnowflakeUtil.randomAlphaNumeric(255 - TestUtil.GENERATED_SCHEMA_PREFIX.length()); - try (Connection con = getConnection(); - Statement stmt = con.createStatement()) { + try (Statement stmt = connection.createStatement()) { stmt.execute("create schema " + schemaName); stmt.execute("use schema " + schemaName); stmt.execute("drop schema " + schemaName); @@ -257,8 +253,7 @@ public void testSchemaWith255CharactersDoesNotCauseException() throws SQLExcepti /** Added in > 3.14.4 */ @Test public void testQueryIdIsSetOnFailedQueryExecute() throws SQLException { - try (Connection con = getConnection(); - Statement stmt = con.createStatement()) { + try (Statement stmt = connection.createStatement()) { assertNull(stmt.unwrap(SnowflakeStatement.class).getQueryID()); try { stmt.execute("use database not_existing_database"); @@ -274,8 +269,7 @@ public void testQueryIdIsSetOnFailedQueryExecute() throws SQLException { /** Added in > 3.14.4 */ @Test public void testQueryIdIsSetOnFailedExecuteUpdate() throws SQLException { - try (Connection con = getConnection(); - Statement stmt = con.createStatement()) { + try (Statement stmt = connection.createStatement()) { assertNull(stmt.unwrap(SnowflakeStatement.class).getQueryID()); try { stmt.executeUpdate("update not_existing_table set a = 1 where id = 42"); @@ -291,8 +285,7 @@ public void testQueryIdIsSetOnFailedExecuteUpdate() throws SQLException { /** Added in > 3.14.4 */ @Test public void testQueryIdIsSetOnFailedExecuteQuery() throws SQLException { - try (Connection con = getConnection(); - Statement stmt = con.createStatement()) { + try (Statement stmt = connection.createStatement()) { assertNull(stmt.unwrap(SnowflakeStatement.class).getQueryID()); try { stmt.executeQuery("select * from not_existing_table");