提交 55a36ec7 编写于 作者: L liu ze jian

Ddl test modification completed(ddl测试修改完成)

上级 abe619fb
......@@ -80,15 +80,6 @@ public class StartTest {
return getString("assert.path", null);
}
/**
* initialized.
*
* @return initialized
*/
public static boolean isInitialized() {
return Boolean.valueOf(getString("initialized", "false"));
}
@Parameters
public static Collection<String[]> getParameters() {
......@@ -136,9 +127,10 @@ public class StartTest {
@BeforeClass
public static void beforeClass() {
if (isInitialized()) {
if (AssertEngine.isInitialized()) {
InItCreateSchema.createDatabase();
InItCreateSchema.createTable();
AssertEngine.setInitialized(false);
}
}
......@@ -149,8 +141,9 @@ public class StartTest {
@AfterClass
public static void afterClass() {
if (isInitialized()) {
if (AssertEngine.isClean()) {
InItCreateSchema.dropDatabase();
AssertEngine.setClean(false);
}
}
......
......@@ -30,11 +30,14 @@ import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;
import io.shardingjdbc.core.api.ShardingDataSourceFactory;
import io.shardingjdbc.dbtest.StartTest;
import io.shardingjdbc.dbtest.config.bean.AssertDDLDefinition;
import io.shardingjdbc.dbtest.config.bean.AssertDMLDefinition;
import io.shardingjdbc.dbtest.config.bean.ColumnDefinition;
import io.shardingjdbc.dbtest.init.InItCreateSchema;
import io.shardingjdbc.test.sql.SQLCasesLoader;
import lombok.Getter;
import lombok.Setter;
import org.junit.Assert;
import org.xml.sax.SAXException;
......@@ -54,6 +57,19 @@ public class AssertEngine {
public static final Map<String, AssertsDefinition> ASSERTDEFINITIONMAPS = new HashMap<>();
@Getter
@Setter
private static volatile boolean initialized;
@Getter
@Setter
private static volatile boolean clean ;
static {
initialized = Boolean.valueOf(StartTest.getString("initialized", "false"));
clean = Boolean.valueOf(StartTest.getString("initialized", "false"));
}
/**
* add check use cases.
*
......@@ -216,8 +232,8 @@ public class AssertEngine {
private static void doUpdateUsePreparedStatementToExecuteDDL(final String rootPath, final DataSource dataSource, final AssertDDLDefinition anAssert, final String rootsql, final String msg) throws SQLException, ParseException, IOException, SAXException, ParserConfigurationException, XPathExpressionException {
try {
try (Connection con = dataSource.getConnection()) {
InItCreateSchema.dropTable();
InItCreateSchema.createTable();
//InItCreateSchema.dropTable();
//InItCreateSchema.createTable();
DatabaseUtil.updateUsePreparedStatementToExecute(con, rootsql,
anAssert.getParameters());
String expectedDataFile = PathUtil.getPath(anAssert.getExpectedDataFile(), rootPath);
......@@ -258,8 +274,8 @@ public class AssertEngine {
private static void doUpdateUsePreparedStatementToExecuteUpdateDDL(final String rootPath, final DataSource dataSource, final AssertDDLDefinition anAssert, final String rootsql, final String msg) throws SQLException, ParseException, IOException, SAXException, ParserConfigurationException, XPathExpressionException {
try {
try (Connection con = dataSource.getConnection()) {
InItCreateSchema.dropTable();
InItCreateSchema.createTable();
//InItCreateSchema.dropTable();
//InItCreateSchema.createTable();
DatabaseUtil.updateUsePreparedStatementToExecuteUpdate(con, rootsql,
anAssert.getParameters());
String expectedDataFile = PathUtil.getPath(anAssert.getExpectedDataFile(), rootPath);
......@@ -301,8 +317,8 @@ public class AssertEngine {
private static void doUpdateUseStatementToExecuteDDL(final String rootPath, final DataSource dataSource, final AssertDDLDefinition anAssert, final String rootsql, final String msg) throws SQLException, ParseException, IOException, SAXException, ParserConfigurationException, XPathExpressionException {
try {
try (Connection con = dataSource.getConnection()) {
InItCreateSchema.dropTable();
InItCreateSchema.createTable();
//InItCreateSchema.dropTable();
//InItCreateSchema.createTable();
DatabaseUtil.updateUseStatementToExecute(con, rootsql, anAssert.getParameters());
String expectedDataFile = PathUtil.getPath(anAssert.getExpectedDataFile(), rootPath);
......@@ -343,8 +359,8 @@ public class AssertEngine {
private static void doUpdateUseStatementToExecuteUpdateDDL(final String rootPath, final DataSource dataSource, final AssertDDLDefinition anAssert, final String rootsql, final String msg) throws SQLException, ParseException, IOException, SAXException, ParserConfigurationException, XPathExpressionException {
try {
try (Connection con = dataSource.getConnection()) {
InItCreateSchema.dropTable();
InItCreateSchema.createTable();
//InItCreateSchema.dropTable(anAssert.getCleanSql());
//InItCreateSchema.createTable(anAssert.getInitSql());
DatabaseUtil.updateUseStatementToExecuteUpdate(con, rootsql, anAssert.getParameters());
String expectedDataFile = PathUtil.getPath(anAssert.getExpectedDataFile(), rootPath);
DatasetDefinition checkDataset = AnalyzeDataset.analyze(new File(expectedDataFile));
......@@ -354,8 +370,8 @@ public class AssertEngine {
DatabaseUtil.assertConfigs(checkDataset, columnDefinitions, table, msg);
}
} finally {
InItCreateSchema.dropTable();
InItCreateSchema.createTable();
InItCreateSchema.dropTable(anAssert.getCleanSql());
InItCreateSchema.createTable(anAssert.getInitSql());
}
}
......
......@@ -31,8 +31,11 @@ public class AssertDDLDefinition implements AssertDefinition {
@XmlAttribute(name = "id")
private String id;
@XmlAttribute(name = "init-data-file")
private String initDataFile;
@XmlAttribute(name = "init-sql")
private String initSql;
@XmlAttribute(name = "clean-sql")
private String cleanSql;
@XmlAttribute(name = "expected-data-file")
private String expectedDataFile;
......
......@@ -92,6 +92,15 @@ public class InItCreateSchema {
}
}
/**
* Initialize the database table.
*/
public static synchronized void createTable(final String sqlId) {
for (DatabaseType db : DATABASE_SCHEMAS) {
createSchema(db, sqlId);
}
}
/**
* Initialize the database table.
*/
......@@ -101,6 +110,15 @@ public class InItCreateSchema {
}
}
/**
* Initialize the database table.
*/
public static synchronized void dropTable(final String sqlId) {
for (DatabaseType db : DATABASE_SCHEMAS) {
dropSchema(db, sqlId);
}
}
/**
* Create a database.
*/
......@@ -205,33 +223,38 @@ public class InItCreateSchema {
createShardingSchema(dbType);
}
private static void createSchema(final DatabaseType dbType, final String sqlId) {
createShardingSchema(dbType, sqlId);
}
private static void dropSchema(final DatabaseType dbType) {
dropShardingSchema(dbType);
}
private static void dropSchema(final DatabaseType dbType, final String sqlId) {
dropShardingSchema(dbType, sqlId);
}
private static void createShardingSchema(final DatabaseType dbType) {
Connection conn = null;
ResultSet resultSet = null;
StringReader sr = null;
try {
for (int i = 0; i < 10; i++) {
for (String each : DATABASES) {
List<String> databases = AnalyzeDatabase.analyze(InItCreateSchema.class.getClassLoader()
.getResource("integrate/dbtest").getPath() + "/" + each + "/database.xml");
for (String database : databases) {
conn = initialConnection(database, dbType);
List<String> tableSqlIds = AnalyzeSql.analyze(InItCreateSchema.class.getClassLoader()
.getResource("integrate/dbtest").getPath() + "/" + each + "/table/create-table.xml");
List<String> tableSqls = new ArrayList<>();
for (String tableSqlId : tableSqlIds) {
tableSqls.add(SQLCasesLoader.getInstance().getSchemaSQLCaseMap(tableSqlId));
}
sr = new StringReader(StringUtils.join(tableSqls, ";\n"));
resultSet = RunScript.execute(conn, sr);
for (String each : DATABASES) {
List<String> databases = AnalyzeDatabase.analyze(InItCreateSchema.class.getClassLoader()
.getResource("integrate/dbtest").getPath() + "/" + each + "/database.xml");
for (String database : databases) {
conn = initialConnection(database, dbType);
List<String> tableSqlIds = AnalyzeSql.analyze(InItCreateSchema.class.getClassLoader()
.getResource("integrate/dbtest").getPath() + "/" + each + "/table/create-table.xml");
List<String> tableSqls = new ArrayList<>();
for (String tableSqlId : tableSqlIds) {
tableSqls.add(SQLCasesLoader.getInstance().getSchemaSQLCaseMap(tableSqlId));
}
sr = new StringReader(StringUtils.join(tableSqls, ";\n"));
resultSet = RunScript.execute(conn, sr);
}
}
......@@ -258,31 +281,113 @@ public class InItCreateSchema {
}
}
private static void createShardingSchema(final DatabaseType dbType, final String sqlId) {
Connection conn = null;
ResultSet resultSet = null;
StringReader sr = null;
try {
for (String each : DATABASES) {
List<String> databases = AnalyzeDatabase.analyze(InItCreateSchema.class.getClassLoader()
.getResource("integrate/dbtest").getPath() + "/" + each + "/database.xml");
for (String database : databases) {
conn = initialConnection(database, dbType);
List<String> tableSqls = new ArrayList<>();
tableSqls.add(SQLCasesLoader.getInstance().getSchemaSQLCaseMap(sqlId));
sr = new StringReader(StringUtils.join(tableSqls, ";\n"));
resultSet = RunScript.execute(conn, sr);
}
}
} catch (SQLException | ParserConfigurationException | IOException | XPathExpressionException | SAXException e) {
e.printStackTrace();
} finally {
if (sr != null) {
sr.close();
}
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
private static void dropShardingSchema(final DatabaseType dbType) {
Connection conn = null;
ResultSet resultSet = null;
StringReader sr = null;
try {
for (int i = 0; i < 10; i++) {
for (String each : DATABASES) {
List<String> databases = AnalyzeDatabase.analyze(InItCreateSchema.class.getClassLoader()
.getResource("integrate/dbtest").getPath() + "/" + each + "/database.xml");
for (String database : databases) {
conn = initialConnection(database, dbType);
List<String> tableSqlIds = AnalyzeSql.analyze(InItCreateSchema.class.getClassLoader()
.getResource("integrate/dbtest").getPath() + "/" + each + "/table/drop-table.xml");
List<String> tableSqls = new ArrayList<>();
for (String tableSqlId : tableSqlIds) {
tableSqls.add(SQLCasesLoader.getInstance().getSchemaSQLCaseMap(tableSqlId));
}
sr = new StringReader(StringUtils.join(tableSqls, ";\n"));
resultSet = RunScript.execute(conn, sr);
for (String each : DATABASES) {
List<String> databases = AnalyzeDatabase.analyze(InItCreateSchema.class.getClassLoader()
.getResource("integrate/dbtest").getPath() + "/" + each + "/database.xml");
for (String database : databases) {
conn = initialConnection(database, dbType);
List<String> tableSqlIds = AnalyzeSql.analyze(InItCreateSchema.class.getClassLoader()
.getResource("integrate/dbtest").getPath() + "/" + each + "/table/drop-table.xml");
List<String> tableSqls = new ArrayList<>();
for (String tableSqlId : tableSqlIds) {
tableSqls.add(SQLCasesLoader.getInstance().getSchemaSQLCaseMap(tableSqlId));
}
sr = new StringReader(StringUtils.join(tableSqls, ";\n"));
resultSet = RunScript.execute(conn, sr);
}
}
} catch (SQLException | ParserConfigurationException | IOException | XPathExpressionException | SAXException e) {
e.printStackTrace();
} finally {
if (sr != null) {
sr.close();
}
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
private static void dropShardingSchema(final DatabaseType dbType, final String sqlId) {
Connection conn = null;
ResultSet resultSet = null;
StringReader sr = null;
try {
for (String each : DATABASES) {
List<String> databases = AnalyzeDatabase.analyze(InItCreateSchema.class.getClassLoader()
.getResource("integrate/dbtest").getPath() + "/" + each + "/database.xml");
for (String database : databases) {
conn = initialConnection(database, dbType);
List<String> tableSqls = new ArrayList<>();
tableSqls.add(SQLCasesLoader.getInstance().getSchemaSQLCaseMap(sqlId));
sr = new StringReader(StringUtils.join(tableSqls, ";\n"));
resultSet = RunScript.execute(conn, sr);
}
}
} catch (SQLException | ParserConfigurationException | IOException | XPathExpressionException | SAXException e) {
e.printStackTrace();
} finally {
......
......@@ -9,7 +9,8 @@
</assertDQL>
<assertDML id="test2" init-data-file="./init" expected-update="1" sql="assertUpdateWithoutAlias"
expected-sql="assertSelectEqualsWithSingleTable"
expected-data-file="./asserts/dml/update.xml">
expected-data-file="./asserts/dml/update.xml"
>
<parameters>
<parameter value="update" />
<parameter value="1000" type="int"/>
......@@ -20,7 +21,7 @@
<parameter value="1000" type="int"/>
</expected-parameters>
</assertDML>
<assertDDL id="test3" init-data-file="./init" expected-update="1" sql="assertAlterTableTOrder"
<assertDDL id="test3" init-sql="dbCreateTOrder" clean-sql="dbDropTOrder" expected-update="1" sql="assertAlterTableTOrder"
table="t_order"
expected-data-file="./asserts/ddl/update.xml">
</assertDDL>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册