This is an automated email from the ASF dual-hosted git repository.

jianbin pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/incubator-seata.git


The following commit(s) were added to refs/heads/2.x by this push:
     new 721dce6a47 optimize: Add some checks to RegistryFactory (#7345)
721dce6a47 is described below

commit 721dce6a47a64b33cf154ff07adeee890010e029
Author: YoWuwuuuw <2216348...@qq.com>
AuthorDate: Mon May 26 10:27:49 2025 +0800

    optimize: Add some checks to RegistryFactory (#7345)
---
 changes/en-us/2.x.md                               |   4 +-
 changes/zh-cn/2.x.md                               |   3 +-
 discovery/seata-discovery-core/pom.xml             |   6 +
 .../discovery/registry/MultiRegistryFactory.java   |  33 +++--
 .../seata/discovery/registry/RegistryFactory.java  |  20 +--
 .../seata/discovery/registry/RegistryType.java     |   4 +-
 .../registry/MockNacosRegistryProvider.java        |  30 ++++
 .../registry/MockNacosRegistryService.java         |  57 ++++++++
 .../registry/MultiRegistryFactoryTest.java         | 153 +++++++++++++++++++++
 .../discovery/registry/RegistryFactoryTest.java    |  92 +++++++++++++
 ...pache.seata.discovery.registry.RegistryProvider |  17 +++
 .../nacos/NacosRegistryServiceImplTest.java        |   2 +-
 12 files changed, 392 insertions(+), 29 deletions(-)

diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md
index a8fef0c576..dbfcfbc29c 100644
--- a/changes/en-us/2.x.md
+++ b/changes/en-us/2.x.md
@@ -37,8 +37,9 @@ Add changes here for all PR submitted to the 2.x branch.
 - [[#7282](https://github.com/apache/incubator-seata/pull/7282)] optimize 
unexpected NullPointerException in lookup method in FileRegistryServiceImpl 
class
 - [[#7310](https://github.com/seata/seata/pull/7310)] Optimize minor issues in 
the naming-server
 - [[#7329](https://github.com/apache/incubator-seata/pull/7329)] upgrade 
tomcat to 9.0.100
-- [[#7344](https://github.com/apache/incubator-seata/pull/7344)] raft mode 
performs transaction size check in advance
 - [[#7337](https://github.com/apache/incubator-seata/pull/7337)] Add 
ChannelEventListener support to prevent memory leaks
+- [[#7344](https://github.com/apache/incubator-seata/pull/7344)] raft mode 
performs transaction size check in advance
+- [[#7345](https://github.com/apache/incubator-seata/pull/7345)] add empty 
check and duplicate type check to RegistryFactory
 - [[#7350](https://github.com/apache/incubator-seata/pull/7350)] optimize 
codecov.yml
 - [[#7360](https://github.com/apache/incubator-seata/pull/7360)] Update 
resource cleanup logic for channel disconnection
 
@@ -70,6 +71,7 @@ Add changes here for all PR submitted to the 2.x branch.
 - [[#7205](https://github.com/apache/incubator-seata/issues/7205)] add UT for 
namingserver module
 - [[#7359](https://github.com/apache/incubator-seata/issues/7359)] merge 
submodule test reports
 
+
 ### refactor:
 
 - [[#7315](https://github.com/apache/incubator-seata/pull/7315)] Refactor log 
testing to use ListAppender for more accurate and efficient log capture
diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md
index dc139bd5be..5e6c0d17ed 100644
--- a/changes/zh-cn/2.x.md
+++ b/changes/zh-cn/2.x.md
@@ -35,8 +35,9 @@
 - [[#7282](https://github.com/apache/incubator-seata/pull/7282)] 
优化FileRegistryServiceImpl类lookup的NullPointerException问题
 - [[#7310](https://github.com/seata/seata/pull/7310)] 优化naming-server中的一些小问题
 - [[#7329](https://github.com/apache/incubator-seata/pull/7329)] 将 tomcat 升级到 
9.0.100
-- [[#7344](https://github.com/apache/incubator-seata/pull/7344)] raft模式提前检查事务大小
 - [[#7337](https://github.com/apache/incubator-seata/pull/7337)] 添加 
ChannelEventListener 支持以防止内存泄漏
+- [[#7344](https://github.com/apache/incubator-seata/pull/7344)] raft模式提前检查事务大小
+- [[#7345](https://github.com/apache/incubator-seata/pull/7345)] 为 
RegistryFactory 增加空校验与重复类型检查
 - [[#7350](https://github.com/apache/incubator-seata/pull/7350)] 优化单测覆盖配置
 - [[#7360](https://github.com/apache/incubator-seata/pull/7360)] 
更新通道断开连接时的资源清理逻辑
 
diff --git a/discovery/seata-discovery-core/pom.xml 
b/discovery/seata-discovery-core/pom.xml
index 7aa7b77b37..412be452fd 100644
--- a/discovery/seata-discovery-core/pom.xml
+++ b/discovery/seata-discovery-core/pom.xml
@@ -35,5 +35,11 @@
             <artifactId>seata-config-core</artifactId>
             <version>${project.version}</version>
         </dependency>
+
+        <dependency>
+            <groupId>ch.qos.logback</groupId>
+            <artifactId>logback-classic</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
diff --git 
a/discovery/seata-discovery-core/src/main/java/org/apache/seata/discovery/registry/MultiRegistryFactory.java
 
b/discovery/seata-discovery-core/src/main/java/org/apache/seata/discovery/registry/MultiRegistryFactory.java
index 3d2803f7a0..34c15ab81e 100644
--- 
a/discovery/seata-discovery-core/src/main/java/org/apache/seata/discovery/registry/MultiRegistryFactory.java
+++ 
b/discovery/seata-discovery-core/src/main/java/org/apache/seata/discovery/registry/MultiRegistryFactory.java
@@ -17,12 +17,14 @@
 package org.apache.seata.discovery.registry;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 import java.util.Objects;
+import java.util.Set;
+import java.util.TreeSet;
 
 import org.apache.seata.common.ConfigurationKeys;
 import org.apache.seata.common.Constants;
-import org.apache.seata.common.exception.NotSupportYetException;
 import org.apache.seata.common.loader.EnhancedServiceLoader;
 import org.apache.seata.common.util.StringUtils;
 import org.apache.seata.config.ConfigurationFactory;
@@ -31,7 +33,6 @@ import org.slf4j.LoggerFactory;
 
 /**
  * The type multiple Registry factory.
- *
  */
 public class MultiRegistryFactory {
 
@@ -48,25 +49,27 @@ public class MultiRegistryFactory {
 
     private static List<RegistryService> buildRegistryServices() {
         List<RegistryService> registryServices = new ArrayList<>();
-        String registryTypeNamesStr =
-            
ConfigurationFactory.CURRENT_FILE_INSTANCE.getConfig(ConfigurationKeys.FILE_ROOT_REGISTRY
-                + ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR + 
ConfigurationKeys.FILE_ROOT_TYPE);
+
+        String registryTypeNamesStr = 
ConfigurationFactory.CURRENT_FILE_INSTANCE.getConfig(
+                ConfigurationKeys.FILE_ROOT_REGISTRY + 
ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR + ConfigurationKeys.FILE_ROOT_TYPE);
+
+        // If blank, use default configuration
         if (StringUtils.isBlank(registryTypeNamesStr)) {
             registryTypeNamesStr = RegistryType.File.name();
         }
-        String[] registryTypeNames = 
registryTypeNamesStr.split(Constants.REGISTRY_TYPE_SPLIT_CHAR);
-        if (registryTypeNames.length > 1) {
-            LOGGER.info("use multi registry center type: {}", 
registryTypeNamesStr);
+
+        Set<String> registryTypeNames = new 
TreeSet<>(String.CASE_INSENSITIVE_ORDER);
+        
registryTypeNames.addAll(Arrays.asList(registryTypeNamesStr.split(Constants.REGISTRY_TYPE_SPLIT_CHAR)));
+
+        if (registryTypeNames.size() > 1) {
+            LOGGER.info("use multi registry center type: {}", 
registryTypeNames);
         }
+
         for (String registryTypeName : registryTypeNames) {
-            RegistryType registryType;
-            try {
-                registryType = RegistryType.getType(registryTypeName);
-            } catch (Exception exx) {
-                throw new NotSupportYetException("not support registry type: " 
+ registryTypeName);
-            }
+            RegistryType registryType = RegistryType.getType(registryTypeName);
+
             RegistryService registryService = EnhancedServiceLoader
-                .load(RegistryProvider.class, 
Objects.requireNonNull(registryType).name()).provide();
+                    .load(RegistryProvider.class, 
Objects.requireNonNull(registryType).name()).provide();
             registryServices.add(registryService);
         }
         return registryServices;
diff --git 
a/discovery/seata-discovery-core/src/main/java/org/apache/seata/discovery/registry/RegistryFactory.java
 
b/discovery/seata-discovery-core/src/main/java/org/apache/seata/discovery/registry/RegistryFactory.java
index 501725c824..45910382e4 100644
--- 
a/discovery/seata-discovery-core/src/main/java/org/apache/seata/discovery/registry/RegistryFactory.java
+++ 
b/discovery/seata-discovery-core/src/main/java/org/apache/seata/discovery/registry/RegistryFactory.java
@@ -18,8 +18,8 @@ package org.apache.seata.discovery.registry;
 
 import java.util.Objects;
 
-import org.apache.seata.common.exception.NotSupportYetException;
 import org.apache.seata.common.loader.EnhancedServiceLoader;
+import org.apache.seata.common.util.StringUtils;
 import org.apache.seata.config.ConfigurationFactory;
 import org.apache.seata.config.ConfigurationKeys;
 import org.slf4j.Logger;
@@ -27,7 +27,6 @@ import org.slf4j.LoggerFactory;
 
 /**
  * The type Registry factory.
- *
  */
 public class RegistryFactory {
 
@@ -43,16 +42,17 @@ public class RegistryFactory {
     }
 
     private static RegistryService buildRegistryService() {
-        RegistryType registryType;
         String registryTypeName = 
ConfigurationFactory.CURRENT_FILE_INSTANCE.getConfig(
-            ConfigurationKeys.FILE_ROOT_REGISTRY + 
ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR
-                + ConfigurationKeys.FILE_ROOT_TYPE);
-        LOGGER.info("use registry center type: {}", registryTypeName);
-        try {
-            registryType = RegistryType.getType(registryTypeName);
-        } catch (Exception exx) {
-            throw new NotSupportYetException("not support registry type: " + 
registryTypeName);
+                ConfigurationKeys.FILE_ROOT_REGISTRY + 
ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR + ConfigurationKeys.FILE_ROOT_TYPE);
+
+        // If blank, use default configuration
+        if (StringUtils.isBlank(registryTypeName)) {
+            registryTypeName = RegistryType.File.name();
         }
+
+        LOGGER.info("use registry center type: {}", registryTypeName);
+
+        RegistryType registryType = RegistryType.getType(registryTypeName);
         return EnhancedServiceLoader.load(RegistryProvider.class, 
Objects.requireNonNull(registryType).name()).provide();
 
     }
diff --git 
a/discovery/seata-discovery-core/src/main/java/org/apache/seata/discovery/registry/RegistryType.java
 
b/discovery/seata-discovery-core/src/main/java/org/apache/seata/discovery/registry/RegistryType.java
index cad816d42c..4ae18a3048 100644
--- 
a/discovery/seata-discovery-core/src/main/java/org/apache/seata/discovery/registry/RegistryType.java
+++ 
b/discovery/seata-discovery-core/src/main/java/org/apache/seata/discovery/registry/RegistryType.java
@@ -16,6 +16,8 @@
  */
 package org.apache.seata.discovery.registry;
 
+import org.apache.seata.common.exception.NotSupportYetException;
+
 /**
  * The enum Registry type.
  *
@@ -78,6 +80,6 @@ public enum RegistryType {
                 return registryType;
             }
         }
-        throw new IllegalArgumentException("not support registry type: " + 
name);
+        throw new NotSupportYetException("not support registry type: " + name);
     }
 }
diff --git 
a/discovery/seata-discovery-core/src/test/java/org/apache/seata/discovery/registry/MockNacosRegistryProvider.java
 
b/discovery/seata-discovery-core/src/test/java/org/apache/seata/discovery/registry/MockNacosRegistryProvider.java
new file mode 100644
index 0000000000..efd5529c19
--- /dev/null
+++ 
b/discovery/seata-discovery-core/src/test/java/org/apache/seata/discovery/registry/MockNacosRegistryProvider.java
@@ -0,0 +1,30 @@
+/*
+ * 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.seata.discovery.registry;
+
+import org.apache.seata.common.loader.LoadLevel;
+
+/**
+ * the mock nacos RegistryProvider
+ */
+@LoadLevel(name = "Nacos", order = 1)
+public class MockNacosRegistryProvider implements RegistryProvider {
+    @Override
+    public RegistryService provide() {
+        return new MockNacosRegistryService();
+    }
+} 
\ No newline at end of file
diff --git 
a/discovery/seata-discovery-core/src/test/java/org/apache/seata/discovery/registry/MockNacosRegistryService.java
 
b/discovery/seata-discovery-core/src/test/java/org/apache/seata/discovery/registry/MockNacosRegistryService.java
new file mode 100644
index 0000000000..834c691231
--- /dev/null
+++ 
b/discovery/seata-discovery-core/src/test/java/org/apache/seata/discovery/registry/MockNacosRegistryService.java
@@ -0,0 +1,57 @@
+/*
+ * 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.seata.discovery.registry;
+
+import java.net.InetSocketAddress;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * the mock nacos RegistryService
+ */
+public class MockNacosRegistryService implements RegistryService<Object> {
+
+    @Override
+    public void register(InetSocketAddress address) throws Exception {
+
+    }
+
+    @Override
+    public void unregister(InetSocketAddress address) throws Exception {
+
+    }
+
+    @Override
+    public void subscribe(String cluster, Object listener) throws Exception {
+
+    }
+
+    @Override
+    public void unsubscribe(String cluster, Object listener) throws Exception {
+
+    }
+
+    @Override
+    public List<InetSocketAddress> lookup(String key) throws Exception {
+        return new ArrayList<>();
+    }
+
+    @Override
+    public void close() throws Exception {
+
+    }
+} 
\ No newline at end of file
diff --git 
a/discovery/seata-discovery-core/src/test/java/org/apache/seata/discovery/registry/MultiRegistryFactoryTest.java
 
b/discovery/seata-discovery-core/src/test/java/org/apache/seata/discovery/registry/MultiRegistryFactoryTest.java
new file mode 100644
index 0000000000..fa37c675c3
--- /dev/null
+++ 
b/discovery/seata-discovery-core/src/test/java/org/apache/seata/discovery/registry/MultiRegistryFactoryTest.java
@@ -0,0 +1,153 @@
+/*
+ * 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.seata.discovery.registry;
+
+import ch.qos.logback.classic.Level;
+import ch.qos.logback.classic.Logger;
+import ch.qos.logback.classic.spi.ILoggingEvent;
+import ch.qos.logback.core.read.ListAppender;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import org.apache.seata.common.ConfigurationKeys;
+import org.apache.seata.common.Constants;
+import org.apache.seata.common.exception.NotSupportYetException;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.slf4j.LoggerFactory;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * The type Multi registry factory test.
+ */
+public class MultiRegistryFactoryTest {
+
+    private static final String REGISTRY_TYPE_KEY =
+            ConfigurationKeys.FILE_ROOT_REGISTRY + 
ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR + ConfigurationKeys.FILE_ROOT_TYPE;
+
+    private final List<Logger> watchedLoggers = new ArrayList<>();
+    private final ListAppender<ILoggingEvent> logWatcher = new 
ListAppender<>();
+
+    @BeforeEach
+    void setUp() {
+        logWatcher.start();
+
+        Logger logger = ((Logger) 
LoggerFactory.getLogger(MultiRegistryFactory.class.getName()));
+        logger.addAppender(logWatcher);
+
+        watchedLoggers.add(logger);
+    }
+
+    @AfterEach
+    public void tearDown() {
+        System.clearProperty(REGISTRY_TYPE_KEY);
+        watchedLoggers.forEach(Logger::detachAndStopAllAppenders);
+    }
+
+    /**
+     * Test getInstances with default config.
+     */
+    @Test
+    public void testGetInstancesWithDefaultConfig() {
+        System.setProperty(REGISTRY_TYPE_KEY, RegistryType.File.name());
+
+        List<RegistryService> instances = MultiRegistryFactory.getInstances();
+        assertFalse(instances.isEmpty());
+
+        for (RegistryService service : instances) {
+            assertEquals(FileRegistryServiceImpl.class, service.getClass());
+        }
+    }
+
+    @Test
+    public void testGetInstancesWithSameRegistryTypes() throws Throwable {
+        String sameRegistryType = "File,file";
+        System.setProperty(REGISTRY_TYPE_KEY, sameRegistryType);
+        List<RegistryService> instances = invokeBuildRegistryServices();
+
+        assertEquals(1, instances.size());
+        assertEquals(FileRegistryServiceImpl.class, 
instances.get(0).getClass());
+        assertTrue(getLogs(Level.INFO).isEmpty());
+    }
+
+    @Test
+    public void testGetInstancesWithDifferentRegistryTypes() throws Throwable {
+        String differentRegistryType = "File,file" + 
Constants.REGISTRY_TYPE_SPLIT_CHAR + RegistryType.Nacos.name();
+        System.setProperty(REGISTRY_TYPE_KEY, differentRegistryType);
+        List<RegistryService> instances = invokeBuildRegistryServices();
+
+        assertEquals(2, instances.size());
+        assertEquals(MockNacosRegistryService.class, 
instances.get(1).getClass());
+        assertEquals("use multi registry center type: [File, Nacos]", 
getLogs(Level.INFO).get(0));
+    }
+
+    /**
+     * Test buildRegistryServices with blank registry type.
+     * when the registry type is blank, the default registry type is File
+     */
+    @Test
+    public void testGetInstancesWithBlankRegistryType() throws Throwable {
+        System.setProperty(REGISTRY_TYPE_KEY, "");
+
+        List<RegistryService> instances = invokeBuildRegistryServices();
+        assertEquals(FileRegistryServiceImpl.class, 
instances.get(0).getClass());
+    }
+
+    /**
+     * Test buildRegistryServices with invalid registry type.
+     */
+    @Test
+    public void testGetInstancesWithInvalidRegistryType() {
+        String invalidRegistryType = "InvalidRegistryType";
+        System.setProperty(REGISTRY_TYPE_KEY, invalidRegistryType);
+
+        
assertThatThrownBy(MultiRegistryFactoryTest::invokeBuildRegistryServices)
+                .isExactlyInstanceOf(NotSupportYetException.class)
+                .hasMessage("not support registry type: " + 
invalidRegistryType);
+    }
+
+    /**
+     * Use reflection to call the buildRegistryServices method
+     */
+    private static List<RegistryService> invokeBuildRegistryServices() throws 
Throwable {
+        Method buildMethod = 
MultiRegistryFactory.class.getDeclaredMethod("buildRegistryServices");
+        buildMethod.setAccessible(true);
+
+        try {
+            return (List<RegistryService>) buildMethod.invoke(null);
+        } catch (InvocationTargetException e) {
+            throw e.getTargetException();
+        }
+    }
+
+    private List<String> getLogs(Level level) {
+        return logWatcher.list.stream()
+                .filter(event -> 
event.getLoggerName().endsWith(MultiRegistryFactory.class.getName())
+                        && event.getLevel().equals(level))
+                .map(ILoggingEvent::getFormattedMessage)
+                .collect(Collectors.toList());
+    }
+}
diff --git 
a/discovery/seata-discovery-core/src/test/java/org/apache/seata/discovery/registry/RegistryFactoryTest.java
 
b/discovery/seata-discovery-core/src/test/java/org/apache/seata/discovery/registry/RegistryFactoryTest.java
new file mode 100644
index 0000000000..f0059d9187
--- /dev/null
+++ 
b/discovery/seata-discovery-core/src/test/java/org/apache/seata/discovery/registry/RegistryFactoryTest.java
@@ -0,0 +1,92 @@
+/*
+ * 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.seata.discovery.registry;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import org.apache.seata.common.ConfigurationKeys;
+import org.apache.seata.common.exception.NotSupportYetException;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * The type Registry factory test.
+ */
+public class RegistryFactoryTest {
+
+    private static final String REGISTRY_TYPE_KEY =
+            ConfigurationKeys.FILE_ROOT_REGISTRY + 
ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR + ConfigurationKeys.FILE_ROOT_TYPE;
+
+    @AfterEach
+    public void tearDown() {
+        System.clearProperty(REGISTRY_TYPE_KEY);
+    }
+
+    /**
+     * Test getInstance with default config.
+     */
+    @Test
+    public void testGetInstanceWithDefaultConfig() {
+        System.setProperty(REGISTRY_TYPE_KEY, RegistryType.File.name());
+
+        RegistryService instance = RegistryFactory.getInstance();
+        assertEquals(FileRegistryServiceImpl.class, instance.getClass());
+    }
+
+    /**
+     * Test buildRegistryService with invalid registry type.
+     */
+    @Test
+    public void testGetInstanceOfInvalidRegistryType() {
+        String invalidRegistryType = "InvalidRegistryType";
+        System.setProperty(REGISTRY_TYPE_KEY, invalidRegistryType);
+
+        assertThatThrownBy(RegistryFactoryTest::invokeBuildRegistryService)
+            .isExactlyInstanceOf(NotSupportYetException.class)
+            .hasMessage("not support registry type: " + invalidRegistryType);
+    }
+
+    /**
+     * Test buildRegistryService with blank registry type.
+     * when the registry type is blank, the default registry type is File
+     */
+    @Test
+    public void testGetInstancesWithBlankRegistryType() throws Throwable {
+        System.setProperty(REGISTRY_TYPE_KEY, "");
+
+        RegistryService instance = invokeBuildRegistryService();
+        assertEquals(FileRegistryServiceImpl.class, instance.getClass());
+    }
+
+    /**
+     * Use reflection to call the buildRegistryService method
+     */
+    private static RegistryService invokeBuildRegistryService() throws 
Throwable {
+        Method buildMethod = 
RegistryFactory.class.getDeclaredMethod("buildRegistryService");
+
+        buildMethod.setAccessible(true);
+        try {
+            return (RegistryService) buildMethod.invoke(null);
+        } catch (InvocationTargetException e) {
+            throw e.getTargetException();
+        }
+    }
+}
diff --git 
a/discovery/seata-discovery-core/src/test/resources/META-INF/services/org.apache.seata.discovery.registry.RegistryProvider
 
b/discovery/seata-discovery-core/src/test/resources/META-INF/services/org.apache.seata.discovery.registry.RegistryProvider
new file mode 100644
index 0000000000..f3809a6d55
--- /dev/null
+++ 
b/discovery/seata-discovery-core/src/test/resources/META-INF/services/org.apache.seata.discovery.registry.RegistryProvider
@@ -0,0 +1,17 @@
+#
+# 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.seata.discovery.registry.MockNacosRegistryProvider 
\ No newline at end of file
diff --git 
a/discovery/seata-discovery-nacos/src/test/java/org/apache/seata/discovery/registry/nacos/NacosRegistryServiceImplTest.java
 
b/discovery/seata-discovery-nacos/src/test/java/org/apache/seata/discovery/registry/nacos/NacosRegistryServiceImplTest.java
index d70d4438bf..e00bcc0a9c 100644
--- 
a/discovery/seata-discovery-nacos/src/test/java/org/apache/seata/discovery/registry/nacos/NacosRegistryServiceImplTest.java
+++ 
b/discovery/seata-discovery-nacos/src/test/java/org/apache/seata/discovery/registry/nacos/NacosRegistryServiceImplTest.java
@@ -41,4 +41,4 @@ public class NacosRegistryServiceImplTest {
     }
 
 
-}
+}
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org
For additional commands, e-mail: notifications-h...@seata.apache.org

Reply via email to