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 9a0091660ef Add more test cases on MySQLDatabasePrivilegeCheckerTest 
(#38371)
9a0091660ef is described below

commit 9a0091660ef33b9ccc6f97624d3df70a2b76557b
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Mar 7 23:32:44 2026 +0800

    Add more test cases on MySQLDatabasePrivilegeCheckerTest (#38371)
---
 .../checker/MySQLDatabasePrivilegeCheckerTest.java | 32 +++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)

diff --git 
a/database/connector/dialect/mysql/src/test/java/org/apache/shardingsphere/database/connector/mysql/checker/MySQLDatabasePrivilegeCheckerTest.java
 
b/database/connector/dialect/mysql/src/test/java/org/apache/shardingsphere/database/connector/mysql/checker/MySQLDatabasePrivilegeCheckerTest.java
index 13c62e261d0..8cfb170c7f9 100644
--- 
a/database/connector/dialect/mysql/src/test/java/org/apache/shardingsphere/database/connector/mysql/checker/MySQLDatabasePrivilegeCheckerTest.java
+++ 
b/database/connector/dialect/mysql/src/test/java/org/apache/shardingsphere/database/connector/mysql/checker/MySQLDatabasePrivilegeCheckerTest.java
@@ -23,6 +23,7 @@ import 
org.apache.shardingsphere.database.connector.core.exception.CheckDatabase
 import 
org.apache.shardingsphere.database.connector.core.exception.MissingRequiredPrivilegeException;
 import 
org.apache.shardingsphere.database.connector.core.spi.DatabaseTypedSPILoader;
 import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import org.hamcrest.CoreMatchers;
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
@@ -41,6 +42,7 @@ import java.sql.SQLException;
 import java.util.stream.Stream;
 
 import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.hamcrest.MatcherAssert.assertThat;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.verify;
@@ -149,7 +151,7 @@ class MySQLDatabasePrivilegeCheckerTest {
     @Test
     void assertCheckFailureWhenReadResultSetAndCloseResultSetFail() throws 
SQLException {
         mockShowGrantsQuery();
-        when(resultSet.next()).thenThrow(new SQLException("mocked result set 
failure"));
+        when(resultSet.next()).thenThrow(SQLException.class);
         doThrow(SQLException.class).when(resultSet).close();
         assertThrows(CheckDatabaseEnvironmentFailedException.class, () -> 
checker.check(dataSource, PrivilegeCheckType.PIPELINE));
     }
@@ -162,6 +164,24 @@ class MySQLDatabasePrivilegeCheckerTest {
         assertThrows(CheckDatabaseEnvironmentFailedException.class, () -> 
checker.check(dataSource, PrivilegeCheckType.PIPELINE));
     }
     
+    @Test
+    void assertCheckFailureWhenCloseResultSetFailsAfterMatchedPrivilege() 
throws SQLException {
+        mockShowGrantsQuery();
+        when(resultSet.next()).thenReturn(true);
+        when(resultSet.getString(1)).thenReturn("GRANT ALL PRIVILEGES ON *.* 
TO '%'@'%'");
+        doThrow(SQLException.class).when(resultSet).close();
+        assertThrows(CheckDatabaseEnvironmentFailedException.class, () -> 
checker.check(dataSource, PrivilegeCheckType.PIPELINE));
+    }
+    
+    @Test
+    void 
assertCheckFailureWhenClosePreparedStatementFailsAfterMatchedPrivilege() throws 
SQLException {
+        mockShowGrantsQuery();
+        when(resultSet.next()).thenReturn(true);
+        when(resultSet.getString(1)).thenReturn("GRANT ALL PRIVILEGES ON *.* 
TO '%'@'%'");
+        doThrow(new SQLException("mocked close prepared statement 
failure")).when(preparedStatement).close();
+        assertThrows(CheckDatabaseEnvironmentFailedException.class, () -> 
checker.check(dataSource, PrivilegeCheckType.PIPELINE));
+    }
+    
     @Test
     void assertCheckFailureWhenCloseConnectionFails() throws SQLException {
         mockShowGrantsQuery();
@@ -171,6 +191,16 @@ class MySQLDatabasePrivilegeCheckerTest {
         assertThrows(CheckDatabaseEnvironmentFailedException.class, () -> 
checker.check(dataSource, PrivilegeCheckType.PIPELINE));
     }
     
+    @Test
+    void assertCheckWithMissingPrivilegeAndCloseConnectionFailure() throws 
SQLException {
+        mockShowGrantsQuery();
+        SQLException expected = new SQLException("mocked close connection 
failure");
+        doThrow(expected).when(connection).close();
+        MissingRequiredPrivilegeException actual = 
assertThrows(MissingRequiredPrivilegeException.class, () -> 
checker.check(dataSource, PrivilegeCheckType.PIPELINE));
+        assertThat(actual.getSuppressed().length, CoreMatchers.is(1));
+        assertThat(actual.getSuppressed()[0], CoreMatchers.is(expected));
+    }
+    
     @Test
     void assertCheckSkipsXAInMySQL5() throws SQLException {
         when(dataSource.getConnection()).thenReturn(connection);

Reply via email to