This is an automated email from the ASF dual-hosted git repository.
panjuan 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 bebd54d Refactor SpringBootStarterTest (#15252)
bebd54d is described below
commit bebd54d2a22ca86f97e7b58b0c655867de9bfaa7
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Feb 5 22:30:33 2022 +0800
Refactor SpringBootStarterTest (#15252)
---
.../spring/boot/SpringBootStarterTest.java | 156 ++++++++++-----------
...on-common.properties => application.properties} | 12 +-
2 files changed, 83 insertions(+), 85 deletions(-)
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/java/org/apache/shardingsphere/spring/boot/SpringBootStarterTest.java
b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/java/org/apache/shardingsphere/spring/boot/SpringBootStarterTest.java
index 9bff14d..2cf5fe8 100644
---
a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/java/org/apache/shardingsphere/spring/boot/SpringBootStarterTest.java
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/java/org/apache/shardingsphere/spring/boot/SpringBootStarterTest.java
@@ -23,7 +23,6 @@ import
org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataS
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.encrypt.rule.EncryptTable;
import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
-import org.apache.shardingsphere.infra.database.DefaultSchema;
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.datanode.DataNodeUtil;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
@@ -41,13 +40,11 @@ import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;
import
org.apache.shardingsphere.sharding.algorithm.sharding.inline.InlineShardingAlgorithm;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sharding.rule.TableRule;
-import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
import org.apache.shardingsphere.sql.parser.api.CacheOption;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
@@ -55,19 +52,16 @@ import javax.sql.DataSource;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
-import java.util.List;
import java.util.Map;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringBootStarterTest.class)
@SpringBootApplication
-@ActiveProfiles("common")
public class SpringBootStarterTest {
@Resource
@@ -75,15 +69,15 @@ public class SpringBootStarterTest {
@Test
public void assertDataSources() {
- Map<String, DataSource> dataSources =
dataSource.getContextManager().getMetaDataContexts().getMetaData(DefaultSchema.LOGIC_NAME).getResource().getDataSources();
+ Map<String, DataSource> dataSources =
dataSource.getContextManager().getMetaDataContexts().getMetaData("foo_db").getResource().getDataSources();
assertThat(dataSources.size(), is(2));
- assertTrue(dataSources.containsKey("ds_0"));
- assertTrue(dataSources.containsKey("ds_1"));
+ assertTrue(dataSources.containsKey("ds0"));
+ assertTrue(dataSources.containsKey("ds1"));
}
@Test
public void assertRules() {
- Collection<ShardingSphereRule> rules =
dataSource.getContextManager().getMetaDataContexts().getMetaData(DefaultSchema.LOGIC_NAME).getRuleMetaData().getRules();
+ Collection<ShardingSphereRule> rules =
dataSource.getContextManager().getMetaDataContexts().getMetaData("foo_db").getRuleMetaData().getRules();
assertThat(rules.size(), is(5));
for (ShardingSphereRule each : rules) {
if (each instanceof ShardingRule) {
@@ -100,94 +94,98 @@ public class SpringBootStarterTest {
}
}
- private void assertShardingRule(final ShardingRule rule) {
- assertThat(rule.getDataSourceNames(), is(Sets.newHashSet("ds_0",
"ds_1")));
- Map<String, ShardingAlgorithm> shardingAlgorithmMap =
rule.getShardingAlgorithms();
- assertNotNull(shardingAlgorithmMap);
- InlineShardingAlgorithm databaseShardingAlgorithm =
(InlineShardingAlgorithm) shardingAlgorithmMap.get("databaseShardingAlgorithm");
-
assertThat(databaseShardingAlgorithm.getProps().getProperty("algorithm-expression"),
is("ds_$->{user_id % 2}"));
- InlineShardingAlgorithm orderTableShardingAlgorithm =
(InlineShardingAlgorithm)
shardingAlgorithmMap.get("orderTableShardingAlgorithm");
+ private void assertShardingRule(final ShardingRule actual) {
+ assertThat(actual.getDataSourceNames(), is(Sets.newHashSet("ds0",
"ds1")));
+ InlineShardingAlgorithm databaseShardingAlgorithm =
(InlineShardingAlgorithm)
actual.getShardingAlgorithms().get("databaseShardingAlgorithm");
+
assertThat(databaseShardingAlgorithm.getProps().getProperty("algorithm-expression"),
is("ds$->{user_id % 2}"));
+ InlineShardingAlgorithm orderTableShardingAlgorithm =
(InlineShardingAlgorithm)
actual.getShardingAlgorithms().get("orderTableShardingAlgorithm");
assertThat(orderTableShardingAlgorithm.getProps().getProperty("algorithm-expression"),
is("t_order_$->{order_id % 2}"));
- Collection<TableRule> tableRules = rule.getTableRules().values();
- assertNotNull(tableRules);
- assertThat(tableRules.size(), is(1));
- TableRule tableRule = tableRules.iterator().next();
- assertThat(tableRule.getLogicTable(), is("t_order"));
- List<DataNode> dataNodes = Arrays.asList(new
DataNode("ds_0.t_order_0"), new DataNode("ds_0.t_order_1"), new
DataNode("ds_1.t_order_0"), new DataNode("ds_1.t_order_1"));
- assertThat(tableRule.getActualDataNodes(), is(dataNodes));
- assertThat(tableRule.getActualDatasourceNames(),
is(Sets.newHashSet("ds_0", "ds_1")));
- assertThat(tableRule.getDataNodeGroups(),
is(DataNodeUtil.getDataNodeGroups(dataNodes)));
- assertThat(tableRule.getDatasourceToTablesMap(),
is(ImmutableMap.of("ds_1", Sets.newHashSet("t_order_0", "t_order_1"), "ds_0",
Sets.newHashSet("t_order_0", "t_order_1"))));
- assertThat(rule.getDefaultShardingColumn(), is("user_id"));
+ assertShardingTableRules(actual.getTableRules());
+ assertThat(actual.getDefaultShardingColumn(), is("user_id"));
}
- private void assertReadwriteSplittingRule(final ReadwriteSplittingRule
rule) {
- assertThat(rule.getDataSourceMapper(),
is(Collections.singletonMap("readwrite_ds", Arrays.asList("write_ds",
"read_ds_0", "read_ds_1"))));
- ReadwriteSplittingDataSourceRule dataSourceRule =
rule.getSingleDataSourceRule();
- assertNotNull(dataSourceRule);
+ private void assertShardingTableRules(final Map<String, TableRule> actual)
{
+ assertThat(actual.size(), is(1));
+ assertThat(actual.keySet().iterator().next(), is("t_order"));
+ assertShardingTableRule(actual.values().iterator().next());
+ }
+
+ private void assertShardingTableRule(final TableRule actual) {
+ assertThat(actual.getLogicTable(), is("t_order"));
+ Collection<DataNode> dataNodes = Arrays.asList(new
DataNode("ds0.t_order_0"), new DataNode("ds0.t_order_1"), new
DataNode("ds1.t_order_0"), new DataNode("ds1.t_order_1"));
+ assertThat(actual.getActualDataNodes(), is(dataNodes));
+ assertThat(actual.getActualDatasourceNames(),
is(Sets.newHashSet("ds0", "ds1")));
+ assertThat(actual.getDataNodeGroups(),
is(DataNodeUtil.getDataNodeGroups(dataNodes)));
+ assertThat(actual.getDatasourceToTablesMap(),
is(ImmutableMap.of("ds1", Sets.newHashSet("t_order_0", "t_order_1"), "ds0",
Sets.newHashSet("t_order_0", "t_order_1"))));
+ }
+
+ private void assertReadwriteSplittingRule(final ReadwriteSplittingRule
actual) {
+ assertThat(actual.getDataSourceMapper(),
is(Collections.singletonMap("readwrite_ds", Arrays.asList("write_ds",
"read_ds_0", "read_ds_1"))));
+ ReadwriteSplittingDataSourceRule dataSourceRule =
actual.getSingleDataSourceRule();
assertThat(dataSourceRule.getName(), is("readwrite_ds"));
- assertThat(dataSourceRule.getReadwriteSplittingType(),
instanceOf(StaticReadwriteSplittingType.class));
- StaticReadwriteSplittingType staticReadwriteSplitting =
(StaticReadwriteSplittingType) dataSourceRule.getReadwriteSplittingType();
- assertThat(staticReadwriteSplitting.getWriteDataSource(),
is("write_ds"));
- assertThat(staticReadwriteSplitting.getReadDataSources(),
is(Arrays.asList("read_ds_0", "read_ds_1")));
+ StaticReadwriteSplittingType staticReadwriteSplittingType =
(StaticReadwriteSplittingType) dataSourceRule.getReadwriteSplittingType();
+ assertThat(staticReadwriteSplittingType.getWriteDataSource(),
is("write_ds"));
+ assertThat(staticReadwriteSplittingType.getReadDataSources(),
is(Arrays.asList("read_ds_0", "read_ds_1")));
assertThat(dataSourceRule.getLoadBalancer(),
instanceOf(RandomReplicaLoadBalanceAlgorithm.class));
assertThat(dataSourceRule.getDataSourceMapper(),
is(Collections.singletonMap("readwrite_ds", Arrays.asList("write_ds",
"read_ds_0", "read_ds_1"))));
}
- private void assertEncryptRule(final EncryptRule rule) {
- assertTrue(rule.findEncryptTable("t_order").isPresent());
- EncryptTable table = rule.findEncryptTable("t_order").get();
- assertThat(table.getLogicColumn("pwd_cipher"), is("pwd"));
- assertThat(table.getPlainColumns(),
is(Collections.singletonList("pwd_plain")));
- assertThat(table.getAssistedQueryColumns(),
is(Collections.singletonList("pwd_assisted_query_cipher")));
- assertThat(rule.getCipherColumn("t_order", "pwd"), is("pwd_cipher"));
- assertThat(rule.getAssistedQueryColumns("t_order"),
is(Collections.singletonList("pwd_assisted_query_cipher")));
- assertThat(rule.getLogicAndCipherColumns("t_order"),
is(Collections.singletonMap("pwd", "pwd_cipher")));
- assertThat(rule.getEncryptValues(DefaultSchema.LOGIC_NAME, "t_order",
"pwd", Collections.singletonList("pwd_plain")),
- is(Collections.singletonList("V/RkV1+dVv80Y3csT3cR4g==")));
+ private void assertEncryptRule(final EncryptRule actual) {
+ assertTrue(actual.findEncryptTable("t_order").isPresent());
+ assertEncryptTable(actual.findEncryptTable("t_order").get());
+ assertThat(actual.getCipherColumn("t_order", "pwd"), is("pwd_cipher"));
+ assertThat(actual.getAssistedQueryColumns("t_order"),
is(Collections.singletonList("pwd_assisted_query_cipher")));
+ assertThat(actual.getLogicAndCipherColumns("t_order"),
is(Collections.singletonMap("pwd", "pwd_cipher")));
+ assertThat(actual.getEncryptValues("foo_db", "t_order", "pwd",
Collections.singletonList("pwd_plain")),
is(Collections.singletonList("V/RkV1+dVv80Y3csT3cR4g==")));
}
- private void assertShadowRule(final ShadowRule rule) {
- assertShadowDataSourceMappings(rule.getShadowDataSourceMappings());
- assertShadowAlgorithms(rule.getShadowAlgorithms());
- assertShadowTableRules(rule.getShadowTableRules());
+ private void assertEncryptTable(final EncryptTable actual) {
+ assertThat(actual.getLogicColumn("pwd_cipher"), is("pwd"));
+ assertThat(actual.getPlainColumns(),
is(Collections.singletonList("pwd_plain")));
+ assertThat(actual.getAssistedQueryColumns(),
is(Collections.singletonList("pwd_assisted_query_cipher")));
}
- private void assertShadowTableRules(final Map<String, ShadowTableRule>
shadowTableRules) {
- assertThat(shadowTableRules.size(), is(2));
- assertThat(shadowTableRules.get("t_order").getTableName(),
is("t_order"));
-
assertThat(shadowTableRules.get("t_order").getShadowDataSources().size(),
is(1));
-
assertThat(shadowTableRules.get("t_order").getHintShadowAlgorithmNames().size(),
is(1));
-
assertThat(shadowTableRules.get("t_order").getColumnShadowAlgorithmNames().size(),
is(2));
- assertThat(shadowTableRules.get("t_user").getTableName(),
is("t_user"));
-
assertThat(shadowTableRules.get("t_user").getShadowDataSources().size(), is(1));
-
assertThat(shadowTableRules.get("t_user").getHintShadowAlgorithmNames().size(),
is(1));
-
assertThat(shadowTableRules.get("t_user").getColumnShadowAlgorithmNames().size(),
is(0));
+ private void assertShadowRule(final ShadowRule actual) {
+ assertShadowDataSourceMappings(actual.getShadowDataSourceMappings());
+ assertShadowAlgorithms(actual.getShadowAlgorithms());
+ assertShadowTableRules(actual.getShadowTableRules());
}
- private void assertSQLParserRule(final SQLParserRule sqlParserRule) {
- assertThat(sqlParserRule.isSqlCommentParseEnabled(), is(true));
- assertCacheOption(sqlParserRule.getSqlStatementCache());
- assertCacheOption(sqlParserRule.getParseTreeCache());
+ private void assertShadowDataSourceMappings(final Map<String,
ShadowDataSourceRule> actual) {
+ assertThat(actual.size(), is(1));
+ assertThat(actual.get("shadow-data-source").getSourceDataSource(),
is("ds"));
+ assertThat(actual.get("shadow-data-source").getShadowDataSource(),
is("ds-shadow"));
}
-
- private void assertCacheOption(final CacheOption cacheOption) {
- assertThat(cacheOption.getInitialCapacity(), is(1024));
- assertThat(cacheOption.getMaximumSize(), is(1024L));
- assertThat(cacheOption.getConcurrencyLevel(), is(4));
+
+ private void assertShadowAlgorithms(final Map<String, ShadowAlgorithm>
actual) {
+ assertThat(actual.size(), is(3));
+ assertThat(actual.get("user-id-match-algorithm"),
instanceOf(ColumnShadowAlgorithm.class));
+ assertThat(actual.get("order-id-match-algorithm"),
instanceOf(ColumnShadowAlgorithm.class));
+ assertThat(actual.get("simple-hint-algorithm"),
instanceOf(HintShadowAlgorithm.class));
}
- private void assertShadowAlgorithms(final Map<String, ShadowAlgorithm>
shadowAlgorithms) {
- assertThat(shadowAlgorithms.size(), is(3));
- assertThat(shadowAlgorithms.get("user-id-match-algorithm") instanceof
ColumnShadowAlgorithm, is(true));
- assertThat(shadowAlgorithms.get("order-id-match-algorithm") instanceof
ColumnShadowAlgorithm, is(true));
- assertThat(shadowAlgorithms.get("simple-hint-algorithm") instanceof
HintShadowAlgorithm, is(true));
+ private void assertShadowTableRules(final Map<String, ShadowTableRule>
actual) {
+ assertThat(actual.size(), is(2));
+ assertThat(actual.get("t_order").getTableName(), is("t_order"));
+ assertThat(actual.get("t_order").getShadowDataSources().size(), is(1));
+ assertThat(actual.get("t_order").getHintShadowAlgorithmNames().size(),
is(1));
+
assertThat(actual.get("t_order").getColumnShadowAlgorithmNames().size(), is(2));
+ assertThat(actual.get("t_user").getTableName(), is("t_user"));
+ assertThat(actual.get("t_user").getShadowDataSources().size(), is(1));
+ assertThat(actual.get("t_user").getHintShadowAlgorithmNames().size(),
is(1));
+
assertThat(actual.get("t_user").getColumnShadowAlgorithmNames().size(), is(0));
}
- private void assertShadowDataSourceMappings(final Map<String,
ShadowDataSourceRule> shadowDataSourceMappings) {
- assertThat(shadowDataSourceMappings.size(), is(1));
-
assertThat(shadowDataSourceMappings.get("shadow-data-source").getSourceDataSource(),
is("ds"));
-
assertThat(shadowDataSourceMappings.get("shadow-data-source").getShadowDataSource(),
is("ds-shadow"));
+ private void assertSQLParserRule(final SQLParserRule actual) {
+ assertTrue(actual.isSqlCommentParseEnabled());
+ assertCacheOption(actual.getSqlStatementCache());
+ assertCacheOption(actual.getParseTreeCache());
+ }
+
+ private void assertCacheOption(final CacheOption actual) {
+ assertThat(actual.getInitialCapacity(), is(1024));
+ assertThat(actual.getMaximumSize(), is(1024L));
+ assertThat(actual.getConcurrencyLevel(), is(4));
}
@Test
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application-common.properties
b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application.properties
similarity index 92%
rename from
shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application-common.properties
rename to
shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application.properties
index 7ec3fe9..c553adb 100644
---
a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application-common.properties
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter/src/test/resources/application.properties
@@ -15,12 +15,12 @@
# limitations under the License.
#
-spring.shardingsphere.schema.name=logic_db
+spring.shardingsphere.schema.name=foo_db
-spring.shardingsphere.datasource.names=ds_${0..1}
+spring.shardingsphere.datasource.names=ds${0..1}
-spring.shardingsphere.datasource.ds_0.type=org.apache.shardingsphere.test.mock.MockedDataSource
-spring.shardingsphere.datasource.ds_1.type=org.apache.shardingsphere.test.mock.MockedDataSource
+spring.shardingsphere.datasource.ds0.type=org.apache.shardingsphere.test.mock.MockedDataSource
+spring.shardingsphere.datasource.ds1.type=org.apache.shardingsphere.test.mock.MockedDataSource
spring.shardingsphere.rules.readwrite-splitting.load-balancers.random.type=RANDOM
@@ -30,12 +30,12 @@
spring.shardingsphere.rules.readwrite-splitting.data-sources.readwrite_ds.props.
spring.shardingsphere.rules.readwrite-splitting.data-sources.readwrite_ds.load-balancer-name=random
spring.shardingsphere.rules.sharding.sharding-algorithms.databaseShardingAlgorithm.type=INLINE
-spring.shardingsphere.rules.sharding.sharding-algorithms.databaseShardingAlgorithm.props.algorithm-expression=ds_$->{user_id
% 2}
+spring.shardingsphere.rules.sharding.sharding-algorithms.databaseShardingAlgorithm.props.algorithm-expression=ds$->{user_id
% 2}
spring.shardingsphere.rules.sharding.sharding-algorithms.orderTableShardingAlgorithm.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.orderTableShardingAlgorithm.props.algorithm-expression=t_order_$->{order_id
% 2}
-spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes=ds_$->{0..1}.t_order_$->{0..1}
+spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes=ds$->{0..1}.t_order_$->{0..1}
spring.shardingsphere.rules.sharding.tables.t_order.database-strategy.standard.sharding-column=user_id
spring.shardingsphere.rules.sharding.tables.t_order.database-strategy.standard.sharding-algorithm-name=databaseShardingAlgorithm