提交 ae582641 编写于 作者: H haocao

New integrate sql test framework 25th version.

上级 f17e7b1a
/*
* 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 com.dangdang.ddframe.rdb.integrate.masterslave;
import com.dangdang.ddframe.rdb.integrate.AbstractDBUnitTest;
import com.dangdang.ddframe.rdb.integrate.fixture.SingleKeyModuloDatabaseShardingAlgorithm;
import com.dangdang.ddframe.rdb.integrate.fixture.SingleKeyModuloTableShardingAlgorithm;
import com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.TableRule;
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy;
import com.dangdang.ddframe.rdb.sharding.hint.HintManagerHolder;
import com.dangdang.ddframe.rdb.sharding.jdbc.core.datasource.MasterSlaveDataSource;
import com.dangdang.ddframe.rdb.sharding.jdbc.core.datasource.ShardingDataSource;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import javax.sql.DataSource;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public abstract class AbstractShardingMasterSlaveDBUnitTest extends AbstractDBUnitTest {
private static boolean isShutdown;
private static ShardingDataSource shardingDataSource;
@Before
@After
public void reset() throws NoSuchFieldException, IllegalAccessException {
HintManagerHolder.clear();
MasterSlaveDataSource.resetDMLFlag();
}
@Override
protected List<String> getDataSetFiles() {
return Arrays.asList(
"integrate/dataset/masterslave/init/master_0.xml",
"integrate/dataset/masterslave/init/master_1.xml",
"integrate/dataset/masterslave/init/master_2.xml",
"integrate/dataset/masterslave/init/master_3.xml",
"integrate/dataset/masterslave/init/master_4.xml",
"integrate/dataset/masterslave/init/master_5.xml",
"integrate/dataset/masterslave/init/master_6.xml",
"integrate/dataset/masterslave/init/master_7.xml",
"integrate/dataset/masterslave/init/master_8.xml",
"integrate/dataset/masterslave/init/master_9.xml",
"integrate/dataset/masterslave/init/slave_0.xml",
"integrate/dataset/masterslave/init/slave_1.xml",
"integrate/dataset/masterslave/init/slave_2.xml",
"integrate/dataset/masterslave/init/slave_3.xml",
"integrate/dataset/masterslave/init/slave_4.xml",
"integrate/dataset/masterslave/init/slave_5.xml",
"integrate/dataset/masterslave/init/slave_6.xml",
"integrate/dataset/masterslave/init/slave_7.xml",
"integrate/dataset/masterslave/init/slave_8.xml",
"integrate/dataset/masterslave/init/slave_9.xml");
}
protected final ShardingDataSource getShardingDataSource() {
if (null != shardingDataSource && !isShutdown) {
return shardingDataSource;
}
isShutdown = false;
Map<String, DataSource> masterSlaveDataSourceMap = createDataSourceMap("dataSource_%s");
MasterSlaveDataSource masterSlaveDs0 = new MasterSlaveDataSource("ms_0", masterSlaveDataSourceMap.get("dataSource_master_0"),
Collections.singletonList(masterSlaveDataSourceMap.get("dataSource_slave_0")));
MasterSlaveDataSource masterSlaveDs1 = new MasterSlaveDataSource("ms_1", masterSlaveDataSourceMap.get("dataSource_master_1"),
Collections.singletonList(masterSlaveDataSourceMap.get("dataSource_slave_1")));
MasterSlaveDataSource masterSlaveDs2 = new MasterSlaveDataSource("ms_2", masterSlaveDataSourceMap.get("dataSource_master_2"),
Collections.singletonList(masterSlaveDataSourceMap.get("dataSource_slave_2")));
MasterSlaveDataSource masterSlaveDs3 = new MasterSlaveDataSource("ms_3", masterSlaveDataSourceMap.get("dataSource_master_3"),
Collections.singletonList(masterSlaveDataSourceMap.get("dataSource_slave_3")));
MasterSlaveDataSource masterSlaveDs4 = new MasterSlaveDataSource("ms_4", masterSlaveDataSourceMap.get("dataSource_master_4"),
Collections.singletonList(masterSlaveDataSourceMap.get("dataSource_slave_4")));
MasterSlaveDataSource masterSlaveDs5 = new MasterSlaveDataSource("ms_5", masterSlaveDataSourceMap.get("dataSource_master_5"),
Collections.singletonList(masterSlaveDataSourceMap.get("dataSource_slave_5")));
MasterSlaveDataSource masterSlaveDs6 = new MasterSlaveDataSource("ms_6", masterSlaveDataSourceMap.get("dataSource_master_6"),
Collections.singletonList(masterSlaveDataSourceMap.get("dataSource_slave_6")));
MasterSlaveDataSource masterSlaveDs7 = new MasterSlaveDataSource("ms_7", masterSlaveDataSourceMap.get("dataSource_master_7"),
Collections.singletonList(masterSlaveDataSourceMap.get("dataSource_slave_7")));
MasterSlaveDataSource masterSlaveDs8 = new MasterSlaveDataSource("ms_8", masterSlaveDataSourceMap.get("dataSource_master_8"),
Collections.singletonList(masterSlaveDataSourceMap.get("dataSource_slave_8")));
MasterSlaveDataSource masterSlaveDs9 = new MasterSlaveDataSource("ms_9", masterSlaveDataSourceMap.get("dataSource_master_9"),
Collections.singletonList(masterSlaveDataSourceMap.get("dataSource_slave_9")));
Map<String, DataSource> dataSourceMap = new HashMap<>(10);
dataSourceMap.put("ms_0", masterSlaveDs0);
dataSourceMap.put("ms_1", masterSlaveDs1);
dataSourceMap.put("ms_2", masterSlaveDs2);
dataSourceMap.put("ms_3", masterSlaveDs3);
dataSourceMap.put("ms_4", masterSlaveDs4);
dataSourceMap.put("ms_5", masterSlaveDs5);
dataSourceMap.put("ms_6", masterSlaveDs6);
dataSourceMap.put("ms_7", masterSlaveDs7);
dataSourceMap.put("ms_8", masterSlaveDs8);
dataSourceMap.put("ms_9", masterSlaveDs9);
DataSourceRule dataSourceRule = new DataSourceRule(dataSourceMap);
TableRule orderTableRule = TableRule.builder("t_order").actualTables(Arrays.asList(
"t_order_0",
"t_order_1",
"t_order_2",
"t_order_3",
"t_order_4",
"t_order_5",
"t_order_6",
"t_order_7",
"t_order_8",
"t_order_9")).dataSourceRule(dataSourceRule).build();
TableRule orderItemTableRule = TableRule.builder("t_order_item").actualTables(Arrays.asList(
"t_order_item_0",
"t_order_item_1",
"t_order_item_2",
"t_order_item_3",
"t_order_item_4",
"t_order_item_5",
"t_order_item_6",
"t_order_item_7",
"t_order_item_8",
"t_order_item_9")).dataSourceRule(dataSourceRule).build();
TableRule configRule = TableRule.builder("t_config").dataSourceRule(dataSourceRule).build();
ShardingRule shardingRule = ShardingRule.builder().dataSourceRule(dataSourceRule).tableRules(Arrays.asList(orderTableRule, orderItemTableRule, configRule))
.bindingTableRules(Collections.singletonList(new BindingTableRule(Arrays.asList(orderTableRule, orderItemTableRule))))
.databaseShardingStrategy(new DatabaseShardingStrategy("user_id", new SingleKeyModuloDatabaseShardingAlgorithm()))
.tableShardingStrategy(new TableShardingStrategy("order_id", new SingleKeyModuloTableShardingAlgorithm())).build();
shardingDataSource = new ShardingDataSource(shardingRule);
return shardingDataSource;
}
@AfterClass
public static void clear() {
isShutdown = true;
shardingDataSource.close();
}
}
/*
* 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 com.dangdang.ddframe.rdb.integrate.masterslave;
import com.dangdang.ddframe.rdb.integrate.masterslave.pstatement.ShardingMasterSlaveForPStatementWithDMLTest;
import com.dangdang.ddframe.rdb.integrate.masterslave.pstatement.ShardingMasterSlaveForPStatementWithSelectTest;
import com.dangdang.ddframe.rdb.integrate.masterslave.statement.ShardingMasterSlaveForStatementWithDMLTest;
import com.dangdang.ddframe.rdb.integrate.masterslave.statement.ShardingMasterSlaveForStatementWithSelectTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
ShardingMasterSlaveForPStatementWithDMLTest.class,
ShardingMasterSlaveForPStatementWithSelectTest.class,
ShardingMasterSlaveForStatementWithDMLTest.class,
ShardingMasterSlaveForStatementWithSelectTest.class
})
public class AllShardingMasterAndSlaveTests {
}
/*
* 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 com.dangdang.ddframe.rdb.integrate.masterslave.pstatement;
import com.dangdang.ddframe.rdb.integrate.masterslave.AbstractShardingMasterSlaveDBUnitTest;
import com.dangdang.ddframe.rdb.sharding.api.HintManager;
import com.dangdang.ddframe.rdb.sharding.jdbc.core.datasource.ShardingDataSource;
import com.dangdang.ddframe.rdb.sharding.constant.SQLType;
import org.dbunit.DatabaseUnitException;
import org.junit.Before;
import org.junit.Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import static com.dangdang.ddframe.rdb.integrate.util.SqlPlaceholderUtil.replacePreparedStatement;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public final class ShardingMasterSlaveForPStatementWithDMLTest extends AbstractShardingMasterSlaveDBUnitTest {
private ShardingDataSource shardingDataSource;
@Before
public void init() throws SQLException {
shardingDataSource = getShardingDataSource();
}
@Test
public void assertInsertWithAllPlaceholders() throws SQLException, DatabaseUnitException {
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
try (Connection connection = shardingDataSource.getConnection()) {
PreparedStatement preparedStatement = connection.prepareStatement(getDatabaseTestSQL().getInsertWithAllPlaceholdersSql());
preparedStatement.setInt(1, i);
preparedStatement.setInt(2, j);
preparedStatement.setString(3, "insert");
preparedStatement.executeUpdate();
}
}
}
assertDataSet("insert", "insert");
}
@Test
public void assertInsertWithHint() throws SQLException, DatabaseUnitException {
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
try (
Connection connection = shardingDataSource.getConnection();
HintManager hintManager = HintManager.getInstance()
) {
hintManager.addDatabaseShardingValue("t_order", "user_id", j);
hintManager.addTableShardingValue("t_order", "order_id", i);
PreparedStatement preparedStatement = connection.prepareStatement(getDatabaseTestSQL().getInsertWithAllPlaceholdersSql());
preparedStatement.setInt(1, i);
preparedStatement.setInt(2, j);
preparedStatement.setString(3, "insert");
preparedStatement.executeUpdate();
}
}
}
try (HintManager hintManager = HintManager.getInstance()) {
hintManager.setMasterRouteOnly();
assertDataSet("insert", "insert");
}
}
@Test
public void assertInsertWithoutPlaceholder() throws SQLException, DatabaseUnitException {
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
try (Connection connection = shardingDataSource.getConnection()) {
PreparedStatement preparedStatement = connection.prepareStatement(String.format(getDatabaseTestSQL().getInsertWithoutPlaceholderSql(), i, j));
preparedStatement.executeUpdate();
}
}
}
assertDataSet("insert", "insert");
}
@Test
public void assertInsertWithPlaceholdersForShardingKeys() throws SQLException, DatabaseUnitException {
String sql = "INSERT INTO `t_order` (`order_id`, `user_id`, `status`) VALUES (%s, %s, ?)";
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
try (Connection connection = shardingDataSource.getConnection()) {
PreparedStatement preparedStatement = connection.prepareStatement(String.format(getDatabaseTestSQL().getInsertWithPartialPlaceholdersSql(), i, j));
preparedStatement.setString(1, "insert");
preparedStatement.executeUpdate();
}
}
}
assertDataSet("insert", "insert");
}
@Test
public void assertUpdateWithoutAlias() throws SQLException, DatabaseUnitException {
for (int i = 10; i < 20; i++) {
for (int j = 0; j < 10; j++) {
try (Connection connection = shardingDataSource.getConnection()) {
PreparedStatement preparedStatement = connection.prepareStatement(replacePreparedStatement(getDatabaseTestSQL().getUpdateWithoutAliasSql()));
preparedStatement.setString(1, "updated");
preparedStatement.setInt(2, i * 100 + j);
preparedStatement.setInt(3, i);
assertThat(preparedStatement.executeUpdate(), is(1));
}
}
}
assertDataSet("update", "updated");
}
@Test
public void assertUpdateWithAlias() throws SQLException, DatabaseUnitException {
if (isAliasSupport()) {
for (int i = 10; i < 20; i++) {
for (int j = 0; j < 10; j++) {
try (Connection connection = shardingDataSource.getConnection()) {
PreparedStatement preparedStatement = connection.prepareStatement(getDatabaseTestSQL().getUpdateWithAliasSql());
preparedStatement.setString(1, "updated");
preparedStatement.setInt(2, i * 100 + j);
preparedStatement.setInt(3, i);
assertThat(preparedStatement.executeUpdate(), is(1));
}
}
}
assertDataSet("update", "updated");
}
}
@Test
public void assertDeleteWithoutAlias() throws SQLException, DatabaseUnitException {
for (int i = 10; i < 20; i++) {
for (int j = 0; j < 10; j++) {
try (Connection connection = shardingDataSource.getConnection()) {
PreparedStatement preparedStatement = connection.prepareStatement(replacePreparedStatement(getDatabaseTestSQL().getDeleteWithoutAliasSql()));
preparedStatement.setInt(1, i * 100 + j);
preparedStatement.setInt(2, i);
preparedStatement.setString(3, "init_master");
assertThat(preparedStatement.executeUpdate(), is(1));
}
}
}
assertDataSet("delete", "init");
}
protected void assertDataSet(final String expectedDataSetPattern, final String status) throws SQLException, DatabaseUnitException {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
assertDataSet(String.format("integrate/dataset/masterslave/expect/%s/master_%s.xml", expectedDataSetPattern, i),
shardingDataSource.getConnection().getConnection(String.format("ms_%s", i), SQLType.INSERT),
String.format("t_order_%s", j), String.format(getDatabaseTestSQL().getAssertSelectShardingTablesWithStatusSql(), j), status);
}
}
}
}
/*
* 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 com.dangdang.ddframe.rdb.integrate.masterslave.pstatement;
import com.dangdang.ddframe.rdb.integrate.masterslave.AbstractShardingMasterSlaveDBUnitTest;
import com.dangdang.ddframe.rdb.sharding.api.HintManager;
import com.dangdang.ddframe.rdb.sharding.jdbc.core.datasource.ShardingDataSource;
import org.dbunit.DatabaseUnitException;
import org.junit.Before;
import org.junit.Test;
import java.sql.SQLException;
import static com.dangdang.ddframe.rdb.integrate.util.SqlPlaceholderUtil.replacePreparedStatement;
public class ShardingMasterSlaveForPStatementWithSelectTest extends AbstractShardingMasterSlaveDBUnitTest {
private ShardingDataSource shardingDataSource;
@Before
public void init() throws SQLException, NoSuchFieldException, IllegalAccessException {
shardingDataSource = getShardingDataSource();
reset();
}
@Test
public void assertSelectForFullTableNameWithSingleTable() throws SQLException, DatabaseUnitException {
String sql = replacePreparedStatement(getDatabaseTestSQL().getSelectForFullTableNameWithSingleTableSql());
assertDataSet("integrate/dataset/masterslave/expect/select/SelectEqualsWithSingleTable_0.xml", shardingDataSource.getConnection(), "t_order", sql, 10, 1000);
}
@Test
public void assertSelectEqualsWithSingleTable() throws SQLException, DatabaseUnitException {
String sql = replacePreparedStatement(getDatabaseTestSQL().getSelectEqualsWithSingleTableSql());
assertDataSet("integrate/dataset/masterslave/expect/select/SelectEqualsWithSingleTable_0.xml", shardingDataSource.getConnection(), "t_order", sql, 10, 1000);
assertDataSet("integrate/dataset/masterslave/expect/select/SelectEqualsWithSingleTable_1.xml", shardingDataSource.getConnection(), "t_order", sql, 12, 1201);
assertDataSet("integrate/dataset/Empty.xml", shardingDataSource.getConnection(), "t_order", sql, 12, 1000);
}
@Test
public void assertSelectBetweenWithSingleTable() throws SQLException, DatabaseUnitException {
String sql = replacePreparedStatement(getDatabaseTestSQL().getSelectBetweenWithSingleTableSql());
assertDataSet("integrate/dataset/masterslave/expect/select/SelectBetweenWithSingleTable.xml", shardingDataSource.getConnection(), "t_order", sql, 10, 12, 1009, 1108);
assertDataSet("integrate/dataset/Empty.xml", shardingDataSource.getConnection(), "t_order", sql, 10, 12, 1309, 1408);
}
@Test
public void assertSelectInWithSingleTable() throws SQLException, DatabaseUnitException {
String sql = replacePreparedStatement(getDatabaseTestSQL().getSelectInWithSingleTableSql());
assertDataSet("integrate/dataset/masterslave/expect/select/SelectInWithSingleTable_0.xml", shardingDataSource.getConnection(), "t_order", sql, 10, 12, 15, 1009, 1208);
assertDataSet("integrate/dataset/masterslave/expect/select/SelectInWithSingleTable_1.xml", shardingDataSource.getConnection(), "t_order", sql, 10, 12, 15, 1009, 1108);
assertDataSet("integrate/dataset/Empty.xml", shardingDataSource.getConnection(), "t_order", sql, 10, 12, 15, 1309, 1408);
}
@Test
public void assertSelectLimitWithBindingTable() throws SQLException, DatabaseUnitException {
String sql = replacePreparedStatement(getDatabaseTestSQL().getSelectPagingWithOffsetAndRowCountSql());
assertDataSet("integrate/dataset/masterslave/expect/select/SelectLimitWithBindingTable.xml", getShardingDataSource().getConnection(), "t_order_item", sql, 10, 19, 1000, 1909, 2, 2);
assertDataSet("integrate/dataset/Empty.xml", getShardingDataSource().getConnection(), "t_order_item", sql, 10, 19, 1000, 1909, 10000, 2);
}
@Test
public void assertSelectLimitWithBindingTableWithoutOffset() throws SQLException, DatabaseUnitException {
String sql = replacePreparedStatement(getDatabaseTestSQL().getSelectPagingWithRowCountSql());
assertDataSet("integrate/dataset/masterslave/expect/select/SelectLimitWithBindingTableWithoutOffset.xml", getShardingDataSource().getConnection(), "t_order_item", sql, 10, 19, 1000, 1909, 2);
assertDataSet("integrate/dataset/Empty.xml", getShardingDataSource().getConnection(), "t_order_item", sql, 10, 19, 1000, 1909, 0);
}
@Test
public void assertSelectGroupByWithBindingTable() throws SQLException, DatabaseUnitException {
String sql = replacePreparedStatement(getDatabaseTestSQL().getSelectGroupWithBindingTableSql());
assertDataSet("integrate/dataset/masterslave/expect/select/SelectGroupByWithBindingTable.xml", getShardingDataSource().getConnection(), "t_order_item", sql, 10, 19, 1000, 1909);
assertDataSet("integrate/dataset/Empty.xml", getShardingDataSource().getConnection(), "t_order_item", sql, 1, 9, 1000, 1909);
}
@Test
public void assertSelectGroupByWithoutGroupedColumn() throws SQLException, DatabaseUnitException {
String sql = replacePreparedStatement(getDatabaseTestSQL().getSelectGroupWithoutGroupedColumnSql());
assertDataSet("integrate/dataset/masterslave/expect/select/SelectGroupByWithoutGroupedColumn.xml", getShardingDataSource().getConnection(), "t_order_item", sql, 10, 19, 1000, 1909);
assertDataSet("integrate/dataset/Empty.xml", getShardingDataSource().getConnection(), "t_order_item", sql, 1, 9, 1000, 1909);
}
@Test
public void assertSelectForHint() throws SQLException, DatabaseUnitException {
HintManager hintManager = HintManager.getInstance();
hintManager.addDatabaseShardingValue("t_order", "user_id", 10);
hintManager.addTableShardingValue("t_order", "order_id", 1000);
String sql = getDatabaseTestSQL().getSelectForFullTableNameWithSingleTableSql();
assertDataSet("integrate/dataset/masterslave/expect/select/SelectEqualsWithSingleTable_0.xml", shardingDataSource.getConnection(), "t_order", sql, 10, 1000);
}
@Test
public void assertSelectForHintAndForceMaster() throws SQLException, DatabaseUnitException {
HintManager hintManager = HintManager.getInstance();
hintManager.setMasterRouteOnly();
hintManager.addDatabaseShardingValue("t_order", "user_id", 10);
hintManager.addTableShardingValue("t_order", "order_id", 1000);
String sql = getDatabaseTestSQL().getSelectForFullTableNameWithSingleTableSql();
assertDataSet("integrate/dataset/masterslave/expect/select/SelectEqualsWithSingleMasterTable_0.xml", shardingDataSource.getConnection(), "t_order", sql, 10, 1000);
}
}
/*
* 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 com.dangdang.ddframe.rdb.integrate.masterslave.statement;
import com.dangdang.ddframe.rdb.integrate.masterslave.AbstractShardingMasterSlaveDBUnitTest;
import com.dangdang.ddframe.rdb.sharding.constant.SQLType;
import com.dangdang.ddframe.rdb.sharding.jdbc.core.datasource.ShardingDataSource;
import org.dbunit.DatabaseUnitException;
import org.junit.Before;
import org.junit.Test;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
public final class ShardingMasterSlaveForStatementWithDMLTest extends AbstractShardingMasterSlaveDBUnitTest {
private ShardingDataSource shardingDataSource;
@Before
public void init() throws SQLException {
shardingDataSource = getShardingDataSource();
}
@Test
public void assertUpdateWithoutShardingValue() throws SQLException, DatabaseUnitException {
assertSelectUpdate(false);
try (Connection connection = getShardingDataSource().getConnection()) {
Statement stmt = connection.createStatement();
assertThat(stmt.executeUpdate(String.format(getDatabaseTestSQL().getUpdateWithoutShardingValueSql(), "'updated'", "'init_master'")), is(100));
}
assertDataSet("update", "updated");
assertSelectUpdate(true);
}
private void assertSelectUpdate(final boolean updated) throws SQLException {
try (Connection connection = getShardingDataSource().getConnection()) {
Statement stmt = connection.createStatement();
if (updated) {
assertTrue(stmt.executeQuery(String.format(getDatabaseTestSQL().getAssertSelectWithStatusSql(), "'updated'")).next());
} else {
assertFalse(stmt.executeQuery(String.format(getDatabaseTestSQL().getAssertSelectWithStatusSql(), "'updated'")).next());
}
}
}
@Test
public void assertDeleteWithoutShardingValue() throws SQLException, DatabaseUnitException {
try (Connection connection = getShardingDataSource().getConnection()) {
Statement stmt = connection.createStatement();
assertThat(stmt.executeUpdate(String.format(getDatabaseTestSQL().getDeleteWithoutShardingValueSql(), "'init_master'")), is(100));
}
assertDataSet("delete", "init");
}
protected void assertDataSet(final String expectedDataSetPattern, final String status) throws SQLException, DatabaseUnitException {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
assertDataSet(String.format("integrate/dataset/masterslave/expect/%s/master_%s.xml", expectedDataSetPattern, i),
shardingDataSource.getConnection().getConnection(String.format("ms_%s", i), SQLType.INSERT),
String.format("t_order_%s", j), String.format(getDatabaseTestSQL().getAssertSelectShardingTablesWithStatusSql(), j), status);
}
}
}
}
/*
* 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 com.dangdang.ddframe.rdb.integrate.masterslave.statement;
import com.dangdang.ddframe.rdb.integrate.masterslave.AbstractShardingMasterSlaveDBUnitTest;
import org.dbunit.DatabaseUnitException;
import org.junit.Test;
import java.sql.SQLException;
import static com.dangdang.ddframe.rdb.integrate.util.SqlPlaceholderUtil.replacePreparedStatement;
import static com.dangdang.ddframe.rdb.sharding.constant.DatabaseType.Oracle;
import static com.dangdang.ddframe.rdb.sharding.constant.DatabaseType.SQLServer;
public final class ShardingMasterSlaveForStatementWithSelectTest extends AbstractShardingMasterSlaveDBUnitTest {
@Test
public void assertSelectPagingWithOffsetAndRowCountSql() throws SQLException, DatabaseUnitException {
if (currentDbType() == SQLServer) {
assertSelectPaging("SelectPagingWithOffsetAndRowCountSql.xml", getDatabaseTestSQL().getSelectPagingWithOffsetAndRowCountSql(), 2, 10, 19, 1000, 1909, 1);
} else if (currentDbType() == Oracle) {
assertSelectPaging("SelectPagingWithOffsetAndRowCountSql.xml", getDatabaseTestSQL().getSelectPagingWithOffsetAndRowCountSql(), 10, 19, 1000, 1909, 4, 2);
} else {
assertSelectPaging("SelectPagingWithOffsetAndRowCountSql.xml", getDatabaseTestSQL().getSelectPagingWithOffsetAndRowCountSql(), 10, 19, 1000, 1909, 2, 2);
}
}
@Test
public void assertSelectPagingWithRowCountSql() throws SQLException, DatabaseUnitException {
if (currentDbType() == SQLServer) {
assertSelectPaging("SelectPagingWithRowCountSql.xml", getDatabaseTestSQL().getSelectPagingWithRowCountSql(), 2, 10, 19, 1000, 1909);
} else {
assertSelectPaging("SelectPagingWithRowCountSql.xml", getDatabaseTestSQL().getSelectPagingWithRowCountSql(), 10, 19, 1000, 1909, 2);
}
}
private void assertSelectPaging(final String expectedDataSetFileName, final String sql, final Object... params) throws SQLException, DatabaseUnitException {
assertDataSet("integrate/dataset/masterslave/expect/select/" + currentDbType().name().toLowerCase() + "/" + expectedDataSetFileName,
getShardingDataSource().getConnection(), "t_order_item", replacePreparedStatement(sql), params);
}
@Test
public void assertSelectGroupByWithBindingTable() throws SQLException, DatabaseUnitException {
String sql = getDatabaseTestSQL().getSelectGroupWithBindingTableSql();
assertDataSet("integrate/dataset/masterslave/expect/select/SelectGroupByWithBindingTable.xml", getShardingDataSource().getConnection(), "t_order_item", String.format(sql, 10, 19, 1000, 1909));
assertDataSet("integrate/dataset/Empty.xml", getShardingDataSource().getConnection(), "t_order_item", String.format(sql, 1, 9, 1000, 1909));
}
@Test
public void assertSelectGroupByWithoutGroupedColumn() throws SQLException, DatabaseUnitException {
String sql = getDatabaseTestSQL().getSelectGroupWithoutGroupedColumnSql();
assertDataSet("integrate/dataset/masterslave/expect/select/SelectGroupByWithoutGroupedColumn.xml", getShardingDataSource().getConnection(),
"t_order_item", String.format(sql, 10, 19, 1000, 1909));
assertDataSet("integrate/dataset/Empty.xml", getShardingDataSource().getConnection(), "t_order_item", String.format(sql, 1, 9, 1000, 1909));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册