提交 550bf240 编写于 作者: T terrymanu

refactor config yaml test case

上级 1250a83e
......@@ -17,15 +17,15 @@
package com.dangdang.ddframe.rdb.sharding.config;
import com.dangdang.ddframe.rdb.sharding.config.yaml.YamlShardingConfigTest;
import com.dangdang.ddframe.rdb.sharding.config.yaml.YamlintegratedTest;
import com.dangdang.ddframe.rdb.sharding.config.yaml.YamlIntegratedTest;
import com.dangdang.ddframe.rdb.sharding.config.yaml.YamlShardingDataSourceTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
YamlShardingConfigTest.class,
YamlintegratedTest.class
YamlShardingDataSourceTest.class,
YamlIntegratedTest.class
})
public class AllYamlShardingConfigTests {
public class AllYamlTests {
}
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed 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.
* </p>
*/
package com.dangdang.ddframe.rdb.sharding.config.yaml;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.Collection;
import javax.sql.DataSource;
import com.dangdang.ddframe.rdb.sharding.config.yaml.api.YamlShardingDataSource;
import com.google.common.base.Function;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import lombok.RequiredArgsConstructor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@RunWith(Parameterized.class)
@RequiredArgsConstructor
public class YamlShardingConfigTest extends AbstractYamlShardingDataSourceTest {
private final String filePath;
private final boolean hasDataSource;
@Parameterized.Parameters(name = "{index}:{0}-{1}")
public static Collection init() {
return Arrays.asList(new Object[][]{
{"/configWithDataSourceWithoutProps.yaml", true},
{"/configWithoutDataSourceWithoutProps.yaml", false},
{"/configWithDataSourceWithProps.yaml", true},
{"/configWithoutDataSourceWithProps.yaml", false},
});
}
@Test
public void testWithDataSource() throws SQLException, URISyntaxException, IOException {
File yamlFile = new File(YamlShardingConfigTest.class.getResource(filePath).toURI());
DataSource dataSource;
if (hasDataSource) {
dataSource = new YamlShardingDataSource(yamlFile);
} else {
dataSource = new YamlShardingDataSource(Maps.asMap(Sets.newHashSet("db0", "db1"), new Function<String, DataSource>() {
@Override
public DataSource apply(final String key) {
return createDataSource(key);
}
}), yamlFile);
}
try (Connection conn = dataSource.getConnection();
Statement stm = conn.createStatement()) {
stm.executeQuery("SELECT * FROM t_order");
stm.executeQuery("SELECT * FROM t_order_item");
stm.executeQuery("SELECT * FROM config");
}
}
}
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed 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.
* </p>
*/
package com.dangdang.ddframe.rdb.sharding.config.yaml;
import com.dangdang.ddframe.rdb.sharding.api.rule.DynamicDataNode;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.TableRule;
import com.dangdang.ddframe.rdb.sharding.config.common.api.ShardingRuleBuilder;
import com.dangdang.ddframe.rdb.sharding.config.common.api.config.ShardingRuleConfig;
import com.dangdang.ddframe.rdb.sharding.config.yaml.api.YamlShardingDataSource;
import com.dangdang.ddframe.rdb.sharding.jdbc.ShardingContext;
import com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.dbcp.BasicDataSource;
import org.h2.Driver;
import org.junit.Test;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
@Slf4j
public class YamlShardingDataSourceTest {
@Test
public void assertAll() throws IOException, NoSuchFieldException, IllegalAccessException, URISyntaxException {
ShardingRule shardingRule = getShardingRule("/config/config-all.yaml");
assertThat(shardingRule.getTableRules().size(), is(3));
assertThat(shardingRule.getBindingTableRules().size(), is(1));
assertThat(Arrays.asList(shardingRule.getTableRules().toArray()), hasItems(shardingRule.getBindingTableRules().iterator().next().getTableRules().toArray()));
assertThat(shardingRule.getDataSourceRule().getDefaultDataSourceName(), is("db0"));
}
@Test
public void assertMin() throws IOException, NoSuchFieldException, IllegalAccessException, URISyntaxException {
Map<String, DataSource> dataSourceMap = new HashMap<>(1);
dataSourceMap.put("ds", createDataSource());
ShardingRule shardingRule = getShardingRule(dataSourceMap, "/config/config-min.yaml");
assertThat(shardingRule.getTableRules().size(), is(1));
}
@Test
public void assertDynamic() throws IOException, NoSuchFieldException, IllegalAccessException, URISyntaxException {
Map<String, DataSource> dataSourceMap = new HashMap<>(1);
dataSourceMap.put("ds", createDataSource());
ShardingRule shardingRule = getShardingRule(dataSourceMap, "/config/config-dynamic.yaml");
int i = 0;
for (TableRule each : shardingRule.getTableRules()) {
i++;
assertThat(each.getActualTables().size(), is(2));
assertThat(each.getActualTables(), hasItem(new DynamicDataNode("db0")));
assertThat(each.getActualTables(), hasItem(new DynamicDataNode("db1")));
switch (i) {
case 1:
assertThat(each.getLogicTable(), is("config"));
break;
case 2:
assertThat(each.getLogicTable(), is("t_order"));
break;
case 3:
assertThat(each.getLogicTable(), is("t_order_item"));
break;
default:
fail();
}
}
}
@Test(expected = IllegalArgumentException.class)
public void assertClassNotFound() throws IOException, NoSuchFieldException, IllegalAccessException, URISyntaxException {
getShardingRule("/config/config-classNotFound.yaml");
}
@Test(expected = IllegalArgumentException.class)
public void assertBindingError() throws IOException, NoSuchFieldException, IllegalAccessException, URISyntaxException {
Map<String, DataSource> dataSourceMap = new HashMap<>(1);
dataSourceMap.put("ds", createDataSource());
ShardingRule shardingRule = getShardingRule(dataSourceMap, "/config/config-bindingError.yaml");
for (TableRule tableRule : shardingRule.getBindingTableRules().iterator().next().getTableRules()) {
log.info(tableRule.toString());
}
}
private ShardingRule getShardingRule(final String fileName) throws NoSuchFieldException, IllegalAccessException, URISyntaxException, IOException {
return getShardingRule(new YamlShardingDataSource(new File(getClass().getResource(fileName).toURI())));
}
private ShardingRule getShardingRule(final Map<String, DataSource> dataSourceMap, final String fileName) throws NoSuchFieldException, IllegalAccessException, URISyntaxException, IOException {
return getShardingRule(new YamlShardingDataSource(dataSourceMap, new File(getClass().getResource(fileName).toURI())));
}
private ShardingRule getShardingRule(final ShardingDataSource shardingDataSource) throws NoSuchFieldException, IllegalAccessException, URISyntaxException, IOException {
Field field = ShardingDataSource.class.getDeclaredField("shardingContext");
field.setAccessible(true);
ShardingContext shardingContext = (ShardingContext) field.get(shardingDataSource);
return shardingContext.getShardingRule();
}
private DataSource createDataSource() {
BasicDataSource result = new BasicDataSource();
result.setDriverClassName(Driver.class.getName());
result.setUrl("jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL");
result.setUsername("sa");
result.setPassword("");
return result;
}
}
......@@ -17,97 +17,64 @@
package com.dangdang.ddframe.rdb.sharding.config.yaml;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Collection;
import javax.sql.DataSource;
import com.dangdang.ddframe.rdb.sharding.api.rule.DynamicDataNode;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.TableRule;
import com.dangdang.ddframe.rdb.sharding.config.common.api.ShardingRuleBuilder;
import com.dangdang.ddframe.rdb.sharding.config.common.api.config.ShardingRuleConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.dbcp.BasicDataSource;
import com.dangdang.ddframe.rdb.sharding.config.yaml.api.YamlShardingDataSource;
import com.google.common.base.Function;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import lombok.RequiredArgsConstructor;
import org.junit.Test;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
@RunWith(Parameterized.class)
@RequiredArgsConstructor
public class YamlIntegratedTest extends AbstractYamlShardingDataSourceTest {
@Slf4j
public class YamlintegratedTest {
private final String filePath;
@Test
public void testAll() {
Yaml yaml = new Yaml(new Constructor(ShardingRuleConfig.class));
ShardingRuleConfig config = (ShardingRuleConfig) yaml.load(YamlintegratedTest.class.getResourceAsStream("/config/config-all.yaml"));
ShardingRule shardingRule = new ShardingRuleBuilder("config-all.yaml", config).build();
assertThat(shardingRule.getTableRules().size(), is(3));
assertThat(shardingRule.getBindingTableRules().size(), is(1));
assertThat(Arrays.asList(shardingRule.getTableRules().toArray()), hasItems(shardingRule.getBindingTableRules().iterator().next().getTableRules().toArray()));
assertThat(shardingRule.getDataSourceRule().getDefaultDataSourceName(), is("db0"));
}
private final boolean hasDataSource;
@Test
public void testMin() {
Yaml yaml = new Yaml(new Constructor(ShardingRuleConfig.class));
Map<String, DataSource> dsMap = new HashMap<>();
dsMap.put("ds", new BasicDataSource());
ShardingRuleConfig config = (ShardingRuleConfig) yaml.load(YamlintegratedTest.class.getResourceAsStream("/config/config-min.yaml"));
ShardingRule shardingRule = new ShardingRuleBuilder("config-min.yaml", dsMap, config).build();
assertThat(shardingRule.getTableRules().size(), is(1));
@Parameterized.Parameters(name = "{index}:{0}-{1}")
public static Collection init() {
return Arrays.asList(new Object[][]{
{"/configWithDataSourceWithoutProps.yaml", true},
{"/configWithoutDataSourceWithoutProps.yaml", false},
{"/configWithDataSourceWithProps.yaml", true},
{"/configWithoutDataSourceWithProps.yaml", false},
});
}
@Test
public void testDynamic() {
Yaml yaml = new Yaml(new Constructor(ShardingRuleConfig.class));
Map<String, DataSource> dsMap = new HashMap<>();
dsMap.put("ds", new BasicDataSource());
ShardingRuleConfig config = (ShardingRuleConfig) yaml.load(YamlintegratedTest.class.getResourceAsStream("/config/config-dynamic.yaml"));
ShardingRule shardingRule = new ShardingRuleBuilder("config-dynamic.yaml", dsMap, config).build();
int i = 0;
for (TableRule each : shardingRule.getTableRules()) {
i++;
assertThat(each.getActualTables().size(), is(2));
assertThat(each.getActualTables(), hasItem(new DynamicDataNode("db0")));
assertThat(each.getActualTables(), hasItem(new DynamicDataNode("db1")));
switch (i) {
case 1:
assertThat(each.getLogicTable(), is("config"));
break;
case 2:
assertThat(each.getLogicTable(), is("t_order"));
break;
case 3:
assertThat(each.getLogicTable(), is("t_order_item"));
break;
default:
fail();
}
public void testWithDataSource() throws SQLException, URISyntaxException, IOException {
File yamlFile = new File(YamlIntegratedTest.class.getResource(filePath).toURI());
DataSource dataSource;
if (hasDataSource) {
dataSource = new YamlShardingDataSource(yamlFile);
} else {
dataSource = new YamlShardingDataSource(Maps.asMap(Sets.newHashSet("db0", "db1"), new Function<String, DataSource>() {
@Override
public DataSource apply(final String key) {
return createDataSource(key);
}
}), yamlFile);
}
@Test(expected = IllegalArgumentException.class)
public void testClassNotFound() {
Yaml yaml = new Yaml(new Constructor(ShardingRuleConfig.class));
ShardingRuleConfig config = (ShardingRuleConfig) yaml.load(YamlintegratedTest.class.getResourceAsStream("/config/config-classNotFound.yaml"));
new ShardingRuleBuilder(config).build();
}
@Test(expected = IllegalArgumentException.class)
public void testBindingError() {
Yaml yaml = new Yaml(new Constructor(ShardingRuleConfig.class));
Map<String, DataSource> dsMap = new HashMap<>();
dsMap.put("ds", new BasicDataSource());
ShardingRuleConfig config = (ShardingRuleConfig) yaml.load(YamlintegratedTest.class.getResourceAsStream("/config/config-bindingError.yaml"));
ShardingRule shardingRule = new ShardingRuleBuilder("config-bindingError.yaml", dsMap, config).build();
for (TableRule tableRule : shardingRule.getBindingTableRules().iterator().next().getTableRules()) {
log.info(tableRule.toString());
try (Connection conn = dataSource.getConnection();
Statement stm = conn.createStatement()) {
stm.executeQuery("SELECT * FROM t_order");
stm.executeQuery("SELECT * FROM t_order_item");
stm.executeQuery("SELECT * FROM config");
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册