未验证 提交 a3e392d8 编写于 作者: L Liang Zhang 提交者: GitHub

Refactor Revise (#6932)

* Refactor SpringBootStarterTest

* Refactor SpringBootJNDIDataSourceTest

* Remove useless raw test

* fix test case
上级 4dcca4f9
......@@ -18,7 +18,7 @@
package org.apache.shardingsphere.masterslave.spring.boot;
import org.apache.shardingsphere.masterslave.algorithm.RandomMasterSlaveLoadBalanceAlgorithm;
import org.apache.shardingsphere.masterslave.api.config.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.masterslave.algorithm.config.AlgorithmProvidedMasterSlaveRuleConfiguration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.autoconfigure.SpringBootApplication;
......@@ -40,7 +40,7 @@ public class MasterSlaveSpringBootStarterTest {
private RandomMasterSlaveLoadBalanceAlgorithm random;
@Resource
private MasterSlaveRuleConfiguration masterSlaveRuleConfiguration;
private AlgorithmProvidedMasterSlaveRuleConfiguration masterSlaveRuleConfiguration;
@Test
public void assertLoadBalanceAlgorithm() {
......
......@@ -17,7 +17,6 @@
package org.apache.shardingsphere.spring.boot;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
......@@ -41,6 +40,7 @@ import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
......
......@@ -15,11 +15,12 @@
* limitations under the License.
*/
package org.apache.shardingsphere.spring.boot.type;
package org.apache.shardingsphere.spring.boot;
import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
import org.apache.shardingsphere.spring.boot.fixture.TestJndiInitialContextFactory;
import org.apache.shardingsphere.test.MockedDataSource;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.autoconfigure.SpringBootApplication;
......@@ -28,34 +29,35 @@ import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import javax.naming.Context;
import javax.sql.DataSource;
import java.util.Map;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringBootShadowTest.class)
@SpringBootTest(classes = SpringBootJNDIDataSourceTest.class)
@SpringBootApplication
@ActiveProfiles("shadow")
public class SpringBootShadowTest {
@ActiveProfiles("jndi")
public class SpringBootJNDIDataSourceTest {
@Resource
private DataSource dataSource;
private ShardingSphereDataSource dataSource;
@Test
public void assertSqlShow() {
assertTrue(((ShardingSphereDataSource) dataSource).getSchemaContexts().getProps().<Boolean>getValue(ConfigurationPropertyKey.SQL_SHOW));
@BeforeClass
public static void setUp() {
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, TestJndiInitialContextFactory.class.getName());
TestJndiInitialContextFactory.bind("java:comp/env/jdbc/jndi0", new MockedDataSource());
TestJndiInitialContextFactory.bind("java:comp/env/jdbc/jndi1", new MockedDataSource());
}
@Test
public void assertDataSource() {
assertTrue(dataSource instanceof ShardingSphereDataSource);
assertShadowRule();
}
private void assertShadowRule() {
ShadowRule shadowRule = (ShadowRule) ((ShardingSphereDataSource) dataSource).getSchemaContexts().getDefaultSchemaContext().getSchema().getRules().iterator().next();
assertThat(shadowRule.getColumn(), is("is_shadow"));
public void assertDatasourceMap() {
Map<String, DataSource> dataSourceMap = dataSource.getDataSourceMap();
assertThat(dataSourceMap.size(), is(2));
assertTrue(dataSourceMap.containsKey("jndi0"));
assertTrue(dataSourceMap.containsKey("jndi1"));
}
}
......@@ -15,12 +15,15 @@
* limitations under the License.
*/
package org.apache.shardingsphere.spring.boot.type;
package org.apache.shardingsphere.spring.boot;
import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.masterslave.rule.MasterSlaveDataSourceRule;
import org.apache.shardingsphere.masterslave.rule.MasterSlaveRule;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.autoconfigure.SpringBootApplication;
......@@ -29,7 +32,6 @@ import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Collection;
import static org.hamcrest.CoreMatchers.is;
......@@ -37,30 +39,57 @@ import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringBootMasterSlaveTest.class)
@SpringBootTest(classes = SpringBootStarterTest.class)
@SpringBootApplication
@ActiveProfiles("masterslave")
public class SpringBootMasterSlaveTest {
@ActiveProfiles("common")
public class SpringBootStarterTest {
@Resource
private DataSource dataSource;
private ShardingSphereDataSource dataSource;
@Test
public void assertDataSource() {
assertTrue(dataSource instanceof ShardingSphereDataSource);
assertThat(((ShardingSphereDataSource) dataSource).getDataSourceMap().size(), is(3));
Collection<ShardingSphereRule> rules = ((ShardingSphereDataSource) dataSource).getSchemaContexts().getDefaultSchemaContext().getSchema().getRules();
assertThat(rules.size(), is(1));
assertMasterSlaveRule((MasterSlaveRule) rules.iterator().next());
public void assertDataSourceMap() {
assertThat(dataSource.getDataSourceMap().size(), is(2));
assertTrue(dataSource.getDataSourceMap().containsKey("ds_0"));
assertTrue(dataSource.getDataSourceMap().containsKey("ds_1"));
}
@Test
public void assertRules() {
Collection<ShardingSphereRule> rules = dataSource.getSchemaContexts().getDefaultSchemaContext().getSchema().getRules();
assertThat(rules.size(), is(2));
for (ShardingSphereRule each : rules) {
if (each instanceof ShardingRule) {
assertShardingRule((ShardingRule) each);
} else if (each instanceof MasterSlaveRule) {
assertMasterSlaveRule((MasterSlaveRule) each);
} else if (each instanceof EncryptRule) {
assertEncryptRule((EncryptRule) each);
} else if (each instanceof ShadowRule) {
assertShadowRule((ShadowRule) each);
}
}
}
private void assertShardingRule(final ShardingRule rule) {
// TODO
}
private void assertMasterSlaveRule(final MasterSlaveRule rule) {
MasterSlaveDataSourceRule dataSourceRule = rule.getSingleDataSourceRule();
assertThat(dataSourceRule.getName(), is("ds_ms"));
assertThat(dataSourceRule.getMasterDataSourceName(), is("ds_master"));
assertThat(dataSourceRule.getSlaveDataSourceNames().size(), is(2));
assertThat(dataSourceRule.getSlaveDataSourceNames().get(0), is("ds_slave_0"));
assertThat(dataSourceRule.getSlaveDataSourceNames().get(1), is("ds_slave_1"));
assertThat(dataSourceRule.getLoadBalancer().getType(), is("RANDOM"));
// TODO
}
private void assertEncryptRule(final EncryptRule rule) {
// TODO
}
private void assertShadowRule(final ShadowRule rule) {
// TODO
}
@Test
public void assertProperties() {
assertTrue(dataSource.getSchemaContexts().getProps().<Boolean>getValue(ConfigurationPropertyKey.SQL_SHOW));
assertThat(dataSource.getSchemaContexts().getProps().getValue(ConfigurationPropertyKey.EXECUTOR_SIZE), is(10));
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.spring.boot.fixture;
import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
import java.util.Properties;
@Getter
@Setter
public final class TestEncryptAlgorithm implements EncryptAlgorithm {
private Properties props = new Properties();
@Override
public void init() {
}
@Override
public String encrypt(final Object plaintext) {
return "";
}
@Override
public Object decrypt(final String ciphertext) {
return "";
}
@Override
public String getType() {
return "test";
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.spring.boot.type;
import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
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;
import javax.sql.DataSource;
import java.util.Optional;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringBootEncryptTest.class)
@SpringBootApplication
@ActiveProfiles("encrypt")
public class SpringBootEncryptTest {
@Resource
private DataSource dataSource;
@Test
public void assertSqlShow() {
assertTrue(((ShardingSphereDataSource) dataSource).getSchemaContexts().getProps().<Boolean>getValue(ConfigurationPropertyKey.SQL_SHOW));
}
@Test
public void assertWithEncryptDataSource() {
assertTrue(dataSource instanceof ShardingSphereDataSource);
assertThat(((ShardingSphereDataSource) dataSource).getDataSourceMap().size(), is(1));
}
@Test
public void assertWithEncryptRule() {
EncryptRule encryptRule = (EncryptRule) ((ShardingSphereDataSource) dataSource).getSchemaContexts().getDefaultSchemaContext().getSchema().getRules().iterator().next();
assertThat(encryptRule.getEncryptTableNames().size(), is(1));
assertTrue(encryptRule.findEncryptor("t_order", "user_id").isPresent());
assertThat(encryptRule.getCipherColumn("t_order", "user_id"), is("user_encrypt"));
assertThat(encryptRule.findPlainColumn("t_order", "user_id"), is(Optional.of("user_decrypt")));
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.spring.boot.type;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
import org.apache.shardingsphere.spring.boot.fixture.TestJndiInitialContextFactory;
import org.junit.BeforeClass;
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;
import javax.naming.Context;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringBootJNDITest.class)
@SpringBootApplication
@ActiveProfiles("jndi")
public class SpringBootJNDITest {
private static final String TEST_DATA_SOURCE_URL = "jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL";
private static final String JNDI_DATA_SOURCE_0 = "java:comp/env/jdbc/jndi0";
private static final String JNDI_DATA_SOURCE_1 = "java:comp/env/jdbc/jndi1";
@Resource
private DataSource dataSource;
@BeforeClass
public static void setUpBeforeClass() {
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, TestJndiInitialContextFactory.class.getName());
TestJndiInitialContextFactory.bind(JNDI_DATA_SOURCE_0, createNewDataSource("jndi0"));
TestJndiInitialContextFactory.bind(JNDI_DATA_SOURCE_1, createNewDataSource("jndi1"));
}
private static DataSource createNewDataSource(final String dsName) {
BasicDataSource result = new BasicDataSource();
result.setUrl(String.format(TEST_DATA_SOURCE_URL, dsName));
result.setUsername("sa");
result.setPassword("");
result.setDriverClassName("org.h2.Driver");
return result;
}
@Test
public void assertJndiDatasource() throws SQLException {
assertThat(dataSource, instanceOf(ShardingSphereDataSource.class));
Map<String, DataSource> dataSourceMap = ((ShardingSphereDataSource) dataSource).getDataSourceMap();
assertThat(dataSourceMap.size(), is(2));
assertTrue(dataSourceMap.containsKey("jndi0"));
assertTrue(dataSourceMap.containsKey("jndi1"));
assertDatasource(dataSourceMap.get("jndi0"), String.format(TEST_DATA_SOURCE_URL, "jndi0"));
assertDatasource(dataSourceMap.get("jndi1"), String.format(TEST_DATA_SOURCE_URL, "jndi1"));
}
private void assertDatasource(final DataSource actualDatasource, final String expectedJDBCUrl) throws SQLException {
String expected = expectedJDBCUrl.substring(0, expectedJDBCUrl.indexOf(';'));
try (Connection connection = actualDatasource.getConnection()) {
assertThat(connection.getMetaData().getURL(), is(expected));
}
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.spring.boot.type;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import javax.sql.DataSource;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringBootRawDataSourceTest.class)
@ActiveProfiles("raw")
@SpringBootApplication
public class SpringBootRawDataSourceTest {
@Resource
private ApplicationContext context;
@Test(expected = NoSuchBeanDefinitionException.class)
public void assertDataSource() {
context.getBean(DataSource.class);
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package org.apache.shardingsphere.spring.boot.type;
import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.kernel.context.SchemaContexts;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sharding.rule.TableRule;
import org.apache.shardingsphere.sharding.strategy.standard.StandardShardingStrategy;
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;
import javax.sql.DataSource;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringBootShardingTest.class)
@SpringBootApplication
@ActiveProfiles("sharding")
public class SpringBootShardingTest {
@Resource
private DataSource dataSource;
@Test
public void assertWithShardingSphereDataSource() {
assertThat(dataSource, instanceOf(ShardingSphereDataSource.class));
SchemaContexts schemaContexts = ((ShardingSphereDataSource) dataSource).getSchemaContexts();
assertThat(((ShardingSphereDataSource) dataSource).getDataSourceMap().size(), is(3));
assertTrue(schemaContexts.getProps().<Boolean>getValue(ConfigurationPropertyKey.SQL_SHOW));
assertTrue(schemaContexts.getProps().getValue(ConfigurationPropertyKey.SQL_SHOW));
assertThat(schemaContexts.getProps().getValue(ConfigurationPropertyKey.EXECUTOR_SIZE), is(100));
}
@Test
public void assertWithShardingSphereDataSourceNames() {
SchemaContexts schemaContexts = ((ShardingSphereDataSource) dataSource).getSchemaContexts();
ShardingRule shardingRule = (ShardingRule) schemaContexts.getDefaultSchemaContext().getSchema().getRules().iterator().next();
assertThat(shardingRule.getDataSourceNames().size(), is(2));
assertTrue(shardingRule.getDataSourceNames().contains("ds_0"));
assertTrue(shardingRule.getDataSourceNames().contains("ds_1"));
}
@Test
public void assertWithTableRules() {
SchemaContexts schemaContexts = ((ShardingSphereDataSource) dataSource).getSchemaContexts();
ShardingRule shardingRule = (ShardingRule) schemaContexts.getDefaultSchemaContext().getSchema().getRules().iterator().next();
assertThat(shardingRule.getTableRules().size(), is(2));
TableRule tableRule1 = shardingRule.getTableRule("t_order");
assertThat(tableRule1.getActualDataNodes().size(), is(4));
assertTrue(tableRule1.getActualDataNodes().contains(new DataNode("ds_0", "t_order_0")));
assertTrue(tableRule1.getActualDataNodes().contains(new DataNode("ds_0", "t_order_1")));
assertTrue(tableRule1.getActualDataNodes().contains(new DataNode("ds_1", "t_order_0")));
assertTrue(tableRule1.getActualDataNodes().contains(new DataNode("ds_1", "t_order_1")));
assertThat(tableRule1.getTableShardingStrategy(), instanceOf(StandardShardingStrategy.class));
assertThat(tableRule1.getTableShardingStrategy().getShardingColumns().iterator().next(), is("order_id"));
assertTrue(tableRule1.getGenerateKeyColumn().isPresent());
assertThat(tableRule1.getGenerateKeyColumn().get(), is("order_id"));
TableRule tableRule2 = shardingRule.getTableRule("t_order_item");
assertThat(tableRule2.getActualDataNodes().size(), is(4));
assertTrue(tableRule2.getActualDataNodes().contains(new DataNode("ds_0", "t_order_item_0")));
assertTrue(tableRule2.getActualDataNodes().contains(new DataNode("ds_0", "t_order_item_1")));
assertTrue(tableRule2.getActualDataNodes().contains(new DataNode("ds_1", "t_order_item_0")));
assertTrue(tableRule2.getActualDataNodes().contains(new DataNode("ds_1", "t_order_item_1")));
assertThat(tableRule1.getTableShardingStrategy(), instanceOf(StandardShardingStrategy.class));
assertThat(tableRule1.getTableShardingStrategy().getShardingColumns().iterator().next(), is("order_id"));
assertTrue(tableRule2.getGenerateKeyColumn().isPresent());
assertThat(tableRule2.getGenerateKeyColumn().get(), is("order_item_id"));
}
@Test
public void assertWithBindingTableRules() {
SchemaContexts schemaContexts = ((ShardingSphereDataSource) dataSource).getSchemaContexts();
ShardingRule shardingRule = (ShardingRule) schemaContexts.getDefaultSchemaContext().getSchema().getRules().iterator().next();
assertThat(shardingRule.getBindingTableRules().size(), is(2));
TableRule tableRule1 = shardingRule.getTableRule("t_order");
assertThat(tableRule1.getLogicTable(), is("t_order"));
assertThat(tableRule1.getActualDataNodes().size(), is(4));
assertTrue(tableRule1.getActualDataNodes().contains(new DataNode("ds_0", "t_order_0")));
assertTrue(tableRule1.getActualDataNodes().contains(new DataNode("ds_0", "t_order_1")));
assertTrue(tableRule1.getActualDataNodes().contains(new DataNode("ds_1", "t_order_0")));
assertTrue(tableRule1.getActualDataNodes().contains(new DataNode("ds_1", "t_order_1")));
assertThat(tableRule1.getTableShardingStrategy(), instanceOf(StandardShardingStrategy.class));
assertThat(tableRule1.getTableShardingStrategy().getShardingColumns().iterator().next(), is("order_id"));
assertTrue(tableRule1.getGenerateKeyColumn().isPresent());
assertThat(tableRule1.getGenerateKeyColumn().get(), is("order_id"));
TableRule tableRule2 = shardingRule.getTableRule("t_order_item");
assertThat(tableRule2.getLogicTable(), is("t_order_item"));
assertThat(tableRule2.getActualDataNodes().size(), is(4));
assertTrue(tableRule2.getActualDataNodes().contains(new DataNode("ds_0", "t_order_item_0")));
assertTrue(tableRule2.getActualDataNodes().contains(new DataNode("ds_0", "t_order_item_1")));
assertTrue(tableRule2.getActualDataNodes().contains(new DataNode("ds_1", "t_order_item_0")));
assertTrue(tableRule2.getActualDataNodes().contains(new DataNode("ds_1", "t_order_item_1")));
assertThat(tableRule1.getTableShardingStrategy(), instanceOf(StandardShardingStrategy.class));
assertThat(tableRule1.getTableShardingStrategy().getShardingColumns().iterator().next(), is("order_id"));
assertTrue(tableRule2.getGenerateKeyColumn().isPresent());
assertThat(tableRule2.getGenerateKeyColumn().get(), is("order_item_id"));
}
@Test
public void assertWithBroadcastTables() {
SchemaContexts schemaContexts = ((ShardingSphereDataSource) dataSource).getSchemaContexts();
ShardingRule shardingRule = (ShardingRule) schemaContexts.getDefaultSchemaContext().getSchema().getRules().iterator().next();
assertThat(shardingRule.getBroadcastTables().size(), is(1));
assertThat(shardingRule.getBroadcastTables().iterator().next(), is("t_config"));
}
}
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#
org.apache.shardingsphere.spring.boot.fixture.TestEncryptAlgorithm
......@@ -15,46 +15,34 @@
# limitations under the License.
#
spring.shardingsphere.datasource.names=ds,ds_${0..1}
spring.shardingsphere.datasource.names=ds_${0..1}
spring.shardingsphere.datasource.ds.type=org.apache.shardingsphere.test.MockedDataSource
spring.shardingsphere.datasource.ds_0.type=org.apache.shardingsphere.test.MockedDataSource
spring.shardingsphere.datasource.ds_1.type=org.apache.shardingsphere.test.MockedDataSource
spring.shardingsphere.rules.sharding.default-database-strategy.standard.sharding-column=user_id
spring.shardingsphere.rules.sharding.default-database-strategy.standard.sharding-algorithm-name=database_inline
spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes=ds_${0..1}.t_order_${0..1}
# TODO add master-slave config
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.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.database-strategy.standard.sharding-column=user_id
spring.shardingsphere.rules.sharding.tables.t_order.database-strategy.standard.sharding-algorithm-name=databaseShardingAlgorithm
spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-column=order_id
spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-algorithm-name=t_order_inline
spring.shardingsphere.rules.sharding.tables.t_order.key-generate-strategy.column=order_id
spring.shardingsphere.rules.sharding.tables.t_order.key-generate-strategy.key-generator-name=snowflake
spring.shardingsphere.rules.sharding.tables.t_order_item.actual-data-nodes=ds_${0..1}.t_order_item_${0..1}
spring.shardingsphere.rules.sharding.tables.t_order_item.table-strategy.standard.sharding-column=order_id
spring.shardingsphere.rules.sharding.tables.t_order_item.table-strategy.standard.sharding-algorithm-name=t_order_item_inline
spring.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.column=order_item_id
spring.shardingsphere.rules.sharding.tables.t_order_item.key-generate-strategy.key-generator-name=snowflake
spring.shardingsphere.rules.sharding.binding-tables=t_order,t_order_item
spring.shardingsphere.rules.sharding.broadcast-tables=t_config
spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.props.algorithm.expression=ds_$->{user_id % 2}
spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_inline.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_inline.props.algorithm.expression=t_order_$->{order_id % 2}
spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_item_inline.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.t_order_item_inline.props.algorithm.expression=t_order_item_$->{order_id % 2}
spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE
spring.shardingsphere.rules.sharding.key-generators.snowflake.props.worker.id=123
spring.shardingsphere.rules.encrypt.encryptors.order_encrypt.type=test
spring.shardingsphere.rules.encrypt.encryptors.order_encrypt.props.appToken=business
spring.shardingsphere.rules.encrypt.tables.t_order.columns.pwd.cipher-column=cipher_pwd
spring.shardingsphere.rules.encrypt.tables.t_order.columns.pwd.encryptor-name=order_encrypt
spring.shardingsphere.rules.encrypt.tables.t_order.columns.name.cipher-column=cipher_name
spring.shardingsphere.rules.encrypt.tables.t_order.columns.name.plain-column=plain_name
spring.shardingsphere.rules.encrypt.tables.t_order.columns.name.encryptor-name=order_encrypt
spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-algorithm-name=orderTableShardingAlgorithm
spring.shardingsphere.rules.encrypt.encryptors.aesEncryptor.type=AES
spring.shardingsphere.rules.encrypt.encryptors.aesEncryptor.props.aes.key.value=123456
spring.shardingsphere.rules.encrypt.tables.t_order.columns.pwd.cipher-column=pwd_cipher
spring.shardingsphere.rules.encrypt.tables.t_order.columns.pwd.plain-column=pwd_plain
spring.shardingsphere.rules.encrypt.tables.t_order.columns.pwd.encryptor-name=aesEncryptor
# TODO add shadow config
spring.shardingsphere.props.sql.show=true
spring.shardingsphere.props.executor.size=100
spring.shardingsphere.props.executor.size=10
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#
spring.shardingsphere.datasource.name=ds
spring.shardingsphere.datasource.ds.type=org.apache.shardingsphere.test.MockedDataSource
spring.shardingsphere.rules.encrypt.tables.t_order.columns.user_id.cipher-column=user_encrypt
spring.shardingsphere.rules.encrypt.tables.t_order.columns.user_id.assisted-query-column=user_assisted
spring.shardingsphere.rules.encrypt.tables.t_order.columns.user_id.plain-column=user_decrypt
spring.shardingsphere.rules.encrypt.tables.t_order.columns.user_id.encryptor-name=aes_encryptor
spring.shardingsphere.rules.encrypt.encryptors.aes_encryptor.type=AES
spring.shardingsphere.rules.encrypt.encryptors.aes_encryptor.props.aes.key.value=123456
spring.shardingsphere.props.sql.show=true
spring.shardingsphere.props.query.with.cipher.column=true
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#
spring.example.datasource.name=ds_master
spring.shardingsphere.datasource.names=${spring.example.datasource.name},ds_slave_${0..1}
spring.shardingsphere.datasource.ds_master.type=org.apache.shardingsphere.test.MockedDataSource
spring.shardingsphere.datasource.ds_slave_0.type=org.apache.shardingsphere.test.MockedDataSource
spring.shardingsphere.datasource.ds_slave_1.type=org.apache.shardingsphere.test.MockedDataSource
spring.shardingsphere.rules.master-slave.data-sources.ds_ms.master-data-source-name=ds_master
spring.shardingsphere.rules.master-slave.data-sources.ds_ms.slave-data-source-names=ds_slave_0,ds_slave_1
spring.shardingsphere.rules.master-slave.data-sources.ds_ms.load-balancer-name=random
spring.shardingsphere.rules.master-slave.load-balancers.random.type=RANDOM
spring.shardingsphere.props.sql.show=true
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#
spring.application.name=none.sharding.test
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.enabled=false
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#
spring.shardingsphere.datasource.names=ds,shadow_ds
spring.shardingsphere.datasource.ds.type=org.apache.shardingsphere.test.MockedDataSource
spring.shardingsphere.datasource.shadow_ds.type=org.apache.shardingsphere.test.MockedDataSource
spring.shardingsphere.rules.shadow.column=is_shadow
spring.shardingsphere.rules.shadow.shadow-mappings.ds=shadow_ds
spring.shardingsphere.props.sql.show=true
spring.shardingsphere.props.executor.size=100
......@@ -17,18 +17,18 @@
package org.apache.shardingsphere.sql.parser.binder.metadata.table;
import java.sql.ResultSet;
import java.util.Optional;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.sql.parser.binder.metadata.MetaDataConnection;
import org.apache.shardingsphere.sql.parser.binder.metadata.column.ColumnMetaDataLoader;
import org.apache.shardingsphere.sql.parser.binder.metadata.index.IndexMetaDataLoader;
import org.apache.shardingsphere.sql.parser.binder.metadata.util.JdbcUtil;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.shardingsphere.sql.parser.binder.metadata.util.JdbcUtil;
import java.util.Optional;
/**
* Table meta data loader.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册