Search in sources :

Example 56 with ShardingRule

use of io.shardingjdbc.core.rule.ShardingRule in project sharding-jdbc by shardingjdbc.

the class ShardingNamespaceTest method assertDefaultActualDataNodes.

@Test
public void assertDefaultActualDataNodes() {
    ShardingDataSource multiTableRulesDataSource = this.applicationContext.getBean("multiTableRulesDataSource", ShardingDataSource.class);
    Object shardingContext = FieldValueUtil.getFieldValue(multiTableRulesDataSource, "shardingContext", true);
    ShardingRule shardingRule = (ShardingRule) FieldValueUtil.getFieldValue(shardingContext, "shardingRule");
    assertThat(shardingRule.getTableRules().size(), is(2));
    Iterator<TableRule> tableRules = shardingRule.getTableRules().iterator();
    TableRule orderRule = tableRules.next();
    assertThat(orderRule.getActualDataNodes().size(), is(2));
    assertTrue(orderRule.getActualDataNodes().contains(new DataNode("dbtbl_0", "t_order")));
    assertTrue(orderRule.getActualDataNodes().contains(new DataNode("dbtbl_1", "t_order")));
    TableRule orderItemRule = tableRules.next();
    assertThat(orderItemRule.getActualDataNodes().size(), is(2));
    assertTrue(orderItemRule.getActualDataNodes().contains(new DataNode("dbtbl_0", "t_order_item")));
    assertTrue(orderItemRule.getActualDataNodes().contains(new DataNode("dbtbl_1", "t_order_item")));
}
Also used : BindingTableRule(io.shardingjdbc.core.rule.BindingTableRule) TableRule(io.shardingjdbc.core.rule.TableRule) ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) SpringShardingDataSource(io.shardingjdbc.spring.datasource.SpringShardingDataSource) DataNode(io.shardingjdbc.core.rule.DataNode) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) Test(org.junit.Test)

Example 57 with ShardingRule

use of io.shardingjdbc.core.rule.ShardingRule in project sharding-jdbc by shardingjdbc.

the class ShardingNamespaceTest method assertMultiBindingTableRulesDatasource.

@Test
public void assertMultiBindingTableRulesDatasource() {
    ShardingRule shardingRule = getShardingRule("multiBindingTableRulesDatasource");
    assertThat(shardingRule.getBindingTableRules().size(), is(2));
    Iterator<BindingTableRule> bindingTableRules = shardingRule.getBindingTableRules().iterator();
    BindingTableRule orderRule = bindingTableRules.next();
    assertThat(orderRule.getBindingActualTable("dbtbl_0", "t_order", "t_order_item"), is("t_order"));
    assertThat(orderRule.getBindingActualTable("dbtbl_1", "t_order", "t_order_item"), is("t_order"));
    BindingTableRule userRule = bindingTableRules.next();
    assertThat(userRule.getBindingActualTable("dbtbl_0", "t_user", "t_user_detail"), is("t_user"));
    assertThat(userRule.getBindingActualTable("dbtbl_1", "t_user", "t_user_detail"), is("t_user"));
}
Also used : BindingTableRule(io.shardingjdbc.core.rule.BindingTableRule) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) Test(org.junit.Test)

Example 58 with ShardingRule

use of io.shardingjdbc.core.rule.ShardingRule in project sharding-jdbc by shardingjdbc.

the class ShardingNamespaceTest method assertBindingTableRuleDatasource.

@Test
public void assertBindingTableRuleDatasource() {
    ShardingRule shardingRule = getShardingRule("bindingTableRuleDatasource");
    assertThat(shardingRule.getBindingTableRules().size(), is(1));
    BindingTableRule bindingTableRule = shardingRule.getBindingTableRules().iterator().next();
    assertThat(bindingTableRule.getBindingActualTable("dbtbl_0", "t_order", "t_order_item"), is("t_order"));
    assertThat(bindingTableRule.getBindingActualTable("dbtbl_1", "t_order", "t_order_item"), is("t_order"));
}
Also used : BindingTableRule(io.shardingjdbc.core.rule.BindingTableRule) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) Test(org.junit.Test)

Example 59 with ShardingRule

use of io.shardingjdbc.core.rule.ShardingRule in project sharding-jdbc by shardingjdbc.

the class ShardingNamespaceTest method assertSimpleShardingDataSource.

@Test
public void assertSimpleShardingDataSource() {
    Map<String, DataSource> dataSourceMap = getDataSourceMap("simpleShardingDataSource");
    assertNotNull(dataSourceMap.get("dbtbl_0"));
    ShardingRule shardingRule = getShardingRule("simpleShardingDataSource");
    assertThat(shardingRule.getTableRules().size(), is(1));
    assertThat(shardingRule.getTableRules().iterator().next().getLogicTable(), is("t_order"));
}
Also used : ShardingRule(io.shardingjdbc.core.rule.ShardingRule) DataSource(javax.sql.DataSource) ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) SpringShardingDataSource(io.shardingjdbc.spring.datasource.SpringShardingDataSource) Test(org.junit.Test)

Example 60 with ShardingRule

use of io.shardingjdbc.core.rule.ShardingRule in project sharding-jdbc by shardingjdbc.

the class ShardingNamespaceTest method getShardingRule.

private ShardingRule getShardingRule(final String shardingDataSourceName) {
    ShardingDataSource shardingDataSource = this.applicationContext.getBean(shardingDataSourceName, ShardingDataSource.class);
    Object shardingContext = FieldValueUtil.getFieldValue(shardingDataSource, "shardingContext", true);
    return (ShardingRule) FieldValueUtil.getFieldValue(shardingContext, "shardingRule");
}
Also used : ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) SpringShardingDataSource(io.shardingjdbc.spring.datasource.SpringShardingDataSource) ShardingRule(io.shardingjdbc.core.rule.ShardingRule)

Aggregations

ShardingRule (io.shardingjdbc.core.rule.ShardingRule)60 Test (org.junit.Test)35 ShardingDataSource (io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource)20 SQLParsingEngine (io.shardingjdbc.core.parsing.SQLParsingEngine)19 ShardingRuleConfiguration (io.shardingjdbc.core.api.config.ShardingRuleConfiguration)16 TableRuleConfiguration (io.shardingjdbc.core.api.config.TableRuleConfiguration)14 DataSource (javax.sql.DataSource)14 BindingTableRule (io.shardingjdbc.core.rule.BindingTableRule)10 StandardShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration)8 DMLStatement (io.shardingjdbc.core.parsing.parser.sql.dml.DMLStatement)8 Map (java.util.Map)8 ComplexShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration)7 DatabaseType (io.shardingjdbc.core.constant.DatabaseType)7 TableRule (io.shardingjdbc.core.rule.TableRule)6 SQLShardingRule (io.shardingjdbc.core.integrate.jaxb.SQLShardingRule)5 InsertStatement (io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement)5 HashMap (java.util.HashMap)5 Before (org.junit.Before)5 TestComplexKeysShardingAlgorithm (io.shardingjdbc.core.api.algorithm.fixture.TestComplexKeysShardingAlgorithm)4 InlineShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.InlineShardingStrategyConfiguration)4