提交 61b90a38 编写于 作者: L liu ze jian

提交tab 退格问题

上级 ddf0c75c
......@@ -28,67 +28,67 @@ import io.shardingjdbc.dbtest.init.InItCreateSchema;
@RunWith(value = Parameterized.class)
public class StartTest {
private String path;
private String id;
public StartTest(final String path, final String id) {
this.path = path;
this.id = id;
}
@Parameters
public static Collection<String[]> getParams() {
String assertPath = ConfigRuntime.getAssertPath();
assertPath = PathUtils.getPath(assertPath);
List<String> paths = FileUtils.getAllFilePaths(new File(assertPath), "assert-", "xml");
List<String[]> result = new ArrayList<>();
try {
for (String each : paths) {
AssertsDefinition assertsDefinition = AnalyzeConfig.analyze(each);
List<AssertDefinition> asserts = assertsDefinition.getAsserts();
List<String> ls = new ArrayList<>();
for (AssertDefinition eachAssertDefinition : asserts) {
if (ls.contains(eachAssertDefinition.getId())) {
throw new DbTestException("ID can't be repeated");
}
result.add(new String[] { each, eachAssertDefinition.getId() });
}
AssertEngine.addAssertDefinition(each, assertsDefinition);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
}
return result;
}
@BeforeClass
public static void beforeClass() {
if (ConfigRuntime.isInitialized()) {
InItCreateSchema.createDatabase();
InItCreateSchema.initTable();
}
}
@Test
public void test() {
try {
AssertEngine.runAssert(path, id);
} catch (Exception e) {
e.printStackTrace();
}
}
@AfterClass
public static void afterClass() {
if (ConfigRuntime.isInitialized()) {
InItCreateSchema.dropDatabase();
}
}
private String path;
private String id;
public StartTest(final String path, final String id) {
this.path = path;
this.id = id;
}
@Parameters
public static Collection<String[]> getParams() {
String assertPath = ConfigRuntime.getAssertPath();
assertPath = PathUtils.getPath(assertPath);
List<String> paths = FileUtils.getAllFilePaths(new File(assertPath), "assert-", "xml");
List<String[]> result = new ArrayList<>();
try {
for (String each : paths) {
AssertsDefinition assertsDefinition = AnalyzeConfig.analyze(each);
List<AssertDefinition> asserts = assertsDefinition.getAsserts();
List<String> ls = new ArrayList<>();
for (AssertDefinition eachAssertDefinition : asserts) {
if (ls.contains(eachAssertDefinition.getId())) {
throw new DbTestException("ID can't be repeated");
}
result.add(new String[]{each, eachAssertDefinition.getId()});
}
AssertEngine.addAssertDefinition(each, assertsDefinition);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
}
return result;
}
@BeforeClass
public static void beforeClass() {
if (ConfigRuntime.isInitialized()) {
InItCreateSchema.createDatabase();
InItCreateSchema.initTable();
}
}
@Test
public void test() {
try {
AssertEngine.runAssert(path, id);
} catch (Exception e) {
e.printStackTrace();
}
}
@AfterClass
public static void afterClass() {
if (ConfigRuntime.isInitialized()) {
InItCreateSchema.dropDatabase();
}
}
}
\ No newline at end of file
......@@ -9,110 +9,110 @@ import java.util.Set;
public class ConfigRuntime {
public static Set<String> getDbs() {
Map<String, String> dbs = ConfigUtils.getDatas("database");
Set<String> result = new HashSet<>();
for (Map.Entry<String, String> each : dbs.entrySet()) {
String[] dbsts = each.getKey().split("\\.");
result.add(dbsts[1]);
}
return result;
}
public static Set<String> getDbs() {
Map<String, String> dbs = ConfigUtils.getDatas("database");
Set<String> result = new HashSet<>();
for (Map.Entry<String, String> each : dbs.entrySet()) {
String[] dbsts = each.getKey().split("\\.");
result.add(dbsts[1]);
}
return result;
}
private static String getJdbcConfig(final String key) {
return ConfigUtils.getString("database." + key, "");
}
private static String getJdbcConfig(final String key) {
return ConfigUtils.getString("database." + key, "");
}
public static String getDriverClassName(final DatabaseType type) {
switch (type) {
case H2:
return getJdbcConfig("h2.driver");
case MySQL:
return getJdbcConfig("mysql.driver");
case Oracle:
return getJdbcConfig("oracle.driver");
case SQLServer:
return getJdbcConfig("sqlserver.driver");
case PostgreSQL:
return getJdbcConfig("postgresql.driver");
default:
return getJdbcConfig("h2.driver");
}
}
public static String getDriverClassName(final DatabaseType type) {
switch (type) {
case H2:
return getJdbcConfig("h2.driver");
case MySQL:
return getJdbcConfig("mysql.driver");
case Oracle:
return getJdbcConfig("oracle.driver");
case SQLServer:
return getJdbcConfig("sqlserver.driver");
case PostgreSQL:
return getJdbcConfig("postgresql.driver");
default:
return getJdbcConfig("h2.driver");
}
}
public static String getURL(final DatabaseType type, final String dbName) {
switch (type) {
case H2:
return String.format(getJdbcConfig("h2.url"), dbName);
case MySQL:
return String.format(getJdbcConfig("mysql.url"), dbName);
case Oracle:
return String.format(getJdbcConfig("oracle.url"), dbName);
case SQLServer:
return String.format(getJdbcConfig("sqlserver.url"), dbName);
case PostgreSQL:
return String.format(getJdbcConfig("postgresql.url"), dbName);
default:
return String.format(getJdbcConfig("h2.url"), dbName);
}
}
public static String getURL(final DatabaseType type, final String dbName) {
switch (type) {
case H2:
return String.format(getJdbcConfig("h2.url"), dbName);
case MySQL:
return String.format(getJdbcConfig("mysql.url"), dbName);
case Oracle:
return String.format(getJdbcConfig("oracle.url"), dbName);
case SQLServer:
return String.format(getJdbcConfig("sqlserver.url"), dbName);
case PostgreSQL:
return String.format(getJdbcConfig("postgresql.url"), dbName);
default:
return String.format(getJdbcConfig("h2.url"), dbName);
}
}
public static String getUsername(final DatabaseType type) {
switch (type) {
case H2:
return getJdbcConfig("h2.username");
case MySQL:
return getJdbcConfig("mysql.username");
case Oracle:
return getJdbcConfig("oracle.username");
case SQLServer:
return getJdbcConfig("sqlserver.username");
case PostgreSQL:
return getJdbcConfig("postgresql.username");
default:
return getJdbcConfig("h2.driver");
}
}
public static String getUsername(final DatabaseType type) {
switch (type) {
case H2:
return getJdbcConfig("h2.username");
case MySQL:
return getJdbcConfig("mysql.username");
case Oracle:
return getJdbcConfig("oracle.username");
case SQLServer:
return getJdbcConfig("sqlserver.username");
case PostgreSQL:
return getJdbcConfig("postgresql.username");
default:
return getJdbcConfig("h2.driver");
}
}
public static String getPassword(final DatabaseType type) {
switch (type) {
case H2:
return getJdbcConfig("h2.password");
case MySQL:
return getJdbcConfig("mysql.password");
case Oracle:
return getJdbcConfig("oracle.password");
case SQLServer:
return getJdbcConfig("sqlserver.password");
case PostgreSQL:
return getJdbcConfig("postgresql.password");
default:
return getJdbcConfig("h2.driver");
}
}
public static String getPassword(final DatabaseType type) {
switch (type) {
case H2:
return getJdbcConfig("h2.password");
case MySQL:
return getJdbcConfig("mysql.password");
case Oracle:
return getJdbcConfig("oracle.password");
case SQLServer:
return getJdbcConfig("sqlserver.password");
case PostgreSQL:
return getJdbcConfig("postgresql.password");
default:
return getJdbcConfig("h2.driver");
}
}
public static String getDefualtdb(final DatabaseType type) {
switch (type) {
case H2:
return getJdbcConfig("h2.defualtdb");
case MySQL:
return getJdbcConfig("mysql.defualtdb");
case Oracle:
return getJdbcConfig("oracle.defualtdb");
case SQLServer:
return getJdbcConfig("sqlserver.defualtdb");
case PostgreSQL:
return getJdbcConfig("postgresql.defualtdb");
default:
return getJdbcConfig("h2.defualtdb");
}
}
public static String getDefualtdb(final DatabaseType type) {
switch (type) {
case H2:
return getJdbcConfig("h2.defualtdb");
case MySQL:
return getJdbcConfig("mysql.defualtdb");
case Oracle:
return getJdbcConfig("oracle.defualtdb");
case SQLServer:
return getJdbcConfig("sqlserver.defualtdb");
case PostgreSQL:
return getJdbcConfig("postgresql.defualtdb");
default:
return getJdbcConfig("h2.defualtdb");
}
}
public static String getAssertPath() {
return ConfigUtils.getString("assert.path", null);
}
public static String getAssertPath() {
return ConfigUtils.getString("assert.path", null);
}
public static boolean isInitialized() {
return Boolean.valueOf(ConfigUtils.getString("initialized", "false"));
}
public static boolean isInitialized() {
return Boolean.valueOf(ConfigUtils.getString("initialized", "false"));
}
}
......@@ -6,30 +6,30 @@ import java.util.Properties;
public class ConfigUtils {
private static Properties config = null;
private static Properties config = null;
static {
try {
config = new Properties();
config.load(ConfigUtils.class.getClassLoader().getResourceAsStream("integrate/env.properties"));
} catch (Exception e) {
e.printStackTrace();
}
}
static {
try {
config = new Properties();
config.load(ConfigUtils.class.getClassLoader().getResourceAsStream("integrate/env.properties"));
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getString(final String key, final String defaultValue) {
return config.getProperty(key, defaultValue);
}
public static String getString(final String key, final String defaultValue) {
return config.getProperty(key, defaultValue);
}
public static Map<String, String> getDatas(final String startKey) {
Map<String, String> result = new HashMap<>();
for (Map.Entry<Object, Object> eachEntry : config.entrySet()) {
String key = (String) eachEntry.getKey();
if (key.startsWith(startKey)) {
result.put(key, (String) eachEntry.getValue());
}
}
return result;
}
public static Map<String, String> getDatas(final String startKey) {
Map<String, String> result = new HashMap<>();
for (Map.Entry<Object, Object> eachEntry : config.entrySet()) {
String key = (String) eachEntry.getKey();
if (key.startsWith(startKey)) {
result.put(key, (String) eachEntry.getValue());
}
}
return result;
}
}
......@@ -4,15 +4,15 @@ import io.shardingjdbc.core.constant.DatabaseType;
public class DatabaseTypeUtils {
public static DatabaseType getDatabaseType(final String type) {
public static DatabaseType getDatabaseType(final String type) {
DatabaseType[] databaseTypes = DatabaseType.values();
for (DatabaseType each : databaseTypes) {
if (type.equalsIgnoreCase(each.name())) {
return each;
}
}
return DatabaseType.H2;
}
DatabaseType[] databaseTypes = DatabaseType.values();
for (DatabaseType each : databaseTypes) {
if (type.equalsIgnoreCase(each.name())) {
return each;
}
}
return DatabaseType.H2;
}
}
......@@ -9,61 +9,61 @@ import java.util.List;
*/
public class FileUtils {
/**
* 查找某个目录下的所有文件
*
* @param filePath
* @param prefixFile
* @return
*/
public static List<String> getAllFilePaths(final File filePath, final String prefixFile, final String suffix) {
List<String> result = new ArrayList<>();
File[] files = filePath.listFiles();
if (files == null) {
return result;
}
for (File each : files) {
if (each.isDirectory()) {
getSubFilePaths(each, result, prefixFile, suffix);
} else {
getFiles(prefixFile, suffix, result, each);
/**
* 查找某个目录下的所有文件
*
* @param filePath
* @param prefixFile
* @return
*/
public static List<String> getAllFilePaths(final File filePath, final String prefixFile, final String suffix) {
List<String> result = new ArrayList<>();
File[] files = filePath.listFiles();
if (files == null) {
return result;
}
for (File each : files) {
if (each.isDirectory()) {
getSubFilePaths(each, result, prefixFile, suffix);
} else {
getFiles(prefixFile, suffix, result, each);
}
}
return result;
}
}
}
return result;
}
private static void getFiles(final String prefixFile, final String suffix, final List<String> filePaths, final File f) {
if (prefixFile != null) {
if (f.getName().startsWith(prefixFile)) {
if (suffix != null) {
if (f.getName().endsWith("." + suffix)) {
filePaths.add(f.getPath());
}
} else {
filePaths.add(f.getPath());
}
}
} else {
filePaths.add(f.getPath());
}
}
private static void getFiles(final String prefixFile, final String suffix, final List<String> filePaths, final File f) {
if (prefixFile != null) {
if (f.getName().startsWith(prefixFile)) {
if (suffix != null) {
if (f.getName().endsWith("." + suffix)) {
filePaths.add(f.getPath());
}
} else {
filePaths.add(f.getPath());
}
}
} else {
filePaths.add(f.getPath());
}
}
private static List<String> getSubFilePaths(final File filePath, final List<String> filePaths, final String prefixFile,
final String suffix) {
File[] files = filePath.listFiles();
List<String> result = filePaths;
if (files == null) {
return result;
}
for (File each : files) {
if (each.isDirectory()) {
getSubFilePaths(each, result, prefixFile, suffix);
} else {
getFiles(prefixFile, suffix, result, each);
}
}
return result;
}
private static List<String> getSubFilePaths(final File filePath, final List<String> filePaths, final String prefixFile,
final String suffix) {
File[] files = filePath.listFiles();
List<String> result = filePaths;
if (files == null) {
return result;
}
for (File each : files) {
if (each.isDirectory()) {
getSubFilePaths(each, result, prefixFile, suffix);
} else {
getFiles(prefixFile, suffix, result, each);
}
}
return result;
}
}
......@@ -4,39 +4,39 @@ import io.shardingjdbc.dbtest.exception.DbTestException;
public class PathUtils {
private static final String BasePath = PathUtils.class.getClassLoader().getResource("").getPath();
private static final String BasePath = PathUtils.class.getClassLoader().getResource("").getPath();
/**
* 获取资源路径
*
* @param path
* @return
*/
public static String getPath(final String path, final String parent) {
if (path == null) {
throw new DbTestException("路径不能为空");
}
/**
* 获取资源路径
*
* @param path
* @return
*/
public static String getPath(final String path, final String parent) {
if (path == null) {
throw new DbTestException("路径不能为空");
}
String result = path;
if (result.startsWith("classpath:")) {
result = result.substring("classpath:".length());
result = BasePath + result;
return result;
}
if (parent != null) {
return parent + result;
}
return result;
}
String result = path;
if (result.startsWith("classpath:")) {
result = result.substring("classpath:".length());
result = BasePath + result;
return result;
}
if (parent != null) {
return parent + result;
}
return result;
}
/**
* 获取资源路径
*
* @param path
* @return
*/
public static String getPath(final String path) {
return getPath(path, null);
}
/**
* 获取资源路径
*
* @param path
* @return
*/
public static String getPath(final String path) {
return getPath(path, null);
}
}
......@@ -19,39 +19,39 @@ import org.xml.sax.SAXException;
public class XMLUtil {
public static Document parseFile(final File file) throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
DocumentBuilder db = dbf.newDocumentBuilder();
Document result = db.parse(file);
return result;
}
public static Document parseStream(final InputStream in) throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
DocumentBuilder db = dbf.newDocumentBuilder();
Document result = db.parse(in);
return result;
}
public static String getNodeValue(final Node node) {
return node.getTextContent();
}
public static NodeList getNodeList(final Node node, final String xpath) throws XPathExpressionException {
XPathFactory factory = XPathFactory.newInstance();
XPath oXpath = factory.newXPath();
NodeList result = (NodeList) oXpath.evaluate(xpath, node, XPathConstants.NODESET);
return result;
}
public static Node getNode(final Node node, final String xpath) throws XPathExpressionException {
XPathFactory factory = XPathFactory.newInstance();
XPath oXpath = factory.newXPath();
Node result = (Node) oXpath.evaluate(xpath, node, XPathConstants.NODE);
return result;
}
public static Document parseFile(final File file) throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
DocumentBuilder db = dbf.newDocumentBuilder();
Document result = db.parse(file);
return result;
}
public static Document parseStream(final InputStream in) throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
DocumentBuilder db = dbf.newDocumentBuilder();
Document result = db.parse(in);
return result;
}
public static String getNodeValue(final Node node) {
return node.getTextContent();
}
public static NodeList getNodeList(final Node node, final String xpath) throws XPathExpressionException {
XPathFactory factory = XPathFactory.newInstance();
XPath oXpath = factory.newXPath();
NodeList result = (NodeList) oXpath.evaluate(xpath, node, XPathConstants.NODESET);
return result;
}
public static Node getNode(final Node node, final String xpath) throws XPathExpressionException {
XPathFactory factory = XPathFactory.newInstance();
XPath oXpath = factory.newXPath();
Node result = (Node) oXpath.evaluate(xpath, node, XPathConstants.NODE);
return result;
}
}
......@@ -11,13 +11,13 @@ import io.shardingjdbc.dbtest.config.bean.AssertsDefinition;
public class AnalyzeConfig {
public static AssertsDefinition analyze(final String path) throws IOException, JAXBException {
JAXBContext context = JAXBContext.newInstance(AssertsDefinition.class);
public static AssertsDefinition analyze(final String path) throws IOException, JAXBException {
JAXBContext context = JAXBContext.newInstance(AssertsDefinition.class);
Unmarshaller unmarshal = context.createUnmarshaller();
FileReader reader = new FileReader(path);
return (AssertsDefinition) unmarshal.unmarshal(reader);
Unmarshaller unmarshal = context.createUnmarshaller();
FileReader reader = new FileReader(path);
return (AssertsDefinition) unmarshal.unmarshal(reader);
}
}
}
package io.shardingjdbc.dbtest.config.bean;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
......
......@@ -33,21 +33,21 @@ import lombok.Setter;
@XmlAccessorType(XmlAccessType.FIELD)
public final class AggregationSelectItem {
@XmlAttribute(name = "inner-expression")
private String innerExpression;
@XmlAttribute(name = "inner-expression")
private String innerExpression;
@XmlAttribute(name = "aggregation-type")
private String aggregationType;
@XmlAttribute(name = "aggregation-type")
private String aggregationType;
@XmlAttribute
private String alias;
@XmlAttribute
private String alias;
@XmlAttribute
private String option;
@XmlAttribute
private String option;
@XmlAttribute
private Integer index;
@XmlAttribute
private Integer index;
@XmlElement(name = "derived-column")
private List<AggregationSelectItem> derivedColumns = new ArrayList<>(2);
@XmlElement(name = "derived-column")
private List<AggregationSelectItem> derivedColumns = new ArrayList<>(2);
}
......@@ -21,54 +21,54 @@ import io.shardingjdbc.dbtest.common.XMLUtil;
public class AnalyzeDataset {
public static DatasetDefinition analyze(final String path)
throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
return analyze(new File(path));
}
public static DatasetDefinition analyze(final String path)
throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
return analyze(new File(path));
}
public static DatasetDefinition analyze(final File file)
throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
public static DatasetDefinition analyze(final File file)
throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
Document doc = XMLUtil.parseFile(file);
Node rootNode = XMLUtil.getNode(doc, "/dataset");
NodeList firstNodeList = rootNode.getChildNodes();
DatasetDefinition result = new DatasetDefinition();
for (int i = 0; i < firstNodeList.getLength(); i++) {
Node firstNode = firstNodeList.item(i);
if (firstNode.getNodeType() == Node.ELEMENT_NODE) {
Document doc = XMLUtil.parseFile(file);
Node rootNode = XMLUtil.getNode(doc, "/dataset");
NodeList firstNodeList = rootNode.getChildNodes();
DatasetDefinition result = new DatasetDefinition();
for (int i = 0; i < firstNodeList.getLength(); i++) {
Node firstNode = firstNodeList.item(i);
if (firstNode.getNodeType() == Node.ELEMENT_NODE) {
if ("table-config".equals(firstNode.getNodeName())) {
analyzeTableConfig(result, firstNode);
} else {
Map<String, List<Map<String, String>>> datas = result.getDatas();
String tableName = firstNode.getNodeName();
List<Map<String, String>> datalists = datas.get(tableName);
if (datalists == null) {
datalists = new ArrayList<>();
datas.put(tableName, datalists);
}
if ("table-config".equals(firstNode.getNodeName())) {
analyzeTableConfig(result, firstNode);
} else {
Map<String, List<Map<String, String>>> datas = result.getDatas();
String tableName = firstNode.getNodeName();
List<Map<String, String>> datalists = datas.get(tableName);
if (datalists == null) {
datalists = new ArrayList<>();
datas.put(tableName, datalists);
}
NamedNodeMap attrMap = firstNode.getAttributes();
NamedNodeMap attrMap = firstNode.getAttributes();
Map<String, String> datacols = new HashMap<>();
datalists.add(datacols);
for (int j = 0; j < attrMap.getLength(); j++) {
Node nodeAttr = attrMap.item(j);
Attr attr = (Attr) nodeAttr;
String attrName = attr.getName();
String attrValue = attr.getValue();
datacols.put(attrName, attrValue);
}
}
}
}
return result;
}
Map<String, String> datacols = new HashMap<>();
datalists.add(datacols);
for (int j = 0; j < attrMap.getLength(); j++) {
Node nodeAttr = attrMap.item(j);
Attr attr = (Attr) nodeAttr;
String attrName = attr.getName();
String attrValue = attr.getValue();
datacols.put(attrName, attrValue);
}
}
}
}
return result;
}
private static void analyzeTableConfig(final DatasetDefinition datasetDefinition, final Node firstNode) {
NodeList secondNodeList = firstNode.getChildNodes();
Map<String, Map<String, String>> configs = datasetDefinition.getConfigs();
for (int j = 0; j < secondNodeList.getLength(); j++) {
private static void analyzeTableConfig(final DatasetDefinition datasetDefinition, final Node firstNode) {
NodeList secondNodeList = firstNode.getChildNodes();
Map<String, Map<String, String>> configs = datasetDefinition.getConfigs();
for (int j = 0; j < secondNodeList.getLength(); j++) {
Node secondNode = secondNodeList.item(j);
if (secondNode.getNodeType() == Node.ELEMENT_NODE) {
Map<String, String> maps = new HashMap<>();
......@@ -82,6 +82,6 @@ public class AnalyzeDataset {
}
}
}
}
}
}
......@@ -9,14 +9,14 @@ import lombok.Data;
@Data
public class DatasetDefinition {
/**
* Map<table,Map<column,type>>
*/
private Map<String, Map<String, String>> configs = new HashMap<>();
/**
* Map<table,Map<column,type>>
*/
private Map<String, Map<String, String>> configs = new HashMap<>();
/**
* Map<table,List<Map<column,data>>>
*/
private Map<String, List<Map<String, String>>> datas = new HashMap<>();
/**
* Map<table,List<Map<column,data>>>
*/
private Map<String, List<Map<String, String>>> datas = new HashMap<>();
}
......@@ -18,35 +18,35 @@ import io.shardingjdbc.core.rule.ShardingRule;
*/
public class DataSourceUtil {
public static DataSource getDataSource(final String path) throws IOException, SQLException {
return ShardingDataSourceFactory.createDataSource(new File(path));
}
public static Map<String, DataSource> getDataSourceMap(final ShardingDataSource shardingDataSource)
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
ShardingContext shardingContext = getShardingContext(shardingDataSource);
return shardingContext.getShardingRule().getDataSourceMap();
}
public static ShardingRule getShardingRule(final ShardingDataSource shardingDataSource)
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
ShardingContext shardingContext = getShardingContext(shardingDataSource);
return shardingContext.getShardingRule();
}
public static ShardingContext getShardingContext(final ShardingDataSource shardingDataSource)
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
Field field = shardingDataSource.getClass().getDeclaredField("shardingContext");
field.setAccessible(true);
return (ShardingContext) field.get(shardingDataSource);
}
public static String getDatabaseName(final String dataSetFile) {
String fileName = new File(dataSetFile).getName();
if (-1 == fileName.lastIndexOf(".")) {
return fileName;
}
return fileName.substring(0, fileName.lastIndexOf("."));
}
public static DataSource getDataSource(final String path) throws IOException, SQLException {
return ShardingDataSourceFactory.createDataSource(new File(path));
}
public static Map<String, DataSource> getDataSourceMap(final ShardingDataSource shardingDataSource)
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
ShardingContext shardingContext = getShardingContext(shardingDataSource);
return shardingContext.getShardingRule().getDataSourceMap();
}
public static ShardingRule getShardingRule(final ShardingDataSource shardingDataSource)
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
ShardingContext shardingContext = getShardingContext(shardingDataSource);
return shardingContext.getShardingRule();
}
public static ShardingContext getShardingContext(final ShardingDataSource shardingDataSource)
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
Field field = shardingDataSource.getClass().getDeclaredField("shardingContext");
field.setAccessible(true);
return (ShardingContext) field.get(shardingDataSource);
}
public static String getDatabaseName(final String dataSetFile) {
String fileName = new File(dataSetFile).getName();
if (-1 == fileName.lastIndexOf(".")) {
return fileName;
}
return fileName.substring(0, fileName.lastIndexOf("."));
}
}
......@@ -2,9 +2,9 @@ package io.shardingjdbc.dbtest.exception;
public class DbTestException extends RuntimeException {
private static final long serialVersionUID = 8269224755642356888L;
private static final long serialVersionUID = 8269224755642356888L;
public DbTestException(final String message){
public DbTestException(final String message) {
super(message);
}
......
......@@ -17,166 +17,166 @@ import io.shardingjdbc.dbtest.common.DatabaseTypeUtils;
public class InItCreateSchema {
/**
* 初始化数据库表
*/
public static synchronized void initTable() {
for (String db : ConfigRuntime.getDbs()) {
DatabaseType databaseType = DatabaseTypeUtils.getDatabaseType(db);
createSchema(databaseType);
}
}
/**
* 创建数据库
*/
public static void createDatabase() {
Connection conn = null;
try {
for (String each : ConfigRuntime.getDbs()) {
DatabaseType databaseType = DatabaseTypeUtils.getDatabaseType(each);
conn = initialConnection(null, databaseType);
String packing = "default";
if (DatabaseType.Oracle == databaseType) {
packing = "oracle";
}
RunScript.execute(conn, new InputStreamReader(InItCreateSchema.class.getClassLoader()
.getResourceAsStream("integrate/schema/" + packing + "/manual_schema_create.sql")));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
/**
* 初始化数据库表
*/
public static synchronized void dropDatabase() {
Connection conn = null;
try {
for (String each : ConfigRuntime.getDbs()) {
DatabaseType databaseType = DatabaseTypeUtils.getDatabaseType(each);
conn = initialConnection(null, databaseType);
String packing = "default";
if (DatabaseType.Oracle == databaseType) {
packing = "oracle";
}
RunScript.execute(conn, new InputStreamReader(InItCreateSchema.class.getClassLoader()
.getResourceAsStream("integrate/schema/" + packing + "/manual_schema_drop.sql")));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
initTable();
}
private static void createSchema(final DatabaseType dbType) {
createJdbcSchema(dbType);
createMasterSlaveOnlySchema(dbType);
createShardingSchema(dbType);
}
private static void createShardingSchema(final DatabaseType dbType) {
try {
Connection conn;
for (int i = 0; i < 10; i++) {
for (String database : Arrays.asList("db", "dbtbl", "nullable", "master", "slave")) {
conn = initialConnection(database + "_" + i, dbType);
RunScript.execute(conn, new InputStreamReader(InItCreateSchema.class.getClassLoader()
.getResourceAsStream("integrate/schema/table/" + database + ".sql")));
conn.close();
}
}
conn = initialConnection("tbl", dbType);
RunScript.execute(conn, new InputStreamReader(
InItCreateSchema.class.getClassLoader().getResourceAsStream("integrate/schema/table/tbl.sql")));
conn.close();
} catch (final SQLException ex) {
ex.printStackTrace();
}
}
private static void createMasterSlaveOnlySchema(final DatabaseType dbType) {
try {
Connection conn;
for (String database : Arrays.asList("master_only", "slave_only")) {
conn = initialConnection(database, dbType);
RunScript.execute(conn, new InputStreamReader(InItCreateSchema.class.getClassLoader()
.getResourceAsStream("integrate/schema/table/" + database + ".sql")));
conn.close();
}
} catch (final SQLException ex) {
ex.printStackTrace();
}
}
private static void createJdbcSchema(final DatabaseType dbType) {
try {
Connection conn;
for (int i = 0; i < 2; i++) {
conn = initialConnection("jdbc_" + i, dbType);
RunScript.execute(conn, new InputStreamReader(InItCreateSchema.class.getClassLoader()
.getResourceAsStream("integrate/schema/table/jdbc.sql")));
conn.close();
}
} catch (final SQLException ex) {
ex.printStackTrace();
}
}
protected static String getDatabaseName(final String dataSetFile) {
String fileName = new File(dataSetFile).getName();
if (-1 == fileName.lastIndexOf(".")) {
return fileName;
}
return fileName.substring(0, fileName.lastIndexOf("."));
}
private static BasicDataSource buildDataSource(final String dbName, final DatabaseType type) {
String newDbName = dbName;
BasicDataSource result = new BasicDataSource();
result.setDriverClassName(ConfigRuntime.getDriverClassName(type));
if (newDbName == null) {
newDbName = ConfigRuntime.getDefualtdb(type);
}
result.setUrl(ConfigRuntime.getURL(type, newDbName));
result.setUsername(ConfigRuntime.getUsername(type));
result.setPassword(ConfigRuntime.getPassword(type));
result.setMaxActive(1);
if (DatabaseType.Oracle == type) {
result.setConnectionInitSqls(Collections.singleton("ALTER SESSION SET CURRENT_SCHEMA = " + newDbName));
}
return result;
}
private static Connection initialConnection(final String dbName, final DatabaseType type) throws SQLException {
return buildDataSource(dbName, type).getConnection();
}
/**
* 初始化数据库表
*/
public static synchronized void initTable() {
for (String db : ConfigRuntime.getDbs()) {
DatabaseType databaseType = DatabaseTypeUtils.getDatabaseType(db);
createSchema(databaseType);
}
}
/**
* 创建数据库
*/
public static void createDatabase() {
Connection conn = null;
try {
for (String each : ConfigRuntime.getDbs()) {
DatabaseType databaseType = DatabaseTypeUtils.getDatabaseType(each);
conn = initialConnection(null, databaseType);
String packing = "default";
if (DatabaseType.Oracle == databaseType) {
packing = "oracle";
}
RunScript.execute(conn, new InputStreamReader(InItCreateSchema.class.getClassLoader()
.getResourceAsStream("integrate/schema/" + packing + "/manual_schema_create.sql")));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
/**
* 初始化数据库表
*/
public static synchronized void dropDatabase() {
Connection conn = null;
try {
for (String each : ConfigRuntime.getDbs()) {
DatabaseType databaseType = DatabaseTypeUtils.getDatabaseType(each);
conn = initialConnection(null, databaseType);
String packing = "default";
if (DatabaseType.Oracle == databaseType) {
packing = "oracle";
}
RunScript.execute(conn, new InputStreamReader(InItCreateSchema.class.getClassLoader()
.getResourceAsStream("integrate/schema/" + packing + "/manual_schema_drop.sql")));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
initTable();
}
private static void createSchema(final DatabaseType dbType) {
createJdbcSchema(dbType);
createMasterSlaveOnlySchema(dbType);
createShardingSchema(dbType);
}
private static void createShardingSchema(final DatabaseType dbType) {
try {
Connection conn;
for (int i = 0; i < 10; i++) {
for (String database : Arrays.asList("db", "dbtbl", "nullable", "master", "slave")) {
conn = initialConnection(database + "_" + i, dbType);
RunScript.execute(conn, new InputStreamReader(InItCreateSchema.class.getClassLoader()
.getResourceAsStream("integrate/schema/table/" + database + ".sql")));
conn.close();
}
}
conn = initialConnection("tbl", dbType);
RunScript.execute(conn, new InputStreamReader(
InItCreateSchema.class.getClassLoader().getResourceAsStream("integrate/schema/table/tbl.sql")));
conn.close();
} catch (final SQLException ex) {
ex.printStackTrace();
}
}
private static void createMasterSlaveOnlySchema(final DatabaseType dbType) {
try {
Connection conn;
for (String database : Arrays.asList("master_only", "slave_only")) {
conn = initialConnection(database, dbType);
RunScript.execute(conn, new InputStreamReader(InItCreateSchema.class.getClassLoader()
.getResourceAsStream("integrate/schema/table/" + database + ".sql")));
conn.close();
}
} catch (final SQLException ex) {
ex.printStackTrace();
}
}
private static void createJdbcSchema(final DatabaseType dbType) {
try {
Connection conn;
for (int i = 0; i < 2; i++) {
conn = initialConnection("jdbc_" + i, dbType);
RunScript.execute(conn, new InputStreamReader(InItCreateSchema.class.getClassLoader()
.getResourceAsStream("integrate/schema/table/jdbc.sql")));
conn.close();
}
} catch (final SQLException ex) {
ex.printStackTrace();
}
}
protected static String getDatabaseName(final String dataSetFile) {
String fileName = new File(dataSetFile).getName();
if (-1 == fileName.lastIndexOf(".")) {
return fileName;
}
return fileName.substring(0, fileName.lastIndexOf("."));
}
private static BasicDataSource buildDataSource(final String dbName, final DatabaseType type) {
String newDbName = dbName;
BasicDataSource result = new BasicDataSource();
result.setDriverClassName(ConfigRuntime.getDriverClassName(type));
if (newDbName == null) {
newDbName = ConfigRuntime.getDefualtdb(type);
}
result.setUrl(ConfigRuntime.getURL(type, newDbName));
result.setUsername(ConfigRuntime.getUsername(type));
result.setPassword(ConfigRuntime.getPassword(type));
result.setMaxActive(1);
if (DatabaseType.Oracle == type) {
result.setConnectionInitSqls(Collections.singleton("ALTER SESSION SET CURRENT_SCHEMA = " + newDbName));
}
return result;
}
private static Connection initialConnection(final String dbName, final DatabaseType type) throws SQLException {
return buildDataSource(dbName, type).getConnection();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册