You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2026/03/12 14:35:20 UTC
(shardingsphere) branch master updated: Add more test cases on PostgreSQLDialectExceptionMapperTest (#38406)
This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 8865d670fe2 Add more test cases on PostgreSQLDialectExceptionMapperTest (#38406)
8865d670fe2 is described below
commit 8865d670fe28b0677a633aa50e9fc26bbd703a13
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Thu Mar 12 22:35:12 2026 +0800
Add more test cases on PostgreSQLDialectExceptionMapperTest (#38406)
---
.../PostgreSQLDialectExceptionMapperTest.java | 81 ++++++++++++++++------
1 file changed, 61 insertions(+), 20 deletions(-)
diff --git a/database/exception/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/exception/postgresql/mapper/PostgreSQLDialectExceptionMapperTest.java b/database/exception/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/exception/postgresql/mapper/PostgreSQLDialectExceptionMapperTest.java
index 26b17867704..ff85f3445c6 100644
--- a/database/exception/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/exception/postgresql/mapper/PostgreSQLDialectExceptionMapperTest.java
+++ b/database/exception/dialect/postgresql/src/test/java/org/apache/shardingsphere/database/exception/postgresql/mapper/PostgreSQLDialectExceptionMapperTest.java
@@ -17,43 +17,84 @@
package org.apache.shardingsphere.database.exception.postgresql.mapper;
+import org.apache.shardingsphere.database.connector.core.spi.DatabaseTypedSPILoader;
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
import org.apache.shardingsphere.database.exception.core.exception.SQLDialectException;
import org.apache.shardingsphere.database.exception.core.exception.connection.TooManyConnectionsException;
import org.apache.shardingsphere.database.exception.core.exception.data.InsertColumnsAndValuesMismatchedException;
import org.apache.shardingsphere.database.exception.core.exception.data.InvalidParameterValueException;
+import org.apache.shardingsphere.database.exception.core.exception.syntax.column.ColumnNotFoundException;
import org.apache.shardingsphere.database.exception.core.exception.syntax.database.DatabaseCreateExistsException;
+import org.apache.shardingsphere.database.exception.core.exception.syntax.database.UnknownDatabaseException;
+import org.apache.shardingsphere.database.exception.core.exception.syntax.table.NoSuchTableException;
+import org.apache.shardingsphere.database.exception.core.exception.syntax.table.TableExistsException;
import org.apache.shardingsphere.database.exception.core.exception.transaction.InTransactionException;
-import org.junit.jupiter.api.extension.ExtensionContext;
+import org.apache.shardingsphere.database.exception.core.mapper.SQLDialectExceptionMapper;
+import org.apache.shardingsphere.database.exception.postgresql.exception.PostgreSQLException;
+import org.apache.shardingsphere.database.exception.postgresql.exception.authority.EmptyUsernameException;
+import org.apache.shardingsphere.database.exception.postgresql.exception.authority.InvalidPasswordException;
+import org.apache.shardingsphere.database.exception.postgresql.exception.authority.PrivilegeNotGrantedException;
+import org.apache.shardingsphere.database.exception.postgresql.exception.authority.UnknownUsernameException;
+import org.apache.shardingsphere.database.exception.postgresql.exception.protocol.ProtocolViolationException;
+import org.apache.shardingsphere.database.exception.postgresql.sqlstate.PostgreSQLState;
+import org.apache.shardingsphere.database.exception.postgresql.vendor.PostgreSQLVendorError;
+import org.apache.shardingsphere.infra.exception.external.sql.vendor.VendorError;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
-import org.junit.jupiter.params.provider.ArgumentsProvider;
-import org.junit.jupiter.params.provider.ArgumentsSource;
-import org.junit.jupiter.params.support.ParameterDeclarations;
-import org.postgresql.util.PSQLState;
+import org.junit.jupiter.params.provider.MethodSource;
import java.util.stream.Stream;
-import static org.hamcrest.Matchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
class PostgreSQLDialectExceptionMapperTest {
- @ParameterizedTest(name = "{1} -> {0}")
- @ArgumentsSource(TestCaseArgumentsProvider.class)
- void assertConvert(final Class<SQLDialectException> sqlDialectExceptionClazz, final String sqlState) {
- assertThat(new PostgreSQLDialectExceptionMapper().convert(mock(sqlDialectExceptionClazz)).getSQLState(), is(sqlState));
+ private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "PostgreSQL");
+
+ private final SQLDialectExceptionMapper mapper = DatabaseTypedSPILoader.getService(SQLDialectExceptionMapper.class, databaseType);
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("convertArguments")
+ void assertConvert(final String name, final Class<? extends SQLDialectException> sqlDialectExceptionClass, final VendorError vendorError, final String severity) {
+ PostgreSQLException actual = (PostgreSQLException) mapper.convert(mock(sqlDialectExceptionClass));
+ assertThat(actual.getSQLState(), is(vendorError.getSqlState().getValue()));
+ assertNotNull(actual.getServerErrorMessage());
+ assertThat(actual.getServerErrorMessage().getSqlState(), is(vendorError.getSqlState().getValue()));
+ assertThat(actual.getServerErrorMessage().getSeverity(), is(severity));
+ }
+
+ @Test
+ void assertConvertWithUnexpectedException() {
+ SQLDialectException sqlDialectException = mock(SQLDialectException.class);
+ when(sqlDialectException.getMessage()).thenReturn("unexpected");
+ PostgreSQLException actual = (PostgreSQLException) mapper.convert(sqlDialectException);
+ assertThat(actual.getMessage(), is("unexpected"));
+ assertThat(actual.getSQLState(), is(PostgreSQLState.UNEXPECTED_ERROR.getValue()));
+ assertNull(actual.getServerErrorMessage());
}
- private static final class TestCaseArgumentsProvider implements ArgumentsProvider {
-
- @Override
- public Stream<? extends Arguments> provideArguments(final ParameterDeclarations parameters, final ExtensionContext context) {
- return Stream.of(Arguments.of(DatabaseCreateExistsException.class, "42P04"),
- Arguments.of(InTransactionException.class, PSQLState.TRANSACTION_STATE_INVALID.getState()),
- Arguments.of(InsertColumnsAndValuesMismatchedException.class, PSQLState.SYNTAX_ERROR.getState()),
- Arguments.of(InvalidParameterValueException.class, PSQLState.INVALID_PARAMETER_VALUE.getState()),
- Arguments.of(TooManyConnectionsException.class, PSQLState.CONNECTION_REJECTED.getState()));
- }
+ private static Stream<Arguments> convertArguments() {
+ return Stream.of(
+ Arguments.of("unknown_database", UnknownDatabaseException.class, PostgreSQLVendorError.INVALID_CATALOG_NAME, "FATAL"),
+ Arguments.of("database_create_exists", DatabaseCreateExistsException.class, PostgreSQLVendorError.DUPLICATE_DATABASE, "FATAL"),
+ Arguments.of("no_such_table", NoSuchTableException.class, PostgreSQLVendorError.NO_SUCH_TABLE, "FATAL"),
+ Arguments.of("table_exists", TableExistsException.class, PostgreSQLVendorError.DUPLICATE_TABLE, "ERROR"),
+ Arguments.of("in_transaction", InTransactionException.class, PostgreSQLVendorError.TRANSACTION_STATE_INVALID, "ERROR"),
+ Arguments.of("insert_columns_and_values_mismatched", InsertColumnsAndValuesMismatchedException.class, PostgreSQLVendorError.WRONG_VALUE_COUNT_ON_ROW, "ERROR"),
+ Arguments.of("invalid_parameter_value", InvalidParameterValueException.class, PostgreSQLVendorError.INVALID_PARAMETER_VALUE, "ERROR"),
+ Arguments.of("too_many_connections", TooManyConnectionsException.class, PostgreSQLVendorError.DATA_SOURCE_REJECTED_CONNECTION_ATTEMPT, "ERROR"),
+ Arguments.of("unknown_username", UnknownUsernameException.class, PostgreSQLVendorError.INVALID_AUTHORIZATION_SPECIFICATION, "FATAL"),
+ Arguments.of("invalid_password", InvalidPasswordException.class, PostgreSQLVendorError.INVALID_PASSWORD, "FATAL"),
+ Arguments.of("privilege_not_granted", PrivilegeNotGrantedException.class, PostgreSQLVendorError.PRIVILEGE_NOT_GRANTED, "FATAL"),
+ Arguments.of("empty_username", EmptyUsernameException.class, PostgreSQLVendorError.NO_USERNAME, "FATAL"),
+ Arguments.of("protocol_violation", ProtocolViolationException.class, PostgreSQLVendorError.PROTOCOL_VIOLATION, "FATAL"),
+ Arguments.of("column_not_found", ColumnNotFoundException.class, PostgreSQLVendorError.UNDEFINED_COLUMN, "FATAL"));
}
}