You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2021/01/28 06:16:08 UTC
[shardingsphere] branch master updated: Fix#8351 (#9192)
This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 a05b956 Fix#8351 (#9192)
a05b956 is described below
commit a05b9564425ca045fcbe2c7329c742d4eccf8477
Author: huanghao495430759 <34...@users.noreply.github.com>
AuthorDate: Thu Jan 28 14:14:04 2021 +0800
Fix#8351 (#9192)
* fix issue#8351:Redesign SchemaBuilderTest.
Co-authored-by: huanghao-jk <hu...@360jinrong.net>
---
.../metadata/schema/builder/SchemaBuilderTest.java | 84 +++++++++++++++++-----
.../schema/builder/TableMetaDataBuilderTest.java | 2 +-
...tainedFixtureRuleBasedTableMetaDataBuilder.java | 10 +--
.../fixture/rule/DataNodeContainedFixtureRule.java | 30 +++++---
4 files changed, 94 insertions(+), 32 deletions(-)
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/builder/SchemaBuilderTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/builder/SchemaBuilderTest.java
index 3bde21a..08253f4 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/builder/SchemaBuilderTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/builder/SchemaBuilderTest.java
@@ -17,11 +17,13 @@
package org.apache.shardingsphere.infra.metadata.schema.builder;
+import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.schema.fixture.rule.CommonFixtureRule;
import org.apache.shardingsphere.infra.metadata.schema.fixture.rule.DataNodeContainedFixtureRule;
-import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
+import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
@@ -29,6 +31,9 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
@@ -36,31 +41,78 @@ import java.util.Collections;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public final class SchemaBuilderTest {
-
+ private static final String TEST_CATALOG = "catalog";
+
+ private static final String TEST_SCHEMA = "schema";
+
+ private static final String TABLE_TYPE = "TABLE";
+
+ private static final String VIEW_TYPE = "VIEW";
+
+ private static final String TABLE_NAME = "TABLE_NAME";
+
+ private final String[] unConfiguredTableNames = new String[]{"unconfigured_table1", "unconfigured_table2"};
+
+ private SchemaBuilderMaterials schemaBuilderMaterials;
+
@Mock
private DatabaseType databaseType;
-
+
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private DataSource dataSource;
-
+
@Mock
private ConfigurationProperties props;
-
- @Test
- public void assertBuild() throws SQLException {
- ShardingSphereSchema actual = SchemaBuilder.build(
- new SchemaBuilderMaterials(databaseType, Collections.singletonMap("logic_db", dataSource), Arrays.asList(new CommonFixtureRule(), new DataNodeContainedFixtureRule()), props));
- assertSchema(actual);
+
+ @Before
+ public void setUp() {
+ schemaBuilderMaterials = new SchemaBuilderMaterials(databaseType, Collections.singletonMap("logic_db", dataSource), Arrays.asList(new CommonFixtureRule(), new DataNodeContainedFixtureRule()),
+ props);
}
-
- private void assertSchema(final ShardingSphereSchema actual) {
+
+ @Test
+ public void assertBuildOfAllShardingTables() throws SQLException {
+ ShardingSphereSchema actual = SchemaBuilder.build(schemaBuilderMaterials);
assertThat(actual.getAllTableNames().size(), is(2));
- assertTrue(actual.containsTable("data_node_routed_table_0"));
- assertTrue(actual.get("data_node_routed_table_0").getColumns().containsKey("id"));
- assertTrue(actual.containsTable("data_node_routed_table_1"));
- assertTrue(actual.get("data_node_routed_table_1").getColumns().containsKey("id"));
+ assertSchemaOfShardingTables(actual);
+ }
+
+ private void assertSchemaOfShardingTables(final ShardingSphereSchema actual) {
+ assertTrue(actual.containsTable("data_node_routed_table1"));
+ assertTrue(actual.get("data_node_routed_table1").getColumns().containsKey("id"));
+ assertTrue(actual.containsTable("data_node_routed_table2"));
+ assertTrue(actual.get("data_node_routed_table2").getColumns().containsKey("id"));
+ }
+
+ @Test
+ @SneakyThrows(SQLException.class)
+ public void assertBuildOfShardingTablesAndUnConfiguredTables() {
+ ResultSet resultSet = mock(ResultSet.class, Answers.RETURNS_DEEP_STUBS);
+ DatabaseMetaData databaseMetaData = mock(DatabaseMetaData.class, Answers.RETURNS_DEEP_STUBS);
+ Connection connection = mock(Connection.class, Answers.RETURNS_DEEP_STUBS);
+ when(dataSource.getConnection()).thenReturn(connection);
+ when(connection.getMetaData()).thenReturn(databaseMetaData);
+ when(connection.getCatalog()).thenReturn(TEST_CATALOG);
+ when(connection.getSchema()).thenReturn(TEST_SCHEMA);
+ when(databaseMetaData.getTables(connection.getCatalog(), connection.getSchema(), null, new String[]{TABLE_TYPE, VIEW_TYPE})).thenReturn(resultSet);
+ when(resultSet.next()).thenReturn(true, true, true, true, true, true, false);
+ String[] mockReturnTables = new String[]{unConfiguredTableNames[1], "data_node_routed_table1_0", "data_node_routed_table1_1", "data_node_routed_table2_0", "data_node_routed_table2_1"};
+ when(resultSet.getString(TABLE_NAME)).thenReturn(unConfiguredTableNames[0], mockReturnTables);
+ ShardingSphereSchema actual = SchemaBuilder.build(schemaBuilderMaterials);
+ assertThat(actual.getAllTableNames().size(), is(4));
+ assertSchemaOfShardingTablesAndUnConfiguredTables(actual);
+ }
+
+ private void assertSchemaOfShardingTablesAndUnConfiguredTables(final ShardingSphereSchema actual) {
+ assertSchemaOfShardingTables(actual);
+ assertTrue(actual.containsTable(unConfiguredTableNames[0]));
+ assertThat(actual.get(unConfiguredTableNames[0]).getColumns().size(), is(0));
+ assertTrue(actual.containsTable(unConfiguredTableNames[1]));
+ assertThat(actual.get(unConfiguredTableNames[1]).getColumns().size(), is(0));
}
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/builder/TableMetaDataBuilderTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/builder/TableMetaDataBuilderTest.java
index 8558187..7f0f2d7 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/builder/TableMetaDataBuilderTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/builder/TableMetaDataBuilderTest.java
@@ -49,7 +49,7 @@ public final class TableMetaDataBuilderTest {
@Test
public void assertBuildWithExistedTableName() throws SQLException {
- assertTrue(TableMetaDataBuilder.build("data_node_routed_table_0", new SchemaBuilderMaterials(
+ assertTrue(TableMetaDataBuilder.build("data_node_routed_table1", new SchemaBuilderMaterials(
databaseType, Collections.singletonMap("logic_db", dataSource), Arrays.asList(new CommonFixtureRule(), new DataNodeContainedFixtureRule()), props)).isPresent());
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/fixture/loader/DataNodeContainedFixtureRuleBasedTableMetaDataBuilder.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/fixture/loader/DataNodeContainedFixtureRuleBasedTableMetaDataBuilder.java
index 6705cea..e4e67de 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/fixture/loader/DataNodeContainedFixtureRuleBasedTableMetaDataBuilder.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/fixture/loader/DataNodeContainedFixtureRuleBasedTableMetaDataBuilder.java
@@ -31,25 +31,25 @@ import java.util.Map;
import java.util.Optional;
public final class DataNodeContainedFixtureRuleBasedTableMetaDataBuilder implements RuleBasedTableMetaDataBuilder<DataNodeContainedFixtureRule> {
-
+
@Override
public Optional<TableMetaData> load(final String tableName, final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap,
final DataNodes dataNodes, final DataNodeContainedFixtureRule rule, final ConfigurationProperties props) {
- return ("data_node_routed_table_0".equals(tableName) || "data_node_routed_table_1".equals(tableName))
+ return ("data_node_routed_table1".equals(tableName) || "data_node_routed_table2".equals(tableName))
? Optional.of(new TableMetaData(Collections.emptyList(), Collections.emptyList())) : Optional.empty();
}
-
+
@Override
public TableMetaData decorate(final String tableName, final TableMetaData tableMetaData, final DataNodeContainedFixtureRule rule) {
ColumnMetaData columnMetaData = new ColumnMetaData("id", 1, "INT", true, true, false);
return new TableMetaData(Collections.singletonList(columnMetaData), Collections.emptyList());
}
-
+
@Override
public int getOrder() {
return 1;
}
-
+
@Override
public Class<DataNodeContainedFixtureRule> getTypeClass() {
return DataNodeContainedFixtureRule.class;
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/fixture/rule/DataNodeContainedFixtureRule.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/fixture/rule/DataNodeContainedFixtureRule.java
index aca92f5..77ccb32 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/fixture/rule/DataNodeContainedFixtureRule.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/fixture/rule/DataNodeContainedFixtureRule.java
@@ -21,40 +21,50 @@ import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.rule.type.DataNodeContainedRule;
import org.apache.shardingsphere.infra.rule.type.TableContainedRule;
-import java.util.Arrays;
import java.util.Collection;
+import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
+import java.util.stream.Collectors;
public final class DataNodeContainedFixtureRule implements DataNodeContainedRule, TableContainedRule {
-
+
+ private final Map<String, String> actualTableNameMaps = new HashMap<>(4);
+
+ public DataNodeContainedFixtureRule() {
+ actualTableNameMaps.putIfAbsent("data_node_routed_table1_0", "data_node_routed_table1");
+ actualTableNameMaps.putIfAbsent("data_node_routed_table1_1", "data_node_routed_table1");
+ actualTableNameMaps.putIfAbsent("data_node_routed_table2_0", "data_node_routed_table2");
+ actualTableNameMaps.putIfAbsent("data_node_routed_table2_1", "data_node_routed_table2");
+ }
+
@Override
public Map<String, Collection<DataNode>> getAllDataNodes() {
return null;
}
-
+
@Override
public Collection<String> getAllActualTables() {
- return Arrays.asList("data_node_routed_table_0", "data_node_routed_table_2");
+ return actualTableNameMaps.keySet();
}
-
+
@Override
public Optional<String> findFirstActualTable(final String logicTable) {
return Optional.empty();
}
-
+
@Override
public boolean isNeedAccumulate(final Collection<String> tables) {
return false;
}
-
+
@Override
public Optional<String> findLogicTableByActualTable(final String actualTable) {
- return Optional.empty();
+ return Optional.ofNullable(actualTableNameMaps.get(actualTable));
}
-
+
@Override
public Collection<String> getTables() {
- return Arrays.asList("data_node_routed_table_0", "data_node_routed_table_1");
+ return actualTableNameMaps.values().stream().collect(Collectors.toSet());
}
}