This is an automated email from the ASF dual-hosted git repository. ppa pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/ignite-3.git
The following commit(s) were added to refs/heads/main by this push: new 56214b3d1c IGNITE-20476 Sql. Improved SQL exceptions checking in tests (#2616) 56214b3d1c is described below commit 56214b3d1c14314fc870660016834b5a92bed2c9 Author: ygerzhedovich <41903880+ygerzhedov...@users.noreply.github.com> AuthorDate: Thu Sep 28 14:21:02 2023 +0300 IGNITE-20476 Sql. Improved SQL exceptions checking in tests (#2616) --- .../client/handler/JdbcQueryEventHandlerImpl.java | 3 +- modules/client/build.gradle | 1 + .../apache/ignite/client/ClientMetricsTest.java | 9 ++-- .../internal/lang/IgniteExceptionMapperUtil.java | 1 + modules/jdbc/build.gradle | 5 ++ .../apache/ignite/jdbc/ItJdbcBatchSelfTest.java | 18 ++++---- .../ignite/jdbc/ItJdbcComplexQuerySelfTest.java | 19 +++----- .../apache/ignite/jdbc/ItJdbcErrorsSelfTest.java | 17 +++---- .../ignite/jdbc/ItJdbcStatementSelfTest.java | 26 +++++------ .../apache/ignite/jdbc/ItJdbcTransactionTest.java | 11 ++--- .../internal/jdbc/PreparedStatementParamsTest.java | 21 +++++---- .../org/apache/ignite/jdbc/util/JdbcTestUtils.java | 3 +- modules/runner/build.gradle | 2 + .../apache/ignite/internal/jdbc/ItJdbcTest.java | 5 +- .../internal/sql/api/ItSqlAsynchronousApiTest.java | 6 ++- .../ignite/internal/sql/engine/ItDmlTest.java | 33 +++++-------- .../sql/engine/ItDynamicParameterTest.java | 8 ++-- .../sql/engine/ItPkOnlyTableCrossApiTest.java | 20 +++++--- .../org/apache/ignite/internal/ssl/ItSslTest.java | 11 +++-- .../engine/exec/exp/IgniteSqlFunctionsTest.java | 5 +- .../sql/DistributionZoneSqlDdlParserTest.java | 8 ++-- .../engine/sql/IgniteSqlDecimalLiteralTest.java | 12 ++--- .../sql/engine/sql/IgniteSqlParserTest.java | 54 +++++++++++----------- .../engine/sql/SqlAlterColumnDdlParserTest.java | 14 ++++-- .../internal/sql/engine/sql/SqlDdlParserTest.java | 17 ++++--- .../internal/sql/engine/util/SqlTestUtils.java | 3 -- 26 files changed, 168 insertions(+), 164 deletions(-) diff --git a/modules/client-handler/src/main/java/org/apache/ignite/client/handler/JdbcQueryEventHandlerImpl.java b/modules/client-handler/src/main/java/org/apache/ignite/client/handler/JdbcQueryEventHandlerImpl.java index 58a6300640..fabcb33355 100644 --- a/modules/client-handler/src/main/java/org/apache/ignite/client/handler/JdbcQueryEventHandlerImpl.java +++ b/modules/client-handler/src/main/java/org/apache/ignite/client/handler/JdbcQueryEventHandlerImpl.java @@ -173,8 +173,7 @@ public class JdbcQueryEventHandlerImpl implements JdbcQueryEventHandler { String msg = getErrorMessage(t); - return new JdbcQueryExecuteResult(Response.STATUS_FAILED, - "Exception while executing query [query=" + req.sqlQuery() + "]. Error message:" + msg); + return new JdbcQueryExecuteResult(Response.STATUS_FAILED, msg); }); } diff --git a/modules/client/build.gradle b/modules/client/build.gradle index 92b50d8aa6..b13531ca99 100644 --- a/modules/client/build.gradle +++ b/modules/client/build.gradle @@ -53,6 +53,7 @@ dependencies { testImplementation project(':ignite-metrics') testImplementation(testFixtures(project(':ignite-core'))) testImplementation(testFixtures(project(':ignite-configuration'))) + testImplementation(testFixtures(project(':ignite-sql-engine'))) testImplementation libs.hamcrest.core testImplementation libs.junit5.params testImplementation libs.mockito.junit diff --git a/modules/client/src/test/java/org/apache/ignite/client/ClientMetricsTest.java b/modules/client/src/test/java/org/apache/ignite/client/ClientMetricsTest.java index 8dfb28aaa2..06fc4e5ef7 100644 --- a/modules/client/src/test/java/org/apache/ignite/client/ClientMetricsTest.java +++ b/modules/client/src/test/java/org/apache/ignite/client/ClientMetricsTest.java @@ -18,9 +18,9 @@ package org.apache.ignite.client; import static org.apache.ignite.client.fakes.FakeIgniteTables.TABLE_ONE_COLUMN; +import static org.apache.ignite.internal.sql.engine.util.SqlTestUtils.assertThrowsSqlException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.lang.reflect.Constructor; @@ -41,7 +41,7 @@ import org.apache.ignite.internal.metrics.MetricSet; import org.apache.ignite.internal.testframework.BaseIgniteAbstractTest; import org.apache.ignite.internal.testframework.IgniteTestUtils; import org.apache.ignite.internal.util.IgniteUtils; -import org.apache.ignite.sql.SqlException; +import org.apache.ignite.lang.ErrorGroups.Sql; import org.apache.ignite.table.Table; import org.apache.ignite.table.Tuple; import org.junit.jupiter.api.AfterEach; @@ -185,7 +185,10 @@ public class ClientMetricsTest extends BaseIgniteAbstractTest { assertEquals(1, metrics().requestsSent()); assertEquals(0, metrics().requestsRetried()); - assertThrows(SqlException.class, () -> client.sql().createSession().execute(null, FakeSession.FAILED_SQL)); + assertThrowsSqlException( + Sql.STMT_VALIDATION_ERR, + "Query failed", + () -> client.sql().createSession().execute(null, FakeSession.FAILED_SQL)); assertEquals(0, metrics().requestsActive()); assertEquals(1, metrics().requestsFailed()); diff --git a/modules/core/src/main/java/org/apache/ignite/internal/lang/IgniteExceptionMapperUtil.java b/modules/core/src/main/java/org/apache/ignite/internal/lang/IgniteExceptionMapperUtil.java index 9487918a4b..e5b43059aa 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/lang/IgniteExceptionMapperUtil.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/lang/IgniteExceptionMapperUtil.java @@ -102,6 +102,7 @@ public class IgniteExceptionMapperUtil { } if (origin instanceof IgniteException || origin instanceof IgniteCheckedException) { + return origin; } diff --git a/modules/jdbc/build.gradle b/modules/jdbc/build.gradle index 79171fc050..ee3fff55a6 100644 --- a/modules/jdbc/build.gradle +++ b/modules/jdbc/build.gradle @@ -22,6 +22,7 @@ plugins { apply from: "$rootDir/buildscripts/java-core.gradle" apply from: "$rootDir/buildscripts/publishing.gradle" apply from: "$rootDir/buildscripts/java-junit5.gradle" +apply from: "$rootDir/buildscripts/java-test-fixtures.gradle" apply from: "$rootDir/buildscripts/java-integration-test.gradle" dependencies { @@ -40,8 +41,12 @@ dependencies { integrationTestImplementation testFixtures(project(":ignite-api")) integrationTestImplementation testFixtures(project(":ignite-core")) integrationTestImplementation testFixtures(project(":ignite-sql-engine")) + integrationTestImplementation testFixtures(project(':ignite-jdbc')) integrationTestImplementation project(":ignite-runner") integrationTestImplementation project(":ignite-api") + + testFixturesImplementation testFixtures(project(":ignite-core")) + testFixturesImplementation libs.hamcrest.core } description = 'ignite-jdbc' diff --git a/modules/jdbc/src/integrationTest/java/org/apache/ignite/jdbc/ItJdbcBatchSelfTest.java b/modules/jdbc/src/integrationTest/java/org/apache/ignite/jdbc/ItJdbcBatchSelfTest.java index 208a3a9847..ffcb8db0ed 100644 --- a/modules/jdbc/src/integrationTest/java/org/apache/ignite/jdbc/ItJdbcBatchSelfTest.java +++ b/modules/jdbc/src/integrationTest/java/org/apache/ignite/jdbc/ItJdbcBatchSelfTest.java @@ -17,6 +17,7 @@ package org.apache.ignite.jdbc; +import static org.apache.ignite.jdbc.util.JdbcTestUtils.assertThrowsSqlException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.junit.jupiter.api.Assertions.assertArrayEquals; @@ -39,7 +40,6 @@ import java.time.LocalTime; import java.util.Arrays; import org.apache.ignite.internal.jdbc.proto.IgniteQueryErrorCode; import org.apache.ignite.internal.jdbc.proto.SqlStateCode; -import org.apache.ignite.jdbc.util.JdbcTestUtils; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; @@ -129,7 +129,7 @@ public class ItJdbcBatchSelfTest extends AbstractJdbcSelfTest { stmt.addBatch(ins1 + ";" + ins2); - JdbcTestUtils.assertThrowsSqlException( + assertThrowsSqlException( BatchUpdateException.class, "Multiple statements are not allowed.", () -> stmt.executeBatch()); @@ -143,17 +143,17 @@ public class ItJdbcBatchSelfTest extends AbstractJdbcSelfTest { stmt2.close(); pstmt2.close(); - JdbcTestUtils.assertThrowsSqlException("Statement is closed.", () -> stmt2.addBatch("")); + assertThrowsSqlException("Statement is closed.", () -> stmt2.addBatch("")); - JdbcTestUtils.assertThrowsSqlException("Statement is closed.", stmt2::clearBatch); + assertThrowsSqlException("Statement is closed.", stmt2::clearBatch); - JdbcTestUtils.assertThrowsSqlException("Statement is closed.", stmt2::executeBatch); + assertThrowsSqlException("Statement is closed.", stmt2::executeBatch); - JdbcTestUtils.assertThrowsSqlException("Statement is closed.", pstmt2::addBatch); + assertThrowsSqlException("Statement is closed.", pstmt2::addBatch); - JdbcTestUtils.assertThrowsSqlException("Statement is closed.", pstmt2::clearBatch); + assertThrowsSqlException("Statement is closed.", pstmt2::clearBatch); - JdbcTestUtils.assertThrowsSqlException("Statement is closed.", pstmt2::executeBatch); + assertThrowsSqlException("Statement is closed.", pstmt2::executeBatch); } @Test @@ -217,7 +217,7 @@ public class ItJdbcBatchSelfTest extends AbstractJdbcSelfTest { stmt.addBatch("insert into Person (id, firstName, lastName, age) values " + generateValues(100, 7)); - BatchUpdateException e = JdbcTestUtils.assertThrowsSqlException( + BatchUpdateException e = assertThrowsSqlException( BatchUpdateException.class, "Invalid SQL statement type. Expected [DML, DDL] but got QUERY", stmt::executeBatch); diff --git a/modules/jdbc/src/integrationTest/java/org/apache/ignite/jdbc/ItJdbcComplexQuerySelfTest.java b/modules/jdbc/src/integrationTest/java/org/apache/ignite/jdbc/ItJdbcComplexQuerySelfTest.java index 73aa0764ad..f6f3614c68 100644 --- a/modules/jdbc/src/integrationTest/java/org/apache/ignite/jdbc/ItJdbcComplexQuerySelfTest.java +++ b/modules/jdbc/src/integrationTest/java/org/apache/ignite/jdbc/ItJdbcComplexQuerySelfTest.java @@ -23,12 +23,11 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.fail; import java.sql.ResultSet; -import java.sql.SQLException; import java.sql.Statement; +import org.apache.ignite.jdbc.util.JdbcTestUtils; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -209,21 +208,17 @@ public class ItJdbcComplexQuerySelfTest extends AbstractJdbcSelfTest { } // Check non-indexed field. - assertThrows(SQLException.class, () -> { - try (ResultSet rs = stmt.executeQuery("select * from PUBLIC.Org where name = 2")) { - assertFalse(rs.next()); - } - }); + JdbcTestUtils.assertThrowsSqlException( + "For input string: \"B\"", + () -> stmt.executeQuery("select * from PUBLIC.Org where name = 2")); // Check indexed field. try (ResultSet rs = stmt.executeQuery("select * from PUBLIC.Person where name = '2'")) { assertFalse(rs.next()); } - assertThrows(SQLException.class, () -> { - try (ResultSet rs = stmt.executeQuery("select * from PUBLIC.Person where name = 2")) { - assertFalse(rs.next()); - } - }); + JdbcTestUtils.assertThrowsSqlException( + "For input string: \"Mike Green\"", + () -> stmt.executeQuery("select * from PUBLIC.Person where name = 2")); } } diff --git a/modules/jdbc/src/integrationTest/java/org/apache/ignite/jdbc/ItJdbcErrorsSelfTest.java b/modules/jdbc/src/integrationTest/java/org/apache/ignite/jdbc/ItJdbcErrorsSelfTest.java index 3389be7b55..3cda98d6ff 100644 --- a/modules/jdbc/src/integrationTest/java/org/apache/ignite/jdbc/ItJdbcErrorsSelfTest.java +++ b/modules/jdbc/src/integrationTest/java/org/apache/ignite/jdbc/ItJdbcErrorsSelfTest.java @@ -20,11 +20,8 @@ package org.apache.ignite.jdbc; import static org.apache.ignite.internal.jdbc.proto.SqlStateCode.CLIENT_CONNECTION_FAILED; import static org.apache.ignite.internal.jdbc.proto.SqlStateCode.INVALID_TRANSACTION_LEVEL; import static org.apache.ignite.internal.jdbc.proto.SqlStateCode.UNSUPPORTED_OPERATION; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsString; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -33,7 +30,7 @@ import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; -import org.apache.ignite.internal.testframework.IgniteTestUtils; +import org.apache.ignite.jdbc.util.JdbcTestUtils; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @@ -73,9 +70,7 @@ public class ItJdbcErrorsSelfTest extends ItJdbcErrorsAbstractSelfTest { + "NAME VARCHAR)" ); - SQLException ex = assertThrows(SQLException.class, () -> stmt.executeUpdate("non sql stuff")); - - assertThat(ex.getMessage(), containsString("Failed to parse query")); + JdbcTestUtils.assertThrowsSqlException("Failed to parse query", () -> stmt.executeUpdate("non sql stuff")); } try (Statement stmt = conn.createStatement()) { @@ -88,10 +83,10 @@ public class ItJdbcErrorsSelfTest extends ItJdbcErrorsAbstractSelfTest { + " BALANCE DOUBLE)" ); - IgniteTestUtils.assertThrows( - SQLException.class, - () -> stmt.executeUpdate("CREATE TABLE ACCOUNTS (ACCOUNT_ID INT PRIMARY KEY)"), - "Table with name 'PUBLIC.ACCOUNTS' already exists" + JdbcTestUtils.assertThrowsSqlException( + "Table with name 'PUBLIC.ACCOUNTS' already exists", + () -> stmt.executeUpdate("CREATE TABLE ACCOUNTS (ACCOUNT_ID INT PRIMARY KEY)") + ); } } diff --git a/modules/jdbc/src/integrationTest/java/org/apache/ignite/jdbc/ItJdbcStatementSelfTest.java b/modules/jdbc/src/integrationTest/java/org/apache/ignite/jdbc/ItJdbcStatementSelfTest.java index 9b98a413ac..59642f2e0d 100644 --- a/modules/jdbc/src/integrationTest/java/org/apache/ignite/jdbc/ItJdbcStatementSelfTest.java +++ b/modules/jdbc/src/integrationTest/java/org/apache/ignite/jdbc/ItJdbcStatementSelfTest.java @@ -17,6 +17,7 @@ package org.apache.ignite.jdbc; +import static org.apache.ignite.jdbc.util.JdbcTestUtils.assertThrowsSqlException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -32,7 +33,6 @@ import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; import java.sql.Statement; import java.util.UUID; -import org.apache.ignite.jdbc.util.JdbcTestUtils; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; @@ -185,7 +185,7 @@ public class ItJdbcStatementSelfTest extends ItJdbcAbstractStatementSelfTest { @Test public void testExecuteWrongFetchCount() throws Exception { try (Statement statement = conn.createStatement()) { - assertThrows(SQLException.class, () -> statement.setFetchSize(-2)); + assertThrowsSqlException("Fetch size must be greater than zero.", () -> statement.setFetchSize(-2)); } } @@ -481,7 +481,7 @@ public class ItJdbcStatementSelfTest extends ItJdbcAbstractStatementSelfTest { public void testExecuteUpdateProducesResultSet() { final String sqlText = "select * from TEST;"; - JdbcTestUtils.assertThrowsSqlException( + assertThrowsSqlException( "Invalid SQL statement type", () -> stmt.executeUpdate(sqlText)); } @@ -500,7 +500,7 @@ public class ItJdbcStatementSelfTest extends ItJdbcAbstractStatementSelfTest { stmt.executeUpdate("DROP TABLE " + tableName); - JdbcTestUtils.assertThrowsSqlException( + assertThrowsSqlException( "Failed to validate query", () -> stmt.executeQuery("SELECT COUNT(*) FROM " + tableName)); } @@ -529,7 +529,7 @@ public class ItJdbcStatementSelfTest extends ItJdbcAbstractStatementSelfTest { public void testGetSetMaxFieldSizeUnsupported() throws Exception { assertEquals(0, stmt.getMaxFieldSize()); - JdbcTestUtils.assertThrowsSqlException( + assertThrowsSqlException( SQLFeatureNotSupportedException.class, "Field size limitation is not supported", () -> stmt.setMaxFieldSize(100)); @@ -549,7 +549,7 @@ public class ItJdbcStatementSelfTest extends ItJdbcAbstractStatementSelfTest { public void testGetSetMaxRows() throws Exception { assertEquals(0, stmt.getMaxRows()); - JdbcTestUtils.assertThrowsSqlException("Invalid max rows value", () -> stmt.setMaxRows(-1)); + assertThrowsSqlException("Invalid max rows value", () -> stmt.setMaxRows(-1)); assertEquals(0, stmt.getMaxRows()); @@ -579,7 +579,7 @@ public class ItJdbcStatementSelfTest extends ItJdbcAbstractStatementSelfTest { public void testGetSetQueryTimeout() throws Exception { assertEquals(0, stmt.getQueryTimeout()); - JdbcTestUtils.assertThrowsSqlException("Invalid timeout value", () -> stmt.setQueryTimeout(-1)); + assertThrowsSqlException("Invalid timeout value", () -> stmt.setQueryTimeout(-1)); assertEquals(0, stmt.getQueryTimeout()); @@ -602,7 +602,7 @@ public class ItJdbcStatementSelfTest extends ItJdbcAbstractStatementSelfTest { public void testMaxFieldSize() throws Exception { assertTrue(stmt.getMaxFieldSize() >= 0); - JdbcTestUtils.assertThrowsSqlException("Invalid field limit", () -> stmt.setMaxFieldSize(-1)); + assertThrowsSqlException("Invalid field limit", () -> stmt.setMaxFieldSize(-1)); checkNotSupported(() -> stmt.setMaxFieldSize(100)); } @@ -710,7 +710,7 @@ public class ItJdbcStatementSelfTest extends ItJdbcAbstractStatementSelfTest { public void testFetchDirection() throws Exception { assertEquals(ResultSet.FETCH_FORWARD, stmt.getFetchDirection()); - JdbcTestUtils.assertThrowsSqlException( + assertThrowsSqlException( SQLFeatureNotSupportedException.class, "Only forward direction is supported.", () -> stmt.setFetchDirection(ResultSet.FETCH_REVERSE) @@ -725,12 +725,12 @@ public class ItJdbcStatementSelfTest extends ItJdbcAbstractStatementSelfTest { @Test public void testAutogenerated() { - JdbcTestUtils.assertThrowsSqlException( + assertThrowsSqlException( "Invalid autoGeneratedKeys value", () -> stmt.executeUpdate("select 1", -1) ); - JdbcTestUtils.assertThrowsSqlException( + assertThrowsSqlException( "Invalid autoGeneratedKeys value", () -> stmt.execute("select 1", -1) ); @@ -757,7 +757,7 @@ public class ItJdbcStatementSelfTest extends ItJdbcAbstractStatementSelfTest { // Put query to cache. stmt.executeQuery("select 1;"); - JdbcTestUtils.assertThrowsSqlException( + assertThrowsSqlException( "Invalid SQL statement type", () -> stmt.executeUpdate("select 1;") ); @@ -767,7 +767,7 @@ public class ItJdbcStatementSelfTest extends ItJdbcAbstractStatementSelfTest { @Test public void testStatementTypeMismatchUpdate() throws Exception { - JdbcTestUtils.assertThrowsSqlException( + assertThrowsSqlException( "Invalid SQL statement type", () -> stmt.executeQuery("update TEST set NAME='28' where ID=1") ); diff --git a/modules/jdbc/src/integrationTest/java/org/apache/ignite/jdbc/ItJdbcTransactionTest.java b/modules/jdbc/src/integrationTest/java/org/apache/ignite/jdbc/ItJdbcTransactionTest.java index f48c46862d..c1d9aba50c 100644 --- a/modules/jdbc/src/integrationTest/java/org/apache/ignite/jdbc/ItJdbcTransactionTest.java +++ b/modules/jdbc/src/integrationTest/java/org/apache/ignite/jdbc/ItJdbcTransactionTest.java @@ -17,8 +17,8 @@ package org.apache.ignite.jdbc; +import static org.apache.ignite.jdbc.util.JdbcTestUtils.assertThrowsSqlException; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.sql.Connection; @@ -118,8 +118,7 @@ public class ItJdbcTransactionTest extends AbstractJdbcSelfTest { // Starting transaction. stmt.executeUpdate("insert into TEST (ID) values (1)"); - SQLException ex = assertThrows(SQLException.class, () -> stmt.executeUpdate("drop table TEST")); - assertTrue(ex.getMessage().contains("DDL doesn't support transactions.")); + assertThrowsSqlException("DDL doesn't support transactions.", () -> stmt.executeUpdate("drop table TEST")); assertEquals(1, rowsCount(conn)); assertEquals(0, rowsCount(ItJdbcTransactionTest.conn)); @@ -225,8 +224,7 @@ public class ItJdbcTransactionTest extends AbstractJdbcSelfTest { try (Connection conn = DriverManager.getConnection(URL)) { conn.setAutoCommit(true); - SQLException t = assertThrows(SQLException.class, conn::commit); - assertEquals("Transaction cannot be committed explicitly in auto-commit mode.", t.getMessage()); + assertThrowsSqlException("Transaction cannot be committed explicitly in auto-commit mode.", conn::commit); } } @@ -238,8 +236,7 @@ public class ItJdbcTransactionTest extends AbstractJdbcSelfTest { try (Connection conn = DriverManager.getConnection(URL)) { conn.setAutoCommit(true); - SQLException t = assertThrows(SQLException.class, conn::rollback); - assertEquals("Transaction cannot be rolled back explicitly in auto-commit mode.", t.getMessage()); + assertThrowsSqlException("Transaction cannot be rolled back explicitly in auto-commit mode.", conn::rollback); } } diff --git a/modules/jdbc/src/test/java/org/apache/ignite/internal/jdbc/PreparedStatementParamsTest.java b/modules/jdbc/src/test/java/org/apache/ignite/internal/jdbc/PreparedStatementParamsTest.java index b71301c8a7..1234d85706 100644 --- a/modules/jdbc/src/test/java/org/apache/ignite/internal/jdbc/PreparedStatementParamsTest.java +++ b/modules/jdbc/src/test/java/org/apache/ignite/internal/jdbc/PreparedStatementParamsTest.java @@ -17,6 +17,7 @@ package org.apache.ignite.internal.jdbc; +import static org.apache.ignite.jdbc.util.JdbcTestUtils.assertThrowsSqlException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.junit.jupiter.api.Assertions.assertArrayEquals; @@ -264,7 +265,7 @@ public class PreparedStatementParamsTest extends BaseIgniteAbstractTest { UUID uuid = new UUID(0, 0); SQLType sqlType = Mockito.mock(SQLType.class); - SQLException err = checkSetParameterFails((s) -> s.setObject(1, uuid, sqlType, 1)); + SQLException err = checkSetParameterFails("setObject not implemented", (s) -> s.setObject(1, uuid, sqlType, 1)); assertInstanceOf(SQLFeatureNotSupportedException.class, err); } @@ -297,12 +298,12 @@ public class PreparedStatementParamsTest extends BaseIgniteAbstractTest { public void testSetNullForNotSupportedTypes(JDBCType jdbcType) { int typeCode = jdbcType.getVendorTypeNumber(); - SQLException err = checkSetParameterFails((s) -> s.setNull(1, typeCode)); + SQLException err = checkSetParameterFails("Type is not supported", (s) -> s.setNull(1, typeCode)); assertInstanceOf(SQLFeatureNotSupportedException.class, err); assertThat(err.getMessage(), containsString("Type is not supported")); // setNull with typename - SQLException err2 = checkSetParameterFails((s) -> s.setNull(1, typeCode, jdbcType.getName())); + SQLException err2 = checkSetParameterFails("Type is not supported", (s) -> s.setNull(1, typeCode, jdbcType.getName())); assertInstanceOf(SQLFeatureNotSupportedException.class, err2); assertThat(err2.getMessage(), containsString("Type is not supported")); } @@ -335,12 +336,14 @@ public class PreparedStatementParamsTest extends BaseIgniteAbstractTest { } /** Expects that the given action that uses {@code PreparedStatement} throws an {@link SQLException}. */ - private SQLException checkSetParameterFails(StatementConsumer action) { - return Assertions.assertThrows(SQLException.class, () -> { - try (PreparedStatement pstmt = conn.prepareStatement("SELECT ?")) { - action.consume(pstmt); - } - }); + private SQLException checkSetParameterFails(String expectedErrorMessage, StatementConsumer action) { + return assertThrowsSqlException( + expectedErrorMessage, + () -> { + try (PreparedStatement pstmt = conn.prepareStatement("SELECT ?")) { + action.consume(pstmt); + } + }); } /** diff --git a/modules/jdbc/src/integrationTest/java/org/apache/ignite/jdbc/util/JdbcTestUtils.java b/modules/jdbc/src/testFixtures/java/org/apache/ignite/jdbc/util/JdbcTestUtils.java similarity index 97% rename from modules/jdbc/src/integrationTest/java/org/apache/ignite/jdbc/util/JdbcTestUtils.java rename to modules/jdbc/src/testFixtures/java/org/apache/ignite/jdbc/util/JdbcTestUtils.java index 94ba7779f8..f42a6f22da 100644 --- a/modules/jdbc/src/integrationTest/java/org/apache/ignite/jdbc/util/JdbcTestUtils.java +++ b/modules/jdbc/src/testFixtures/java/org/apache/ignite/jdbc/util/JdbcTestUtils.java @@ -37,8 +37,9 @@ public class JdbcTestUtils { * @return Thrown the {@link SQLException}. */ public static <T extends SQLException> T assertThrowsSqlException(Class<T> expectedType, Executable executable) { - return assertThrows(expectedType, executable); + T ex = assertThrows(expectedType, executable); + return ex; } /** diff --git a/modules/runner/build.gradle b/modules/runner/build.gradle index 02fe02c42b..a01dbd6083 100644 --- a/modules/runner/build.gradle +++ b/modules/runner/build.gradle @@ -100,6 +100,7 @@ dependencies { testImplementation testFixtures(project(':ignite-configuration')) testImplementation testFixtures(project(':ignite-vault')) testImplementation testFixtures(project(':ignite-metastorage')) + testImplementation testFixtures(project(':ignite-jdbc')) testImplementation libs.hamcrest.core testImplementation libs.hamcrest.optional testImplementation libs.hamcrest.path @@ -161,6 +162,7 @@ dependencies { integrationTestImplementation testFixtures(project(':ignite-transactions')) integrationTestImplementation testFixtures(project(':ignite-runner')) integrationTestImplementation testFixtures(project(':ignite-placement-driver-api')) + integrationTestImplementation testFixtures(project(':ignite-jdbc')) integrationTestImplementation libs.jetbrains.annotations integrationTestImplementation libs.awaitility integrationTestImplementation libs.rocksdb.jni diff --git a/modules/runner/src/integrationTest/java/org/apache/ignite/internal/jdbc/ItJdbcTest.java b/modules/runner/src/integrationTest/java/org/apache/ignite/internal/jdbc/ItJdbcTest.java index 884629ec79..585a8217d7 100644 --- a/modules/runner/src/integrationTest/java/org/apache/ignite/internal/jdbc/ItJdbcTest.java +++ b/modules/runner/src/integrationTest/java/org/apache/ignite/internal/jdbc/ItJdbcTest.java @@ -17,8 +17,6 @@ package org.apache.ignite.internal.jdbc; -import static org.junit.jupiter.api.Assertions.assertThrows; - import java.nio.file.Path; import java.sql.Connection; import java.sql.DriverManager; @@ -26,6 +24,7 @@ import java.sql.SQLException; import org.apache.ignite.internal.Cluster; import org.apache.ignite.internal.IgniteIntegrationTest; import org.apache.ignite.internal.testframework.WorkDirectory; +import org.apache.ignite.jdbc.util.JdbcTestUtils; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; @@ -98,7 +97,7 @@ class ItJdbcTest extends IgniteIntegrationTest { @DisplayName("Jdbc client can not connect without basic authentication configured") void jdbcCanNotConnectWithoutBasicAuthentication() { var url = "jdbc:ignite:thin://127.0.0.1:10800"; - assertThrows(SQLException.class, () -> DriverManager.getConnection(url)); + JdbcTestUtils.assertThrowsSqlException("Failed to connect to server", () -> DriverManager.getConnection(url)); } @Test diff --git a/modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/api/ItSqlAsynchronousApiTest.java b/modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/api/ItSqlAsynchronousApiTest.java index 176090cc25..069db0aab4 100644 --- a/modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/api/ItSqlAsynchronousApiTest.java +++ b/modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/api/ItSqlAsynchronousApiTest.java @@ -346,13 +346,15 @@ public class ItSqlAsynchronousApiTest extends ClusterPerClassIntegrationTest { // Outdated tx. Transaction outerTx0 = outerTx; + //ToDo: IGNITE-20387 , here should be used assertThrowsSqlException method with code and message `"Transaction is already finished" IgniteException e = assertThrows(IgniteException.class, () -> checkDml(1, ses, "INSERT INTO TEST VALUES (?, ?)", outerTx0, ROW_COUNT, Integer.MAX_VALUE)); assertEquals(Transactions.TX_FAILED_READ_WRITE_OPERATION_ERR, e.code()); - e = assertThrows(SqlException.class, + assertThrowsSqlException( + Sql.CONSTRAINT_VIOLATION_ERR, + "PK unique constraint is violated", () -> checkDml(1, ses, "INSERT INTO TEST VALUES (?, ?)", ROW_COUNT, Integer.MAX_VALUE)); - assertEquals(Sql.CONSTRAINT_VIOLATION_ERR, e.code()); AsyncResultSet rs = await(ses.executeAsync(null, "SELECT VAL0 FROM TEST ORDER BY VAL0")); diff --git a/modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/engine/ItDmlTest.java b/modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/engine/ItDmlTest.java index 0a464df2b7..3d7f594174 100644 --- a/modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/engine/ItDmlTest.java +++ b/modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/engine/ItDmlTest.java @@ -18,8 +18,6 @@ package org.apache.ignite.internal.sql.engine; import static org.apache.ignite.internal.sql.engine.util.SqlTestUtils.assertThrowsSqlException; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsString; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -35,7 +33,6 @@ import org.apache.ignite.internal.sql.engine.exec.rel.AbstractNode; import org.apache.ignite.internal.testframework.WithSystemProperty; import org.apache.ignite.lang.ErrorGroups.Sql; import org.apache.ignite.lang.IgniteException; -import org.apache.ignite.sql.SqlException; import org.apache.ignite.tx.Transaction; import org.jetbrains.annotations.Nullable; import org.junit.jupiter.api.AfterEach; @@ -69,9 +66,10 @@ public class ItDmlTest extends ClusterPerClassIntegrationTest { .check(); { - SqlException ex = assertThrows(SqlException.class, () -> sql("INSERT INTO my VALUES (?, ?)", 0, 2)); - - checkDuplicatePk(ex); + assertThrowsSqlException( + Sql.CONSTRAINT_VIOLATION_ERR, + "PK unique constraint is violated", + () -> sql("INSERT INTO my VALUES (?, ?)", 0, 2)); } assertQuery("DELETE FROM my WHERE id=?") @@ -89,9 +87,10 @@ public class ItDmlTest extends ClusterPerClassIntegrationTest { .check(); { - SqlException ex = assertThrows(SqlException.class, () -> sql("INSERT INTO my VALUES (?, ?)", 0, 3)); - - checkDuplicatePk(ex); + assertThrowsSqlException( + Sql.CONSTRAINT_VIOLATION_ERR, + "PK unique constraint is violated", + () -> sql("INSERT INTO my VALUES (?, ?)", 0, 3)); } } @@ -118,13 +117,12 @@ public class ItDmlTest extends ClusterPerClassIntegrationTest { .returns(1L) .check(); - var ex = assertThrowsSqlException( + assertThrowsSqlException( Sql.CONSTRAINT_VIOLATION_ERR, "PK unique constraint is violated", () -> sql("INSERT INTO test VALUES (0, 0), (1, 1), (2, 2)") ); - checkDuplicatePk(ex); assertQuery("SELECT count(*) FROM test") .returns(1L) @@ -169,14 +167,12 @@ public class ItDmlTest extends ClusterPerClassIntegrationTest { .map(Object::toString) .collect(Collectors.joining("), (", "(", ")")); - SqlException ex = assertThrowsSqlException( + assertThrowsSqlException( Sql.CONSTRAINT_VIOLATION_ERR, "PK unique constraint is violated", () -> sql(insertStatement) ); - checkDuplicatePk(ex); - assertQuery("SELECT count(*) FROM test") .returns(0L) .check(); @@ -399,15 +395,13 @@ public class ItDmlTest extends ClusterPerClassIntegrationTest { sql("CREATE TABLE test2 (k int PRIMARY KEY, a int, b int)"); - SqlException ex = assertThrowsSqlException( + assertThrowsSqlException( Sql.CONSTRAINT_VIOLATION_ERR, "PK unique constraint is violated", () -> sql( "MERGE INTO test2 USING test1 ON test1.a = test2.a " + "WHEN MATCHED THEN UPDATE SET b = test1.b + 1 " + "WHEN NOT MATCHED THEN INSERT (k, a, b) VALUES (0, a, b)")); - - checkDuplicatePk(ex); } /** @@ -671,9 +665,4 @@ public class ItDmlTest extends ClusterPerClassIntegrationTest { sql("DELETE FROM test WHERE a = 0"); assertQuery("SELECT d FROM test").returnNothing().check(); } - - private static void checkDuplicatePk(IgniteException ex) { - assertEquals(Sql.CONSTRAINT_VIOLATION_ERR, ex.code()); - assertThat(ex.getMessage(), containsString("PK unique constraint is violated")); - } } diff --git a/modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/engine/ItDynamicParameterTest.java b/modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/engine/ItDynamicParameterTest.java index 1e5f6fa865..0e462940cd 100644 --- a/modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/engine/ItDynamicParameterTest.java +++ b/modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/engine/ItDynamicParameterTest.java @@ -32,10 +32,8 @@ import org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory; import org.apache.ignite.internal.sql.engine.util.Commons; import org.apache.ignite.internal.sql.engine.util.MetadataMatcher; import org.apache.ignite.internal.sql.engine.util.SqlTestUtils; -import org.apache.ignite.internal.testframework.IgniteTestUtils; import org.apache.ignite.lang.ErrorGroups.Sql; import org.apache.ignite.sql.ColumnType; -import org.apache.ignite.sql.SqlException; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -92,8 +90,10 @@ public class ItDynamicParameterTest extends ClusterPerClassIntegrationTest { assertQuery("SELECT id FROM person WHERE name LIKE ? ORDER BY id LIMIT ? OFFSET ?").withParams("I%", 1, 1).returns(2).check(); assertQuery("SELECT id from person WHERE salary<? and id<?").withParams(15, 3).returns(0).check(); - IgniteTestUtils.assertThrowsWithCause(() -> sql("SELECT LAST_DAY(?)", Date.valueOf("2022-01-01")), - SqlException.class, "Unsupported dynamic parameter defined"); + assertThrowsSqlException( + Sql.STMT_VALIDATION_ERR, + "Unsupported dynamic parameter defined", + () -> sql("SELECT LAST_DAY(?)", Date.valueOf("2022-01-01"))); LocalDate date1 = LocalDate.parse("2022-01-01"); LocalDate date2 = LocalDate.parse("2022-01-31"); diff --git a/modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/engine/ItPkOnlyTableCrossApiTest.java b/modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/engine/ItPkOnlyTableCrossApiTest.java index d6ba604742..e968759e65 100644 --- a/modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/engine/ItPkOnlyTableCrossApiTest.java +++ b/modules/runner/src/integrationTest/java/org/apache/ignite/internal/sql/engine/ItPkOnlyTableCrossApiTest.java @@ -17,6 +17,7 @@ package org.apache.ignite.internal.sql.engine; +import static org.apache.ignite.internal.sql.engine.util.SqlTestUtils.assertThrowsSqlException; import static org.apache.ignite.lang.ErrorGroups.Sql.CONSTRAINT_VIOLATION_ERR; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -32,7 +33,6 @@ import java.util.function.Consumer; import org.apache.ignite.lang.IgniteException; import org.apache.ignite.lang.NullableValue; import org.apache.ignite.lang.UnexpectedNullValueException; -import org.apache.ignite.sql.SqlException; import org.apache.ignite.table.KeyValueView; import org.apache.ignite.table.RecordView; import org.apache.ignite.table.Table; @@ -256,18 +256,24 @@ public class ItPkOnlyTableCrossApiTest extends ClusterPerClassIntegrationTest { rwTx -> { recordView.upsert(rwTx, Tuple.create().set("id", 0).set("name", names[0])); - SqlException ex = assertThrows(SqlException.class, () -> sql(rwTx, String.format(sqlInsert, 0, names[0]))); - assertEquals(CONSTRAINT_VIOLATION_ERR, ex.code()); + assertThrowsSqlException( + CONSTRAINT_VIOLATION_ERR, + "PK unique constraint is violated", + () -> sql(rwTx, String.format(sqlInsert, 0, names[0]))); kvView.put(rwTx, new KeyObject(1, names[1]), null); - ex = assertThrows(SqlException.class, () -> sql(rwTx, String.format(sqlInsert, 1, names[1]))); - assertEquals(CONSTRAINT_VIOLATION_ERR, ex.code()); + assertThrowsSqlException( + CONSTRAINT_VIOLATION_ERR, + "PK unique constraint is violated", + () -> sql(rwTx, String.format(sqlInsert, 1, names[1]))); binView.put(rwTx, Tuple.create().set("id", 2).set("name", names[2]), Tuple.create()); - ex = assertThrows(SqlException.class, () -> sql(rwTx, String.format(sqlInsert, 2, names[2]))); - assertEquals(CONSTRAINT_VIOLATION_ERR, ex.code()); + assertThrowsSqlException( + CONSTRAINT_VIOLATION_ERR, + "PK unique constraint is violated", + () -> sql(rwTx, String.format(sqlInsert, 2, names[2]))); sql(rwTx, String.format(sqlInsert, 3, names[3])); }, diff --git a/modules/runner/src/integrationTest/java/org/apache/ignite/internal/ssl/ItSslTest.java b/modules/runner/src/integrationTest/java/org/apache/ignite/internal/ssl/ItSslTest.java index 653449f663..d1bf959de2 100644 --- a/modules/runner/src/integrationTest/java/org/apache/ignite/internal/ssl/ItSslTest.java +++ b/modules/runner/src/integrationTest/java/org/apache/ignite/internal/ssl/ItSslTest.java @@ -23,6 +23,7 @@ import static org.apache.ignite.internal.testframework.IgniteTestUtils.getResour import static org.apache.ignite.internal.testframework.IgniteTestUtils.testNodeName; import static org.apache.ignite.internal.testframework.matchers.CompletableFutureExceptionMatcher.willTimeoutIn; import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willCompleteSuccessfully; +import static org.apache.ignite.jdbc.util.JdbcTestUtils.assertThrowsSqlException; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; @@ -154,7 +155,7 @@ public class ItSslTest extends IgniteIntegrationTest { + "&trustStorePath=" + trustStorePath + "&trustStoreType=JKS" + "&trustStorePassword=" + password; - assertThrows(SQLException.class, () -> DriverManager.getConnection(url)); + assertThrowsSqlException(SQLException.class, () -> DriverManager.getConnection(url)); } } @@ -221,7 +222,7 @@ public class ItSslTest extends IgniteIntegrationTest { @Test void jdbcCannotConnectWithoutSsl() { var url = "jdbc:ignite:thin://127.0.0.1:10800"; - assertThrows(SQLException.class, () -> DriverManager.getConnection(url)); + assertThrowsSqlException(SQLException.class, () -> DriverManager.getConnection(url)); } @Test @@ -296,7 +297,7 @@ public class ItSslTest extends IgniteIntegrationTest { @Test void jdbcCannotConnectWithoutSsl() { var url = "jdbc:ignite:thin://127.0.0.1:10800"; - assertThrows(SQLException.class, () -> DriverManager.getConnection(url)); + assertThrowsSqlException(SQLException.class, () -> DriverManager.getConnection(url)); } @Test @@ -348,7 +349,7 @@ public class ItSslTest extends IgniteIntegrationTest { + "&trustStorePath=" + trustStorePath + "&trustStoreType=JKS" + "&trustStorePassword=" + password; - assertThrows(SQLException.class, () -> DriverManager.getConnection(url)); + assertThrowsSqlException(SQLException.class, () -> DriverManager.getConnection(url)); } @Test @@ -437,7 +438,7 @@ public class ItSslTest extends IgniteIntegrationTest { @Test void jdbcCannotConnectWithoutSsl() { var url = "jdbc:ignite:thin://127.0.0.1:10800"; - assertThrows(SQLException.class, () -> DriverManager.getConnection(url)); + assertThrowsSqlException(SQLException.class, () -> DriverManager.getConnection(url)); } @Test diff --git a/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctionsTest.java b/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctionsTest.java index 962dc1b6f3..df38db058b 100644 --- a/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctionsTest.java +++ b/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/exec/exp/IgniteSqlFunctionsTest.java @@ -17,6 +17,7 @@ package org.apache.ignite.internal.sql.engine.exec.exp; +import static org.apache.ignite.internal.sql.engine.util.SqlTestUtils.assertThrowsSqlException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; @@ -26,7 +27,7 @@ import java.math.BigDecimal; import java.util.function.Supplier; import org.apache.calcite.sql.type.SqlTypeName; import org.apache.ignite.internal.sql.engine.type.IgniteTypeSystem; -import org.apache.ignite.sql.SqlException; +import org.apache.ignite.lang.ErrorGroups.Sql; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; @@ -238,7 +239,7 @@ public class IgniteSqlFunctionsTest { BigDecimal expected = convert.get(); assertEquals(new BigDecimal(result), expected); } else { - assertThrows(SqlException.class, convert::get); + assertThrowsSqlException(Sql.RUNTIME_ERR, "Numeric field overflow", convert::get); } } } diff --git a/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/DistributionZoneSqlDdlParserTest.java b/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/DistributionZoneSqlDdlParserTest.java index ed43df437b..29a35777b8 100644 --- a/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/DistributionZoneSqlDdlParserTest.java +++ b/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/DistributionZoneSqlDdlParserTest.java @@ -17,6 +17,7 @@ package org.apache.ignite.internal.sql.engine.sql; +import static org.apache.ignite.internal.sql.engine.util.SqlTestUtils.assertThrowsSqlException; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -26,7 +27,6 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.List; @@ -36,7 +36,7 @@ import org.apache.calcite.sql.SqlNode; import org.apache.calcite.sql.SqlWriter; import org.apache.calcite.sql.pretty.SqlPrettyWriter; import org.apache.ignite.internal.sql.engine.prepare.ddl.ZoneOptionEnum; -import org.apache.ignite.sql.SqlException; +import org.apache.ignite.lang.ErrorGroups.Sql; import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; @@ -178,7 +178,7 @@ public class DistributionZoneSqlDdlParserTest extends AbstractDdlParserTest { */ @Test public void alterZoneRenameToWithCompoundIdIsIllegal() { - assertThrows(SqlException.class, () -> parse("alter zone a.test_zone rename to b.zone1")); + assertThrowsSqlException(Sql.STMT_PARSE_ERR, "Failed to parse query", () -> parse("alter zone a.test_zone rename to b.zone1")); } /** @@ -243,7 +243,7 @@ public class DistributionZoneSqlDdlParserTest extends AbstractDdlParserTest { */ @Test public void alterZoneSetNoOptionsIsIllegal() { - assertThrows(SqlException.class, () -> parse("alter zone test_zone set")); + assertThrowsSqlException(Sql.STMT_PARSE_ERR, "Failed to parse query", () -> parse("alter zone test_zone set")); } /** diff --git a/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/IgniteSqlDecimalLiteralTest.java b/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/IgniteSqlDecimalLiteralTest.java index 59d43f26b9..64d551885a 100644 --- a/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/IgniteSqlDecimalLiteralTest.java +++ b/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/IgniteSqlDecimalLiteralTest.java @@ -18,11 +18,9 @@ package org.apache.ignite.internal.sql.engine.sql; import static org.apache.ignite.internal.lang.IgniteStringFormatter.format; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsString; +import static org.apache.ignite.internal.sql.engine.util.SqlTestUtils.assertThrowsSqlException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.math.BigDecimal; @@ -39,7 +37,7 @@ import org.apache.ignite.internal.sql.engine.planner.AbstractPlannerTest; import org.apache.ignite.internal.sql.engine.rel.IgniteRel; import org.apache.ignite.internal.sql.engine.schema.IgniteSchema; import org.apache.ignite.internal.sql.engine.util.Commons; -import org.apache.ignite.sql.SqlException; +import org.apache.ignite.lang.ErrorGroups.Sql; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; @@ -152,9 +150,7 @@ public class IgniteSqlDecimalLiteralTest extends AbstractPlannerTest { }) public void testParserRejectsInvalidValues(String value) { var query = format("SELECT {}", value); - var err = assertThrows(SqlException.class, () -> parseQuery(query)); - - assertThat(err.getMessage(), containsString("Invalid decimal literal")); + assertThrowsSqlException(Sql.STMT_PARSE_ERR, "Invalid decimal literal", () -> parseQuery(query)); } /** @@ -171,7 +167,7 @@ public class IgniteSqlDecimalLiteralTest extends AbstractPlannerTest { public void testParserRejectInvalidForms(String value) { var query = format("SELECT {}", value); - assertThrows(SqlException.class, () -> parseQuery(query)); + assertThrowsSqlException(Sql.STMT_PARSE_ERR, "Failed to parse query:", () -> parseQuery(query)); } private static SqlNode parseQuery(String qry) { diff --git a/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/IgniteSqlParserTest.java b/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/IgniteSqlParserTest.java index b1daee57e0..6af20f5495 100644 --- a/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/IgniteSqlParserTest.java +++ b/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/IgniteSqlParserTest.java @@ -17,15 +17,12 @@ package org.apache.ignite.internal.sql.engine.sql; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.equalTo; +import static org.apache.ignite.internal.sql.engine.util.SqlTestUtils.assertThrowsSqlException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.List; -import org.apache.ignite.sql.SqlException; +import org.apache.ignite.lang.ErrorGroups.Sql; import org.junit.jupiter.api.Test; /** @@ -54,68 +51,73 @@ public class IgniteSqlParserTest { */ @Test public void testStatementModeRejectMultipleStatements() { - SqlException t = assertThrows(SqlException.class, + assertThrowsSqlException( + Sql.STMT_VALIDATION_ERR, + "Multiple statements are not allowed", () -> IgniteSqlParser.parse("SELECT 1; SELECT 2", StatementParseResult.MODE)); - assertThat(t.getMessage(), containsString("Multiple statements are not allowed")); } @Test public void testGrammarViolation() { - SqlException t = assertThrows(SqlException.class, + assertThrowsSqlException( + Sql.STMT_PARSE_ERR, + "Failed to parse query: Encountered \"TABEL\" at line 1, column 7", () -> IgniteSqlParser.parse("ALTER TABEL foo ADD COLUMN bar INT", StatementParseResult.MODE)); - assertThat(t.getMessage(), equalTo("Failed to parse query: Encountered \"TABEL\" at line 1, column 7")); } @Test public void testGrammarViolationButExceptionIsReplacedInsideParser() { - SqlException t = assertThrows(SqlException.class, + assertThrowsSqlException( + Sql.STMT_PARSE_ERR, + "Failed to parse query: Incorrect syntax near the keyword 'FROM' at line 1, column 12", () -> IgniteSqlParser.parse("SELECT ALL FROM foo", StatementParseResult.MODE)); - assertThat(t.getMessage(), equalTo("Failed to parse query: Incorrect syntax near the keyword 'FROM' at line 1, column 12")); } @Test public void testEmptyString() { - SqlException t = assertThrows(SqlException.class, + assertThrowsSqlException(Sql.STMT_PARSE_ERR, + "Failed to parse query: Encountered \"<EOF>\" at line 0, column 0", () -> IgniteSqlParser.parse("", StatementParseResult.MODE)); - assertThat(t.getMessage(), equalTo("Failed to parse query: Encountered \"<EOF>\" at line 0, column 0")); } @Test public void testCommentedQuery() { - SqlException t = assertThrows(SqlException.class, + assertThrowsSqlException( + Sql.STMT_PARSE_ERR, + "Failed to parse query: Encountered \"<EOF>\" at line 1, column 11", () -> IgniteSqlParser.parse("-- SELECT 1", StatementParseResult.MODE)); - assertThat(t.getMessage(), equalTo("Failed to parse query: Encountered \"<EOF>\" at line 1, column 11")); } @Test public void testTokenizationError() { - SqlException t = assertThrows(SqlException.class, + assertThrowsSqlException( + Sql.STMT_PARSE_ERR, + "Failed to parse query: Lexical error at line 1, column 11. Encountered: \"#\" (35), after : \"\"", () -> IgniteSqlParser.parse("SELECT foo#bar", StatementParseResult.MODE)); - assertThat(t.getMessage(), equalTo("Failed to parse query: Lexical error at line 1, column 11. " - + " Encountered: \"#\" (35), after : \"\"")); } @Test public void testExpressionInsteadOfStatement() { - SqlException t = assertThrows(SqlException.class, + assertThrowsSqlException( + Sql.STMT_PARSE_ERR, + "Failed to parse query: Non-query expression encountered in illegal context. At line 1, column 1", () -> IgniteSqlParser.parse("a = 2", StatementParseResult.MODE)); - assertThat(t.getMessage(), equalTo("Failed to parse query: Non-query expression encountered " - + "in illegal context. At line 1, column 1")); } @Test public void testExpressionInsteadOfSubStatement() { - SqlException t = assertThrows(SqlException.class, + assertThrowsSqlException( + Sql.STMT_PARSE_ERR, + "Failed to parse query: Non-query expression encountered in illegal context. At line 1, column 21", () -> IgniteSqlParser.parse("INSERT INTO t (c1) (a = 2)", StatementParseResult.MODE)); - assertThat(t.getMessage(), equalTo("Failed to parse query: Non-query expression encountered " - + "in illegal context. At line 1, column 21")); } @Test public void testInvalidDecimalLiteral() { - SqlException t = assertThrows(SqlException.class, + assertThrowsSqlException( + Sql.STMT_PARSE_ERR, + "Failed to parse query: Invalid decimal literal. At line 1, column 16", () -> IgniteSqlParser.parse("SELECT decimal '2a'", StatementParseResult.MODE)); - assertThat(t.getMessage(), equalTo("Failed to parse query: Invalid decimal literal. At line 1, column 16")); } } diff --git a/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/SqlAlterColumnDdlParserTest.java b/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/SqlAlterColumnDdlParserTest.java index a345bfc210..14c72d6a4c 100644 --- a/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/SqlAlterColumnDdlParserTest.java +++ b/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/SqlAlterColumnDdlParserTest.java @@ -17,12 +17,12 @@ package org.apache.ignite.internal.sql.engine.sql; +import static org.apache.ignite.internal.sql.engine.util.SqlTestUtils.assertThrowsSqlException; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.List; import org.apache.calcite.sql.SqlLiteral; @@ -31,7 +31,7 @@ import org.apache.calcite.sql.pretty.SqlFormatOptions; import org.apache.calcite.sql.pretty.SqlPrettyWriter; import org.apache.calcite.sql.type.SqlTypeName; import org.apache.ignite.internal.lang.IgniteStringFormatter; -import org.apache.ignite.sql.SqlException; +import org.apache.ignite.lang.ErrorGroups.Sql; import org.jetbrains.annotations.Nullable; import org.junit.jupiter.api.Test; @@ -78,7 +78,10 @@ public class SqlAlterColumnDdlParserTest extends AbstractDdlParserTest { assertThat(dflt, instanceOf(SqlLiteral.class)); assertThat(((SqlLiteral) dflt).getValueAs(Integer.class), equalTo(10)); - assertThrows(SqlException.class, () -> parse(QUERY_PREFIX + "SET DEFAULT FUNC")); + assertThrowsSqlException( + Sql.STMT_PARSE_ERR, + "Failed to parse query: Encountered \"FUNC\"", + () -> parse(QUERY_PREFIX + "SET DEFAULT FUNC")); } /** @@ -101,7 +104,10 @@ public class SqlAlterColumnDdlParserTest extends AbstractDdlParserTest { validateDataType("SET DATA TYPE INTEGER NOT NULL DEFAULT -1", "INTEGER", true, -1); validateDataType("SET DATA TYPE INTEGER NULL DEFAULT NULL", "INTEGER", false, null); - assertThrows(SqlException.class, () -> parse(QUERY_PREFIX + "SET DATA TYPE INTEGER DEFAULT FUNC")); + assertThrowsSqlException( + Sql.STMT_PARSE_ERR, + "Failed to parse query: Encountered \"FUNC\"", + () -> parse(QUERY_PREFIX + "SET DATA TYPE INTEGER DEFAULT FUNC")); } private void validateDataType(String querySuffix, @Nullable String typeName, @Nullable Boolean notNull, @Nullable Object expDefault) { diff --git a/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/SqlDdlParserTest.java b/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/SqlDdlParserTest.java index 65f0e78f46..928052d79d 100644 --- a/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/SqlDdlParserTest.java +++ b/modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/sql/SqlDdlParserTest.java @@ -18,15 +18,14 @@ package org.apache.ignite.internal.sql.engine.sql; import static java.util.Collections.singleton; +import static org.apache.ignite.internal.sql.engine.util.SqlTestUtils.assertThrowsSqlException; import static org.hamcrest.CoreMatchers.endsWith; import static org.hamcrest.CoreMatchers.hasItem; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.List; import java.util.Objects; @@ -41,7 +40,7 @@ import org.apache.calcite.sql.SqlNode; import org.apache.calcite.sql.ddl.SqlColumnDeclaration; import org.apache.calcite.sql.ddl.SqlKeyConstraint; import org.apache.calcite.sql.pretty.SqlPrettyWriter; -import org.apache.ignite.sql.SqlException; +import org.apache.ignite.lang.ErrorGroups.Sql; import org.hamcrest.CustomMatcher; import org.hamcrest.Matcher; import org.junit.jupiter.api.Test; @@ -394,8 +393,10 @@ public class SqlDdlParserTest extends AbstractDdlParserTest { public void sortDirectionMustNotBeSpecifiedForHashIndex() { var query = "create index my_index on my_table using hash (col1, col2 asc)"; - var ex = assertThrows(SqlException.class, () -> parse(query)); - assertThat(ex.getMessage(), containsString("Encountered \"asc\"")); + assertThrowsSqlException( + Sql.STMT_PARSE_ERR, + "Encountered \"asc\"", + () -> parse(query)); } @Test @@ -512,8 +513,10 @@ public class SqlDdlParserTest extends AbstractDdlParserTest { @Test public void timestampWithLocalTimeZoneIsNotSupported() { Consumer<String> checker = (query) -> { - var ex = assertThrows(SqlException.class, () -> parse(query)); - assertThat(ex.getMessage(), containsString("Encountered \"WITH\"")); + assertThrowsSqlException( + Sql.STMT_PARSE_ERR, + "Encountered \"WITH\"", + () -> parse(query)); }; checker.accept("CREATE TABLE test (ts TIMESTAMP WITH LOCAL TIME ZONE)"); diff --git a/modules/sql-engine/src/testFixtures/java/org/apache/ignite/internal/sql/engine/util/SqlTestUtils.java b/modules/sql-engine/src/testFixtures/java/org/apache/ignite/internal/sql/engine/util/SqlTestUtils.java index 9ae42322e2..9a5b901e96 100644 --- a/modules/sql-engine/src/testFixtures/java/org/apache/ignite/internal/sql/engine/util/SqlTestUtils.java +++ b/modules/sql-engine/src/testFixtures/java/org/apache/ignite/internal/sql/engine/util/SqlTestUtils.java @@ -18,7 +18,6 @@ package org.apache.ignite.internal.sql.engine.util; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.IsNot.not; import static org.hamcrest.core.StringContains.containsString; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -81,8 +80,6 @@ public class SqlTestUtils { assertThat("Error message", ex.getMessage(), containsString(expectedMessage)); - assertThat("Exception shouldn't be in internal package", ex.getClass().getPackageName(), not(containsString("internal"))); - return ex; }