diff --git a/src/main/java/net/snowflake/client/core/ExecTimeTelemetryData.java b/src/main/java/net/snowflake/client/core/ExecTimeTelemetryData.java index a9d40a054..91d45f29f 100644 --- a/src/main/java/net/snowflake/client/core/ExecTimeTelemetryData.java +++ b/src/main/java/net/snowflake/client/core/ExecTimeTelemetryData.java @@ -154,7 +154,7 @@ public String generateTelemetry() { value.put("ProcessResultChunkStart", this.processResultChunk.getStart()); value.put("ProcessResultChunkEnd", this.processResultChunk.getEnd()); value.put("CreateResultSetStart", this.createResultSet.getStart()); - value.put("CreatResultSetEnd", this.createResultSet.getEnd()); + value.put("CreateResultSetEnd", this.createResultSet.getEnd()); value.put("QueryEnd", this.query.getEnd()); value.put("BatchID", this.batchId); value.put("QueryID", this.queryId); diff --git a/src/test/java/net/snowflake/client/TestUtil.java b/src/test/java/net/snowflake/client/TestUtil.java index afed53dd7..76487bcb4 100644 --- a/src/test/java/net/snowflake/client/TestUtil.java +++ b/src/test/java/net/snowflake/client/TestUtil.java @@ -5,9 +5,12 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.sql.SQLException; import java.sql.Statement; import java.util.Arrays; import java.util.List; @@ -128,4 +131,17 @@ public static void withRandomSchema( statement.execute("DROP SCHEMA " + customSchema); } } + + public interface MethodRaisesSQLException { + void run() throws SQLException; + } + + public static void expectSnowflakeLoggedFeatureNotSupportedException(MethodRaisesSQLException f) { + try { + f.run(); + fail("must raise exception"); + } catch (SQLException ex) { + assertEquals(ex.getClass().getSimpleName(), "SnowflakeLoggedFeatureNotSupportedException"); + } + } } diff --git a/src/test/java/net/snowflake/client/config/SFClientConfigParserTest.java b/src/test/java/net/snowflake/client/config/SFClientConfigParserTest.java index 413b732ff..a00784f68 100644 --- a/src/test/java/net/snowflake/client/config/SFClientConfigParserTest.java +++ b/src/test/java/net/snowflake/client/config/SFClientConfigParserTest.java @@ -47,6 +47,7 @@ public void testLoadSFClientConfigValidPath() throws IOException { SFClientConfigParser.loadSFClientConfig(configFilePath.toString()); assertEquals("info", actualConfig.getCommonProps().getLogLevel()); assertEquals("/jdbc.log", actualConfig.getCommonProps().getLogPath()); + assertEquals("config.json", actualConfig.getConfigFilePath()); } @Test diff --git a/src/test/java/net/snowflake/client/core/ExecTimeTelemetryDataTest.java b/src/test/java/net/snowflake/client/core/ExecTimeTelemetryDataTest.java new file mode 100644 index 000000000..f7ad06b46 --- /dev/null +++ b/src/test/java/net/snowflake/client/core/ExecTimeTelemetryDataTest.java @@ -0,0 +1,84 @@ +package net.snowflake.client.core; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import net.minidev.json.JSONObject; +import net.minidev.json.parser.JSONParser; +import net.minidev.json.parser.ParseException; +import net.snowflake.client.jdbc.telemetryOOB.TelemetryService; +import org.junit.Test; + +public class ExecTimeTelemetryDataTest { + + @Test + public void testExecTimeTelemetryData() throws ParseException { + ExecTimeTelemetryData execTimeTelemetryData = new ExecTimeTelemetryData(); + execTimeTelemetryData.sendData = true; + execTimeTelemetryData.setBindStart(); + execTimeTelemetryData.setOCSPStatus(true); + execTimeTelemetryData.setBindEnd(); + execTimeTelemetryData.setHttpClientStart(); + execTimeTelemetryData.setHttpClientEnd(); + execTimeTelemetryData.setGzipStart(); + execTimeTelemetryData.setGzipEnd(); + execTimeTelemetryData.setQueryEnd(); + execTimeTelemetryData.setQueryId("queryid"); + execTimeTelemetryData.setProcessResultChunkStart(); + execTimeTelemetryData.setProcessResultChunkEnd(); + execTimeTelemetryData.setResponseIOStreamStart(); + execTimeTelemetryData.setResponseIOStreamEnd(); + execTimeTelemetryData.setCreateResultSetStart(); + execTimeTelemetryData.setCreateResultSetEnd(); + execTimeTelemetryData.incrementRetryCount(); + execTimeTelemetryData.setRequestId("mockId"); + execTimeTelemetryData.addRetryLocation("retry"); + + String telemetry = execTimeTelemetryData.generateTelemetry(); + JSONParser parser = new JSONParser(JSONParser.MODE_JSON_SIMPLE); + JSONObject json = (JSONObject) parser.parse(telemetry); + assertNotNull(json.get("BindStart")); + assertNotNull(json.get("BindEnd")); + assertEquals(json.get("ocspEnabled"), true); + assertNotNull(json.get("HttpClientStart")); + assertNotNull(json.get("HttpClientEnd")); + assertNotNull(json.get("GzipStart")); + assertNotNull(json.get("GzipEnd")); + assertNotNull(json.get("QueryEnd")); + assertEquals(json.get("QueryID"), "queryid"); + assertNotNull(json.get("ProcessResultChunkStart")); + assertNotNull(json.get("ProcessResultChunkEnd")); + assertNotNull(json.get("ResponseIOStreamStart")); + assertNotNull(json.get("CreateResultSetStart")); + assertNotNull(json.get("CreateResultSetEnd")); + assertNotNull(json.get("ElapsedQueryTime")); + assertNotNull(json.get("ElapsedResultProcessTime")); + assertNull(json.get("QueryFunction")); + assertNull(json.get("BatchID")); + assertEquals(((Long) json.get("RetryCount")).intValue(), 1); + assertEquals(json.get("RequestID"), "mockId"); + assertEquals(json.get("RetryLocations"), "retry"); + assertEquals(json.get("Urgent"), true); + assertEquals(json.get("eventType"), "ExecutionTimeRecord"); + } + + @Test + public void testRetryLocation() throws ParseException { + TelemetryService.enableHTAP(); + ExecTimeTelemetryData execTimeTelemetryData = + new ExecTimeTelemetryData("queryFunction", "batchId"); + execTimeTelemetryData.addRetryLocation("hello"); + execTimeTelemetryData.addRetryLocation("world"); + execTimeTelemetryData.sendData = true; + String telemetry = execTimeTelemetryData.generateTelemetry(); + + JSONParser parser = new JSONParser(JSONParser.MODE_JSON_SIMPLE); + JSONObject json = (JSONObject) parser.parse(telemetry); + assertEquals(json.get("QueryFunction"), "queryFunction"); + assertEquals(json.get("BatchID"), "batchId"); + assertNotNull(json.get("QueryStart")); + assertEquals(json.get("RetryLocations"), "hello, world"); + TelemetryService.disableHTAP(); + } +} diff --git a/src/test/java/net/snowflake/client/core/QueryContextCacheTest.java b/src/test/java/net/snowflake/client/core/QueryContextCacheTest.java index cd841b474..862dd1c40 100644 --- a/src/test/java/net/snowflake/client/core/QueryContextCacheTest.java +++ b/src/test/java/net/snowflake/client/core/QueryContextCacheTest.java @@ -6,6 +6,9 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import org.junit.Test; @@ -217,6 +220,11 @@ public void testSerializeRequestAndDeserializeResponseDataWithNullContext() thro qcc.deserializeQueryContextDTO(requestData); assertCacheDataWithContext(null); + + QueryContextCache mockQcc = spy(qcc); + mockQcc.deserializeQueryContextDTO(null); + verify(mockQcc).clearCache(); + verify(mockQcc, times(2)).logCacheEntries(); } private void assertCacheData() { diff --git a/src/test/java/net/snowflake/client/core/SQLInputOutputTest.java b/src/test/java/net/snowflake/client/core/SQLInputOutputTest.java new file mode 100644 index 000000000..346d43c34 --- /dev/null +++ b/src/test/java/net/snowflake/client/core/SQLInputOutputTest.java @@ -0,0 +1,42 @@ +package net.snowflake.client.core; + +import static net.snowflake.client.TestUtil.expectSnowflakeLoggedFeatureNotSupportedException; +import static org.mockito.Mockito.mock; + +import java.sql.SQLData; +import org.junit.Test; + +public class SQLInputOutputTest { + + @Test + public void testBaseSQLUnSupportedException() { + BaseSqlInput sqlInput = new ArrowSqlInput(null, null, null, null); + expectSnowflakeLoggedFeatureNotSupportedException(sqlInput::readCharacterStream); + expectSnowflakeLoggedFeatureNotSupportedException(sqlInput::readAsciiStream); + expectSnowflakeLoggedFeatureNotSupportedException(sqlInput::readBinaryStream); + expectSnowflakeLoggedFeatureNotSupportedException(sqlInput::readRef); + expectSnowflakeLoggedFeatureNotSupportedException(sqlInput::readBlob); + expectSnowflakeLoggedFeatureNotSupportedException(sqlInput::readClob); + expectSnowflakeLoggedFeatureNotSupportedException(sqlInput::readArray); + expectSnowflakeLoggedFeatureNotSupportedException(sqlInput::readURL); + expectSnowflakeLoggedFeatureNotSupportedException(sqlInput::readNClob); + expectSnowflakeLoggedFeatureNotSupportedException(sqlInput::readNString); + expectSnowflakeLoggedFeatureNotSupportedException(sqlInput::readSQLXML); + expectSnowflakeLoggedFeatureNotSupportedException(sqlInput::readRowId); + } + + @Test + public void testJsonSqlOutPutUnSupportedTest() { + JsonSqlOutput sqloutput = new JsonSqlOutput(mock(SQLData.class), mock(SFBaseSession.class)); + expectSnowflakeLoggedFeatureNotSupportedException(() -> sqloutput.writeRef(null)); + expectSnowflakeLoggedFeatureNotSupportedException(() -> sqloutput.writeBlob(null)); + expectSnowflakeLoggedFeatureNotSupportedException(() -> sqloutput.writeClob(null)); + expectSnowflakeLoggedFeatureNotSupportedException(() -> sqloutput.writeStruct(null)); + expectSnowflakeLoggedFeatureNotSupportedException(() -> sqloutput.writeArray(null)); + expectSnowflakeLoggedFeatureNotSupportedException(() -> sqloutput.writeURL(null)); + expectSnowflakeLoggedFeatureNotSupportedException(() -> sqloutput.writeNString(null)); + expectSnowflakeLoggedFeatureNotSupportedException(() -> sqloutput.writeNClob(null)); + expectSnowflakeLoggedFeatureNotSupportedException(() -> sqloutput.writeRowId(null)); + expectSnowflakeLoggedFeatureNotSupportedException(() -> sqloutput.writeSQLXML(null)); + } +} diff --git a/src/test/java/net/snowflake/client/core/bind/BindExceptionTest.java b/src/test/java/net/snowflake/client/core/bind/BindExceptionTest.java new file mode 100644 index 000000000..f3ae88eee --- /dev/null +++ b/src/test/java/net/snowflake/client/core/bind/BindExceptionTest.java @@ -0,0 +1,23 @@ +package net.snowflake.client.core.bind; + +import static org.junit.Assert.assertEquals; + +import net.snowflake.client.jdbc.telemetry.TelemetryField; +import org.junit.Test; + +public class BindExceptionTest { + + @Test + public void testBindExceptionType() { + assertEquals(BindException.Type.SERIALIZATION.field, TelemetryField.FAILED_BIND_SERIALIZATION); + assertEquals(BindException.Type.UPLOAD.field, TelemetryField.FAILED_BIND_UPLOAD); + assertEquals(BindException.Type.OTHER.field, TelemetryField.FAILED_BIND_OTHER); + } + + @Test + public void testBindExceptionConstructor() { + BindException exception = new BindException("testException", BindException.Type.SERIALIZATION); + assertEquals(exception.getMessage(), "testException"); + assertEquals(exception.type.field, TelemetryField.FAILED_BIND_SERIALIZATION); + } +} diff --git a/src/test/java/net/snowflake/client/jdbc/BaseJDBCTest.java b/src/test/java/net/snowflake/client/jdbc/BaseJDBCTest.java index b8bacc82b..a326dea12 100644 --- a/src/test/java/net/snowflake/client/jdbc/BaseJDBCTest.java +++ b/src/test/java/net/snowflake/client/jdbc/BaseJDBCTest.java @@ -35,6 +35,7 @@ import javax.xml.transform.Result; import javax.xml.transform.Source; import net.snowflake.client.AbstractDriverIT; +import net.snowflake.client.core.SFException; public class BaseJDBCTest extends AbstractDriverIT { // Test UUID unique per session @@ -44,6 +45,10 @@ protected interface MethodRaisesSQLException { void run() throws SQLException; } + protected interface MethodRaisesSFException { + void run() throws SFException; + } + protected interface MethodRaisesSQLClientInfoException { void run() throws SQLClientInfoException; } diff --git a/src/test/java/net/snowflake/client/jdbc/ResultSetAlreadyClosedIT.java b/src/test/java/net/snowflake/client/jdbc/ResultSetAlreadyClosedIT.java index 292d71949..d2939cc8a 100644 --- a/src/test/java/net/snowflake/client/jdbc/ResultSetAlreadyClosedIT.java +++ b/src/test/java/net/snowflake/client/jdbc/ResultSetAlreadyClosedIT.java @@ -22,8 +22,9 @@ public class ResultSetAlreadyClosedIT extends BaseJDBCTest { @Test public void testQueryResultSetAlreadyClosed() throws Throwable { try (Connection connection = getConnection(); - Statement statement = connection.createStatement(); - ResultSet resultSet = statement.executeQuery("select 1")) { + Statement statement = connection.createStatement()) { + ResultSet resultSet = statement.executeQuery("select 1"); + resultSet.close(); checkAlreadyClosed(resultSet); } } @@ -44,9 +45,18 @@ public void testMetadataResultSetAlreadyClosed() throws Throwable { } @Test - public void testEmptyResultSetAlreadyClosed() throws Throwable { - try (ResultSet resultSet = new SnowflakeResultSetV1.EmptyResultSet()) { + public void testResultSetAlreadyClosed() throws Throwable { + try (Connection connection = getConnection(); + Statement statement = connection.createStatement(); + ResultSet resultSet = statement.executeQuery("SELECT 1")) { checkAlreadyClosed(resultSet); + } + } + + @Test + public void testEmptyResultSetAlreadyClosed() throws Throwable { + try (SnowflakeResultSetV1.EmptyResultSet resultSet = + new SnowflakeResultSetV1.EmptyResultSet()) { checkAlreadyClosedEmpty(resultSet); } } @@ -68,7 +78,6 @@ private void checkAlreadyClosed(ResultSet resultSet) throws SQLException { expectResultSetAlreadyClosedException(() -> resultSet.getDouble(1)); expectResultSetAlreadyClosedException(() -> resultSet.getBigDecimal(1)); expectResultSetAlreadyClosedException(() -> resultSet.getBytes(1)); - expectResultSetAlreadyClosedException(() -> resultSet.getString(1)); expectResultSetAlreadyClosedException(() -> resultSet.getDate(1)); expectResultSetAlreadyClosedException(() -> resultSet.getTime(1)); expectResultSetAlreadyClosedException(() -> resultSet.getTimestamp(1)); @@ -105,7 +114,13 @@ private void checkAlreadyClosed(ResultSet resultSet) throws SQLException { expectResultSetAlreadyClosedException(() -> resultSet.getBigDecimal("col1", 38)); expectResultSetAlreadyClosedException(resultSet::getWarnings); + expectResultSetAlreadyClosedException( + () -> resultSet.unwrap(SnowflakeBaseResultSet.class).getWarnings()); + expectResultSetAlreadyClosedException(resultSet::clearWarnings); + expectResultSetAlreadyClosedException( + () -> resultSet.unwrap(SnowflakeBaseResultSet.class).clearWarnings()); + expectResultSetAlreadyClosedException(resultSet::getMetaData); expectResultSetAlreadyClosedException(() -> resultSet.findColumn("col1")); @@ -119,11 +134,20 @@ private void checkAlreadyClosed(ResultSet resultSet) throws SQLException { expectResultSetAlreadyClosedException( () -> resultSet.setFetchDirection(ResultSet.FETCH_FORWARD)); expectResultSetAlreadyClosedException(() -> resultSet.setFetchSize(10)); + expectResultSetAlreadyClosedException( + () -> resultSet.unwrap(SnowflakeBaseResultSet.class).setFetchSize(10)); + expectResultSetAlreadyClosedException(resultSet::getFetchDirection); expectResultSetAlreadyClosedException(resultSet::getFetchSize); expectResultSetAlreadyClosedException(resultSet::getType); expectResultSetAlreadyClosedException(resultSet::getConcurrency); + expectResultSetAlreadyClosedException( + resultSet.unwrap(SnowflakeBaseResultSet.class)::getConcurrency); + expectResultSetAlreadyClosedException(resultSet::getHoldability); + expectResultSetAlreadyClosedException( + resultSet.unwrap(SnowflakeBaseResultSet.class)::getHoldability); + expectResultSetAlreadyClosedException(resultSet::getStatement); } @@ -133,7 +157,8 @@ private void checkAlreadyClosed(ResultSet resultSet) throws SQLException { * @param resultSet * @throws SQLException */ - private void checkAlreadyClosedEmpty(ResultSet resultSet) throws SQLException { + private void checkAlreadyClosedEmpty(SnowflakeResultSetV1.EmptyResultSet resultSet) + throws SQLException { resultSet.close(); resultSet.close(); // second close won't raise exception assertTrue(resultSet.isClosed()); diff --git a/src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java b/src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java index add205145..fb55a9780 100644 --- a/src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java +++ b/src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java @@ -3,6 +3,7 @@ */ package net.snowflake.client.jdbc; +import static net.snowflake.client.TestUtil.expectSnowflakeLoggedFeatureNotSupportedException; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertArrayEquals; @@ -27,7 +28,6 @@ import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; -import java.sql.SQLFeatureNotSupportedException; import java.sql.Statement; import java.sql.Time; import java.sql.Timestamp; @@ -808,14 +808,64 @@ public void testCallStatementType() throws SQLException { * implemented for synchronous queries * */ @Test - public void testNewFeaturesNotSupported() throws SQLException { + public void testNewFeaturesNotSupportedExeceptions() throws SQLException { + try (Connection con = init(); + Statement statement = con.createStatement(); + ResultSet rs = statement.executeQuery("select 1")) { + expectSnowflakeLoggedFeatureNotSupportedException( + rs.unwrap(SnowflakeResultSet.class)::getQueryErrorMessage); + expectSnowflakeLoggedFeatureNotSupportedException( + rs.unwrap(SnowflakeResultSet.class)::getStatus); + expectSnowflakeLoggedFeatureNotSupportedException(() -> rs.getArray(1)); + expectSnowflakeLoggedFeatureNotSupportedException( + () -> rs.unwrap(SnowflakeBaseResultSet.class).getList(1, String.class)); + expectSnowflakeLoggedFeatureNotSupportedException( + () -> rs.unwrap(SnowflakeBaseResultSet.class).getArray(1, String.class)); + expectSnowflakeLoggedFeatureNotSupportedException( + () -> rs.unwrap(SnowflakeBaseResultSet.class).getMap(1, String.class)); + + expectSnowflakeLoggedFeatureNotSupportedException( + () -> rs.unwrap(SnowflakeBaseResultSet.class).getUnicodeStream(1)); + expectSnowflakeLoggedFeatureNotSupportedException( + () -> rs.unwrap(SnowflakeBaseResultSet.class).getUnicodeStream("column1")); + expectSnowflakeLoggedFeatureNotSupportedException( + () -> + rs.unwrap(SnowflakeBaseResultSet.class) + .updateAsciiStream("column1", new FakeInputStream(), 5L)); + expectSnowflakeLoggedFeatureNotSupportedException( + () -> + rs.unwrap(SnowflakeBaseResultSet.class) + .updateBinaryStream("column1", new FakeInputStream(), 5L)); + expectSnowflakeLoggedFeatureNotSupportedException( + () -> + rs.unwrap(SnowflakeBaseResultSet.class) + .updateCharacterStream("column1", new FakeReader(), 5L)); + + expectSnowflakeLoggedFeatureNotSupportedException( + () -> + rs.unwrap(SnowflakeBaseResultSet.class) + .updateAsciiStream(1, new FakeInputStream(), 5L)); + expectSnowflakeLoggedFeatureNotSupportedException( + () -> + rs.unwrap(SnowflakeBaseResultSet.class) + .updateBinaryStream(1, new FakeInputStream(), 5L)); + expectSnowflakeLoggedFeatureNotSupportedException( + () -> + rs.unwrap(SnowflakeBaseResultSet.class) + .updateCharacterStream(1, new FakeReader(), 5L)); + } + } + + @Test + public void testInvalidUnWrap() throws SQLException { try (Connection con = init(); ResultSet rs = con.createStatement().executeQuery("select 1")) { try { - rs.unwrap(SnowflakeResultSet.class).getQueryErrorMessage(); - } catch (SQLFeatureNotSupportedException ex) { - // catch SQLFeatureNotSupportedException - assertEquals("This function is only supported for asynchronous queries.", ex.getMessage()); + rs.unwrap(SnowflakeUtil.class); + } catch (SQLException ex) { + assertEquals( + ex.getMessage(), + "net.snowflake.client.jdbc.SnowflakeResultSetV1 not unwrappable from net.snowflake.client.jdbc.SnowflakeUtil"); } } } diff --git a/src/test/java/net/snowflake/client/jdbc/SnowflakeTypeTest.java b/src/test/java/net/snowflake/client/jdbc/SnowflakeTypeTest.java new file mode 100644 index 000000000..29c58b787 --- /dev/null +++ b/src/test/java/net/snowflake/client/jdbc/SnowflakeTypeTest.java @@ -0,0 +1,108 @@ +package net.snowflake.client.jdbc; + +import static net.snowflake.client.jdbc.SnowflakeType.convertStringToType; +import static net.snowflake.client.jdbc.SnowflakeType.getJavaType; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; + +import java.math.BigDecimal; +import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; +import java.sql.Types; +import org.junit.Test; + +public class SnowflakeTypeTest { + + @Test + public void testSnowflakeType() { + assertEquals(getJavaType(SnowflakeType.CHAR, false), SnowflakeType.JavaDataType.JAVA_STRING); + assertEquals(getJavaType(SnowflakeType.INTEGER, false), SnowflakeType.JavaDataType.JAVA_LONG); + assertEquals( + getJavaType(SnowflakeType.FIXED, false), SnowflakeType.JavaDataType.JAVA_BIGDECIMAL); + assertEquals( + getJavaType(SnowflakeType.TIMESTAMP, false), SnowflakeType.JavaDataType.JAVA_TIMESTAMP); + assertEquals(getJavaType(SnowflakeType.TIME, false), SnowflakeType.JavaDataType.JAVA_TIMESTAMP); + assertEquals( + getJavaType(SnowflakeType.TIMESTAMP_LTZ, false), SnowflakeType.JavaDataType.JAVA_TIMESTAMP); + assertEquals( + getJavaType(SnowflakeType.TIMESTAMP_NTZ, false), SnowflakeType.JavaDataType.JAVA_TIMESTAMP); + assertEquals( + getJavaType(SnowflakeType.TIMESTAMP_TZ, false), SnowflakeType.JavaDataType.JAVA_TIMESTAMP); + assertEquals(getJavaType(SnowflakeType.DATE, false), SnowflakeType.JavaDataType.JAVA_TIMESTAMP); + assertEquals( + getJavaType(SnowflakeType.BOOLEAN, false), SnowflakeType.JavaDataType.JAVA_BOOLEAN); + assertEquals(getJavaType(SnowflakeType.VECTOR, false), SnowflakeType.JavaDataType.JAVA_STRING); + assertEquals(getJavaType(SnowflakeType.BINARY, false), SnowflakeType.JavaDataType.JAVA_BYTES); + assertEquals(getJavaType(SnowflakeType.ANY, false), SnowflakeType.JavaDataType.JAVA_OBJECT); + assertEquals(getJavaType(SnowflakeType.OBJECT, true), SnowflakeType.JavaDataType.JAVA_OBJECT); + assertEquals(getJavaType(SnowflakeType.OBJECT, false), SnowflakeType.JavaDataType.JAVA_STRING); + assertEquals( + getJavaType(SnowflakeType.GEOMETRY, false), SnowflakeType.JavaDataType.JAVA_STRING); + } + + @Test + public void testConvertStringToType() { + assertEquals(convertStringToType(null), Types.NULL); + assertEquals(convertStringToType("decimal"), Types.DECIMAL); + assertEquals(convertStringToType("int"), Types.INTEGER); + assertEquals(convertStringToType("integer"), Types.INTEGER); + assertEquals(convertStringToType("byteint"), Types.INTEGER); + assertEquals(convertStringToType("smallint"), Types.SMALLINT); + assertEquals(convertStringToType("bigint"), Types.BIGINT); + assertEquals(convertStringToType("double"), Types.DOUBLE); + assertEquals(convertStringToType("double precision"), Types.DOUBLE); + assertEquals(convertStringToType("real"), Types.REAL); + assertEquals(convertStringToType("char"), Types.CHAR); + assertEquals(convertStringToType("character"), Types.CHAR); + assertEquals(convertStringToType("varbinary"), Types.VARBINARY); + assertEquals(convertStringToType("boolean"), Types.BOOLEAN); + assertEquals(convertStringToType("date"), Types.DATE); + assertEquals(convertStringToType("time"), Types.TIME); + assertEquals(convertStringToType("timestamp"), Types.TIMESTAMP); + assertEquals(convertStringToType("datetime"), Types.TIMESTAMP); + assertEquals(convertStringToType("timestamp_ntz"), Types.TIMESTAMP); + assertEquals(convertStringToType("timestamp_ltz"), Types.TIMESTAMP_WITH_TIMEZONE); + assertEquals(convertStringToType("timestamp_tz"), Types.TIMESTAMP_WITH_TIMEZONE); + assertEquals(convertStringToType("variant"), Types.OTHER); + assertEquals(convertStringToType("object"), Types.JAVA_OBJECT); + assertEquals(convertStringToType("vector"), SnowflakeUtil.EXTRA_TYPES_VECTOR); + assertEquals(convertStringToType("array"), Types.ARRAY); + assertEquals(convertStringToType("default"), Types.OTHER); + } + + @Test + public void testJavaSQLTypeFind() { + assertNull(SnowflakeType.JavaSQLType.find(200000)); + } + + @Test + public void testJavaSQLTypeLexicalValue() { + assertEquals(SnowflakeType.lexicalValue(1.0f, null, null, null, null), "0x1.0p0"); + assertEquals(SnowflakeType.lexicalValue(new BigDecimal(100.0), null, null, null, null), "100"); + assertEquals( + SnowflakeType.lexicalValue("random".getBytes(), null, null, null, null), "72616E646F6D"); + } + + @Test + public void testJavaTypeToSFType() throws SnowflakeSQLException { + assertEquals(SnowflakeType.javaTypeToSFType(0, null), SnowflakeType.ANY); + assertThrows( + SnowflakeSQLLoggedException.class, + () -> { + SnowflakeType.javaTypeToSFType(2000000, null); + }); + } + + @Test + public void testJavaTypeToClassName() throws SQLException { + assertEquals(SnowflakeType.javaTypeToClassName(Types.DECIMAL), BigDecimal.class.getName()); + assertEquals(SnowflakeType.javaTypeToClassName(Types.TIME), java.sql.Time.class.getName()); + assertEquals(SnowflakeType.javaTypeToClassName(Types.BOOLEAN), Boolean.class.getName()); + assertThrows( + SQLFeatureNotSupportedException.class, + () -> { + SnowflakeType.javaTypeToClassName(-2000000); + }); + } +} diff --git a/src/test/java/net/snowflake/client/jdbc/cloud/storage/SnowflakeAzureClientLatestIT.java b/src/test/java/net/snowflake/client/jdbc/cloud/storage/SnowflakeAzureClientLatestIT.java index c667b7a3f..93539005a 100644 --- a/src/test/java/net/snowflake/client/jdbc/cloud/storage/SnowflakeAzureClientLatestIT.java +++ b/src/test/java/net/snowflake/client/jdbc/cloud/storage/SnowflakeAzureClientLatestIT.java @@ -1,8 +1,11 @@ package net.snowflake.client.jdbc.cloud.storage; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.fail; +import static org.mockito.Mockito.spy; +import com.microsoft.azure.storage.blob.ListBlobItem; import java.sql.Connection; import java.sql.SQLException; import net.snowflake.client.ConditionalIgnoreRule; @@ -17,7 +20,6 @@ import org.junit.Test; public class SnowflakeAzureClientLatestIT extends BaseJDBCTest { - @Test @ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class) public void testAzureClientSetupInvalidEncryptionKeySize() throws SQLException { @@ -37,4 +39,14 @@ public void testAzureClientSetupInvalidEncryptionKeySize() throws SQLException { } } } + + @Test + public void testCloudExceptionTest() { + Iterable mockList = null; + AzureObjectSummariesIterator iterator = new AzureObjectSummariesIterator(mockList); + AzureObjectSummariesIterator spyIterator = spy(iterator); + UnsupportedOperationException ex = + assertThrows(UnsupportedOperationException.class, () -> spyIterator.remove()); + assertEquals(ex.getMessage(), "remove() method not supported"); + } } diff --git a/src/test/java/net/snowflake/client/pooling/LogicalConnectionAlreadyClosedLatestIT.java b/src/test/java/net/snowflake/client/pooling/LogicalConnectionAlreadyClosedLatestIT.java index ac50f7608..ce93928ac 100644 --- a/src/test/java/net/snowflake/client/pooling/LogicalConnectionAlreadyClosedLatestIT.java +++ b/src/test/java/net/snowflake/client/pooling/LogicalConnectionAlreadyClosedLatestIT.java @@ -49,5 +49,6 @@ public void testLogicalConnectionAlreadyClosed() throws SQLException { expectConnectionAlreadyClosedException(() -> logicalConnection.setSchema("fakedb")); expectConnectionAlreadyClosedException( () -> logicalConnection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED)); + expectConnectionAlreadyClosedException(() -> logicalConnection.createArrayOf("faketype", null)); } } diff --git a/src/test/java/net/snowflake/client/pooling/LogicalConnectionLatestIT.java b/src/test/java/net/snowflake/client/pooling/LogicalConnectionLatestIT.java index bf05325e0..d25cdb485 100644 --- a/src/test/java/net/snowflake/client/pooling/LogicalConnectionLatestIT.java +++ b/src/test/java/net/snowflake/client/pooling/LogicalConnectionLatestIT.java @@ -6,8 +6,14 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import java.sql.CallableStatement; import java.sql.Clob; @@ -370,6 +376,77 @@ public void testDatabaseMetaData() throws SQLException { } } + @Test + public void testLogicalConnectionWhenPhysicalConnectionThrowsErrors() throws SQLException { + Connection connection = mock(Connection.class); + SnowflakePooledConnection snowflakePooledConnection = mock(SnowflakePooledConnection.class); + when(snowflakePooledConnection.getPhysicalConnection()).thenReturn(connection); + SQLException sqlException = new SQLException("mocking error"); + when(connection.createStatement()).thenThrow(sqlException); + when(connection.createStatement(1, 2, 3)).thenThrow(sqlException); + + when(connection.prepareStatement("mocksql")).thenThrow(sqlException); + when(connection.prepareCall("mocksql")).thenThrow(sqlException); + when(connection.prepareCall("mocksql", 1, 2, 3)).thenThrow(sqlException); + when(connection.nativeSQL("mocksql")).thenThrow(sqlException); + when(connection.getAutoCommit()).thenThrow(sqlException); + when(connection.getMetaData()).thenThrow(sqlException); + when(connection.isReadOnly()).thenThrow(sqlException); + when(connection.getCatalog()).thenThrow(sqlException); + when(connection.getTransactionIsolation()).thenThrow(sqlException); + when(connection.getWarnings()).thenThrow(sqlException); + when(connection.prepareCall("mocksql", 1, 2)).thenThrow(sqlException); + when(connection.getTypeMap()).thenThrow(sqlException); + when(connection.getHoldability()).thenThrow(sqlException); + when(connection.createClob()).thenThrow(sqlException); + when(connection.getClientInfo("mocksql")).thenThrow(sqlException); + when(connection.getClientInfo()).thenThrow(sqlException); + when(connection.createArrayOf("mock", null)).thenThrow(sqlException); + when(connection.getSchema()).thenThrow(sqlException); + when(connection.getNetworkTimeout()).thenThrow(sqlException); + when(connection.isWrapperFor(Connection.class)).thenThrow(sqlException); + + doThrow(sqlException).when(connection).setAutoCommit(false); + doThrow(sqlException).when(connection).commit(); + doThrow(sqlException).when(connection).rollback(); + doThrow(sqlException).when(connection).setReadOnly(false); + doThrow(sqlException).when(connection).clearWarnings(); + doThrow(sqlException).when(connection).setSchema(null); + doThrow(sqlException).when(connection).abort(null); + doThrow(sqlException).when(connection).setNetworkTimeout(null, 1); + + LogicalConnection logicalConnection = new LogicalConnection(snowflakePooledConnection); + + assertThrows(SQLException.class, logicalConnection::createStatement); + assertThrows(SQLException.class, () -> logicalConnection.createStatement(1, 2, 3)); + assertThrows(SQLException.class, () -> logicalConnection.nativeSQL("mocksql")); + assertThrows(SQLException.class, logicalConnection::getAutoCommit); + assertThrows(SQLException.class, logicalConnection::getMetaData); + assertThrows(SQLException.class, logicalConnection::isReadOnly); + assertThrows(SQLException.class, logicalConnection::getCatalog); + assertThrows(SQLException.class, logicalConnection::getTransactionIsolation); + assertThrows(SQLException.class, logicalConnection::getWarnings); + assertThrows(SQLException.class, () -> logicalConnection.prepareCall("mocksql")); + assertThrows(SQLException.class, logicalConnection::getTypeMap); + assertThrows(SQLException.class, logicalConnection::getHoldability); + assertThrows(SQLException.class, logicalConnection::createClob); + assertThrows(SQLException.class, () -> logicalConnection.getClientInfo("mocksql")); + assertThrows(SQLException.class, logicalConnection::getClientInfo); + assertThrows(SQLException.class, () -> logicalConnection.createArrayOf("mock", null)); + assertThrows(SQLException.class, logicalConnection::getSchema); + assertThrows(SQLException.class, logicalConnection::getNetworkTimeout); + assertThrows(SQLException.class, () -> logicalConnection.isWrapperFor(Connection.class)); + assertThrows(SQLException.class, () -> logicalConnection.setAutoCommit(false)); + assertThrows(SQLException.class, logicalConnection::rollback); + assertThrows(SQLException.class, () -> logicalConnection.setReadOnly(false)); + assertThrows(SQLException.class, logicalConnection::clearWarnings); + assertThrows(SQLException.class, () -> logicalConnection.setSchema(null)); + assertThrows(SQLException.class, () -> logicalConnection.abort(null)); + assertThrows(SQLException.class, () -> logicalConnection.setNetworkTimeout(null, 1)); + + verify(snowflakePooledConnection, times(26)).fireConnectionErrorEvent(sqlException); + } + private SnowflakeConnectionPoolDataSource setProperties( SnowflakeConnectionPoolDataSource poolDataSource) { poolDataSource.setUrl(properties.get("uri"));