import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
import org.apache.shardingsphere.infra.context.runtime.RuntimeContext;
import org.apache.shardingsphere.infra.database.DefaultSchema;
import org.apache.shardingsphere.infra.executor.kernel.ExecutorEngine;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.optimize.context.OptimizerContext;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.metadata.MetaDataContextsBuilder;
import org.apache.shardingsphere.transaction.context.TransactionContexts;
import org.junit.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class ShardingSphereReadWriteSplittingTest {
@Test
public void assertReadWriteSplitting() throws SQLException {
// 创建ShardingSphereRule列表,这里可以添加读写分离规则等
ShardingSphereRule rule = mock(ShardingSphereRule.class);
// 创建ShardingSphereMetaData
ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class);
// 创建事务上下文
TransactionContexts transactionContexts = mock(TransactionContexts.class);
// 创建执行引擎
ExecutorEngine executorEngine = mock(ExecutorEngine.class);
// 创建配置属性
ConfigurationProperties properties = new ConfigurationProperties(new HashMap<>());
// 构建MetaDataContexts
MetaDataContexts metaDataContexts = new MetaDataContextsBuilder(
Collections.singletonMap("ds_0", mock(DataSource.class)),
Collections.singletonMap("ds_0", metaData),
new ShardingSphereRuleMetaData(Collections.singleton(rule)),
properties).build();
// 创建上下文管理器
ContextManager contextManager = new ContextManager(
null,
false,
new MemorySchemaMetaDataLoader(metaDataContexts),
metaDataContexts,
transactionContexts,
executorEngine,
评论已关闭