Skip to content

Commit

Permalink
Refactor master slave integrate test cases 12th.
Browse files Browse the repository at this point in the history
  • Loading branch information
haocao committed Sep 7, 2017
1 parent be9ab09 commit 44b59d8
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@
import com.google.common.collect.Lists;
import lombok.Getter;
import org.dbunit.DatabaseUnitException;
import org.dbunit.database.CachedResultSetTable;
import org.dbunit.database.ForwardOnlyResultSetTable;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.database.ResultSetTableMetaData;
import org.dbunit.dataset.DataSetException;
import org.dbunit.dataset.ITable;
import org.dbunit.dataset.ITableIterator;
import org.dbunit.dataset.ITableMetaData;
import org.dbunit.dataset.ReplacementDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.junit.Test;
Expand All @@ -45,15 +51,16 @@
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static com.dangdang.ddframe.rdb.common.util.SqlPlaceholderUtil.replacePreparedStatement;
import static com.dangdang.ddframe.rdb.common.util.SqlPlaceholderUtil.replaceStatement;
import static com.dangdang.ddframe.rdb.common.util.SQLPlaceholderUtil.replacePreparedStatement;
import static com.dangdang.ddframe.rdb.common.util.SQLPlaceholderUtil.replaceStatement;
import static org.dbunit.Assertion.assertEquals;

@RunWith(Parameterized.class)
Expand Down Expand Up @@ -230,13 +237,22 @@ private void executeQueryWithPreparedStatement(final AbstractDataSourceAdapter a
expectedDataSet.addReplacementObject("[null]", null);
for (ITable each : expectedDataSet.getTables()) {
String tableName = each.getTableMetaData().getTableName();
ITable actualTable = DBUnitUtil.getConnection(new DatabaseEnvironment(DatabaseType.valueFrom(conn.getMetaData().getDatabaseProductName())), conn)
.createTable(tableName, preparedStatement);
IDatabaseConnection connection = DBUnitUtil.getConnection(new DatabaseEnvironment(DatabaseType.valueFrom(conn.getMetaData().getDatabaseProductName())), conn);
// ITable actualTable = connection.createTable(tableName, preparedStatement);
ITable actualTable = createTable(tableName, preparedStatement, connection);
assertEquals(expectedDataSet.getTable(tableName), actualTable);
}
}
}

private CachedResultSetTable createTable(final String tableName, final PreparedStatement preparedStatement, final IDatabaseConnection connection) throws SQLException, DataSetException {
preparedStatement.execute();
ResultSet rs = preparedStatement.getResultSet();
ITableMetaData metaData = new ResultSetTableMetaData(tableName, rs, connection, false);
ForwardOnlyResultSetTable table = new ForwardOnlyResultSetTable(metaData, rs);
return new CachedResultSetTable(table);
}

private void setParameters(final PreparedStatement preparedStatement, final List<String> parameters) throws SQLException {
int index = 1;
for (String each : parameters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class SqlPlaceholderUtil {
public final class SQLPlaceholderUtil {

public static String replaceStatement(final String str, final Object[] args) {
if (args.length == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import java.sql.SQLException;
import java.util.Map;

import static com.dangdang.ddframe.rdb.common.util.SqlPlaceholderUtil.replacePreparedStatement;
import static com.dangdang.ddframe.rdb.common.util.SQLPlaceholderUtil.replacePreparedStatement;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import java.sql.SQLException;
import java.util.Map;

import static com.dangdang.ddframe.rdb.common.util.SqlPlaceholderUtil.replacePreparedStatement;
import static com.dangdang.ddframe.rdb.common.util.SQLPlaceholderUtil.replacePreparedStatement;

public class RoutingDatabaseOnlyWithHintForSelectTest extends AbstractRoutingDatabaseOnlyWithHintTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import java.sql.SQLException;
import java.util.Map;

import static com.dangdang.ddframe.rdb.common.util.SqlPlaceholderUtil.replacePreparedStatement;
import static com.dangdang.ddframe.rdb.common.util.SQLPlaceholderUtil.replacePreparedStatement;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.sql.SQLException;
import java.util.Map;

import static com.dangdang.ddframe.rdb.common.util.SqlPlaceholderUtil.replacePreparedStatement;
import static com.dangdang.ddframe.rdb.common.util.SQLPlaceholderUtil.replacePreparedStatement;

public final class ShardingDatabaseOnlyWithHintForSelectTest extends AbstractShardingDatabaseOnlyWithHintTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import java.util.ArrayList;
import java.util.List;

import static com.dangdang.ddframe.rdb.common.util.SqlPlaceholderUtil.replacePreparedStatement;
import static com.dangdang.ddframe.rdb.common.util.SQLPlaceholderUtil.replacePreparedStatement;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package com.dangdang.ddframe.rdb.sharding.parsing;

import com.dangdang.ddframe.rdb.common.jaxb.helper.SQLStatementHelper;
import com.dangdang.ddframe.rdb.common.util.SqlPlaceholderUtil;
import com.dangdang.ddframe.rdb.common.util.SQLPlaceholderUtil;
import com.dangdang.ddframe.rdb.sharding.api.fixture.ShardingRuleMockBuilder;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.TableRule;
Expand Down Expand Up @@ -54,13 +54,13 @@ public static Collection<Object[]> dataParameters() {

@Test
public void assertStatement() {
assertStatement(new SQLParsingEngine(getDatabaseType(), SqlPlaceholderUtil.replaceStatement(SQLStatementHelper.getSql(getTestCaseName()), parameters), buildShardingRule()).parse());
assertStatement(new SQLParsingEngine(getDatabaseType(), SQLPlaceholderUtil.replaceStatement(SQLStatementHelper.getSql(getTestCaseName()), parameters), buildShardingRule()).parse());
}

@Test
public void assertPreparedStatement() {
for (DatabaseType each : SQLStatementHelper.getTypes(getTestCaseName())) {
assertPreparedStatement(new SQLParsingEngine(each, SqlPlaceholderUtil.replacePreparedStatement(SQLStatementHelper.getSql(getTestCaseName())), buildShardingRule()).parse());
assertPreparedStatement(new SQLParsingEngine(each, SQLPlaceholderUtil.replacePreparedStatement(SQLStatementHelper.getSql(getTestCaseName())), buildShardingRule()).parse());
}
}

Expand Down

0 comments on commit 44b59d8

Please sign in to comment.