Skip to content

Commit

Permalink
Remove the dependency on E2ETestParameter from E2EContainerComposer. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
iamhucong authored Aug 1, 2024
1 parent 9b4bb5b commit 2c3d56a
Show file tree
Hide file tree
Showing 16 changed files with 562 additions and 513 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
import lombok.Getter;
import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.test.e2e.container.compose.ContainerComposer;
import org.apache.shardingsphere.test.e2e.container.compose.ContainerComposerRegistry;
import org.apache.shardingsphere.test.e2e.engine.TotalSuitesCountCalculator;
import org.apache.shardingsphere.test.e2e.env.container.atomic.enums.AdapterMode;
import org.apache.shardingsphere.test.e2e.env.container.atomic.enums.AdapterType;
import org.apache.shardingsphere.test.e2e.env.runtime.scenario.path.ScenarioDataPath;
import org.apache.shardingsphere.test.e2e.framework.param.model.E2ETestParameter;
import org.h2.tools.RunScript;

import javax.sql.DataSource;
Expand All @@ -45,7 +45,7 @@
* E2E container composer.
*/
@Getter
public abstract class E2EContainerComposer {
public final class E2EContainerComposer {

public static final String NOT_VERIFY_FLAG = "NOT_VERIFY";

Expand All @@ -66,23 +66,22 @@ public abstract class E2EContainerComposer {

private final Map<String, DataSource> expectedDataSourceMap;

protected E2EContainerComposer(final E2ETestParameter testParam) {
containerComposer = CONTAINER_COMPOSER_REGISTRY.getContainerComposer(testParam.getKey(), testParam.getScenario(), testParam.getDatabaseType(),
AdapterMode.valueOf(testParam.getMode().toUpperCase()), AdapterType.valueOf(testParam.getAdapter().toUpperCase()));
public E2EContainerComposer(final String key, final String scenario, final DatabaseType databaseType, final AdapterMode adapterMode, final AdapterType adapterType) {
containerComposer = CONTAINER_COMPOSER_REGISTRY.getContainerComposer(key, scenario, databaseType, adapterMode, adapterType);
containerComposer.start();
actualDataSourceMap = containerComposer.getActualDataSourceMap();
targetDataSource = containerComposer.getTargetDataSource();
expectedDataSourceMap = containerComposer.getExpectedDataSourceMap();
executeLogicDatabaseInitSQLFileOnlyOnce(testParam, targetDataSource);
executeLogicDatabaseInitSQLFileOnlyOnce(key, scenario, databaseType, targetDataSource);
}

@SneakyThrows({SQLException.class, IOException.class})
private void executeLogicDatabaseInitSQLFileOnlyOnce(final E2ETestParameter testParam, final DataSource targetDataSource) {
Optional<String> logicDatabaseInitSQLFile = new ScenarioDataPath(testParam.getScenario()).findActualDatabaseInitSQLFile(DefaultDatabase.LOGIC_NAME, testParam.getDatabaseType());
private void executeLogicDatabaseInitSQLFileOnlyOnce(final String key, final String scenario, final DatabaseType databaseType, final DataSource targetDataSource) {
Optional<String> logicDatabaseInitSQLFile = new ScenarioDataPath(scenario).findActualDatabaseInitSQLFile(DefaultDatabase.LOGIC_NAME, databaseType);
if (!logicDatabaseInitSQLFile.isPresent()) {
return;
}
String cacheKey = testParam.getKey() + "-" + System.identityHashCode(targetDataSource);
String cacheKey = key + "-" + System.identityHashCode(targetDataSource);
if (!INITIALIZED_SUITES.contains(cacheKey)) {
synchronized (INITIALIZED_SUITES) {
if (!INITIALIZED_SUITES.contains(cacheKey)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,22 @@
* limitations under the License.
*/

package org.apache.shardingsphere.test.e2e.engine.composer;
package org.apache.shardingsphere.test.e2e.engine.context;

import lombok.Getter;
import org.apache.shardingsphere.test.e2e.framework.type.SQLExecuteType;
import org.apache.shardingsphere.test.e2e.cases.casse.assertion.E2ETestCaseAssertion;
import org.apache.shardingsphere.test.e2e.cases.dataset.DataSet;
import org.apache.shardingsphere.test.e2e.cases.dataset.DataSetLoader;
import org.apache.shardingsphere.test.e2e.cases.value.SQLValue;
import org.apache.shardingsphere.test.e2e.framework.param.model.AssertionTestParameter;
import org.apache.shardingsphere.test.e2e.framework.type.SQLExecuteType;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* Single E2E container composer.
*/
@Getter
public final class SingleE2EContainerComposer extends E2EContainerComposer {
public class SingleE2EContext {

private final String sql;

Expand All @@ -43,20 +40,13 @@ public final class SingleE2EContainerComposer extends E2EContainerComposer {

private final DataSet dataSet;

private final DataSet generatedKeyDataSet;

public SingleE2EContainerComposer(final AssertionTestParameter testParam) {
super(testParam);
public SingleE2EContext(final AssertionTestParameter testParam) {
sql = testParam.getTestCaseContext().getTestCase().getSql();
sqlExecuteType = testParam.getSqlExecuteType();
assertion = testParam.getAssertion();
dataSet = null == assertion || null == assertion.getExpectedDataFile()
? null
: DataSetLoader.load(testParam.getTestCaseContext().getParentPath(), testParam.getScenario(), testParam.getDatabaseType(), testParam.getMode(), assertion.getExpectedDataFile());
generatedKeyDataSet = null == assertion || null == assertion.getExpectedGeneratedKeyDataFile()
? null
: DataSetLoader.load(
testParam.getTestCaseContext().getParentPath(), testParam.getScenario(), testParam.getDatabaseType(), testParam.getMode(), assertion.getExpectedGeneratedKeyDataFile());
}

/**
Expand All @@ -72,4 +62,5 @@ private String getLiteralSQL(final String sql) {
List<Object> params = null == assertion ? Collections.emptyList() : assertion.getSQLValues().stream().map(SQLValue::toString).collect(Collectors.toList());
return params.isEmpty() ? sql : String.format(sql.replace("%", "ÿ").replace("?", "%s"), params.toArray()).replace("ÿ", "%").replace("%%", "%").replace("'%'", "'%%'");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
package org.apache.shardingsphere.test.e2e.engine.type;

import org.apache.shardingsphere.infra.util.datetime.DateTimeFormatterFactory;
import org.apache.shardingsphere.test.e2e.engine.context.SingleE2EContext;
import org.apache.shardingsphere.test.e2e.env.container.atomic.enums.AdapterMode;
import org.apache.shardingsphere.test.e2e.env.container.atomic.enums.AdapterType;
import org.apache.shardingsphere.test.e2e.framework.type.SQLCommandType;
import org.apache.shardingsphere.test.e2e.cases.dataset.metadata.DataSetColumn;
import org.apache.shardingsphere.test.e2e.cases.dataset.metadata.DataSetMetaData;
import org.apache.shardingsphere.test.e2e.cases.dataset.row.DataSetRow;
import org.apache.shardingsphere.test.e2e.engine.arg.E2ETestCaseArgumentsProvider;
import org.apache.shardingsphere.test.e2e.engine.arg.E2ETestCaseSettings;
import org.apache.shardingsphere.test.e2e.engine.composer.E2EContainerComposer;
import org.apache.shardingsphere.test.e2e.engine.composer.SingleE2EContainerComposer;
import org.apache.shardingsphere.test.e2e.framework.param.array.E2ETestParameterFactory;
import org.apache.shardingsphere.test.e2e.framework.param.model.AssertionTestParameter;
import org.junit.jupiter.api.condition.EnabledIf;
Expand Down Expand Up @@ -57,40 +59,42 @@ void assertExecute(final AssertionTestParameter testParam) throws SQLException {
if (null == testParam.getTestCaseContext()) {
return;
}
SingleE2EContainerComposer containerComposer = new SingleE2EContainerComposer(testParam);
assertExecute(containerComposer);
E2EContainerComposer containerComposer = new E2EContainerComposer(testParam.getKey(), testParam.getScenario(), testParam.getDatabaseType(),
AdapterMode.valueOf(testParam.getMode().toUpperCase()), AdapterType.valueOf(testParam.getAdapter().toUpperCase()));
SingleE2EContext singleE2EContext = new SingleE2EContext(testParam);
assertExecute(containerComposer, singleE2EContext);
}

private void assertExecute(final SingleE2EContainerComposer containerComposer) throws SQLException {
private void assertExecute(final E2EContainerComposer containerComposer, final SingleE2EContext singleE2EContext) throws SQLException {
try (Connection connection = containerComposer.getTargetDataSource().getConnection()) {
try (Statement statement = connection.createStatement()) {
statement.execute(containerComposer.getSQL());
assertExecuteResult(containerComposer, statement);
statement.execute(singleE2EContext.getSQL());
assertExecuteResult(singleE2EContext, statement);
}
}
}

private void assertExecuteResult(final SingleE2EContainerComposer containerComposer, final Statement statement) throws SQLException {
private void assertExecuteResult(final SingleE2EContext singleE2EContext, final Statement statement) throws SQLException {
try (ResultSet resultSet = statement.getResultSet()) {
if (null == containerComposer.getAssertion().getAssertionSQL()) {
assertResultSet(containerComposer, resultSet);
if (null == singleE2EContext.getAssertion().getAssertionSQL()) {
assertResultSet(singleE2EContext, resultSet);
} else {
statement.execute(containerComposer.getAssertion().getAssertionSQL().getSql());
statement.execute(singleE2EContext.getAssertion().getAssertionSQL().getSql());
try (ResultSet assertionSQLResultSet = statement.getResultSet()) {
assertResultSet(containerComposer, assertionSQLResultSet);
assertResultSet(singleE2EContext, assertionSQLResultSet);
}
}
}
}

private void assertResultSet(final SingleE2EContainerComposer containerComposer, final ResultSet resultSet) throws SQLException {
assertMetaData(resultSet.getMetaData(), getExpectedColumns(containerComposer));
assertRows(resultSet, containerComposer.getDataSet().getRows());
private void assertResultSet(final SingleE2EContext singleE2EContext, final ResultSet resultSet) throws SQLException {
assertMetaData(resultSet.getMetaData(), getExpectedColumns(singleE2EContext));
assertRows(resultSet, singleE2EContext.getDataSet().getRows());
}

private Collection<DataSetColumn> getExpectedColumns(final SingleE2EContainerComposer containerComposer) {
private Collection<DataSetColumn> getExpectedColumns(final SingleE2EContext singleE2EContext) {
Collection<DataSetColumn> result = new LinkedList<>();
for (DataSetMetaData each : containerComposer.getDataSet().getMetaDataList()) {
for (DataSetMetaData each : singleE2EContext.getDataSet().getMetaDataList()) {
result.addAll(each.getColumns());
}
return result;
Expand Down
Loading

0 comments on commit 2c3d56a

Please sign in to comment.