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 2022/04/30 06:03:42 UTC
[shardingsphere] branch master updated: Add StoragePrivilegeHandlerFactory (#17227)
This is an automated email from the ASF dual-hosted git repository.
zhaojinchao 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 342ffd6bddd Add StoragePrivilegeHandlerFactory (#17227)
342ffd6bddd is described below
commit 342ffd6bddda45f71cff78b7d6ce427f5fde33c8
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat Apr 30 14:03:24 2022 +0800
Add StoragePrivilegeHandlerFactory (#17227)
* Add StoragePrivilegeHandlerFactory
* Add javadoc
---
.../prepare/ExecutionPrepareDecoratorFactory.java | 6 +++++
.../natived/builder/StoragePrivilegeBuilder.java | 8 +------
.../builder/StoragePrivilegeHandlerFactory.java | 25 +++++++++++---------
.../builder/dialect/MySQLPrivilegeHandlerTest.java | 26 +++++++++++----------
.../dialect/OraclePrivilegeHandlerTest.java | 27 +++++++++++-----------
.../dialect/PostgreSQLPrivilegeHandlerTest.java | 26 +++++++++++----------
.../dialect/SQLServerPrivilegeHandlerTest.java | 26 +++++++++++----------
7 files changed, 77 insertions(+), 67 deletions(-)
diff --git a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/ExecutionPrepareDecoratorFactory.java b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/ExecutionPrepareDecoratorFactory.java
index 8aeb4365497..1c520a739ec 100644
--- a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/ExecutionPrepareDecoratorFactory.java
+++ b/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/ExecutionPrepareDecoratorFactory.java
@@ -36,6 +36,12 @@ public final class ExecutionPrepareDecoratorFactory {
ShardingSphereServiceLoader.register(ExecutionPrepareDecorator.class);
}
+ /**
+ * Create new instance of execution prepare decorator.
+ *
+ * @param rules rules
+ * @return new instance of execution prepare decorator.
+ */
@SuppressWarnings("rawtypes")
public static Map<ShardingSphereRule, ExecutionPrepareDecorator> newInstance(final Collection<ShardingSphereRule> rules) {
return OrderedSPIRegistry.getRegisteredServices(ExecutionPrepareDecorator.class, rules);
diff --git a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/natived/builder/StoragePrivilegeBuilder.java b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/natived/builder/StoragePrivilegeBuilder.java
index 4608abcde3e..66c37bb67fa 100644
--- a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/natived/builder/StoragePrivilegeBuilder.java
+++ b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/natived/builder/StoragePrivilegeBuilder.java
@@ -27,8 +27,6 @@ import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.executor.kernel.ExecutorEngine;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
-import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
-import org.apache.shardingsphere.spi.type.typed.TypedSPIRegistry;
import javax.sql.DataSource;
import java.sql.SQLException;
@@ -53,10 +51,6 @@ public final class StoragePrivilegeBuilder {
private static final long FUTURE_GET_TIME_OUT_MILLISECONDS = 5000L;
- static {
- ShardingSphereServiceLoader.register(StoragePrivilegeHandler.class);
- }
-
/**
* Build privileges.
*
@@ -84,7 +78,7 @@ public final class StoragePrivilegeBuilder {
private static Map<ShardingSphereUser, NativePrivileges> buildPrivilegesInStorage(final ShardingSphereMetaData metaData, final Collection<ShardingSphereUser> users) {
DatabaseType databaseType = DatabaseTypeRecognizer.getDatabaseType(metaData.getResource().getAllInstanceDataSources());
- Optional<StoragePrivilegeHandler> handler = TypedSPIRegistry.findRegisteredService(StoragePrivilegeHandler.class, databaseType.getName());
+ Optional<StoragePrivilegeHandler> handler = StoragePrivilegeHandlerFactory.newInstance(databaseType.getName());
if (!handler.isPresent()) {
return buildPrivilegesInCache(users);
}
diff --git a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/ExecutionPrepareDecoratorFactory.java b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/natived/builder/StoragePrivilegeHandlerFactory.java
similarity index 59%
copy from shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/ExecutionPrepareDecoratorFactory.java
copy to shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/natived/builder/StoragePrivilegeHandlerFactory.java
index 8aeb4365497..3009bf29599 100644
--- a/shardingsphere-infra/shardingsphere-infra-executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/prepare/ExecutionPrepareDecoratorFactory.java
+++ b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/main/java/org/apache/shardingsphere/authority/provider/natived/builder/StoragePrivilegeHandlerFactory.java
@@ -15,29 +15,32 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.executor.sql.prepare;
+package org.apache.shardingsphere.authority.provider.natived.builder;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
-import org.apache.shardingsphere.spi.type.ordered.OrderedSPIRegistry;
+import org.apache.shardingsphere.spi.type.typed.TypedSPIRegistry;
-import java.util.Collection;
-import java.util.Map;
+import java.util.Optional;
/**
- * Execution prepare decorator factory.
+ * Storage privilege handler factory.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class ExecutionPrepareDecoratorFactory {
+public final class StoragePrivilegeHandlerFactory {
static {
- ShardingSphereServiceLoader.register(ExecutionPrepareDecorator.class);
+ ShardingSphereServiceLoader.register(StoragePrivilegeHandler.class);
}
- @SuppressWarnings("rawtypes")
- public static Map<ShardingSphereRule, ExecutionPrepareDecorator> newInstance(final Collection<ShardingSphereRule> rules) {
- return OrderedSPIRegistry.getRegisteredServices(ExecutionPrepareDecorator.class, rules);
+ /**
+ * Create new instance of storage privilege handler.
+ *
+ * @param type type of storage privilege handler
+ * @return new instance of storage privilege handler
+ */
+ public static Optional<StoragePrivilegeHandler> newInstance(final String type) {
+ return TypedSPIRegistry.findRegisteredService(StoragePrivilegeHandler.class, type);
}
}
diff --git a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/MySQLPrivilegeHandlerTest.java b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/MySQLPrivilegeHandlerTest.java
index 6a855ca8a8f..b0611d62262 100644
--- a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/MySQLPrivilegeHandlerTest.java
+++ b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/MySQLPrivilegeHandlerTest.java
@@ -19,11 +19,9 @@ package org.apache.shardingsphere.authority.provider.natived.builder.dialect;
import org.apache.shardingsphere.authority.model.PrivilegeType;
import org.apache.shardingsphere.authority.provider.natived.builder.StoragePrivilegeHandler;
+import org.apache.shardingsphere.authority.provider.natived.builder.StoragePrivilegeHandlerFactory;
import org.apache.shardingsphere.authority.provider.natived.model.privilege.NativePrivileges;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
-import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
-import org.apache.shardingsphere.spi.type.typed.TypedSPIRegistry;
-import org.junit.BeforeClass;
import org.junit.Test;
import javax.sql.DataSource;
@@ -35,6 +33,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;
+import java.util.Optional;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.stream.Collectors;
@@ -48,24 +47,23 @@ import static org.mockito.Mockito.when;
public final class MySQLPrivilegeHandlerTest {
- @BeforeClass
- public static void setUp() {
- ShardingSphereServiceLoader.register(StoragePrivilegeHandler.class);
- }
-
@Test
public void assertDiff() throws SQLException {
Collection<ShardingSphereUser> newUsers = createUsers();
newUsers.add(new ShardingSphereUser("root", "", "127.0.0.2"));
DataSource dataSource = mockDataSourceForUsers(newUsers);
- assertDiffUsers(TypedSPIRegistry.getRegisteredService(StoragePrivilegeHandler.class, "MySQL").diff(newUsers, dataSource));
+ Optional<StoragePrivilegeHandler> storagePrivilegeHandler = StoragePrivilegeHandlerFactory.newInstance("MySQL");
+ assertTrue(storagePrivilegeHandler.isPresent());
+ assertDiffUsers(storagePrivilegeHandler.get().diff(newUsers, dataSource));
}
@Test
public void assertCreate() throws SQLException {
Collection<ShardingSphereUser> users = createUsers();
DataSource dataSource = mockDataSourceForUsers(users);
- TypedSPIRegistry.getRegisteredService(StoragePrivilegeHandler.class, "MySQL").create(users, dataSource);
+ Optional<StoragePrivilegeHandler> storagePrivilegeHandler = StoragePrivilegeHandlerFactory.newInstance("MySQL");
+ assertTrue(storagePrivilegeHandler.isPresent());
+ storagePrivilegeHandler.get().create(users, dataSource);
assertCreateUsers(users, dataSource.getConnection().createStatement());
}
@@ -73,7 +71,9 @@ public final class MySQLPrivilegeHandlerTest {
public void assertGrantAll() throws SQLException {
Collection<ShardingSphereUser> users = createUsers();
DataSource dataSource = mockDataSourceForUsers(users);
- TypedSPIRegistry.getRegisteredService(StoragePrivilegeHandler.class, "MySQL").grantAll(users, dataSource);
+ Optional<StoragePrivilegeHandler> storagePrivilegeHandler = StoragePrivilegeHandlerFactory.newInstance("MySQL");
+ assertTrue(storagePrivilegeHandler.isPresent());
+ storagePrivilegeHandler.get().grantAll(users, dataSource);
assertGrantUsersAll(users, dataSource.getConnection().createStatement());
}
@@ -81,7 +81,9 @@ public final class MySQLPrivilegeHandlerTest {
public void assertLoad() throws SQLException {
Collection<ShardingSphereUser> users = createUsers();
DataSource dataSource = mockDataSourceForPrivileges(users);
- assertPrivileges(TypedSPIRegistry.getRegisteredService(StoragePrivilegeHandler.class, "MySQL").load(users, dataSource));
+ Optional<StoragePrivilegeHandler> storagePrivilegeHandler = StoragePrivilegeHandlerFactory.newInstance("MySQL");
+ assertTrue(storagePrivilegeHandler.isPresent());
+ assertPrivileges(storagePrivilegeHandler.get().load(users, dataSource));
}
private Collection<ShardingSphereUser> createUsers() {
diff --git a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/OraclePrivilegeHandlerTest.java b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/OraclePrivilegeHandlerTest.java
index 4af375557c9..138d5f91a47 100644
--- a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/OraclePrivilegeHandlerTest.java
+++ b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/OraclePrivilegeHandlerTest.java
@@ -19,12 +19,10 @@ package org.apache.shardingsphere.authority.provider.natived.builder.dialect;
import org.apache.shardingsphere.authority.model.PrivilegeType;
import org.apache.shardingsphere.authority.provider.natived.builder.StoragePrivilegeHandler;
+import org.apache.shardingsphere.authority.provider.natived.builder.StoragePrivilegeHandlerFactory;
import org.apache.shardingsphere.authority.provider.natived.model.privilege.NativePrivileges;
import org.apache.shardingsphere.authority.provider.natived.model.privilege.database.SchemaPrivileges;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
-import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
-import org.apache.shardingsphere.spi.type.typed.TypedSPIRegistry;
-import org.junit.BeforeClass;
import org.junit.Test;
import javax.sql.DataSource;
@@ -36,6 +34,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;
+import java.util.Optional;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.stream.Collectors;
@@ -49,25 +48,23 @@ import static org.mockito.Mockito.when;
public final class OraclePrivilegeHandlerTest {
- @BeforeClass
- public static void setUp() {
- ShardingSphereServiceLoader.register(StoragePrivilegeHandler.class);
- }
-
@Test
public void assertDiff() throws SQLException {
Collection<ShardingSphereUser> newUsers = createUsers();
newUsers.add(new ShardingSphereUser("user", "", ""));
DataSource dataSource = mockDataSourceForUsers(newUsers);
- Collection<ShardingSphereUser> result = TypedSPIRegistry.getRegisteredService(StoragePrivilegeHandler.class, "Oracle").diff(newUsers, dataSource);
- assertDiffUsers(result);
+ Optional<StoragePrivilegeHandler> storagePrivilegeHandler = StoragePrivilegeHandlerFactory.newInstance("Oracle");
+ assertTrue(storagePrivilegeHandler.isPresent());
+ assertDiffUsers(storagePrivilegeHandler.get().diff(newUsers, dataSource));
}
@Test
public void assertCreate() throws SQLException {
Collection<ShardingSphereUser> users = createUsers();
DataSource dataSource = mockDataSourceForUsers(users);
- TypedSPIRegistry.getRegisteredService(StoragePrivilegeHandler.class, "Oracle").create(users, dataSource);
+ Optional<StoragePrivilegeHandler> storagePrivilegeHandler = StoragePrivilegeHandlerFactory.newInstance("Oracle");
+ assertTrue(storagePrivilegeHandler.isPresent());
+ storagePrivilegeHandler.get().create(users, dataSource);
assertCreateUsers(users, dataSource.getConnection().createStatement());
}
@@ -75,7 +72,9 @@ public final class OraclePrivilegeHandlerTest {
public void assertGrantAll() throws SQLException {
Collection<ShardingSphereUser> users = createUsers();
DataSource dataSource = mockDataSourceForUsers(users);
- TypedSPIRegistry.getRegisteredService(StoragePrivilegeHandler.class, "Oracle").grantAll(users, dataSource);
+ Optional<StoragePrivilegeHandler> storagePrivilegeHandler = StoragePrivilegeHandlerFactory.newInstance("Oracle");
+ assertTrue(storagePrivilegeHandler.isPresent());
+ storagePrivilegeHandler.get().grantAll(users, dataSource);
assertGrantUsersAll(users, dataSource.getConnection().createStatement());
}
@@ -83,7 +82,9 @@ public final class OraclePrivilegeHandlerTest {
public void assertLoad() throws SQLException {
Collection<ShardingSphereUser> users = createUsers();
DataSource dataSource = mockDataSource(users);
- assertPrivileges(TypedSPIRegistry.getRegisteredService(StoragePrivilegeHandler.class, "Oracle").load(users, dataSource));
+ Optional<StoragePrivilegeHandler> storagePrivilegeHandler = StoragePrivilegeHandlerFactory.newInstance("Oracle");
+ assertTrue(storagePrivilegeHandler.isPresent());
+ assertPrivileges(storagePrivilegeHandler.get().load(users, dataSource));
}
private void assertCreateUsers(final Collection<ShardingSphereUser> users, final Statement statement) throws SQLException {
diff --git a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/PostgreSQLPrivilegeHandlerTest.java b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/PostgreSQLPrivilegeHandlerTest.java
index 269742f6812..87134966f5a 100644
--- a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/PostgreSQLPrivilegeHandlerTest.java
+++ b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/PostgreSQLPrivilegeHandlerTest.java
@@ -19,12 +19,10 @@ package org.apache.shardingsphere.authority.provider.natived.builder.dialect;
import org.apache.shardingsphere.authority.model.PrivilegeType;
import org.apache.shardingsphere.authority.provider.natived.builder.StoragePrivilegeHandler;
+import org.apache.shardingsphere.authority.provider.natived.builder.StoragePrivilegeHandlerFactory;
import org.apache.shardingsphere.authority.provider.natived.model.privilege.NativePrivileges;
import org.apache.shardingsphere.authority.provider.natived.model.privilege.database.SchemaPrivileges;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
-import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
-import org.apache.shardingsphere.spi.type.typed.TypedSPIRegistry;
-import org.junit.BeforeClass;
import org.junit.Test;
import javax.sql.DataSource;
@@ -36,6 +34,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;
+import java.util.Optional;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.stream.Collectors;
@@ -49,24 +48,23 @@ import static org.mockito.Mockito.when;
public final class PostgreSQLPrivilegeHandlerTest {
- @BeforeClass
- public static void setUp() {
- ShardingSphereServiceLoader.register(StoragePrivilegeHandler.class);
- }
-
@Test
public void assertDiff() throws SQLException {
Collection<ShardingSphereUser> newUsers = createUsers();
newUsers.add(new ShardingSphereUser("postgres2", "", ""));
DataSource dataSource = mockDataSourceForUsers(newUsers);
- assertDiffUsers(TypedSPIRegistry.getRegisteredService(StoragePrivilegeHandler.class, "PostgreSQL").diff(newUsers, dataSource));
+ Optional<StoragePrivilegeHandler> storagePrivilegeHandler = StoragePrivilegeHandlerFactory.newInstance("PostgreSQL");
+ assertTrue(storagePrivilegeHandler.isPresent());
+ assertDiffUsers(storagePrivilegeHandler.get().diff(newUsers, dataSource));
}
@Test
public void assertCreate() throws SQLException {
Collection<ShardingSphereUser> users = createUsers();
DataSource dataSource = mockDataSourceForUsers(users);
- TypedSPIRegistry.getRegisteredService(StoragePrivilegeHandler.class, "PostgreSQL").create(users, dataSource);
+ Optional<StoragePrivilegeHandler> storagePrivilegeHandler = StoragePrivilegeHandlerFactory.newInstance("PostgreSQL");
+ assertTrue(storagePrivilegeHandler.isPresent());
+ storagePrivilegeHandler.get().create(users, dataSource);
assertCreateUsers(users, dataSource.getConnection().createStatement());
}
@@ -74,7 +72,9 @@ public final class PostgreSQLPrivilegeHandlerTest {
public void assertGrantAll() throws SQLException {
Collection<ShardingSphereUser> users = createUsers();
DataSource dataSource = mockDataSourceForUsers(users);
- TypedSPIRegistry.getRegisteredService(StoragePrivilegeHandler.class, "PostgreSQL").grantAll(users, dataSource);
+ Optional<StoragePrivilegeHandler> storagePrivilegeHandler = StoragePrivilegeHandlerFactory.newInstance("PostgreSQL");
+ assertTrue(storagePrivilegeHandler.isPresent());
+ storagePrivilegeHandler.get().grantAll(users, dataSource);
assertGrantUsersAll(users, dataSource.getConnection().createStatement());
}
@@ -82,7 +82,9 @@ public final class PostgreSQLPrivilegeHandlerTest {
public void assertLoad() throws SQLException {
Collection<ShardingSphereUser> users = createUsers();
DataSource dataSource = mockDataSource(users);
- assertPrivileges(TypedSPIRegistry.getRegisteredService(StoragePrivilegeHandler.class, "PostgreSQL").load(users, dataSource));
+ Optional<StoragePrivilegeHandler> storagePrivilegeHandler = StoragePrivilegeHandlerFactory.newInstance("PostgreSQL");
+ assertTrue(storagePrivilegeHandler.isPresent());
+ assertPrivileges(storagePrivilegeHandler.get().load(users, dataSource));
}
private void assertPrivileges(final Map<ShardingSphereUser, NativePrivileges> actual) {
diff --git a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/SQLServerPrivilegeHandlerTest.java b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/SQLServerPrivilegeHandlerTest.java
index 23023df2c74..2a7ae5189d9 100644
--- a/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/SQLServerPrivilegeHandlerTest.java
+++ b/shardingsphere-kernel/shardingsphere-authority/shardingsphere-authority-core/src/test/java/org/apache/shardingsphere/authority/provider/natived/builder/dialect/SQLServerPrivilegeHandlerTest.java
@@ -19,12 +19,10 @@ package org.apache.shardingsphere.authority.provider.natived.builder.dialect;
import org.apache.shardingsphere.authority.model.PrivilegeType;
import org.apache.shardingsphere.authority.provider.natived.builder.StoragePrivilegeHandler;
+import org.apache.shardingsphere.authority.provider.natived.builder.StoragePrivilegeHandlerFactory;
import org.apache.shardingsphere.authority.provider.natived.model.privilege.NativePrivileges;
import org.apache.shardingsphere.authority.provider.natived.model.privilege.database.SchemaPrivileges;
import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
-import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
-import org.apache.shardingsphere.spi.type.typed.TypedSPIRegistry;
-import org.junit.BeforeClass;
import org.junit.Test;
import javax.sql.DataSource;
@@ -37,6 +35,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;
+import java.util.Optional;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.stream.Collectors;
@@ -50,24 +49,23 @@ import static org.mockito.Mockito.when;
public final class SQLServerPrivilegeHandlerTest {
- @BeforeClass
- public static void setUp() {
- ShardingSphereServiceLoader.register(StoragePrivilegeHandler.class);
- }
-
@Test
public void assertDiff() throws SQLException {
Collection<ShardingSphereUser> newUsers = createUsers();
newUsers.add(new ShardingSphereUser("testUser2", "", ""));
DataSource dataSource = mockDataSourceForUsers(newUsers);
- assertDiffUsers(TypedSPIRegistry.getRegisteredService(StoragePrivilegeHandler.class, "SQLServer").diff(newUsers, dataSource));
+ Optional<StoragePrivilegeHandler> storagePrivilegeHandler = StoragePrivilegeHandlerFactory.newInstance("SQLServer");
+ assertTrue(storagePrivilegeHandler.isPresent());
+ assertDiffUsers(storagePrivilegeHandler.get().diff(newUsers, dataSource));
}
@Test
public void assertCreate() throws SQLException {
Collection<ShardingSphereUser> users = createUsers();
DataSource dataSource = mockDataSourceForUsers(users);
- TypedSPIRegistry.getRegisteredService(StoragePrivilegeHandler.class, "SQLServer").create(users, dataSource);
+ Optional<StoragePrivilegeHandler> storagePrivilegeHandler = StoragePrivilegeHandlerFactory.newInstance("SQLServer");
+ assertTrue(storagePrivilegeHandler.isPresent());
+ storagePrivilegeHandler.get().create(users, dataSource);
assertCreateUsers(users, dataSource.getConnection().createStatement());
}
@@ -75,7 +73,9 @@ public final class SQLServerPrivilegeHandlerTest {
public void assertGrantAll() throws SQLException {
Collection<ShardingSphereUser> users = createUsers();
DataSource dataSource = mockDataSourceForUsers(users);
- TypedSPIRegistry.getRegisteredService(StoragePrivilegeHandler.class, "SQLServer").grantAll(users, dataSource);
+ Optional<StoragePrivilegeHandler> storagePrivilegeHandler = StoragePrivilegeHandlerFactory.newInstance("SQLServer");
+ assertTrue(storagePrivilegeHandler.isPresent());
+ storagePrivilegeHandler.get().grantAll(users, dataSource);
assertGrantUsersAll(users, dataSource.getConnection().createStatement());
}
@@ -83,7 +83,9 @@ public final class SQLServerPrivilegeHandlerTest {
public void assertLoad() throws SQLException {
Collection<ShardingSphereUser> users = createUsers();
DataSource dataSource = mockDataSource(users);
- assertPrivileges(TypedSPIRegistry.getRegisteredService(StoragePrivilegeHandler.class, "SQLServer").load(users, dataSource));
+ Optional<StoragePrivilegeHandler> storagePrivilegeHandler = StoragePrivilegeHandlerFactory.newInstance("SQLServer");
+ assertTrue(storagePrivilegeHandler.isPresent());
+ assertPrivileges(storagePrivilegeHandler.get().load(users, dataSource));
}
private void assertPrivileges(final Map<ShardingSphereUser, NativePrivileges> actual) {