diff --git a/sharding-core/src/test/java/io/shardingjdbc/core/merger/AllMergerTests.java b/sharding-core/src/test/java/io/shardingjdbc/core/merger/AllMergerTests.java index 3130658ae2b920dd6044c8bce0f6a94c4c7f485d..ef1693c804a9971e4d46a2fe3d09bf6cccb781bf 100644 --- a/sharding-core/src/test/java/io/shardingjdbc/core/merger/AllMergerTests.java +++ b/sharding-core/src/test/java/io/shardingjdbc/core/merger/AllMergerTests.java @@ -17,6 +17,11 @@ package io.shardingjdbc.core.merger; +import io.shardingjdbc.core.merger.dal.DALMergeEngineTest; +import io.shardingjdbc.core.merger.dal.show.ShowCreateTableMergedResultTest; +import io.shardingjdbc.core.merger.dal.show.ShowDatabasesMergedResultTest; +import io.shardingjdbc.core.merger.dal.show.ShowOtherMergedResultTest; +import io.shardingjdbc.core.merger.dal.show.ShowTablesMergedResultTest; import io.shardingjdbc.core.merger.dql.DQLMergeEngineTest; import io.shardingjdbc.core.merger.dql.common.DecoratorMergedResultTest; import io.shardingjdbc.core.merger.dql.common.MemoryMergedResultTest; @@ -55,7 +60,13 @@ import org.junit.runners.Suite; AllAggregationTests.class, LimitDecoratorMergedResultTest.class, RowNumberDecoratorMergedResultTest.class, - TopAndRowNumberDecoratorMergedResultTest.class + TopAndRowNumberDecoratorMergedResultTest.class, + DALMergeEngineTest.class, + ShowCreateTableMergedResultTest.class, + ShowDatabasesMergedResultTest.class, + ShowOtherMergedResultTest.class, + ShowTablesMergedResultTest.class, + MergeEngineFactoryTest.class }) public class AllMergerTests { } diff --git a/sharding-core/src/test/java/io/shardingjdbc/core/merger/MergeEngineFactoryTest.java b/sharding-core/src/test/java/io/shardingjdbc/core/merger/MergeEngineFactoryTest.java new file mode 100644 index 0000000000000000000000000000000000000000..1496717a197e7099db5c8d4b55abd395615dea32 --- /dev/null +++ b/sharding-core/src/test/java/io/shardingjdbc/core/merger/MergeEngineFactoryTest.java @@ -0,0 +1,75 @@ +/* + * Copyright 1999-2015 dangdang.com. + *

+ * 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. + *

+ */ + +package io.shardingjdbc.core.merger; + +import com.google.common.collect.Lists; +import io.shardingjdbc.core.merger.dal.DALMergeEngine; +import io.shardingjdbc.core.merger.dql.DQLMergeEngine; +import io.shardingjdbc.core.merger.fixture.TestQueryResult; +import io.shardingjdbc.core.parsing.parser.sql.SQLStatement; +import io.shardingjdbc.core.parsing.parser.sql.dal.DALStatement; +import io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement; +import io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement; +import org.junit.Before; +import org.junit.Test; + +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public final class MergeEngineFactoryTest { + + private List queryResults; + + @Before + public void setUp() throws SQLException { + ResultSet resultSet = mock(ResultSet.class); + ResultSetMetaData resultSetMetaData = mock(ResultSetMetaData.class); + when(resultSet.getMetaData()).thenReturn(resultSetMetaData); + when(resultSetMetaData.getColumnCount()).thenReturn(1); + when(resultSetMetaData.getColumnLabel(1)).thenReturn("label"); + List resultSets = Lists.newArrayList(resultSet); + queryResults = new ArrayList<>(resultSets.size()); + queryResults.add(new TestQueryResult(resultSets.get(0))); + } + + @Test + public void assertNewInstanceWithSelectStatement() throws SQLException { + SQLStatement selectStatement = new SelectStatement(); + assertThat(MergeEngineFactory.newInstance(null, queryResults, selectStatement), instanceOf(DQLMergeEngine.class)); + } + + @Test + public void assertNewInstanceWithDALStatement() throws SQLException { + SQLStatement dalStatement = new DALStatement(); + assertThat(MergeEngineFactory.newInstance(null, queryResults, dalStatement), instanceOf(DALMergeEngine.class)); + } + + @Test(expected = UnsupportedOperationException.class) + public void assertNewInstanceWithOtherStatement() throws SQLException { + SQLStatement insertStatement = new InsertStatement(); + MergeEngineFactory.newInstance(null, queryResults, insertStatement); + } +} diff --git a/sharding-core/src/test/java/io/shardingjdbc/core/merger/dal/show/ShowCreateTableMergedResultTest.java b/sharding-core/src/test/java/io/shardingjdbc/core/merger/dal/show/ShowCreateTableMergedResultTest.java new file mode 100644 index 0000000000000000000000000000000000000000..61ba227b7409904bf1cbbe14ea8a3000159c3822 --- /dev/null +++ b/sharding-core/src/test/java/io/shardingjdbc/core/merger/dal/show/ShowCreateTableMergedResultTest.java @@ -0,0 +1,116 @@ +/* + * Copyright 1999-2015 dangdang.com. + *

+ * 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. + *

+ */ + +package io.shardingjdbc.core.merger.dal.show; + +import com.google.common.collect.Lists; +import io.shardingjdbc.core.api.algorithm.fixture.TestComplexKeysShardingAlgorithm; +import io.shardingjdbc.core.api.config.ShardingRuleConfiguration; +import io.shardingjdbc.core.api.config.TableRuleConfiguration; +import io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration; +import io.shardingjdbc.core.merger.QueryResult; +import io.shardingjdbc.core.merger.fixture.TestQueryResult; +import io.shardingjdbc.core.rule.ShardingRule; +import org.junit.Before; +import org.junit.Test; + +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public final class ShowCreateTableMergedResultTest { + + private ShardingRule shardingRule; + + private List queryResults; + + private ResultSet resultSet; + + @Before + public void setUp() throws SQLException { + TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration(); + tableRuleConfig.setLogicTable("table"); + tableRuleConfig.setActualDataNodes("ds.table_${0..2}"); + tableRuleConfig.setTableShardingStrategyConfig(new ComplexShardingStrategyConfiguration("field1, field2, field3", new TestComplexKeysShardingAlgorithm())); + ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration(); + shardingRuleConfig.getTableRuleConfigs().add(tableRuleConfig); + shardingRule = new ShardingRule(shardingRuleConfig, Lists.newArrayList("ds")); + + resultSet = mock(ResultSet.class); + ResultSetMetaData resultSetMetaData = mock(ResultSetMetaData.class); + when(resultSet.getMetaData()).thenReturn(resultSetMetaData); + when(resultSetMetaData.getColumnCount()).thenReturn(2); + List resultSets = Lists.newArrayList(resultSet); + for (ResultSet each : resultSets) { + when(each.next()).thenReturn(true, false); + } + queryResults = new ArrayList<>(resultSets.size()); + for (ResultSet each : resultSets) { + queryResults.add(new TestQueryResult(each)); + } + } + + @Test + public void assertNextForEmptyQueryResult() throws SQLException { + ShowCreateTableMergedResult showCreateTableMergedResult = new ShowCreateTableMergedResult(shardingRule, new ArrayList()); + assertFalse(showCreateTableMergedResult.next()); + } + + @Test + public void assertNextForTableRuleIsPresentForBackQuotes() throws SQLException { + when(resultSet.getObject(1)).thenReturn("table_0"); + when(resultSet.getObject(2)).thenReturn("CREATE TABLE `t_order` (\n" + + " `id` int(11) NOT NULL AUTO_INCREMENT,\n" + + " `order_id` int(11) NOT NULL COMMENT,\n" + + " `user_id` int(11) NOT NULL COMMENT,\n" + + " `status` tinyint(4) NOT NULL DEFAULT '1',\n" + + " `created_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n" + + " PRIMARY KEY (`id`)\n" + + ") ENGINE=InnoDB AUTO_INCREMENT=121 DEFAULT CHARSET=utf8 COLLATE=utf8_bin"); + ShowCreateTableMergedResult showCreateTableMergedResult = new ShowCreateTableMergedResult(shardingRule, queryResults); + assertTrue(showCreateTableMergedResult.next()); + } + + @Test + public void assertNextForTableRuleIsPresentForNoBackQuotes() throws SQLException { + when(resultSet.getObject(1)).thenReturn("table_0"); + when(resultSet.getObject(2)).thenReturn("CREATE TABLE t_order (\n" + + " `id` int(11) NOT NULL AUTO_INCREMENT,\n" + + " `order_id` int(11) NOT NULL COMMENT,\n" + + " `user_id` int(11) NOT NULL COMMENT,\n" + + " `status` tinyint(4) NOT NULL DEFAULT '1',\n" + + " `created_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n" + + " PRIMARY KEY (`id`)\n" + + ") ENGINE=InnoDB AUTO_INCREMENT=121 DEFAULT CHARSET=utf8 COLLATE=utf8_bin"); + ShowCreateTableMergedResult showCreateTableMergedResult = new ShowCreateTableMergedResult(shardingRule, queryResults); + assertTrue(showCreateTableMergedResult.next()); + } + + @Test + public void assertNextForTableRuleIsNotPresent() throws SQLException { + when(resultSet.getObject(1)).thenReturn("table_3"); + ShowCreateTableMergedResult showCreateTableMergedResult = new ShowCreateTableMergedResult(shardingRule, queryResults); + assertFalse(showCreateTableMergedResult.next()); + } +} diff --git a/sharding-core/src/test/java/io/shardingjdbc/core/merger/dal/show/ShowTablesMergedResultTest.java b/sharding-core/src/test/java/io/shardingjdbc/core/merger/dal/show/ShowTablesMergedResultTest.java index 0e1f74d10cbffb97518a2e89fa30e5dc0a5a32c9..797da83545dde6cc93db0e5f02b44930ce5aa7be 100644 --- a/sharding-core/src/test/java/io/shardingjdbc/core/merger/dal/show/ShowTablesMergedResultTest.java +++ b/sharding-core/src/test/java/io/shardingjdbc/core/merger/dal/show/ShowTablesMergedResultTest.java @@ -62,7 +62,6 @@ public final class ShowTablesMergedResultTest { when(resultSet.getMetaData()).thenReturn(resultSetMetaData); when(resultSetMetaData.getColumnCount()).thenReturn(1); List resultSets = Lists.newArrayList(resultSet); - when(resultSets.get(0).next()).thenReturn(true); for (ResultSet each : resultSets) { when(each.next()).thenReturn(true, false); }