Skip to content

Commit

Permalink
SNOW-1213120: Reuse connections in tests 2 (#1816)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-jf authored Jul 30, 2024
1 parent b33b67e commit d199eda
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down
160 changes: 77 additions & 83 deletions src/test/java/net/snowflake/client/jdbc/ResultSetAsyncIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> 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')");
Expand Down Expand Up @@ -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')");
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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, "");
Expand Down Expand Up @@ -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");
}
}
}
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,29 +14,26 @@
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);
}
}

@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 {
Expand Down
Loading

0 comments on commit d199eda

Please sign in to comment.