Search in sources :

Example 31 with ShardingRule

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

the class OrchestrationShardingNamespaceTest method assertMultiTableRulesDataSource.

@Test
public void assertMultiTableRulesDataSource() {
    ShardingRule shardingRule = getShardingRule("multiTableRulesDataSource");
    assertThat(shardingRule.getTableRules().size(), is(2));
    Iterator<TableRule> tableRules = shardingRule.getTableRules().iterator();
    assertThat(tableRules.next().getLogicTable(), is("t_order"));
    assertThat(tableRules.next().getLogicTable(), is("t_order_item"));
}
Also used : BindingTableRule(io.shardingjdbc.core.rule.BindingTableRule) TableRule(io.shardingjdbc.core.rule.TableRule) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) Test(org.junit.Test)

Example 32 with ShardingRule

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

the class OrchestrationShardingNamespaceTest method assertTableRuleWithAttributesDataSource.

@Test
public void assertTableRuleWithAttributesDataSource() {
    ShardingRule shardingRule = getShardingRule("tableRuleWithAttributesDataSource");
    assertThat(shardingRule.getTableRules().size(), is(1));
    TableRule tableRule = shardingRule.getTableRules().iterator().next();
    assertThat(tableRule.getLogicTable(), is("t_order"));
    assertThat(tableRule.getActualDataNodes().size(), is(8));
    assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_0", "t_order_0")));
    assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_0", "t_order_1")));
    assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_0", "t_order_2")));
    assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_0", "t_order_3")));
    assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_1", "t_order_0")));
    assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_1", "t_order_1")));
    assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_1", "t_order_2")));
    assertTrue(tableRule.getActualDataNodes().contains(new DataNode("dbtbl_1", "t_order_3")));
    assertTrue(Arrays.equals(tableRule.getDatabaseShardingStrategy().getShardingColumns().toArray(new String[] {}), new String[] { this.applicationContext.getBean("standardStrategy", StandardShardingStrategyConfiguration.class).getShardingColumn() }));
    assertTrue(Arrays.equals(tableRule.getTableShardingStrategy().getShardingColumns().toArray(new String[] {}), new String[] { this.applicationContext.getBean("inlineStrategy", InlineShardingStrategyConfiguration.class).getShardingColumn() }));
    assertThat(tableRule.getGenerateKeyColumn(), is("order_id"));
    assertThat(tableRule.getKeyGenerator().getClass().getName(), is(IncrementKeyGenerator.class.getCanonicalName()));
}
Also used : BindingTableRule(io.shardingjdbc.core.rule.BindingTableRule) TableRule(io.shardingjdbc.core.rule.TableRule) StandardShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration) DataNode(io.shardingjdbc.core.rule.DataNode) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) InlineShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.InlineShardingStrategyConfiguration) Test(org.junit.Test)

Example 33 with ShardingRule

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

the class OrchestrationShardingNamespaceTest method assertSimpleShardingDataSource.

@Test
public void assertSimpleShardingDataSource() {
    Map<String, DataSource> dataSourceMap = getDataSourceMap("simpleShardingDataSource");
    ShardingRule shardingRule = getShardingRule("simpleShardingDataSource");
    assertNotNull(dataSourceMap.get("dbtbl_0"));
    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) Test(org.junit.Test)

Example 34 with ShardingRule

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

the class ShardingDataSourceTest method assertRenewWithoutChangeExecutorPoolEngine.

@Test
public void assertRenewWithoutChangeExecutorPoolEngine() throws SQLException, NoSuchFieldException, IllegalAccessException {
    DataSource originalDataSource = mockDataSource("H2");
    Map<String, DataSource> originalDataSourceMap = new HashMap<>(1, 1);
    originalDataSourceMap.put("ds", originalDataSource);
    ShardingDataSource shardingDataSource = createShardingDataSource(originalDataSourceMap);
    ExecutorEngine originExecutorEngine = getExecutorEngine(shardingDataSource);
    DataSource newDataSource = mockDataSource("H2");
    Map<String, DataSource> newDataSourceMap = new HashMap<>(1, 1);
    newDataSourceMap.put("ds", newDataSource);
    shardingDataSource.renew(newDataSourceMap, new ShardingRule(createShardingRuleConfig(newDataSourceMap), newDataSourceMap.keySet()), new Properties());
    assertThat(originExecutorEngine, is(getExecutorEngine(shardingDataSource)));
}
Also used : HashMap(java.util.HashMap) ExecutorEngine(io.shardingjdbc.core.executor.ExecutorEngine) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) Properties(java.util.Properties) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 35 with ShardingRule

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

the class ShardingDataSourceTest method assertRenewWithChangeExecutorEnginePoolSize.

@Test
public void assertRenewWithChangeExecutorEnginePoolSize() throws SQLException, NoSuchFieldException, IllegalAccessException {
    DataSource originalDataSource = mockDataSource("H2");
    Map<String, DataSource> originalDataSourceMap = new HashMap<>(1, 1);
    originalDataSourceMap.put("ds", originalDataSource);
    ShardingDataSource shardingDataSource = createShardingDataSource(originalDataSourceMap);
    final ExecutorEngine originExecutorEngine = getExecutorEngine(shardingDataSource);
    DataSource newDataSource = mockDataSource("H2");
    Map<String, DataSource> newDataSourceMap = new HashMap<>(1, 1);
    newDataSourceMap.put("ds", newDataSource);
    Properties props = new Properties();
    props.setProperty(ShardingPropertiesConstant.EXECUTOR_SIZE.getKey(), "100");
    shardingDataSource.renew(newDataSourceMap, new ShardingRule(createShardingRuleConfig(newDataSourceMap), newDataSourceMap.keySet()), props);
    assertThat(originExecutorEngine, not(getExecutorEngine(shardingDataSource)));
}
Also used : HashMap(java.util.HashMap) ExecutorEngine(io.shardingjdbc.core.executor.ExecutorEngine) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) Properties(java.util.Properties) DataSource(javax.sql.DataSource) Test(org.junit.Test)

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