Skip to content

Commit

Permalink
Merge pull request #16 from shardingjdbc/issue_601
Browse files Browse the repository at this point in the history
Issue 601
  • Loading branch information
qiankunshe authored May 6, 2018
2 parents 064f89c + bbc606a commit 8c0de0b
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.shardingjdbc.dbtest.env.DatabaseEnvironmentManager;
import io.shardingjdbc.dbtest.env.IntegrateTestEnvironment;
import lombok.RequiredArgsConstructor;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -60,6 +61,8 @@ public final class StartTest {

private static boolean isInitialized = IntegrateTestEnvironment.getInstance().isInitialized();

private static boolean isCleaned = IntegrateTestEnvironment.getInstance().isInitialized();

private final String path;

private final String id;
Expand Down Expand Up @@ -126,6 +129,17 @@ public static void setUp() throws JAXBException, IOException, SQLException {
}
}

@AfterClass
// TODO add tearDown for temporary, will remove when original integrate test removed.
public static void tearDown() throws JAXBException, IOException {
if (isCleaned) {
for (String each : SHARDING_RULE_TYPES) {
DatabaseEnvironmentManager.dropDatabase(each);
}
isCleaned = false;
}
}

@Test
public void test() throws JAXBException {
AssertEngine.runAssert(path, id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import io.shardingjdbc.core.constant.DatabaseType;
import io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource;
import io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource;
import io.shardingjdbc.dbtest.env.IntegrateTestEnvironment;
import io.shardingjdbc.dbtest.common.DatabaseEnvironment;
import io.shardingjdbc.dbtest.common.DatabaseUtil;
import io.shardingjdbc.dbtest.config.AnalyzeDataset;
Expand All @@ -35,8 +34,9 @@
import io.shardingjdbc.dbtest.config.bean.DatasetDatabase;
import io.shardingjdbc.dbtest.config.bean.DatasetDefinition;
import io.shardingjdbc.dbtest.config.bean.ParameterDefinition;
import io.shardingjdbc.dbtest.exception.DbTestException;
import io.shardingjdbc.dbtest.env.DatabaseEnvironmentManager;
import io.shardingjdbc.dbtest.env.IntegrateTestEnvironment;
import io.shardingjdbc.dbtest.exception.DbTestException;
import io.shardingjdbc.test.sql.SQLCasesLoader;
import org.apache.commons.lang3.StringUtils;
import org.junit.Assert;
Expand All @@ -53,6 +53,7 @@
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -132,7 +133,7 @@ private static void onlyDatabaseRun(final String shardingRuleType, final String
private static void ddlRun(final DatabaseType databaseType, final String id, final String shardingRuleType, final AssertsDefinition assertsDefinition, final String rootPath, final String msg, final DataSource dataSource) throws SQLException, ParseException, IOException, SAXException, ParserConfigurationException, XPathExpressionException, JAXBException {
for (AssertDDLDefinition each : assertsDefinition.getAssertDDL()) {
if (id.equals(each.getId())) {
List<DatabaseType> databaseTypes = DatabaseEnvironmentManager.getDatabaseTypes(each.getDatabaseConfig());
List<DatabaseType> databaseTypes = getDatabaseTypes(each.getDatabaseConfig());
if (!databaseTypes.contains(databaseType)) {
break;
}
Expand Down Expand Up @@ -183,7 +184,7 @@ private static void ddlRun(final DatabaseType databaseType, final String id, fin

private static void ddlSubRun(final String shardingRuleType, final DatabaseType databaseType, final String rootPath, final String msg, final DataSource dataSource, final AssertDDLDefinition anAssert, final String rootsql, final String expectedDataFile, final List<AssertSubDefinition> subAsserts) throws SQLException, ParseException, IOException, SAXException, ParserConfigurationException, XPathExpressionException, JAXBException {
for (AssertSubDefinition subAssert : subAsserts) {
List<DatabaseType> databaseSubTypes = DatabaseEnvironmentManager.getDatabaseTypes(subAssert.getDatabaseConfig());
List<DatabaseType> databaseSubTypes = getDatabaseTypes(subAssert.getDatabaseConfig());

if (!databaseSubTypes.contains(databaseType)) {
break;
Expand Down Expand Up @@ -230,7 +231,7 @@ private static void ddlSubRun(final String shardingRuleType, final DatabaseType
private static void dmlRun(final String shardingRuleType, final DatabaseType databaseType, final String initDataFile, final String path, final String id, final AssertsDefinition assertsDefinition, final String rootPath, final String msg, final DataSource dataSource, final Map<String, DataSource> dataSourceMaps, final List<String> dbs) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException, SQLException, ParseException {
for (AssertDMLDefinition each : assertsDefinition.getAssertDML()) {
if (id.equals(each.getId())) {
List<DatabaseType> databaseTypes = DatabaseEnvironmentManager.getDatabaseTypes(each.getDatabaseConfig());
List<DatabaseType> databaseTypes = getDatabaseTypes(each.getDatabaseConfig());
if (!databaseTypes.contains(databaseType)) {
break;
}
Expand Down Expand Up @@ -276,7 +277,7 @@ private static void dmlRun(final String shardingRuleType, final DatabaseType dat
resultDoUpdateUsePreparedStatementToExecute = resultDoUpdateUsePreparedStatementToExecute + doUpdateUsePreparedStatementToExecute(expectedDataFile, dataSource, dataSourceMaps, each, rootSQL, mapDatasetDefinition, sqls, msg);
} else {
for (AssertSubDefinition subAssert : subAsserts) {
List<DatabaseType> databaseSubTypes = DatabaseEnvironmentManager.getDatabaseTypes(subAssert.getDatabaseConfig());
List<DatabaseType> databaseSubTypes = getDatabaseTypes(subAssert.getDatabaseConfig());
if (!databaseSubTypes.contains(databaseType)) {
break;
}
Expand Down Expand Up @@ -330,7 +331,7 @@ private static void dmlRun(final String shardingRuleType, final DatabaseType dat
List<AssertSubDefinition> subAsserts = each.getSubAsserts();
if (!subAsserts.isEmpty()) {
for (AssertSubDefinition subAssert : subAsserts) {
List<DatabaseType> databaseSubTypes = DatabaseEnvironmentManager.getDatabaseTypes(subAssert.getDatabaseConfig());
List<DatabaseType> databaseSubTypes = getDatabaseTypes(subAssert.getDatabaseConfig());
if (!databaseSubTypes.contains(databaseType)) {
break;
}
Expand Down Expand Up @@ -391,7 +392,7 @@ private static void dqlRun(final String shardingRuleType, final DatabaseType dat
for (AssertDQLDefinition each : assertsDefinition.getAssertDQL()) {

if (id.equals(each.getId())) {
List<DatabaseType> databaseTypes = DatabaseEnvironmentManager.getDatabaseTypes(each.getDatabaseConfig());
List<DatabaseType> databaseTypes = getDatabaseTypes(each.getDatabaseConfig());
if (!databaseTypes.contains(databaseType)) {
break;
}
Expand Down Expand Up @@ -455,7 +456,7 @@ private static void dqlRun(final String shardingRuleType, final DatabaseType dat

private static void dqlSubRun(final DatabaseType databaseType, final String dbName, final String rootPath, final String msg, final DataSource dataSource, final AssertDQLDefinition anAssert, final String rootSQL, final String expectedDataFile, final List<AssertSubDefinition> subAsserts) throws SQLException, ParseException, IOException, SAXException, ParserConfigurationException, XPathExpressionException {
for (AssertSubDefinition subAssert : subAsserts) {
List<DatabaseType> databaseSubTypes = DatabaseEnvironmentManager.getDatabaseTypes(subAssert.getDatabaseConfig());
List<DatabaseType> databaseSubTypes = getDatabaseTypes(subAssert.getDatabaseConfig());
if (!databaseSubTypes.contains(databaseType)) {
break;
}
Expand Down Expand Up @@ -775,4 +776,12 @@ private static DataSource getDataSource(final Map<String, DataSource> dataSource
private static DataSource getMasterSlaveDataSource(final Map<String, DataSource> dataSourceMap, final String path) throws IOException, SQLException {
return MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, new File(path));
}

private static List<DatabaseType> getDatabaseTypes(final String databaseTypes) {
List<DatabaseType> result = new LinkedList<>();
for (String eachType : databaseTypes.split(",")) {
result.add(DatabaseType.valueOf(eachType));
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class AssertDDLDefinition implements AssertDefinition {
private String expectedDataFile;

@XmlAttribute(name = "database-config")
private String databaseConfig;
private String databaseConfig = "H2,MySQL,Oracle,SQLServer,PostgreSQL";

@XmlAttribute(name = "sql")
private String sql;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class AssertDMLDefinition implements AssertDefinition {
private Integer expectedUpdate;

@XmlAttribute(name = "database-config")
private String databaseConfig;
private String databaseConfig = "H2,MySQL,Oracle,SQLServer,PostgreSQL";

@XmlAttribute(name = "sql")
private String sql;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@

package io.shardingjdbc.dbtest.config.bean;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -45,7 +44,7 @@ public class AssertDQLDefinition implements AssertDefinition {
private String baseConfig;

@XmlAttribute(name = "database-config")
private String databaseConfig;
private String databaseConfig = "H2,MySQL,Oracle,SQLServer,PostgreSQL";

@XmlAttribute(name = "sql")
private String sql;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class AssertSubDefinition {
private String expectedDataFile;

@XmlAttribute(name = "database-config")
private String databaseConfig;
private String databaseConfig = "H2,MySQL,Oracle,SQLServer,PostgreSQL";

@XmlElement(name = "parameter")
private ParameterDefinition parameter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class AssertsDefinition {
private String baseConfig = "db,dbtbl,nullable";

@XmlAttribute(name = "database-config")
private String databaseConfig;
private String databaseConfig = "H2,MySQL,Oracle,SQLServer,PostgreSQL";

@XmlAttribute(name = "masterslave")
private String masterslave;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
import java.net.URL;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
Expand Down Expand Up @@ -128,10 +126,9 @@ private static Collection<String> generateDropDatabaseSQLs(final DatabaseType da
*
* @param shardingRuleType sharding rule type
* @throws JAXBException JAXB exception
* @throws SQLException SQL exception
* @throws IOException IO exception
*/
public static void createTable(final String shardingRuleType) throws JAXBException, SQLException, IOException {
public static void createTable(final String shardingRuleType) throws JAXBException, IOException {
for (DatabaseType each : IntegrateTestEnvironment.getInstance().getDatabaseTypes()) {
DatabaseEnvironmentSchema databaseEnvironmentSchema = getDatabaseInitialization(shardingRuleType);
List<String> databases = databaseEnvironmentSchema.getDatabases();
Expand All @@ -140,6 +137,8 @@ public static void createTable(final String shardingRuleType) throws JAXBExcepti
Connection connection = dataSource.getConnection();
StringReader stringReader = new StringReader(StringUtils.join(databaseEnvironmentSchema.getTableCreateSQLs(), ";\n"))) {
RunScript.execute(connection, stringReader);
} catch (final SQLException ex) {
// TODO schema maybe not exist for oracle only
}
}
}
Expand Down Expand Up @@ -169,27 +168,4 @@ public static void executeSQL(final String shardingRuleType, final DatabaseType
// The table may not exist at the time of deletion(删除时可能表不存在)
}
}

/**
* Get the database type enumeration.
*
* @param typeStrs String database type
* @return database enumeration
*/
public static List<DatabaseType> getDatabaseTypes(final String typeStrs) {
if (StringUtils.isBlank(typeStrs)) {
return Arrays.asList(DatabaseType.values());
}
String[] types = StringUtils.split(typeStrs, ",");
List<DatabaseType> result = new ArrayList<>();
for (String eachType : types) {
DatabaseType[] databaseTypeSrcs = DatabaseType.values();
for (DatabaseType each : databaseTypeSrcs) {
if (eachType.equalsIgnoreCase(each.name())) {
result.add(each);
}
}
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<asserts sharding-rule-config="./config/test" init-data-file="./init" database="h2,mysql,oracle,sqlserver,postgresql">
<asserts sharding-rule-config="./config/test" init-data-file="./init">
<assertDDL id="assertAlterTable" init-sql="CREATE TABLE t_log(id int, status varchar(10))" clean-sql="DROP TABLE t_log" expected-update="1" sql="assertAlterTable" table="t_log" expected-data-file="t_order_update.xml">
</assertDDL>
<assertDDL id="assertAlterTableIfExistAndOnly" init-sql="CREATE TABLE t_log(id int, status varchar(10))" clean-sql="DROP TABLE t_log" expected-update="1" sql="assertAlterTable" table="t_log" expected-data-file="t_order_update.xml">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<asserts sharding-rule-config="./config/test" init-data-file="./init" base-config="db" database="h2,mysql,oracle,sqlserver,postgresql">
<asserts sharding-rule-config="./config/test" init-data-file="./init" base-config="db">
<assertDML id="assertInsertWithAllPlaceholders" expected-update="9" sql="assertInsertWithAllPlaceholders" base-config="db" expected-sql="assertSelectEqualsWithSingleTable">
<subAssert expected-data-file="insert/db_1.xml">
<parameter>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<asserts sharding-rule-config="./config/test" init-data-file="./init" database="h2,mysql,oracle,sqlserver,postgresql">
<asserts sharding-rule-config="./config/test" init-data-file="./init">
<assertDDL id="assertCreateTable" clean-sql="DROP TABLE t_log" expected-update="1" sql="assertCreateTable"
table="t_log"
expected-data-file="assertCreateTable.xml">
Expand All @@ -8,19 +8,19 @@
expected-data-file="assertCreateTable.xml">
</assertDDL>
<assertDDL id="assertCreateTemporaryTable" clean-sql="DROP TABLE t_temp_log" expected-update="1" sql="assertCreateTemporaryTable"
table="t_temp_log" database-config="mysql,postgresql"
table="t_temp_log" database-config="MySQL,PostgreSQL"
expected-data-file="assertCreateTableTempLog.xml">
</assertDDL>
<assertDDL id="assertCreateGlobalTemporaryTable" clean-sql="DROP TABLE t_temp_log" expected-update="1" sql="assertCreateGlobalTemporaryTable"
table="t_temp_log" database-config="oracle,postgresql"
table="t_temp_log" database-config="Oracle,PostgreSQL"
expected-data-file="assertCreateTableTempLog.xml">
</assertDDL>
<assertDDL id="assertCreateLocalTempTable" clean-sql="DROP TABLE t_log" expected-update="1" sql="assertCreateLocalTempTable"
table="t_log" database-config="postgresql"
table="t_log" database-config="PostgreSQL"
expected-data-file="assertCreateTable.xml">
</assertDDL>
<assertDDL id="assertCreateUnloggedTable" clean-sql="DROP TABLE t_log" expected-update="1" sql="assertCreateUnloggedTable"
table="t_log" database-config="postgresql"
table="t_log" database-config="PostgreSQL"
expected-data-file="assertCreateTable.xml">
</assertDDL>
<assertDDL id="assertCreateIndex" init-sql="CREATE TABLE t_log(id int, status varchar(10))" clean-sql="DROP TABLE t_log" expected-update="1" sql="assertCreateIndex"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<asserts sharding-rule-config="./config/test" init-data-file="./init" base-config="db,tbl,dbtbl,masterslave" database="h2,mysql,oracle,sqlserver,postgresql">
<asserts sharding-rule-config="./config/test" init-data-file="./init" base-config="db,tbl,dbtbl,masterslave">
<assertDQL id="assertSelectOne"
sql="assertSelectOne"
expected-data-file="SelectOne.xml">
Expand Down

0 comments on commit 8c0de0b

Please sign in to comment.