提交 19d0002d 编写于 作者: T tuohai666

add test cases for ShowCreateTableMergedResult and MergeEngineFactory, 99% lines covered in merge

上级 5adf3c67
......@@ -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 {
}
/*
* 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 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<QueryResult> 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<ResultSet> 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);
}
}
/*
* 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 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<QueryResult> 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<ResultSet> 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<QueryResult>());
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());
}
}
......@@ -62,7 +62,6 @@ public final class ShowTablesMergedResultTest {
when(resultSet.getMetaData()).thenReturn(resultSetMetaData);
when(resultSetMetaData.getColumnCount()).thenReturn(1);
List<ResultSet> resultSets = Lists.newArrayList(resultSet);
when(resultSets.get(0).next()).thenReturn(true);
for (ResultSet each : resultSets) {
when(each.next()).thenReturn(true, false);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册