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 f426203a43b Add branch-covering tests for XA factory and core helpers 
(#37493)
f426203a43b is described below

commit f426203a43b6f8012a80f1568748121312c4eb3e
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Dec 24 19:37:14 2025 +0800

    Add branch-covering tests for XA factory and core helpers (#37493)
    
    * Add branch-covering tests for XA factory and core helpers
    
    * Add branch-covering tests for XA factory and core helpers
---
 .../jdbc/executor/ProxyJDBCExecutorTest.java       |  96 +++++++++++++
 .../xa/XATCLProxyBackendHandlerFactoryTest.java    |  82 +++++++++++
 .../xa/type/XABeginProxyBackendHandlerTest.java    |  59 ++++++++
 .../util/ClusterExportMetaDataGeneratorTest.java   | 151 +++++++++++++++++++++
 4 files changed, 388 insertions(+)

diff --git 
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/executor/ProxyJDBCExecutorTest.java
 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/executor/ProxyJDBCExecutorTest.java
new file mode 100644
index 00000000000..a76055585d3
--- /dev/null
+++ 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/executor/ProxyJDBCExecutorTest.java
@@ -0,0 +1,96 @@
+/*
+ * 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.proxy.backend.connector.jdbc.executor;
+
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import 
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
+import 
org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroupContext;
+import 
org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroupReportContext;
+import 
org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutionUnit;
+import 
org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutor;
+import 
org.apache.shardingsphere.infra.executor.sql.execute.result.ExecuteResult;
+import 
org.apache.shardingsphere.infra.executor.sql.prepare.driver.jdbc.JDBCDriverType;
+import org.apache.shardingsphere.infra.executor.sql.process.ProcessRegistry;
+import 
org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
+import org.apache.shardingsphere.infra.session.query.QueryContext;
+import org.apache.shardingsphere.mode.manager.ContextManager;
+import 
org.apache.shardingsphere.proxy.backend.connector.DatabaseProxyConnector;
+import 
org.apache.shardingsphere.proxy.backend.connector.jdbc.executor.callback.ProxyJDBCExecutorCallback;
+import 
org.apache.shardingsphere.proxy.backend.connector.jdbc.executor.callback.ProxyJDBCExecutorCallbackFactory;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
+import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
+import org.junit.jupiter.api.Test;
+import org.mockito.MockedStatic;
+import org.mockito.internal.configuration.plugins.Plugins;
+
+import java.sql.SQLException;
+import java.util.Collections;
+import java.util.List;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.when;
+
+class ProxyJDBCExecutorTest {
+    
+    @Test
+    void assertExecuteWithCallbacks() throws SQLException, 
ReflectiveOperationException {
+        final Object originalContextManager = 
Plugins.getMemberAccessor().get(ProxyContext.class.getDeclaredField("contextManager"),
 ProxyContext.getInstance());
+        ContextManager contextManager = mock(ContextManager.class, 
RETURNS_DEEP_STUBS);
+        ProxyContext.init(contextManager);
+        JDBCDriverType type = JDBCDriverType.STATEMENT;
+        ConnectionSession connectionSession = mock(ConnectionSession.class);
+        when(connectionSession.getUsedDatabaseName()).thenReturn("logic_db");
+        DatabaseProxyConnector databaseProxyConnector = 
mock(DatabaseProxyConnector.class);
+        JDBCExecutor jdbcExecutor = mock(JDBCExecutor.class);
+        ProxyJDBCExecutor executor = new ProxyJDBCExecutor(type, 
connectionSession, databaseProxyConnector, jdbcExecutor);
+        SQLStatement sqlStatement = mock(SQLStatement.class);
+        QueryContext queryContext = mockQueryContext(sqlStatement);
+        ExecutionGroupContext<JDBCExecutionUnit> executionGroupContext = new 
ExecutionGroupContext<>(Collections.emptyList(), new 
ExecutionGroupReportContext("pid", "logic_db"));
+        ProxyJDBCExecutorCallback firstCallback = 
mock(ProxyJDBCExecutorCallback.class);
+        ProxyJDBCExecutorCallback secondCallback = 
mock(ProxyJDBCExecutorCallback.class);
+        List<ExecuteResult> expected = 
Collections.singletonList(mock(ExecuteResult.class));
+        try (MockedStatic<ProxyJDBCExecutorCallbackFactory> mockedStatic = 
mockStatic(ProxyJDBCExecutorCallbackFactory.class)) {
+            ResourceMetaData resourceMetaData = 
contextManager.getMetaDataContexts().getMetaData().getDatabase("logic_db").getResourceMetaData();
+            DatabaseType protocolType = 
contextManager.getMetaDataContexts().getMetaData().getDatabase("logic_db").getProtocolType();
+            mockedStatic.when(() -> 
ProxyJDBCExecutorCallbackFactory.newInstance(type, protocolType, 
resourceMetaData, sqlStatement, databaseProxyConnector, true, false, true))
+                    .thenReturn(firstCallback);
+            mockedStatic.when(() -> 
ProxyJDBCExecutorCallbackFactory.newInstance(type, protocolType, 
resourceMetaData, sqlStatement, databaseProxyConnector, true, false, false))
+                    .thenReturn(secondCallback);
+            when(jdbcExecutor.execute(executionGroupContext, firstCallback, 
secondCallback)).thenReturn(expected);
+            assertThat(executor.execute(queryContext, executionGroupContext, 
true, false), is(expected));
+            mockedStatic.verify(() -> 
ProxyJDBCExecutorCallbackFactory.newInstance(type, protocolType, 
resourceMetaData, sqlStatement, databaseProxyConnector, true, false, true));
+            mockedStatic.verify(() -> 
ProxyJDBCExecutorCallbackFactory.newInstance(type, protocolType, 
resourceMetaData, sqlStatement, databaseProxyConnector, true, false, false));
+        }
+        ProcessRegistry.getInstance().remove("pid");
+        
Plugins.getMemberAccessor().set(ProxyContext.class.getDeclaredField("contextManager"),
 ProxyContext.getInstance(), originalContextManager);
+    }
+    
+    private QueryContext mockQueryContext(final SQLStatement sqlStatement) {
+        QueryContext result = mock(QueryContext.class);
+        when(result.getSql()).thenReturn("SELECT 1");
+        SQLStatementContext sqlStatementContext = 
mock(SQLStatementContext.class);
+        when(sqlStatementContext.getSqlStatement()).thenReturn(sqlStatement);
+        when(result.getSqlStatementContext()).thenReturn(sqlStatementContext);
+        return result;
+    }
+}
diff --git 
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/tcl/xa/XATCLProxyBackendHandlerFactoryTest.java
 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/tcl/xa/XATCLProxyBackendHandlerFactoryTest.java
new file mode 100644
index 00000000000..55595d2a510
--- /dev/null
+++ 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/tcl/xa/XATCLProxyBackendHandlerFactoryTest.java
@@ -0,0 +1,82 @@
+/*
+ * 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.proxy.backend.handler.tcl.xa;
+
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import 
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.session.query.QueryContext;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import 
org.apache.shardingsphere.proxy.backend.connector.DatabaseProxyConnectorFactory;
+import 
org.apache.shardingsphere.proxy.backend.connector.ProxyDatabaseConnectionManager;
+import org.apache.shardingsphere.proxy.backend.handler.ProxyBackendHandler;
+import 
org.apache.shardingsphere.proxy.backend.handler.tcl.xa.type.XABeginProxyBackendHandler;
+import 
org.apache.shardingsphere.proxy.backend.handler.tcl.xa.type.XACommitProxyBackendHandler;
+import 
org.apache.shardingsphere.proxy.backend.handler.tcl.xa.type.XAOtherOperationProxyBackendHandler;
+import 
org.apache.shardingsphere.proxy.backend.handler.tcl.xa.type.XARecoveryProxyBackendHandler;
+import 
org.apache.shardingsphere.proxy.backend.handler.tcl.xa.type.XARollbackProxyBackendHandler;
+import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.tcl.xa.XABeginStatement;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.tcl.xa.XACommitStatement;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.tcl.xa.XAPrepareStatement;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.tcl.xa.XARecoveryStatement;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.tcl.xa.XARollbackStatement;
+import 
org.apache.shardingsphere.sql.parser.statement.core.statement.type.tcl.xa.XAStatement;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.mockito.MockedStatic;
+
+import java.util.stream.Stream;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.isA;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.when;
+
+class XATCLProxyBackendHandlerFactoryTest {
+    
+    private static final DatabaseType DATABASE_TYPE = 
TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
+    
+    @ParameterizedTest(name = "{index}: {0}")
+    @MethodSource("provideXAStatements")
+    void assertCreateHandlersForEachXAStatement(final String name, final 
XAStatement statement, final Class<? extends ProxyBackendHandler> 
expectedClass) {
+        QueryContext queryContext = mock(QueryContext.class);
+        ConnectionSession connectionSession = mock(ConnectionSession.class);
+        ProxyDatabaseConnectionManager databaseConnectionManager = 
mock(ProxyDatabaseConnectionManager.class);
+        
when(connectionSession.getDatabaseConnectionManager()).thenReturn(databaseConnectionManager);
+        try (MockedStatic<DatabaseProxyConnectorFactory> mockedStatic = 
mockStatic(DatabaseProxyConnectorFactory.class)) {
+            SQLStatementContext sqlStatementContext = 
mock(SQLStatementContext.class);
+            when(sqlStatementContext.getSqlStatement()).thenReturn(statement);
+            
when(queryContext.getSqlStatementContext()).thenReturn(sqlStatementContext);
+            
assertThat(XATCLProxyBackendHandlerFactory.newInstance(queryContext, 
connectionSession), isA(expectedClass));
+            mockedStatic.verify(() -> 
DatabaseProxyConnectorFactory.newInstance(eq(queryContext), 
eq(databaseConnectionManager), anyBoolean()));
+        }
+    }
+    
+    private static Stream<Arguments> provideXAStatements() {
+        return Stream.of(
+                Arguments.of("recovery", new 
XARecoveryStatement(DATABASE_TYPE), XARecoveryProxyBackendHandler.class),
+                Arguments.of("begin", new XABeginStatement(DATABASE_TYPE, 
"xid"), XABeginProxyBackendHandler.class),
+                Arguments.of("commit", new XACommitStatement(DATABASE_TYPE, 
"xid"), XACommitProxyBackendHandler.class),
+                Arguments.of("rollback", new 
XARollbackStatement(DATABASE_TYPE, "xid"), XARollbackProxyBackendHandler.class),
+                Arguments.of("other", new XAPrepareStatement(DATABASE_TYPE, 
"xid"), XAOtherOperationProxyBackendHandler.class));
+    }
+}
diff --git 
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/tcl/xa/type/XABeginProxyBackendHandlerTest.java
 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/tcl/xa/type/XABeginProxyBackendHandlerTest.java
new file mode 100644
index 00000000000..d202a173434
--- /dev/null
+++ 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/tcl/xa/type/XABeginProxyBackendHandlerTest.java
@@ -0,0 +1,59 @@
+/*
+ * 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.proxy.backend.handler.tcl.xa.type;
+
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
+import 
org.apache.shardingsphere.infra.session.connection.transaction.TransactionConnectionContext;
+import 
org.apache.shardingsphere.proxy.backend.connector.DatabaseProxyConnector;
+import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
+import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
+import org.apache.shardingsphere.transaction.api.TransactionType;
+import org.apache.shardingsphere.transaction.rule.TransactionRule;
+import org.junit.jupiter.api.Test;
+
+import java.sql.SQLException;
+import java.util.Collections;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class XABeginProxyBackendHandlerTest {
+    
+    @Test
+    void assertExecuteStartsTransaction() throws SQLException {
+        ConnectionSession connectionSession = mock(ConnectionSession.class, 
RETURNS_DEEP_STUBS);
+        TransactionConnectionContext transactionContext = new 
TransactionConnectionContext();
+        
when(connectionSession.getConnectionContext().getTransactionContext()).thenReturn(transactionContext);
+        DatabaseProxyConnector databaseProxyConnector = 
mock(DatabaseProxyConnector.class);
+        ResponseHeader expected = mock(ResponseHeader.class);
+        when(databaseProxyConnector.execute()).thenReturn(expected);
+        TransactionRule transactionRule = mock(TransactionRule.class, 
RETURNS_DEEP_STUBS);
+        when(transactionRule.getDefaultType()).thenReturn(TransactionType.XA);
+        ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class);
+        when(metaData.getGlobalRuleMetaData()).thenReturn(new 
RuleMetaData(Collections.singleton(transactionRule)));
+        XABeginProxyBackendHandler handler = new 
XABeginProxyBackendHandler(metaData, connectionSession, databaseProxyConnector);
+        assertThat(handler.execute(), is(expected));
+        assertTrue(transactionContext.isTransactionStarted());
+        assertThat(transactionContext.getTransactionType().orElse(""), 
is(TransactionType.XA.name()));
+    }
+}
diff --git 
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/util/ClusterExportMetaDataGeneratorTest.java
 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/util/ClusterExportMetaDataGeneratorTest.java
new file mode 100644
index 00000000000..d2a1bbf215f
--- /dev/null
+++ 
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/util/ClusterExportMetaDataGeneratorTest.java
@@ -0,0 +1,151 @@
+/*
+ * 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.proxy.backend.util;
+
+import 
org.apache.shardingsphere.globalclock.config.GlobalClockRuleConfiguration;
+import org.apache.shardingsphere.globalclock.provider.GlobalClockProvider;
+import org.apache.shardingsphere.globalclock.rule.GlobalClockRule;
+import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import 
org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import 
org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode;
+import 
org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
+import org.apache.shardingsphere.infra.util.json.JsonUtils;
+import org.apache.shardingsphere.mode.manager.ContextManager;
+import 
org.apache.shardingsphere.proxy.backend.distsql.export.ExportedClusterInfo;
+import org.apache.shardingsphere.proxy.backend.distsql.export.ExportedMetaData;
+import org.apache.shardingsphere.single.config.SingleRuleConfiguration;
+import org.junit.jupiter.api.Test;
+
+import javax.sql.DataSource;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.stream.Collectors;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class ClusterExportMetaDataGeneratorTest {
+    
+    @Test
+    void assertGenerateJsonFormatWithoutData() {
+        ContextManager contextManager = 
mockContextManager(Collections.emptyList(), mockGlobalClockRule(false, null), 
Collections.singleton(mockDatabaseWithoutSources("empty_db")), new 
Properties());
+        ExportedClusterInfo actual = JsonUtils.fromJsonString(new 
ClusterExportMetaDataGenerator(contextManager).generateJsonFormat(), 
ExportedClusterInfo.class);
+        ExportedMetaData actualMetaData = actual.getMetaData();
+        assertTrue(actualMetaData.getDatabases().isEmpty());
+        assertThat(actualMetaData.getProps(), is(""));
+        assertThat(actualMetaData.getRules(), is(""));
+        assertNull(actual.getSnapshotInfo());
+    }
+    
+    @Test
+    void assertGenerateJsonFormatWithSnapshotAndData() {
+        Properties props = new Properties();
+        props.setProperty("sql-show", "true");
+        GlobalClockProvider provider = mock(GlobalClockProvider.class);
+        when(provider.getCurrentTimestamp()).thenReturn(123L);
+        ContextManager contextManager = 
mockContextManager(Collections.singletonList(new 
GlobalClockRuleConfiguration("TSO", "LOCAL", true, new Properties())),
+                mockGlobalClockRule(true, provider), 
Arrays.asList(mockDatabaseWithoutSources("skip_db"), 
mockDatabaseWithStorage()), props);
+        ExportedClusterInfo actual = JsonUtils.fromJsonString(new 
ClusterExportMetaDataGenerator(contextManager).generateJsonFormat(), 
ExportedClusterInfo.class);
+        ExportedMetaData metaData = actual.getMetaData();
+        assertTrue(metaData.getDatabases().containsKey("logic_db"));
+        assertThat(metaData.getDatabases().size(), is(1));
+        assertThat(metaData.getProps(), containsString("props:"));
+        assertThat(metaData.getRules(), containsString("rules:"));
+        assertThat(actual.getSnapshotInfo(), is(notNullValue()));
+        assertThat(actual.getSnapshotInfo().getCsn(), is("123"));
+        assertNotNull(actual.getSnapshotInfo().getCreateTime());
+    }
+    
+    @Test
+    void assertGenerateJsonFormatWithSnapshotFallback() {
+        ContextManager contextManager = 
mockContextManager(Collections.singletonList(new 
GlobalClockRuleConfiguration("TSO", "LOCAL", true, new Properties())),
+                mockGlobalClockRule(true, null), 
Collections.singleton(mockDatabaseWithStorage()), new Properties());
+        ExportedClusterInfo actual = JsonUtils.fromJsonString(new 
ClusterExportMetaDataGenerator(contextManager).generateJsonFormat(), 
ExportedClusterInfo.class);
+        assertThat(actual.getSnapshotInfo().getCsn(), is("0"));
+    }
+    
+    private ContextManager mockContextManager(final 
Collection<RuleConfiguration> globalRules, final GlobalClockRule 
globalClockRule,
+                                              final 
Collection<ShardingSphereDatabase> databases, final Properties props) {
+        ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class);
+        when(metaData.getAllDatabases()).thenReturn(databases);
+        RuleMetaData globalRuleMetaData = mock(RuleMetaData.class);
+        when(globalRuleMetaData.getConfigurations()).thenReturn(globalRules);
+        
when(globalRuleMetaData.getSingleRule(GlobalClockRule.class)).thenReturn(globalClockRule);
+        ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
+        when(result.getMetaDataContexts().getMetaData()).thenReturn(metaData);
+        
when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(globalRuleMetaData);
+        
when(result.getMetaDataContexts().getMetaData().getProps()).thenReturn(new 
ConfigurationProperties(props));
+        return result;
+    }
+    
+    private GlobalClockRule mockGlobalClockRule(final boolean enabled, final 
GlobalClockProvider provider) {
+        GlobalClockRule result = mock(GlobalClockRule.class);
+        GlobalClockRuleConfiguration ruleConfig = 
mock(GlobalClockRuleConfiguration.class);
+        when(ruleConfig.isEnabled()).thenReturn(enabled);
+        when(result.getConfiguration()).thenReturn(ruleConfig);
+        
when(result.getGlobalClockProvider()).thenReturn(Optional.ofNullable(provider));
+        return result;
+    }
+    
+    private ShardingSphereDatabase mockDatabaseWithoutSources(final String 
databaseName) {
+        ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, 
RETURNS_DEEP_STUBS);
+        when(result.getName()).thenReturn(databaseName);
+        
when(result.getResourceMetaData().getAllInstanceDataSourceNames()).thenReturn(Collections.emptyList());
+        return result;
+    }
+    
+    private ShardingSphereDatabase mockDatabaseWithStorage() {
+        ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, 
RETURNS_DEEP_STUBS);
+        when(result.getName()).thenReturn("logic_db");
+        
when(result.getResourceMetaData().getAllInstanceDataSourceNames()).thenReturn(Collections.singletonList("ds_0"));
+        
when(result.getResourceMetaData().getStorageUnits()).thenReturn(mockStorageUnits(Collections.singletonMap("ds_0",
 createDataSourcePoolProperties())));
+        
when(result.getRuleMetaData().getConfigurations()).thenReturn(Collections.singletonList(new
 SingleRuleConfiguration()));
+        return result;
+    }
+    
+    private Map<String, StorageUnit> mockStorageUnits(final Map<String, 
DataSourcePoolProperties> props) {
+        return props.entrySet().stream().collect(Collectors.toMap(
+                Entry::getKey, entry -> new StorageUnit(new 
StorageNode(entry.getKey()), entry.getValue(), mock(DataSource.class))));
+    }
+    
+    private DataSourcePoolProperties createDataSourcePoolProperties() {
+        Map<String, Object> props = new LinkedHashMap<>(3, 1F);
+        props.put("url", "jdbc:h2:mem:ds_0");
+        props.put("username", "sa");
+        props.put("maxPoolSize", 30);
+        return new DataSourcePoolProperties("HikariCP", props);
+    }
+}

Reply via email to