From 9969d1c623b58fd13db8b143dd641413c39c5f1a Mon Sep 17 00:00:00 2001 From: terrymanu Date: Thu, 3 May 2018 23:41:57 +0800 Subject: [PATCH] for #601: remove PathUtil --- .../dbtest/asserts/AssertEngine.java | 267 ++++++++---------- .../shardingjdbc/dbtest/common/PathUtil.java | 48 ---- 2 files changed, 114 insertions(+), 201 deletions(-) delete mode 100644 sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/common/PathUtil.java diff --git a/sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/asserts/AssertEngine.java b/sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/asserts/AssertEngine.java index c1cd52dd3d110..c28457f0d6e11 100644 --- a/sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/asserts/AssertEngine.java +++ b/sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/asserts/AssertEngine.java @@ -23,7 +23,6 @@ import io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource; import io.shardingjdbc.dbtest.IntegrateTestEnvironment; import io.shardingjdbc.dbtest.common.DatabaseUtil; -import io.shardingjdbc.dbtest.common.PathUtil; import io.shardingjdbc.dbtest.config.AnalyzeDataset; import io.shardingjdbc.dbtest.config.bean.AssertDDLDefinition; import io.shardingjdbc.dbtest.config.bean.AssertDMLDefinition; @@ -51,13 +50,14 @@ import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; public class AssertEngine { - public static final Map ASSERTDEFINITIONMAPS = new HashMap<>(); + public static final Map ASSERT_DEFINITION_MAPS = new HashMap<>(); public static final List DEFAULT_DATABASES = Arrays.asList("db", "dbtbl", "nullable"); @@ -68,7 +68,7 @@ public class AssertEngine { * @param assertsDefinition Check use case definitions */ public static void addAssertDefinition(final String assertPath, final AssertsDefinition assertsDefinition) { - ASSERTDEFINITIONMAPS.put(assertPath, assertsDefinition); + ASSERT_DEFINITION_MAPS.put(assertPath, assertsDefinition); } /** @@ -79,46 +79,35 @@ public static void addAssertDefinition(final String assertPath, final AssertsDef * @return Successful implementation */ public static boolean runAssert(final String path, final String id) { - - AssertsDefinition assertsDefinition = ASSERTDEFINITIONMAPS.get(path); - + AssertsDefinition assertsDefinition = ASSERT_DEFINITION_MAPS.get(path); String rootPath = path.substring(0, path.lastIndexOf(File.separator) + 1); assertsDefinition.setPath(rootPath); - try { String msg = "The file path " + path + ", under which id is " + id; - List dbNames = new ArrayList(); + List dbNames = new ArrayList<>(); if (StringUtils.isNotBlank(assertsDefinition.getBaseConfig())) { String[] dbs = StringUtils.split(assertsDefinition.getBaseConfig(), ","); - for (String each : dbs) { - dbNames.add(each); - } + Collections.addAll(dbNames, dbs); } else { dbNames.addAll(AssertEngine.DEFAULT_DATABASES); } - for (String each : dbNames) { - String initDataFile = PathUtil.getPath(assertsDefinition.getInitDataFile(), rootPath); + String initDataFile = rootPath + assertsDefinition.getInitDataFile(); String initDataPath = initDataFile + "/" + each; File fileDirDatabase = new File(initDataPath); if (fileDirDatabase.exists()) { File[] fileDatabases = fileDirDatabase.listFiles(); List dbs = new ArrayList<>(fileDatabases.length); - if (fileDatabases != null) { - for (File fileDatabase : fileDatabases) { - String databaseName = fileDatabase.getName(); - databaseName = databaseName.substring(0, databaseName.indexOf(".")); - dbs.add(databaseName); - } + for (File fileDatabase : fileDatabases) { + String databaseName = fileDatabase.getName(); + databaseName = databaseName.substring(0, databaseName.indexOf(".")); + dbs.add(databaseName); } - onlyDatabaseRun(each, path, id, assertsDefinition, rootPath, msg, initDataPath, dbs); } } - - - } catch (ParseException | XPathExpressionException | SQLException | ParserConfigurationException | SAXException | IOException e) { + } catch (final ParseException | XPathExpressionException | SQLException | ParserConfigurationException | SAXException | IOException e) { throw new DbTestException(e); } return true; @@ -129,17 +118,15 @@ private static void onlyDatabaseRun(final String dbName, final String path, fina try { for (DatabaseType each : IntegrateTestEnvironment.getInstance().getDatabaseTypes()) { Map dataSourceMaps = new HashMap<>(); - for (String db : dbs) { DataSource subDataSource = InItCreateSchema.buildDataSource(db, each); dataSourceMaps.put(db, subDataSource); } - if ("true".equals(assertsDefinition.getMasterslave())) { - String configPath = PathUtil.getPath(assertsDefinition.getShardingRuleConfig(), rootPath) + "-" + dbName + ".yaml"; + String configPath = rootPath + assertsDefinition.getShardingRuleConfig() + "-" + dbName + ".yaml"; dataSource = getMasterSlaveDataSource(dataSourceMaps, configPath); } else { - String configPath = PathUtil.getPath(assertsDefinition.getShardingRuleConfig(), rootPath) + "-" + dbName + ".yaml"; + String configPath = rootPath + assertsDefinition.getShardingRuleConfig() + "-" + dbName + ".yaml"; dataSource = getDataSource(dataSourceMaps, configPath); } dqlRun(each, initDataPath, dbName, path, id, assertsDefinition, rootPath, msg, dataSource, dataSourceMaps, dbs); @@ -162,8 +149,7 @@ private static void ddlRun(final DatabaseType databaseType, final String id, fin if (!databaseTypes.contains(databaseType)) { break; } - AssertDDLDefinition anAssert = each; - String baseConfig = anAssert.getBaseConfig(); + String baseConfig = each.getBaseConfig(); if (StringUtils.isNotBlank(baseConfig)) { String[] baseConfigs = StringUtils.split(baseConfig, ","); boolean flag = true; @@ -177,33 +163,32 @@ private static void ddlRun(final DatabaseType databaseType, final String id, fin continue; } } - String rootsql = anAssert.getSql(); - rootsql = SQLCasesLoader.getInstance().getSupportedSQL(rootsql); - String expectedDataFile = PathUtil.getPath("asserts/ddl/" + dbName + "/" + anAssert.getExpectedDataFile(), rootPath); + String rootSQL = each.getSql(); + rootSQL = SQLCasesLoader.getInstance().getSupportedSQL(rootSQL); + String expectedDataFile = rootPath + "asserts/ddl/" + dbName + "/" + each.getExpectedDataFile(); if (!new File(expectedDataFile).exists()) { - expectedDataFile = PathUtil.getPath("asserts/ddl/" + anAssert.getExpectedDataFile(), rootPath); + expectedDataFile = rootPath + "asserts/ddl/" + each.getExpectedDataFile(); } - if (anAssert.getParameter().getValues().isEmpty() && anAssert.getParameter().getValueReplaces().isEmpty()) { - List subAsserts = anAssert.getSubAsserts(); + if (each.getParameter().getValues().isEmpty() && each.getParameter().getValueReplaces().isEmpty()) { + List subAsserts = each.getSubAsserts(); if (subAsserts.isEmpty()) { - doUpdateUseStatementToExecuteUpdateDDL(databaseType, dbName, expectedDataFile, dataSource, anAssert, rootsql, msg); - doUpdateUseStatementToExecuteDDL(databaseType, dbName, expectedDataFile, dataSource, anAssert, rootsql, msg); - doUpdateUsePreparedStatementToExecuteUpdateDDL(databaseType, dbName, expectedDataFile, dataSource, anAssert, rootsql, msg); - doUpdateUsePreparedStatementToExecuteDDL(databaseType, dbName, expectedDataFile, dataSource, anAssert, rootsql, msg); + doUpdateUseStatementToExecuteUpdateDDL(databaseType, dbName, expectedDataFile, dataSource, each, rootSQL, msg); + doUpdateUseStatementToExecuteDDL(databaseType, dbName, expectedDataFile, dataSource, each, rootSQL, msg); + doUpdateUsePreparedStatementToExecuteUpdateDDL(databaseType, dbName, expectedDataFile, dataSource, each, rootSQL, msg); + doUpdateUsePreparedStatementToExecuteDDL(databaseType, dbName, expectedDataFile, dataSource, each, rootSQL, msg); } else { - ddlSubRun(databaseType, dbName, rootPath, msg, dataSource, anAssert, rootsql, expectedDataFile, subAsserts); + ddlSubRun(databaseType, dbName, rootPath, msg, dataSource, each, rootSQL, expectedDataFile, subAsserts); } } else { - doUpdateUseStatementToExecuteUpdateDDL(databaseType, dbName, expectedDataFile, dataSource, anAssert, rootsql, msg); - doUpdateUseStatementToExecuteDDL(databaseType, dbName, expectedDataFile, dataSource, anAssert, rootsql, msg); - doUpdateUsePreparedStatementToExecuteUpdateDDL(databaseType, dbName, expectedDataFile, dataSource, anAssert, rootsql, msg); - doUpdateUsePreparedStatementToExecuteDDL(databaseType, dbName, expectedDataFile, dataSource, anAssert, rootsql, msg); - List subAsserts = anAssert.getSubAsserts(); + doUpdateUseStatementToExecuteUpdateDDL(databaseType, dbName, expectedDataFile, dataSource, each, rootSQL, msg); + doUpdateUseStatementToExecuteDDL(databaseType, dbName, expectedDataFile, dataSource, each, rootSQL, msg); + doUpdateUsePreparedStatementToExecuteUpdateDDL(databaseType, dbName, expectedDataFile, dataSource, each, rootSQL, msg); + doUpdateUsePreparedStatementToExecuteDDL(databaseType, dbName, expectedDataFile, dataSource, each, rootSQL, msg); + List subAsserts = each.getSubAsserts(); if (!subAsserts.isEmpty()) { - ddlSubRun(databaseType, dbName, rootPath, msg, dataSource, anAssert, rootsql, expectedDataFile, subAsserts); + ddlSubRun(databaseType, dbName, rootPath, msg, dataSource, each, rootSQL, expectedDataFile, subAsserts); } } - break; } } @@ -230,16 +215,15 @@ private static void ddlSubRun(final DatabaseType databaseType, final String dbNa continue; } } - String expectedDataFileSub = subAssert.getExpectedDataFile(); ParameterDefinition parameter = subAssert.getParameter(); String expectedDataFileTmp = expectedDataFile; if (StringUtils.isBlank(expectedDataFileSub)) { expectedDataFileSub = anAssert.getExpectedDataFile(); } else { - expectedDataFileTmp = PathUtil.getPath("asserts/ddl/" + dbName + "/" + expectedDataFileSub, rootPath); + expectedDataFileTmp = rootPath + "asserts/ddl/" + dbName + "/" + expectedDataFileSub; if (!new File(expectedDataFileTmp).exists()) { - expectedDataFileTmp = PathUtil.getPath("asserts/ddl/" + expectedDataFileSub, rootPath); + expectedDataFileTmp = rootPath + "asserts/ddl/" + expectedDataFileSub; } } if (parameter == null) { @@ -263,8 +247,7 @@ private static void dmlRun(final DatabaseType databaseType, final String initDat if (!databaseTypes.contains(databaseType)) { break; } - AssertDMLDefinition anAssert = each; - String baseConfig = anAssert.getBaseConfig(); + String baseConfig = each.getBaseConfig(); if (StringUtils.isNotBlank(baseConfig)) { String[] baseConfigs = StringUtils.split(baseConfig, ","); boolean flag = true; @@ -278,35 +261,32 @@ private static void dmlRun(final DatabaseType databaseType, final String initDat continue; } } - String rootsql = anAssert.getSql(); - rootsql = SQLCasesLoader.getInstance().getSupportedSQL(rootsql); + String rootSQL = each.getSql(); + rootSQL = SQLCasesLoader.getInstance().getSupportedSQL(rootSQL); Map mapDatasetDefinition = new HashMap<>(); Map sqls = new HashMap<>(); getInitDatas(dbs, initDataFile, mapDatasetDefinition, sqls); - if (mapDatasetDefinition.isEmpty()) { throw new DbTestException(path + " Use cases cannot be parsed"); } - if (sqls.isEmpty()) { throw new DbTestException(path + " The use case cannot initialize the data"); } - String expectedDataFile = PathUtil.getPath("asserts/dml/" + dbName + "/" + anAssert.getExpectedDataFile(), rootPath); + String expectedDataFile = rootPath + "asserts/dml/" + dbName + "/" + each.getExpectedDataFile(); if (!new File(expectedDataFile).exists()) { - expectedDataFile = PathUtil.getPath("asserts/dml/" + anAssert.getExpectedDataFile(), rootPath); + expectedDataFile = rootPath + "asserts/dml/" + each.getExpectedDataFile(); } - int resultDoUpdateUseStatementToExecuteUpdate = 0; int resultDoUpdateUseStatementToExecute = 0; int resultDoUpdateUsePreparedStatementToExecuteUpdate = 0; int resultDoUpdateUsePreparedStatementToExecute = 0; - if (anAssert.getParameter().getValues().isEmpty() && anAssert.getParameter().getValueReplaces().isEmpty()) { - List subAsserts = anAssert.getSubAsserts(); + if (each.getParameter().getValues().isEmpty() && each.getParameter().getValueReplaces().isEmpty()) { + List subAsserts = each.getSubAsserts(); if (subAsserts.isEmpty()) { - resultDoUpdateUseStatementToExecuteUpdate = resultDoUpdateUseStatementToExecuteUpdate + doUpdateUseStatementToExecuteUpdate(expectedDataFile, dataSource, dataSourceMaps, anAssert, rootsql, mapDatasetDefinition, sqls, msg); - resultDoUpdateUseStatementToExecute = resultDoUpdateUseStatementToExecute + doUpdateUseStatementToExecute(expectedDataFile, dataSource, dataSourceMaps, anAssert, rootsql, mapDatasetDefinition, sqls, msg); - resultDoUpdateUsePreparedStatementToExecuteUpdate = resultDoUpdateUsePreparedStatementToExecuteUpdate + doUpdateUsePreparedStatementToExecuteUpdate(expectedDataFile, dataSource, dataSourceMaps, anAssert, rootsql, mapDatasetDefinition, sqls, msg); - resultDoUpdateUsePreparedStatementToExecute = resultDoUpdateUsePreparedStatementToExecute + doUpdateUsePreparedStatementToExecute(expectedDataFile, dataSource, dataSourceMaps, anAssert, rootsql, mapDatasetDefinition, sqls, msg); + resultDoUpdateUseStatementToExecuteUpdate = resultDoUpdateUseStatementToExecuteUpdate + doUpdateUseStatementToExecuteUpdate(expectedDataFile, dataSource, dataSourceMaps, each, rootSQL, mapDatasetDefinition, sqls, msg); + resultDoUpdateUseStatementToExecute = resultDoUpdateUseStatementToExecute + doUpdateUseStatementToExecute(expectedDataFile, dataSource, dataSourceMaps, each, rootSQL, mapDatasetDefinition, sqls, msg); + resultDoUpdateUsePreparedStatementToExecuteUpdate = resultDoUpdateUsePreparedStatementToExecuteUpdate + doUpdateUsePreparedStatementToExecuteUpdate(expectedDataFile, dataSource, dataSourceMaps, each, rootSQL, mapDatasetDefinition, sqls, msg); + resultDoUpdateUsePreparedStatementToExecute = resultDoUpdateUsePreparedStatementToExecute + doUpdateUsePreparedStatementToExecute(expectedDataFile, dataSource, dataSourceMaps, each, rootSQL, mapDatasetDefinition, sqls, msg); } else { for (AssertSubDefinition subAssert : subAsserts) { List databaseSubTypes = InItCreateSchema.getDatabaseTypes(subAssert.getDatabaseConfig()); @@ -333,34 +313,34 @@ private static void dmlRun(final DatabaseType databaseType, final String initDat ParameterDefinition expectedParameter = subAssert.getExpectedParameter(); String expectedDataFileTmp = expectedDataFile; if (StringUtils.isBlank(expectedDataFileSub)) { - expectedDataFileSub = anAssert.getExpectedDataFile(); + expectedDataFileSub = each.getExpectedDataFile(); } else { - expectedDataFileTmp = PathUtil.getPath("asserts/dml/" + dbName + "/" + expectedDataFileSub, rootPath); + expectedDataFileTmp = rootPath + "asserts/dml/" + dbName + "/" + expectedDataFileSub; if (!new File(expectedDataFileTmp).exists()) { - expectedDataFileTmp = PathUtil.getPath("asserts/dml/" + expectedDataFileSub, rootPath); + expectedDataFileTmp = rootPath + "asserts/dml/" + expectedDataFileSub; } } if (parameter == null) { - parameter = anAssert.getParameter(); + parameter = each.getParameter(); } if (expectedParameter == null) { - expectedParameter = anAssert.getParameter(); + expectedParameter = each.getParameter(); } - AssertDMLDefinition anAssertSub = new AssertDMLDefinition(anAssert.getId(), - expectedDataFileSub, anAssert.getBaseConfig(), subAssert.getExpectedUpdate(), anAssert.getDatabaseConfig(), anAssert.getSql(), - anAssert.getExpectedSql(), parameter, expectedParameter, anAssert.getSubAsserts()); - resultDoUpdateUseStatementToExecuteUpdate = resultDoUpdateUseStatementToExecuteUpdate + doUpdateUseStatementToExecuteUpdate(expectedDataFileTmp, dataSource, dataSourceMaps, anAssertSub, rootsql, mapDatasetDefinition, sqls, msg); - resultDoUpdateUseStatementToExecute = resultDoUpdateUseStatementToExecute + doUpdateUseStatementToExecute(expectedDataFileTmp, dataSource, dataSourceMaps, anAssertSub, rootsql, mapDatasetDefinition, sqls, msg); - resultDoUpdateUsePreparedStatementToExecuteUpdate = resultDoUpdateUsePreparedStatementToExecuteUpdate + doUpdateUsePreparedStatementToExecuteUpdate(expectedDataFileTmp, dataSource, dataSourceMaps, anAssertSub, rootsql, mapDatasetDefinition, sqls, msg); - resultDoUpdateUsePreparedStatementToExecute = resultDoUpdateUsePreparedStatementToExecute + doUpdateUsePreparedStatementToExecute(expectedDataFileTmp, dataSource, dataSourceMaps, anAssertSub, rootsql, mapDatasetDefinition, sqls, msg); + AssertDMLDefinition anAssertSub = new AssertDMLDefinition(each.getId(), + expectedDataFileSub, each.getBaseConfig(), subAssert.getExpectedUpdate(), each.getDatabaseConfig(), each.getSql(), + each.getExpectedSql(), parameter, expectedParameter, each.getSubAsserts()); + resultDoUpdateUseStatementToExecuteUpdate = resultDoUpdateUseStatementToExecuteUpdate + doUpdateUseStatementToExecuteUpdate(expectedDataFileTmp, dataSource, dataSourceMaps, anAssertSub, rootSQL, mapDatasetDefinition, sqls, msg); + resultDoUpdateUseStatementToExecute = resultDoUpdateUseStatementToExecute + doUpdateUseStatementToExecute(expectedDataFileTmp, dataSource, dataSourceMaps, anAssertSub, rootSQL, mapDatasetDefinition, sqls, msg); + resultDoUpdateUsePreparedStatementToExecuteUpdate = resultDoUpdateUsePreparedStatementToExecuteUpdate + doUpdateUsePreparedStatementToExecuteUpdate(expectedDataFileTmp, dataSource, dataSourceMaps, anAssertSub, rootSQL, mapDatasetDefinition, sqls, msg); + resultDoUpdateUsePreparedStatementToExecute = resultDoUpdateUsePreparedStatementToExecute + doUpdateUsePreparedStatementToExecute(expectedDataFileTmp, dataSource, dataSourceMaps, anAssertSub, rootSQL, mapDatasetDefinition, sqls, msg); } } } else { - resultDoUpdateUseStatementToExecuteUpdate = resultDoUpdateUseStatementToExecuteUpdate + doUpdateUseStatementToExecuteUpdate(expectedDataFile, dataSource, dataSourceMaps, anAssert, rootsql, mapDatasetDefinition, sqls, msg); - resultDoUpdateUseStatementToExecute = resultDoUpdateUseStatementToExecute + doUpdateUseStatementToExecute(expectedDataFile, dataSource, dataSourceMaps, anAssert, rootsql, mapDatasetDefinition, sqls, msg); - resultDoUpdateUsePreparedStatementToExecuteUpdate = resultDoUpdateUsePreparedStatementToExecuteUpdate + doUpdateUsePreparedStatementToExecuteUpdate(expectedDataFile, dataSource, dataSourceMaps, anAssert, rootsql, mapDatasetDefinition, sqls, msg); - resultDoUpdateUsePreparedStatementToExecute = resultDoUpdateUsePreparedStatementToExecute + doUpdateUsePreparedStatementToExecute(expectedDataFile, dataSource, dataSourceMaps, anAssert, rootsql, mapDatasetDefinition, sqls, msg); - List subAsserts = anAssert.getSubAsserts(); + resultDoUpdateUseStatementToExecuteUpdate = resultDoUpdateUseStatementToExecuteUpdate + doUpdateUseStatementToExecuteUpdate(expectedDataFile, dataSource, dataSourceMaps, each, rootSQL, mapDatasetDefinition, sqls, msg); + resultDoUpdateUseStatementToExecute = resultDoUpdateUseStatementToExecute + doUpdateUseStatementToExecute(expectedDataFile, dataSource, dataSourceMaps, each, rootSQL, mapDatasetDefinition, sqls, msg); + resultDoUpdateUsePreparedStatementToExecuteUpdate = resultDoUpdateUsePreparedStatementToExecuteUpdate + doUpdateUsePreparedStatementToExecuteUpdate(expectedDataFile, dataSource, dataSourceMaps, each, rootSQL, mapDatasetDefinition, sqls, msg); + resultDoUpdateUsePreparedStatementToExecute = resultDoUpdateUsePreparedStatementToExecute + doUpdateUsePreparedStatementToExecute(expectedDataFile, dataSource, dataSourceMaps, each, rootSQL, mapDatasetDefinition, sqls, msg); + List subAsserts = each.getSubAsserts(); if (!subAsserts.isEmpty()) { for (AssertSubDefinition subAssert : subAsserts) { List databaseSubTypes = InItCreateSchema.getDatabaseTypes(subAssert.getDatabaseConfig()); @@ -381,47 +361,45 @@ private static void dmlRun(final DatabaseType databaseType, final String initDat continue; } } - String expectedDataFileSub = subAssert.getExpectedDataFile(); ParameterDefinition parameter = subAssert.getParameter(); ParameterDefinition expectedParameter = subAssert.getExpectedParameter(); String expectedDataFileTmp = expectedDataFile; if (StringUtils.isBlank(expectedDataFileSub)) { - expectedDataFileSub = anAssert.getExpectedDataFile(); + expectedDataFileSub = each.getExpectedDataFile(); } else { - expectedDataFileTmp = PathUtil.getPath("asserts/dml/" + dbName + "/" + expectedDataFileSub, rootPath); + expectedDataFileTmp = rootPath + "asserts/dml/" + dbName + "/" + expectedDataFileSub; if (!new File(expectedDataFileTmp).exists()) { - expectedDataFileTmp = PathUtil.getPath("asserts/dml/" + expectedDataFileSub, rootPath); + expectedDataFileTmp = rootPath + "asserts/dml/" + expectedDataFileSub; } } if (parameter == null) { - parameter = anAssert.getParameter(); + parameter = each.getParameter(); } if (expectedParameter == null) { - expectedParameter = anAssert.getParameter(); + expectedParameter = each.getParameter(); } - AssertDMLDefinition anAssertSub = new AssertDMLDefinition(anAssert.getId(), - expectedDataFileSub, anAssert.getBaseConfig(), subAssert.getExpectedUpdate(), anAssert.getDatabaseConfig(), anAssert.getSql(), - anAssert.getExpectedSql(), parameter, expectedParameter, anAssert.getSubAsserts()); - resultDoUpdateUseStatementToExecuteUpdate = resultDoUpdateUseStatementToExecuteUpdate + doUpdateUseStatementToExecuteUpdate(expectedDataFileTmp, dataSource, dataSourceMaps, anAssertSub, rootsql, mapDatasetDefinition, sqls, msg); - resultDoUpdateUseStatementToExecute = resultDoUpdateUseStatementToExecute + doUpdateUseStatementToExecute(expectedDataFileTmp, dataSource, dataSourceMaps, anAssertSub, rootsql, mapDatasetDefinition, sqls, msg); - resultDoUpdateUsePreparedStatementToExecuteUpdate = resultDoUpdateUsePreparedStatementToExecuteUpdate + doUpdateUsePreparedStatementToExecuteUpdate(expectedDataFileTmp, dataSource, dataSourceMaps, anAssertSub, rootsql, mapDatasetDefinition, sqls, msg); - resultDoUpdateUsePreparedStatementToExecute = resultDoUpdateUsePreparedStatementToExecute + doUpdateUsePreparedStatementToExecute(expectedDataFileTmp, dataSource, dataSourceMaps, anAssertSub, rootsql, mapDatasetDefinition, sqls, msg); + AssertDMLDefinition anAssertSub = new AssertDMLDefinition(each.getId(), + expectedDataFileSub, each.getBaseConfig(), subAssert.getExpectedUpdate(), each.getDatabaseConfig(), each.getSql(), + each.getExpectedSql(), parameter, expectedParameter, each.getSubAsserts()); + resultDoUpdateUseStatementToExecuteUpdate = resultDoUpdateUseStatementToExecuteUpdate + doUpdateUseStatementToExecuteUpdate(expectedDataFileTmp, dataSource, dataSourceMaps, anAssertSub, rootSQL, mapDatasetDefinition, sqls, msg); + resultDoUpdateUseStatementToExecute = resultDoUpdateUseStatementToExecute + doUpdateUseStatementToExecute(expectedDataFileTmp, dataSource, dataSourceMaps, anAssertSub, rootSQL, mapDatasetDefinition, sqls, msg); + resultDoUpdateUsePreparedStatementToExecuteUpdate = resultDoUpdateUsePreparedStatementToExecuteUpdate + doUpdateUsePreparedStatementToExecuteUpdate(expectedDataFileTmp, dataSource, dataSourceMaps, anAssertSub, rootSQL, mapDatasetDefinition, sqls, msg); + resultDoUpdateUsePreparedStatementToExecute = resultDoUpdateUsePreparedStatementToExecute + doUpdateUsePreparedStatementToExecute(expectedDataFileTmp, dataSource, dataSourceMaps, anAssertSub, rootSQL, mapDatasetDefinition, sqls, msg); } } } - if (anAssert.getExpectedUpdate() != null) { - Assert.assertEquals("Update row number error UpdateUseStatementToExecuteUpdate" + msg, anAssert.getExpectedUpdate().intValue(), resultDoUpdateUseStatementToExecuteUpdate); - Assert.assertEquals("Update row number error UpdateUseStatementToExecute" + msg, anAssert.getExpectedUpdate().intValue(), resultDoUpdateUseStatementToExecute); - Assert.assertEquals("Update row number error UpdateUsePreparedStatementToExecuteUpdate" + msg, anAssert.getExpectedUpdate().intValue(), resultDoUpdateUsePreparedStatementToExecuteUpdate); - Assert.assertEquals("Update row number error UpdateUsePreparedStatementToExecute" + msg, anAssert.getExpectedUpdate().intValue(), resultDoUpdateUsePreparedStatementToExecute); + if (null != each.getExpectedUpdate()) { + Assert.assertEquals("Update row number error UpdateUseStatementToExecuteUpdate" + msg, each.getExpectedUpdate().intValue(), resultDoUpdateUseStatementToExecuteUpdate); + Assert.assertEquals("Update row number error UpdateUseStatementToExecute" + msg, each.getExpectedUpdate().intValue(), resultDoUpdateUseStatementToExecute); + Assert.assertEquals("Update row number error UpdateUsePreparedStatementToExecuteUpdate" + msg, each.getExpectedUpdate().intValue(), resultDoUpdateUsePreparedStatementToExecuteUpdate); + Assert.assertEquals("Update row number error UpdateUsePreparedStatementToExecute" + msg, each.getExpectedUpdate().intValue(), resultDoUpdateUsePreparedStatementToExecute); } break; } } } - private static void dqlRun(final DatabaseType databaseType, final String initDataFile, final String dbName, final String path, final String id, final AssertsDefinition assertsDefinition, final String rootPath, final String msg, final DataSource dataSource, final Map dataSourceMaps, final List dbs) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException, SQLException, ParseException { for (AssertDQLDefinition each : assertsDefinition.getAssertDQL()) { @@ -430,9 +408,7 @@ private static void dqlRun(final DatabaseType databaseType, final String initDat if (!databaseTypes.contains(databaseType)) { break; } - - AssertDQLDefinition anAssert = each; - String baseConfig = anAssert.getBaseConfig(); + String baseConfig = each.getBaseConfig(); if (StringUtils.isNotBlank(baseConfig)) { String[] baseConfigs = StringUtils.split(baseConfig, ","); boolean flag = true; @@ -446,62 +422,53 @@ private static void dqlRun(final DatabaseType databaseType, final String initDat continue; } } - String rootsql = anAssert.getSql(); - rootsql = SQLCasesLoader.getInstance().getSupportedSQL(rootsql); + String rootSQL = each.getSql(); + rootSQL = SQLCasesLoader.getInstance().getSupportedSQL(rootSQL); Map mapDatasetDefinition = new HashMap<>(); Map sqls = new HashMap<>(); getInitDatas(dbs, initDataFile, mapDatasetDefinition, sqls); - if (mapDatasetDefinition.isEmpty()) { throw new DbTestException(path + " Use cases cannot be parsed"); } - if (sqls.isEmpty()) { throw new DbTestException(path + " The use case cannot initialize the data"); } - try { initTableData(dataSourceMaps, sqls, mapDatasetDefinition); - - String expectedDataFile = PathUtil.getPath("asserts/dql/" + dbName + "/" + anAssert.getExpectedDataFile(), rootPath); + String expectedDataFile = rootPath + "asserts/dql/" + dbName + "/" + each.getExpectedDataFile(); if (!new File(expectedDataFile).exists()) { - expectedDataFile = PathUtil.getPath("asserts/dql/" + anAssert.getExpectedDataFile(), rootPath); + expectedDataFile = rootPath + "asserts/dql/" + each.getExpectedDataFile(); } - - if (anAssert.getParameter().getValues().isEmpty() && anAssert.getParameter().getValueReplaces().isEmpty()) { - List subAsserts = anAssert.getSubAsserts(); + if (each.getParameter().getValues().isEmpty() && each.getParameter().getValueReplaces().isEmpty()) { + List subAsserts = each.getSubAsserts(); if (subAsserts.isEmpty()) { - doSelectUsePreparedStatement(expectedDataFile, dataSource, anAssert, rootsql, msg); - doSelectUsePreparedStatementToExecuteSelect(expectedDataFile, dataSource, anAssert, rootsql, msg); - doSelectUseStatement(expectedDataFile, dataSource, anAssert, rootsql, msg); - doSelectUseStatementToExecuteSelect(expectedDataFile, dataSource, anAssert, rootsql, msg); + doSelectUsePreparedStatement(expectedDataFile, dataSource, each, rootSQL, msg); + doSelectUsePreparedStatementToExecuteSelect(expectedDataFile, dataSource, each, rootSQL, msg); + doSelectUseStatement(expectedDataFile, dataSource, each, rootSQL, msg); + doSelectUseStatementToExecuteSelect(expectedDataFile, dataSource, each, rootSQL, msg); } else { - dqlSubRun(databaseType, dbName, rootPath, msg, dataSource, anAssert, rootsql, expectedDataFile, subAsserts); + dqlSubRun(databaseType, dbName, rootPath, msg, dataSource, each, rootSQL, expectedDataFile, subAsserts); } - } else { - doSelectUsePreparedStatement(expectedDataFile, dataSource, anAssert, rootsql, msg); - doSelectUsePreparedStatementToExecuteSelect(expectedDataFile, dataSource, anAssert, rootsql, msg); - doSelectUseStatement(expectedDataFile, dataSource, anAssert, rootsql, msg); - doSelectUseStatementToExecuteSelect(expectedDataFile, dataSource, anAssert, rootsql, msg); - - List subAsserts = anAssert.getSubAsserts(); + doSelectUsePreparedStatement(expectedDataFile, dataSource, each, rootSQL, msg); + doSelectUsePreparedStatementToExecuteSelect(expectedDataFile, dataSource, each, rootSQL, msg); + doSelectUseStatement(expectedDataFile, dataSource, each, rootSQL, msg); + doSelectUseStatementToExecuteSelect(expectedDataFile, dataSource, each, rootSQL, msg); + List subAsserts = each.getSubAsserts(); if (!subAsserts.isEmpty()) { - dqlSubRun(databaseType, dbName, rootPath, msg, dataSource, anAssert, rootsql, expectedDataFile, subAsserts); + dqlSubRun(databaseType, dbName, rootPath, msg, dataSource, each, rootSQL, expectedDataFile, subAsserts); } } } finally { clearTableData(dataSourceMaps, mapDatasetDefinition); } - } } } - 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 subAsserts) throws SQLException, ParseException, IOException, SAXException, ParserConfigurationException, XPathExpressionException { + 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 subAsserts) throws SQLException, ParseException, IOException, SAXException, ParserConfigurationException, XPathExpressionException { for (AssertSubDefinition subAssert : subAsserts) { List databaseSubTypes = InItCreateSchema.getDatabaseTypes(subAssert.getDatabaseConfig()); - if (!databaseSubTypes.contains(databaseType)) { break; } @@ -519,16 +486,15 @@ private static void dqlSubRun(final DatabaseType databaseType, final String dbNa continue; } } - String expectedDataFileSub = subAssert.getExpectedDataFile(); ParameterDefinition parameter = subAssert.getParameter(); String expectedDataFileTmp = expectedDataFile; if (StringUtils.isBlank(expectedDataFileSub)) { expectedDataFileSub = anAssert.getExpectedDataFile(); } else { - expectedDataFileTmp = PathUtil.getPath("asserts/dql/" + dbName + "/" + expectedDataFileSub, rootPath); + expectedDataFileTmp = rootPath + "asserts/dql/" + dbName + "/" + expectedDataFileSub; if (!new File(expectedDataFileTmp).exists()) { - expectedDataFileTmp = PathUtil.getPath("asserts/dql/" + expectedDataFileSub, rootPath); + expectedDataFileTmp = rootPath + "asserts/dql/" + expectedDataFileSub; } } if (parameter == null) { @@ -538,11 +504,10 @@ private static void dqlSubRun(final DatabaseType databaseType, final String dbNa anAssert.getBaseConfig(), expectedDataFileSub, anAssert.getDatabaseConfig(), anAssert.getSql(), parameter, anAssert.getSubAsserts()); - - doSelectUsePreparedStatement(expectedDataFileTmp, dataSource, anAssertSub, rootsql, msg); - doSelectUsePreparedStatementToExecuteSelect(expectedDataFileTmp, dataSource, anAssertSub, rootsql, msg); - doSelectUseStatement(expectedDataFileTmp, dataSource, anAssertSub, rootsql, msg); - doSelectUseStatementToExecuteSelect(expectedDataFileTmp, dataSource, anAssertSub, rootsql, msg); + doSelectUsePreparedStatement(expectedDataFileTmp, dataSource, anAssertSub, rootSQL, msg); + doSelectUsePreparedStatementToExecuteSelect(expectedDataFileTmp, dataSource, anAssertSub, rootSQL, msg); + doSelectUseStatement(expectedDataFileTmp, dataSource, anAssertSub, rootSQL, msg); + doSelectUseStatementToExecuteSelect(expectedDataFileTmp, dataSource, anAssertSub, rootSQL, msg); } } @@ -599,7 +564,7 @@ private static void doUpdateUsePreparedStatementToExecuteDDL(final DatabaseType private static int doUpdateUsePreparedStatementToExecuteUpdate(final String expectedDataFile, final DataSource dataSource, final Map dataSourceMaps, final AssertDMLDefinition anAssert, final String rootsql, final Map mapDatasetDefinition, final Map sqls, final String msg) throws SQLException, ParseException, IOException, SAXException, ParserConfigurationException, XPathExpressionException { try { initTableData(dataSourceMaps, sqls, mapDatasetDefinition); - try (Connection con = dataSource.getConnection();) { + try (Connection con = dataSource.getConnection()) { int actual = DatabaseUtil.updateUsePreparedStatementToExecuteUpdate(con, rootsql, anAssert.getParameter()); DatasetDefinition checkDataset = AnalyzeDataset.analyze(new File(expectedDataFile), "data"); @@ -607,10 +572,9 @@ private static int doUpdateUsePreparedStatementToExecuteUpdate(final String expe if (anAssert.getExpectedUpdate() != null) { Assert.assertEquals("Update row number error", anAssert.getExpectedUpdate().intValue(), actual); } - - String checksql = anAssert.getExpectedSql(); - checksql = SQLCasesLoader.getInstance().getSupportedSQL(checksql); - DatasetDatabase ddPreparedStatement = DatabaseUtil.selectUsePreparedStatement(con, checksql, + String checkSQL = anAssert.getExpectedSql(); + checkSQL = SQLCasesLoader.getInstance().getSupportedSQL(checkSQL); + DatasetDatabase ddPreparedStatement = DatabaseUtil.selectUsePreparedStatement(con, checkSQL, anAssert.getExpectedParameter()); DatabaseUtil.assertDatas(checkDataset, ddPreparedStatement, msg); @@ -656,9 +620,9 @@ private static int doUpdateUseStatementToExecute(final String expectedDataFile, if (anAssert.getExpectedUpdate() != null) { Assert.assertEquals("Update row number error", anAssert.getExpectedUpdate().intValue(), actual); } - String checksql = anAssert.getExpectedSql(); - checksql = SQLCasesLoader.getInstance().getSupportedSQL(checksql); - DatasetDatabase ddPreparedStatement = DatabaseUtil.selectUsePreparedStatement(con, checksql, + String checkSQL = anAssert.getExpectedSql(); + checkSQL = SQLCasesLoader.getInstance().getSupportedSQL(checkSQL); + DatasetDatabase ddPreparedStatement = DatabaseUtil.selectUsePreparedStatement(con, checkSQL, anAssert.getExpectedParameter()); DatabaseUtil.assertDatas(checkDataset, ddPreparedStatement, msg); return actual; @@ -744,7 +708,6 @@ private static void doSelectUseStatement(final String expectedDataFile, final Da try (Connection con = dataSource.getConnection()) { DatasetDatabase ddStatement = DatabaseUtil.selectUseStatement(con, rootsql, anAssert.getParameter()); DatasetDefinition checkDataset = AnalyzeDataset.analyze(new File(expectedDataFile), "data"); - DatabaseUtil.assertDatas(checkDataset, ddStatement, msg); } } @@ -781,7 +744,6 @@ private static void getInitDatas(final List dbs, final String initDataFi if (file.exists()) { DatasetDefinition datasetDefinition = AnalyzeDataset.analyze(file, null); mapDatasetDefinition.put(each, datasetDefinition); - Map>> datas = datasetDefinition.getDatas(); for (Map.Entry>> eachEntry : datas.entrySet()) { String sql = DatabaseUtil.analyzeSql(eachEntry.getKey(), eachEntry.getValue().get(0)); @@ -813,18 +775,17 @@ private static void initTableData(final Map dataSourceMaps, Map>> datas = datasetDefinition.getDatas(); for (Map.Entry>> eachListEntry : datas.entrySet()) { try (Connection conn = dataSource1.getConnection()) { - DatabaseUtil.insertUsePreparedStatement(conn, sqls.get(eachListEntry.getKey()), - datas.get(eachListEntry.getKey()), configs.get(eachListEntry.getKey())); + DatabaseUtil.insertUsePreparedStatement(conn, sqls.get(eachListEntry.getKey()), datas.get(eachListEntry.getKey()), configs.get(eachListEntry.getKey())); } } } } - public static DataSource getDataSource(final Map dataSourceMap, final String path) throws IOException, SQLException { + private static DataSource getDataSource(final Map dataSourceMap, final String path) throws IOException, SQLException { return ShardingDataSourceFactory.createDataSource(dataSourceMap, new File(path)); } - public static DataSource getMasterSlaveDataSource(final Map dataSourceMap, final String path) throws IOException, SQLException { + private static DataSource getMasterSlaveDataSource(final Map dataSourceMap, final String path) throws IOException, SQLException { return MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, new File(path)); } } diff --git a/sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/common/PathUtil.java b/sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/common/PathUtil.java deleted file mode 100644 index 970c2fc15737b..0000000000000 --- a/sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/common/PathUtil.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 1999-2015 dangdang.com. - *

- * 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. - *

- */ - -package io.shardingjdbc.dbtest.common; - -import io.shardingjdbc.dbtest.exception.DbTestException; - -public class PathUtil { - - private static final String BASE_PATH = PathUtil.class.getClassLoader().getResource("").getPath(); - - /** - * Get the resource path. - * - * @param path path - * @param parent parent path - * @return path - */ - public static String getPath(final String path, final String parent) { - if (path == null) { - throw new DbTestException("The path cannot be empty"); - } - String result = path; - if (result.startsWith("classpath:")) { - result = result.substring("classpath:".length()); - result = BASE_PATH + result; - return result; - } - if (parent != null) { - return parent + result; - } - return result; - } -}