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 ce5adaf95ae Remove XATransactionPrivilegeChecker (#32157)
ce5adaf95ae is described below
commit ce5adaf95aebfdbcc9e54a175cf4dc3af8f23d3d
Author: Raigor <[email protected]>
AuthorDate: Wed Jul 17 21:52:04 2024 +0800
Remove XATransactionPrivilegeChecker (#32157)
* Remove XATransactionPrivilegeChecker
* Remove XATransactionPrivilegeCheckException
---
.../MySQLDatabaseEnvironmentCheckerTest.java | 52 +++++++++++-
.../xa/XAShardingSphereTransactionManager.java | 6 +-
.../checker/XATransactionPrivilegeChecker.java | 35 --------
.../dialect/H2XATransactionPrivilegeChecker.java | 37 --------
.../MySQLXATransactionPrivilegeChecker.java | 77 -----------------
.../XATransactionPrivilegeCheckException.java | 38 ---------
.../jta/exception/XATransactionSQLException.java | 4 -
...atasource.checker.XATransactionPrivilegeChecker | 19 -----
.../MySQLXATransactionPrivilegeCheckerTest.java | 98 ----------------------
9 files changed, 54 insertions(+), 312 deletions(-)
diff --git
a/infra/database/type/mysql/src/test/java/org/apache/shardingsphere/infra/database/mysql/checker/MySQLDatabaseEnvironmentCheckerTest.java
b/infra/database/type/mysql/src/test/java/org/apache/shardingsphere/infra/database/mysql/checker/MySQLDatabaseEnvironmentCheckerTest.java
index 59a61d6e00b..d07ce71701a 100644
---
a/infra/database/type/mysql/src/test/java/org/apache/shardingsphere/infra/database/mysql/checker/MySQLDatabaseEnvironmentCheckerTest.java
+++
b/infra/database/type/mysql/src/test/java/org/apache/shardingsphere/infra/database/mysql/checker/MySQLDatabaseEnvironmentCheckerTest.java
@@ -55,11 +55,11 @@ class MySQLDatabaseEnvironmentCheckerTest {
@BeforeEach
void setUp() throws SQLException {
when(dataSource.getConnection().prepareStatement(anyString())).thenReturn(preparedStatement);
- when(preparedStatement.executeQuery()).thenReturn(resultSet);
}
@Test
void assertCheckPrivilegeWithParticularSuccess() throws SQLException {
+ when(preparedStatement.executeQuery()).thenReturn(resultSet);
when(resultSet.next()).thenReturn(true);
when(resultSet.getString(1)).thenReturn("GRANT REPLICATION SLAVE,
REPLICATION CLIENT ON *.* TO '%'@'%'");
new MySQLDatabaseEnvironmentChecker().checkPrivilege(dataSource,
PrivilegeCheckType.PIPELINE);
@@ -68,6 +68,7 @@ class MySQLDatabaseEnvironmentCheckerTest {
@Test
void assertCheckPrivilegeWithAllSuccess() throws SQLException {
+ when(preparedStatement.executeQuery()).thenReturn(resultSet);
when(resultSet.next()).thenReturn(true);
when(resultSet.getString(1)).thenReturn("GRANT ALL PRIVILEGES CLIENT
ON *.* TO '%'@'%'");
new MySQLDatabaseEnvironmentChecker().checkPrivilege(dataSource,
PrivilegeCheckType.PIPELINE);
@@ -75,18 +76,21 @@ class MySQLDatabaseEnvironmentCheckerTest {
}
@Test
- void assertCheckPrivilegeLackPrivileges() {
+ void assertCheckPrivilegeLackPrivileges() throws SQLException {
+ when(preparedStatement.executeQuery()).thenReturn(resultSet);
assertThrows(MissingRequiredPrivilegeException.class, () -> new
MySQLDatabaseEnvironmentChecker().checkPrivilege(dataSource,
PrivilegeCheckType.PIPELINE));
}
@Test
void assertCheckPrivilegeFailure() throws SQLException {
+ when(preparedStatement.executeQuery()).thenReturn(resultSet);
when(resultSet.next()).thenThrow(new SQLException(""));
assertThrows(CheckDatabaseEnvironmentFailedException.class, () -> new
MySQLDatabaseEnvironmentChecker().checkPrivilege(dataSource,
PrivilegeCheckType.PIPELINE));
}
@Test
void assertCheckVariableSuccess() throws SQLException {
+ when(preparedStatement.executeQuery()).thenReturn(resultSet);
when(resultSet.next()).thenReturn(true, true, true, false);
when(resultSet.getString(1)).thenReturn("LOG_BIN", "BINLOG_FORMAT",
"BINLOG_ROW_IMAGE");
when(resultSet.getString(2)).thenReturn("ON", "ROW", "FULL");
@@ -96,6 +100,7 @@ class MySQLDatabaseEnvironmentCheckerTest {
@Test
void assertCheckVariableWithWrongVariable() throws SQLException {
+ when(preparedStatement.executeQuery()).thenReturn(resultSet);
when(resultSet.next()).thenReturn(true, true, false);
when(resultSet.getString(1)).thenReturn("BINLOG_FORMAT", "LOG_BIN");
when(resultSet.getString(2)).thenReturn("ROW", "OFF");
@@ -104,7 +109,50 @@ class MySQLDatabaseEnvironmentCheckerTest {
@Test
void assertCheckVariableFailure() throws SQLException {
+ when(preparedStatement.executeQuery()).thenReturn(resultSet);
when(resultSet.next()).thenThrow(new SQLException(""));
assertThrows(CheckDatabaseEnvironmentFailedException.class, () -> new
MySQLDatabaseEnvironmentChecker().checkVariable(dataSource));
}
+
+ @Test
+ void assertCheckXAPrivilegeWithParticularSuccessInMySQL8() throws
SQLException {
+ when(preparedStatement.executeQuery()).thenReturn(resultSet);
+
when(dataSource.getConnection().getMetaData().getDatabaseMajorVersion()).thenReturn(8);
+ when(resultSet.next()).thenReturn(true);
+ when(resultSet.getString(1)).thenReturn("GRANT XA_RECOVER_ADMIN ON *.*
TO '%'@'%'");
+ new MySQLDatabaseEnvironmentChecker().checkPrivilege(dataSource,
PrivilegeCheckType.XA);
+ verify(preparedStatement).executeQuery();
+ }
+
+ @Test
+ void assertUnCheckXAPrivilegeInMySQL5() throws SQLException {
+
when(dataSource.getConnection().getMetaData().getDatabaseMajorVersion()).thenReturn(5);
+ new MySQLDatabaseEnvironmentChecker().checkPrivilege(dataSource,
PrivilegeCheckType.XA);
+ verify(preparedStatement, times(0)).executeQuery();
+ }
+
+ @Test
+ void assertCheckXAPrivilegeWithAllSuccessInMySQL8() throws SQLException {
+ when(preparedStatement.executeQuery()).thenReturn(resultSet);
+
when(dataSource.getConnection().getMetaData().getDatabaseMajorVersion()).thenReturn(8);
+ when(resultSet.next()).thenReturn(true);
+ when(resultSet.getString(1)).thenReturn("GRANT ALL PRIVILEGES ON *.*
TO '%'@'%'");
+ new MySQLDatabaseEnvironmentChecker().checkPrivilege(dataSource,
PrivilegeCheckType.XA);
+ verify(preparedStatement).executeQuery();
+ }
+
+ @Test
+ void assertCheckXAPrivilegeLackPrivilegesInMySQL8() throws SQLException {
+ when(preparedStatement.executeQuery()).thenReturn(resultSet);
+
when(dataSource.getConnection().getMetaData().getDatabaseMajorVersion()).thenReturn(8);
+ assertThrows(MissingRequiredPrivilegeException.class, () -> new
MySQLDatabaseEnvironmentChecker().checkPrivilege(dataSource,
PrivilegeCheckType.XA));
+ }
+
+ @Test
+ void assertCheckXAPrivilegeFailureInMySQL8() throws SQLException {
+ when(preparedStatement.executeQuery()).thenReturn(resultSet);
+
when(dataSource.getConnection().getMetaData().getDatabaseMajorVersion()).thenReturn(8);
+ when(resultSet.next()).thenThrow(new SQLException(""));
+ assertThrows(CheckDatabaseEnvironmentFailedException.class, () -> new
MySQLDatabaseEnvironmentChecker().checkPrivilege(dataSource,
PrivilegeCheckType.XA));
+ }
}
diff --git
a/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManager.java
b/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManager.java
index 91456f7ac81..87f3825afd9 100644
---
a/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManager.java
+++
b/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/XAShardingSphereTransactionManager.java
@@ -19,6 +19,8 @@ package org.apache.shardingsphere.transaction.xa;
import com.cedarsoftware.util.CaseInsensitiveMap;
import lombok.SneakyThrows;
+import
org.apache.shardingsphere.infra.database.core.checker.DialectDatabaseEnvironmentChecker;
+import
org.apache.shardingsphere.infra.database.core.checker.PrivilegeCheckType;
import
org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
@@ -29,7 +31,6 @@ import
org.apache.shardingsphere.transaction.core.ResourceDataSource;
import
org.apache.shardingsphere.transaction.exception.TransactionTimeoutException;
import
org.apache.shardingsphere.transaction.spi.ShardingSphereDistributionTransactionManager;
import
org.apache.shardingsphere.transaction.xa.jta.datasource.XATransactionDataSource;
-import
org.apache.shardingsphere.transaction.xa.jta.datasource.checker.XATransactionPrivilegeChecker;
import
org.apache.shardingsphere.transaction.xa.spi.XATransactionManagerProvider;
import javax.sql.DataSource;
@@ -59,7 +60,8 @@ public final class XAShardingSphereTransactionManager
implements ShardingSphereD
@Override
public void init(final Map<String, DatabaseType> databaseTypes, final
Map<String, DataSource> dataSources, final String providerType) {
for (Entry<String, DataSource> entry : dataSources.entrySet()) {
-
DatabaseTypedSPILoader.findService(XATransactionPrivilegeChecker.class,
databaseTypes.get(entry.getKey())).ifPresent(optional ->
optional.check(entry.getValue()));
+
DatabaseTypedSPILoader.findService(DialectDatabaseEnvironmentChecker.class,
databaseTypes.get(entry.getKey()))
+ .ifPresent(optional ->
optional.checkPrivilege(entry.getValue(), PrivilegeCheckType.XA));
}
xaTransactionManagerProvider =
TypedSPILoader.getService(XATransactionManagerProvider.class, providerType);
xaTransactionManagerProvider.init();
diff --git
a/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/checker/XATransactionPrivilegeChecker.java
b/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/checker/XATransactionPrivilegeChecker.java
deleted file mode 100644
index c4dd72dbea1..00000000000
---
a/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/checker/XATransactionPrivilegeChecker.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.transaction.xa.jta.datasource.checker;
-
-import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPI;
-
-import javax.sql.DataSource;
-
-/**
- * XA transaction privilege checker.
- */
-public interface XATransactionPrivilegeChecker extends DatabaseTypedSPI {
-
- /**
- * Check XA transaction privilege.
- *
- * @param dataSource data source to be checked
- */
- void check(DataSource dataSource);
-}
diff --git
a/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/checker/dialect/H2XATransactionPrivilegeChecker.java
b/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/checker/dialect/H2XATransactionPrivilegeChecker.java
deleted file mode 100644
index dfde51cf06f..00000000000
---
a/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/checker/dialect/H2XATransactionPrivilegeChecker.java
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package
org.apache.shardingsphere.transaction.xa.jta.datasource.checker.dialect;
-
-import
org.apache.shardingsphere.transaction.xa.jta.datasource.checker.XATransactionPrivilegeChecker;
-
-import javax.sql.DataSource;
-
-/**
- * XA transaction privilege checker of H2.
- */
-public final class H2XATransactionPrivilegeChecker implements
XATransactionPrivilegeChecker {
-
- @Override
- public void check(final DataSource dataSource) {
- }
-
- @Override
- public String getDatabaseType() {
- return "H2";
- }
-}
diff --git
a/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/checker/dialect/MySQLXATransactionPrivilegeChecker.java
b/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/checker/dialect/MySQLXATransactionPrivilegeChecker.java
deleted file mode 100644
index e0c79ccb55a..00000000000
---
a/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/datasource/checker/dialect/MySQLXATransactionPrivilegeChecker.java
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package
org.apache.shardingsphere.transaction.xa.jta.datasource.checker.dialect;
-
-import
org.apache.shardingsphere.infra.exception.core.external.sql.type.wrapper.SQLWrapperException;
-import
org.apache.shardingsphere.transaction.xa.jta.datasource.checker.XATransactionPrivilegeChecker;
-import
org.apache.shardingsphere.transaction.xa.jta.exception.XATransactionPrivilegeCheckException;
-
-import javax.sql.DataSource;
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.util.Arrays;
-
-/**
- * XA transaction privilege checker of MySQL.
- */
-public final class MySQLXATransactionPrivilegeChecker implements
XATransactionPrivilegeChecker {
-
- private static final String SHOW_GRANTS_SQL = "SHOW GRANTS";
-
- private static final String[][] REQUIRED_PRIVILEGES = {{"ALL PRIVILEGES",
"ON *.*"}, {"XA_RECOVER_ADMIN", "ON *.*"}};
-
- private static final int MYSQL_MAJOR_VERSION_8 = 8;
-
- @Override
- public void check(final DataSource dataSource) {
- try (Connection connection = dataSource.getConnection()) {
- if (MYSQL_MAJOR_VERSION_8 ==
connection.getMetaData().getDatabaseMajorVersion()) {
- checkPrivilege(connection);
- }
- } catch (final SQLException ex) {
- throw new SQLWrapperException(ex);
- }
- }
-
- private void checkPrivilege(final Connection connection) {
- try (
- PreparedStatement preparedStatement =
connection.prepareStatement(SHOW_GRANTS_SQL);
- ResultSet resultSet = preparedStatement.executeQuery()) {
- while (resultSet.next()) {
- String privilege = resultSet.getString(1).toUpperCase();
- if (matchPrivileges(privilege)) {
- return;
- }
- }
- } catch (final SQLException ex) {
- throw new XATransactionPrivilegeCheckException("XA_RECOVER_ADMIN",
ex);
- }
- throw new XATransactionPrivilegeCheckException("XA_RECOVER_ADMIN");
- }
-
- private boolean matchPrivileges(final String privilege) {
- return Arrays.stream(REQUIRED_PRIVILEGES).anyMatch(each ->
Arrays.stream(each).allMatch(privilege::contains));
- }
-
- @Override
- public String getDatabaseType() {
- return "MySQL";
- }
-}
diff --git
a/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/exception/XATransactionPrivilegeCheckException.java
b/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/exception/XATransactionPrivilegeCheckException.java
deleted file mode 100644
index 45c971c5b58..00000000000
---
a/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/exception/XATransactionPrivilegeCheckException.java
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.transaction.xa.jta.exception;
-
-import
org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.XOpenSQLState;
-
-import java.sql.SQLException;
-
-/**
- * XA transaction privilege check exception.
- */
-public final class XATransactionPrivilegeCheckException extends
XATransactionSQLException {
-
- private static final long serialVersionUID = 6073175429050058508L;
-
- public XATransactionPrivilegeCheckException(final String privilege) {
- super(XOpenSQLState.INVALID_TRANSACTION_STATE, 2, "Check XA
transaction privileges failed on data source, please grant '%s' to current
user.", privilege);
- }
-
- public XATransactionPrivilegeCheckException(final String privilege, final
SQLException cause) {
- super(XOpenSQLState.INVALID_TRANSACTION_STATE, 2, cause, "Check XA
transaction privileges failed on data source, please grant '%s' to current
user.", privilege);
- }
-}
diff --git
a/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/exception/XATransactionSQLException.java
b/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/exception/XATransactionSQLException.java
index 7a4d4453564..f5fd7313519 100644
---
a/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/exception/XATransactionSQLException.java
+++
b/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/exception/XATransactionSQLException.java
@@ -34,10 +34,6 @@ public abstract class XATransactionSQLException extends
TransactionSQLException
super(sqlState, getErrorCode(errorCode), reason, messageArgs);
}
- protected XATransactionSQLException(final SQLState sqlState, final int
errorCode, final Exception cause, final String reason, final Object...
messageArgs) {
- super(sqlState, getErrorCode(errorCode), cause, reason, messageArgs);
- }
-
private static int getErrorCode(final int errorCode) {
Preconditions.checkArgument(errorCode >= 0 && errorCode < 100, "The
value range of error code should be [0, 100).");
return XA_CODE * 100 + errorCode;
diff --git
a/kernel/transaction/type/xa/core/src/main/resources/META-INF/services/org.apache.shardingsphere.transaction.xa.jta.datasource.checker.XATransactionPrivilegeChecker
b/kernel/transaction/type/xa/core/src/main/resources/META-INF/services/org.apache.shardingsphere.transaction.xa.jta.datasource.checker.XATransactionPrivilegeChecker
deleted file mode 100644
index 9153b6f0e7a..00000000000
---
a/kernel/transaction/type/xa/core/src/main/resources/META-INF/services/org.apache.shardingsphere.transaction.xa.jta.datasource.checker.XATransactionPrivilegeChecker
+++ /dev/null
@@ -1,19 +0,0 @@
-#
-# Licensed to the Apache Software Foundation (ASF) under one or more
-# contributor license agreements. See the NOTICE file distributed with
-# this work for additional information regarding copyright ownership.
-# The ASF licenses this file to You under the Apache License, Version 2.0
-# (the "License"); you may not use this file except in compliance with
-# the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
-org.apache.shardingsphere.transaction.xa.jta.datasource.checker.dialect.MySQLXATransactionPrivilegeChecker
-org.apache.shardingsphere.transaction.xa.jta.datasource.checker.dialect.H2XATransactionPrivilegeChecker
diff --git
a/kernel/transaction/type/xa/core/src/test/java/org/apache/shardingsphere/transaction/xa/jta/datasource/checker/MySQLXATransactionPrivilegeCheckerTest.java
b/kernel/transaction/type/xa/core/src/test/java/org/apache/shardingsphere/transaction/xa/jta/datasource/checker/MySQLXATransactionPrivilegeCheckerTest.java
deleted file mode 100644
index c3f71baccdd..00000000000
---
a/kernel/transaction/type/xa/core/src/test/java/org/apache/shardingsphere/transaction/xa/jta/datasource/checker/MySQLXATransactionPrivilegeCheckerTest.java
+++ /dev/null
@@ -1,98 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.transaction.xa.jta.datasource.checker;
-
-import
org.apache.shardingsphere.transaction.xa.jta.datasource.checker.dialect.MySQLXATransactionPrivilegeChecker;
-import
org.apache.shardingsphere.transaction.xa.jta.exception.XATransactionPrivilegeCheckException;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-import org.mockito.Answers;
-import org.mockito.Mock;
-import org.mockito.junit.jupiter.MockitoExtension;
-
-import javax.sql.DataSource;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-
-import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.mockito.ArgumentMatchers.anyString;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-@ExtendWith(MockitoExtension.class)
-class MySQLXATransactionPrivilegeCheckerTest {
-
- @Mock
- private PreparedStatement preparedStatement;
-
- @Mock
- private ResultSet resultSet;
-
- @Mock(answer = Answers.RETURNS_DEEP_STUBS)
- private DataSource dataSource;
-
- @BeforeEach
- void setUp() throws SQLException {
-
when(dataSource.getConnection().prepareStatement(anyString())).thenReturn(preparedStatement);
- }
-
- @Test
- void assertCheckPrivilegeWithParticularSuccessInMySQL8() throws
SQLException {
- when(preparedStatement.executeQuery()).thenReturn(resultSet);
-
when(dataSource.getConnection().getMetaData().getDatabaseMajorVersion()).thenReturn(8);
- when(resultSet.next()).thenReturn(true);
- when(resultSet.getString(1)).thenReturn("GRANT XA_RECOVER_ADMIN ON *.*
TO '%'@'%'");
- new MySQLXATransactionPrivilegeChecker().check(dataSource);
- verify(preparedStatement).executeQuery();
- }
-
- @Test
- void assertUnCheckPrivilegeInMySQL5() throws SQLException {
-
when(dataSource.getConnection().getMetaData().getDatabaseMajorVersion()).thenReturn(5);
- new MySQLXATransactionPrivilegeChecker().check(dataSource);
- verify(preparedStatement, times(0)).executeQuery();
- }
-
- @Test
- void assertCheckPrivilegeWithAllSuccessInMySQL8() throws SQLException {
- when(preparedStatement.executeQuery()).thenReturn(resultSet);
-
when(dataSource.getConnection().getMetaData().getDatabaseMajorVersion()).thenReturn(8);
- when(resultSet.next()).thenReturn(true);
- when(resultSet.getString(1)).thenReturn("GRANT ALL PRIVILEGES ON *.*
TO '%'@'%'");
- new MySQLXATransactionPrivilegeChecker().check(dataSource);
- verify(preparedStatement).executeQuery();
- }
-
- @Test
- void assertCheckPrivilegeLackPrivilegesInMySQL8() throws SQLException {
- when(preparedStatement.executeQuery()).thenReturn(resultSet);
-
when(dataSource.getConnection().getMetaData().getDatabaseMajorVersion()).thenReturn(8);
- assertThrows(XATransactionPrivilegeCheckException.class, () -> new
MySQLXATransactionPrivilegeChecker().check(dataSource));
- }
-
- @Test
- void assertCheckPrivilegeFailureInMySQL8() throws SQLException {
- when(preparedStatement.executeQuery()).thenReturn(resultSet);
-
when(dataSource.getConnection().getMetaData().getDatabaseMajorVersion()).thenReturn(8);
- when(resultSet.next()).thenThrow(new SQLException(""));
- assertThrows(XATransactionPrivilegeCheckException.class, () -> new
MySQLXATransactionPrivilegeChecker().check(dataSource));
- }
-}