diff --git a/src/test/java/net/snowflake/client/jdbc/ResultSetAlreadyClosedIT.java b/src/test/java/net/snowflake/client/jdbc/ResultSetAlreadyClosedIT.java index d2939cc8a..82f3c3244 100644 --- a/src/test/java/net/snowflake/client/jdbc/ResultSetAlreadyClosedIT.java +++ b/src/test/java/net/snowflake/client/jdbc/ResultSetAlreadyClosedIT.java @@ -7,7 +7,6 @@ import static org.junit.Assert.assertTrue; import java.math.BigDecimal; -import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.ResultSet; import java.sql.SQLException; @@ -18,11 +17,11 @@ import org.junit.experimental.categories.Category; @Category(TestCategoryResultSet.class) -public class ResultSetAlreadyClosedIT extends BaseJDBCTest { +public class ResultSetAlreadyClosedIT extends BaseJDBCWithSharedConnectionIT { + @Test public void testQueryResultSetAlreadyClosed() throws Throwable { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { ResultSet resultSet = statement.executeQuery("select 1"); resultSet.close(); checkAlreadyClosed(resultSet); @@ -31,23 +30,20 @@ public void testQueryResultSetAlreadyClosed() throws Throwable { @Test public void testMetadataResultSetAlreadyClosed() throws Throwable { - try (Connection connection = getConnection()) { - String database = connection.getCatalog(); - String schema = connection.getSchema(); - DatabaseMetaData metaData = connection.getMetaData(); + String database = connection.getCatalog(); + String schema = connection.getSchema(); + DatabaseMetaData metaData = connection.getMetaData(); - checkAlreadyClosed(metaData.getCatalogs()); - checkAlreadyClosed(metaData.getSchemas()); - checkAlreadyClosed(metaData.getSchemas(database, null)); - checkAlreadyClosed(metaData.getTables(database, schema, null, null)); - checkAlreadyClosed(metaData.getColumns(database, schema, null, null)); - } + checkAlreadyClosed(metaData.getCatalogs()); + checkAlreadyClosed(metaData.getSchemas()); + checkAlreadyClosed(metaData.getSchemas(database, null)); + checkAlreadyClosed(metaData.getTables(database, schema, null, null)); + checkAlreadyClosed(metaData.getColumns(database, schema, null, null)); } @Test public void testResultSetAlreadyClosed() throws Throwable { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement(); + try (Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT 1")) { checkAlreadyClosed(resultSet); } diff --git a/src/test/java/net/snowflake/client/jdbc/ResultSetAsyncIT.java b/src/test/java/net/snowflake/client/jdbc/ResultSetAsyncIT.java index 1351ea4f1..a6a63a65d 100644 --- a/src/test/java/net/snowflake/client/jdbc/ResultSetAsyncIT.java +++ b/src/test/java/net/snowflake/client/jdbc/ResultSetAsyncIT.java @@ -35,13 +35,13 @@ /** Test AsyncResultSet */ @Category(TestCategoryResultSet.class) -public class ResultSetAsyncIT extends BaseJDBCTest { +public class ResultSetAsyncIT extends BaseJDBCWithSharedConnectionIT { + @Test public void testAsyncResultSetFunctionsWithNewSession() throws SQLException { final Map params = getConnectionParameters(); String queryID = null; - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { try { statement.execute("create or replace table test_rsmd(colA number(20, 5), colB string)"); statement.execute("insert into test_rsmd values(1.00, 'str'),(2.00, 'str2')"); @@ -97,8 +97,7 @@ public void testAsyncResultSetFunctionsWithNewSession() throws SQLException { @Test public void testResultSetMetadata() throws SQLException { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { try { statement.execute("create or replace table test_rsmd(colA number(20, 5), colB string)"); statement.execute("insert into test_rsmd values(1.00, 'str'),(2.00, 'str2')"); @@ -148,8 +147,7 @@ public void testResultSetMetadata() throws SQLException { public void testOrderAndClosureFunctions() throws SQLException { // Set up environment String queryID = null; - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { statement.execute("create or replace table test_rsmd(colA number(20, 5), colB string)"); statement.execute("insert into test_rsmd values(1.00, 'str'),(2.00, 'str2')"); try { @@ -192,8 +190,7 @@ public void testOrderAndClosureFunctions() throws SQLException { @Test public void testWasNull() throws SQLException { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement()) { + try (Statement statement = connection.createStatement()) { try { Clob emptyClob = connection.createClob(); emptyClob.setString(1, ""); @@ -244,90 +241,88 @@ public void testGetMethods() throws Throwable { Time time = new Time(500); Timestamp ts = new Timestamp(333); - try (Connection connection = getConnection()) { - Clob clob = connection.createClob(); - clob.setString(1, "hello world"); - try (Statement statement = connection.createStatement()) { - try { - // TODO structuredType - add to test when WRITE is ready - SNOW-1157904 - statement.execute( - "create or replace table test_get(colA integer, colB number, colC number, colD string, colE double, colF float, colG boolean, colH text, colI binary(3), colJ number(38,9), colK int, colL date, colM time, colN timestamp_ltz)"); + Clob clob = connection.createClob(); + clob.setString(1, "hello world"); + try (Statement statement = connection.createStatement()) { + try { + // TODO structuredType - add to test when WRITE is ready - SNOW-1157904 + statement.execute( + "create or replace table test_get(colA integer, colB number, colC number, colD string, colE double, colF float, colG boolean, colH text, colI binary(3), colJ number(38,9), colK int, colL date, colM time, colN timestamp_ltz)"); - try (PreparedStatement prepStatement = connection.prepareStatement(prepInsertString)) { - prepStatement.setInt(1, bigInt); - prepStatement.setLong(2, bigLong); - prepStatement.setLong(3, bigShort); - prepStatement.setString(4, str); - prepStatement.setDouble(5, bigDouble); - prepStatement.setFloat(6, bigFloat); - prepStatement.setBoolean(7, true); - prepStatement.setClob(8, clob); - prepStatement.setBytes(9, bytes); - prepStatement.setBigDecimal(10, bigDecimal); - prepStatement.setByte(11, oneByte); - prepStatement.setDate(12, date); - prepStatement.setTime(13, time); - prepStatement.setTimestamp(14, ts); - prepStatement.execute(); + try (PreparedStatement prepStatement = connection.prepareStatement(prepInsertString)) { + prepStatement.setInt(1, bigInt); + prepStatement.setLong(2, bigLong); + prepStatement.setLong(3, bigShort); + prepStatement.setString(4, str); + prepStatement.setDouble(5, bigDouble); + prepStatement.setFloat(6, bigFloat); + prepStatement.setBoolean(7, true); + prepStatement.setClob(8, clob); + prepStatement.setBytes(9, bytes); + prepStatement.setBigDecimal(10, bigDecimal); + prepStatement.setByte(11, oneByte); + prepStatement.setDate(12, date); + prepStatement.setTime(13, time); + prepStatement.setTimestamp(14, ts); + prepStatement.execute(); - try (ResultSet resultSet = - statement - .unwrap(SnowflakeStatement.class) - .executeAsyncQuery("select * from test_get")) { - resultSet.next(); - assertEquals(bigInt, resultSet.getInt(1)); - assertEquals(bigInt, resultSet.getInt("COLA")); - assertEquals(bigLong, resultSet.getLong(2)); - assertEquals(bigLong, resultSet.getLong("COLB")); - assertEquals(bigShort, resultSet.getShort(3)); - assertEquals(bigShort, resultSet.getShort("COLC")); - assertEquals(str, resultSet.getString(4)); - assertEquals(str, resultSet.getString("COLD")); - Reader reader = resultSet.getCharacterStream("COLD"); - char[] sample = new char[str.length()]; + try (ResultSet resultSet = + statement + .unwrap(SnowflakeStatement.class) + .executeAsyncQuery("select * from test_get")) { + resultSet.next(); + assertEquals(bigInt, resultSet.getInt(1)); + assertEquals(bigInt, resultSet.getInt("COLA")); + assertEquals(bigLong, resultSet.getLong(2)); + assertEquals(bigLong, resultSet.getLong("COLB")); + assertEquals(bigShort, resultSet.getShort(3)); + assertEquals(bigShort, resultSet.getShort("COLC")); + assertEquals(str, resultSet.getString(4)); + assertEquals(str, resultSet.getString("COLD")); + Reader reader = resultSet.getCharacterStream("COLD"); + char[] sample = new char[str.length()]; - assertEquals(str.length(), reader.read(sample)); - assertEquals(str.charAt(0), sample[0]); - assertEquals(str, new String(sample)); + assertEquals(str.length(), reader.read(sample)); + assertEquals(str.charAt(0), sample[0]); + assertEquals(str, new String(sample)); - assertEquals(bigDouble, resultSet.getDouble(5), 0); - assertEquals(bigDouble, resultSet.getDouble("COLE"), 0); - assertEquals(bigFloat, resultSet.getFloat(6), 0); - assertEquals(bigFloat, resultSet.getFloat("COLF"), 0); - assertTrue(resultSet.getBoolean(7)); - assertTrue(resultSet.getBoolean("COLG")); - assertEquals("hello world", resultSet.getClob("COLH").toString()); + assertEquals(bigDouble, resultSet.getDouble(5), 0); + assertEquals(bigDouble, resultSet.getDouble("COLE"), 0); + assertEquals(bigFloat, resultSet.getFloat(6), 0); + assertEquals(bigFloat, resultSet.getFloat("COLF"), 0); + assertTrue(resultSet.getBoolean(7)); + assertTrue(resultSet.getBoolean("COLG")); + assertEquals("hello world", resultSet.getClob("COLH").toString()); - // TODO: figure out why getBytes returns an offset. - // assertEquals(bytes, resultSet.getBytes(9)); - // assertEquals(bytes, resultSet.getBytes("COLI")); + // TODO: figure out why getBytes returns an offset. + // assertEquals(bytes, resultSet.getBytes(9)); + // assertEquals(bytes, resultSet.getBytes("COLI")); - DecimalFormat df = new DecimalFormat("#.00"); - assertEquals(df.format(bigDecimal), df.format(resultSet.getBigDecimal(10))); - assertEquals(df.format(bigDecimal), df.format(resultSet.getBigDecimal("COLJ"))); + DecimalFormat df = new DecimalFormat("#.00"); + assertEquals(df.format(bigDecimal), df.format(resultSet.getBigDecimal(10))); + assertEquals(df.format(bigDecimal), df.format(resultSet.getBigDecimal("COLJ"))); - assertEquals(oneByte, resultSet.getByte(11)); - assertEquals(oneByte, resultSet.getByte("COLK")); + assertEquals(oneByte, resultSet.getByte(11)); + assertEquals(oneByte, resultSet.getByte("COLK")); - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); - assertEquals(sdf.format(date), sdf.format(resultSet.getDate(12))); - assertEquals(sdf.format(date), sdf.format(resultSet.getDate("COLL"))); - assertEquals(time, resultSet.getTime(13)); - assertEquals(time, resultSet.getTime("COLM")); - assertEquals(ts, resultSet.getTimestamp(14)); - assertEquals(ts, resultSet.getTimestamp("COLN")); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + assertEquals(sdf.format(date), sdf.format(resultSet.getDate(12))); + assertEquals(sdf.format(date), sdf.format(resultSet.getDate("COLL"))); + assertEquals(time, resultSet.getTime(13)); + assertEquals(time, resultSet.getTime("COLM")); + assertEquals(ts, resultSet.getTimestamp(14)); + assertEquals(ts, resultSet.getTimestamp("COLN")); - // test getObject - assertEquals(str, resultSet.getObject(4).toString()); - assertEquals(str, resultSet.getObject("COLD").toString()); + // test getObject + assertEquals(str, resultSet.getObject(4).toString()); + assertEquals(str, resultSet.getObject("COLD").toString()); - // test getStatement method - assertEquals(statement, resultSet.getStatement()); - } + // test getStatement method + assertEquals(statement, resultSet.getStatement()); } - } finally { - statement.execute("drop table if exists table_get"); } + } finally { + statement.execute("drop table if exists table_get"); } } } @@ -343,8 +338,7 @@ public void testGetMethods() throws Throwable { */ @Test public void testEmptyResultSet() throws SQLException { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement(); + try (Statement statement = connection.createStatement(); ResultSet rs = statement .unwrap(SnowflakeStatement.class) diff --git a/src/test/java/net/snowflake/client/jdbc/ResultSetFeatureNotSupportedIT.java b/src/test/java/net/snowflake/client/jdbc/ResultSetFeatureNotSupportedIT.java index e71f69d1a..423661c77 100644 --- a/src/test/java/net/snowflake/client/jdbc/ResultSetFeatureNotSupportedIT.java +++ b/src/test/java/net/snowflake/client/jdbc/ResultSetFeatureNotSupportedIT.java @@ -1,7 +1,6 @@ package net.snowflake.client.jdbc; import java.math.BigDecimal; -import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.Date; import java.sql.ResultSet; @@ -15,11 +14,10 @@ import org.junit.experimental.categories.Category; @Category(TestCategoryResultSet.class) -public class ResultSetFeatureNotSupportedIT extends BaseJDBCTest { +public class ResultSetFeatureNotSupportedIT extends BaseJDBCWithSharedConnectionIT { @Test public void testQueryResultSetNotSupportedException() throws Throwable { - try (Connection connection = getConnection(); - Statement statement = connection.createStatement(); + try (Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("select 1")) { checkFeatureNotSupportedException(resultSet); } @@ -27,17 +25,15 @@ public void testQueryResultSetNotSupportedException() throws Throwable { @Test public void testMetadataResultSetNotSupportedException() throws Throwable { - try (Connection connection = getConnection()) { - DatabaseMetaData metaData = connection.getMetaData(); - String database = connection.getCatalog(); - String schema = connection.getSchema(); - - checkFeatureNotSupportedException(metaData.getCatalogs()); - checkFeatureNotSupportedException(metaData.getSchemas()); - checkFeatureNotSupportedException(metaData.getSchemas(database, null)); - checkFeatureNotSupportedException(metaData.getTables(database, schema, null, null)); - checkFeatureNotSupportedException(metaData.getColumns(database, schema, null, null)); - } + DatabaseMetaData metaData = connection.getMetaData(); + String database = connection.getCatalog(); + String schema = connection.getSchema(); + + checkFeatureNotSupportedException(metaData.getCatalogs()); + checkFeatureNotSupportedException(metaData.getSchemas()); + checkFeatureNotSupportedException(metaData.getSchemas(database, null)); + checkFeatureNotSupportedException(metaData.getTables(database, schema, null, null)); + checkFeatureNotSupportedException(metaData.getColumns(database, schema, null, null)); } private void checkFeatureNotSupportedException(ResultSet resultSet) throws SQLException { diff --git a/src/test/java/net/snowflake/client/jdbc/ResultSetJsonVsArrowMultiTZIT.java b/src/test/java/net/snowflake/client/jdbc/ResultSetJsonVsArrowMultiTZIT.java index 6add203f5..f62e7701c 100644 --- a/src/test/java/net/snowflake/client/jdbc/ResultSetJsonVsArrowMultiTZIT.java +++ b/src/test/java/net/snowflake/client/jdbc/ResultSetJsonVsArrowMultiTZIT.java @@ -7,7 +7,6 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; @@ -16,6 +15,7 @@ import java.util.List; import net.snowflake.client.category.TestCategoryArrow; import org.apache.commons.lang3.StringUtils; +import org.junit.Before; import org.junit.Test; import org.junit.experimental.categories.Category; import org.junit.runner.RunWith; @@ -24,7 +24,7 @@ /** Completely compare json and arrow resultSet behaviors */ @RunWith(Parameterized.class) @Category(TestCategoryArrow.class) -public class ResultSetJsonVsArrowMultiTZIT extends BaseJDBCTest { +public class ResultSetJsonVsArrowMultiTZIT 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 @@ -42,9 +42,8 @@ public static Collection data() { private final String queryResultFormat; private final String tz; - public static Connection getConnection(int injectSocketTimeout) throws SQLException { - Connection connection = BaseJDBCTest.getConnection(injectSocketTimeout); - + @Before + public void setSessionTimezone() throws SQLException { try (Statement statement = connection.createStatement()) { statement.execute( "alter session set " @@ -55,7 +54,6 @@ public static Connection getConnection(int injectSocketTimeout) throws SQLExcept + "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; } public ResultSetJsonVsArrowMultiTZIT(String queryResultFormat, String timeZone) { @@ -64,14 +62,12 @@ public ResultSetJsonVsArrowMultiTZIT(String queryResultFormat, String timeZone) tz = timeZone; } - private Connection init(String table, String column, String values) throws SQLException { - Connection con = getConnection(BaseJDBCTest.DONT_INJECT_SOCKET_TIMEOUT); - try (Statement statement = con.createStatement()) { + private void init(String table, String column, String values) throws SQLException { + try (Statement statement = connection.createStatement()) { statement.execute("alter session set jdbc_query_result_format = '" + queryResultFormat + "'"); statement.execute("create or replace table " + table + " " + column); statement.execute("insert into " + table + " values " + values); } - return con; } @Test @@ -112,8 +108,8 @@ public void testDate() throws Exception { String column = "(a date)"; String values = "('" + StringUtils.join(cases, "'),('") + "'), (null)"; - try (Connection con = init(table, column, values); - Statement statement = con.createStatement()) { + init(table, column, values); + try (Statement statement = connection.createStatement()) { try (ResultSet rs = statement.executeQuery("select * from " + table)) { int i = 0; while (i < cases.length) { @@ -137,9 +133,8 @@ public void testTimeWithScale(String[] times, int scale) throws SQLException { String table = "test_arrow_time"; String column = "(a time(" + scale + "))"; String values = "('" + StringUtils.join(times, "'),('") + "'), (null)"; - - try (Connection con = init(table, column, values); - Statement statement = con.createStatement(); + init(table, column, values); + try (Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery("select * from " + table)) { for (int i = 0; i < times.length; i++) { assertTrue(rs.next()); @@ -186,8 +181,8 @@ public void testTimestampNTZWithScale(int scale) throws SQLException { String column = "(a timestamp_ntz(" + scale + "))"; String values = "('" + StringUtils.join(cases, "'),('") + "'), (null)"; - try (Connection con = init(table, column, values); - Statement statement = con.createStatement()) { + init(table, column, values); + try (Statement statement = connection.createStatement()) { try (ResultSet rs = statement.executeQuery("select * from " + table)) { int i = 0; while (i < cases.length) { @@ -220,8 +215,8 @@ public void testTimestampNTZWithNanos() throws SQLException { String column = "(a timestamp_ntz)"; String values = "('" + StringUtils.join(cases, "'),('") + "'), (null)"; - try (Connection con = init(table, column, values); - Statement statement = con.createStatement()) { + init(table, column, values); + try (Statement statement = connection.createStatement()) { try (ResultSet rs = statement.executeQuery("select * from " + table)) { int i = 0; while (i < cases.length) {