From bc7e6ad7d11db1435af69be94710b20eda6212b5 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Thu, 3 May 2018 23:06:07 +0800 Subject: [PATCH] for #601: refactor InItCreateSchema --- .../io/shardingjdbc/dbtest/StartTest.java | 1 - .../dbtest/init/InItCreateSchema.java | 45 ++++++++----------- 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/StartTest.java b/sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/StartTest.java index c1887ff4b2272..37f306df3a5df 100644 --- a/sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/StartTest.java +++ b/sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/StartTest.java @@ -65,7 +65,6 @@ public static Collection getParameters() throws IOException, JAXBExcep List paths = FileUtil.getAllFilePaths(new File(assertPath), "assert-", "xml"); for (String each : paths) { AssertsDefinition assertsDefinition = AnalyzeConfig.analyze(each); - if (StringUtils.isNotBlank(assertsDefinition.getBaseConfig())) { String[] dbs = StringUtils.split(assertsDefinition.getBaseConfig(), ","); for (String db : dbs) { diff --git a/sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/init/InItCreateSchema.java b/sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/init/InItCreateSchema.java index 4fd41e2502348..15d29697973b2 100644 --- a/sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/init/InItCreateSchema.java +++ b/sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/init/InItCreateSchema.java @@ -53,19 +53,10 @@ public final class InItCreateSchema { * * @param database database name */ - public static void addDatabase(String database) { + public static void addDatabase(final String database) { DATABASES.add(database); } - /** - * Initialize the database table. - */ - public static synchronized void createTable() { - for (DatabaseType each : IntegrateTestEnvironment.getInstance().getDatabaseTypes()) { - createSchema(each); - } - } - /** * Create a database. */ @@ -171,6 +162,15 @@ public static synchronized void dropDatabase() { } } + /** + * Initialize the database table. + */ + public static synchronized void createTable() { + for (DatabaseType each : IntegrateTestEnvironment.getInstance().getDatabaseTypes()) { + createSchema(each); + } + } + private static void createSchema(final DatabaseType dbType) { createShardingSchema(dbType); } @@ -191,15 +191,12 @@ private static void createShardingSchema(final DatabaseType dbType) { 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(); + } catch (final SQLException | ParserConfigurationException | IOException | XPathExpressionException | SAXException ex) { + ex.printStackTrace(); } finally { if (sr != null) { sr.close(); @@ -243,7 +240,7 @@ private static void createShardingSchema(final DatabaseType dbType, final String for (String each : DATABASES) { if (dbname != null) { - if (each != dbname) { + if (!each.equals(dbname)) { continue; } } @@ -253,15 +250,12 @@ private static void createShardingSchema(final DatabaseType dbType, final String conn = initialConnection(database, dbType); List 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(); + } catch (final SQLException | ParserConfigurationException | IOException | XPathExpressionException | SAXException ex) { + ex.printStackTrace(); } finally { if (sr != null) { sr.close(); @@ -290,7 +284,7 @@ private static void dropShardingSchema(final DatabaseType dbType, final String s try { for (String each : DATABASES) { if (dbname != null) { - if (each != dbname) { + if (!each.equals(dbname)) { continue; } } @@ -384,17 +378,16 @@ public static List getDatabaseTypes(final String typeStrs) { if (StringUtils.isBlank(typeStrs)) { return Arrays.asList(DatabaseType.values()); } - String[] types = StringUtils.split(typeStrs, ","); - List databaseTypes = new ArrayList<>(); + List result = new ArrayList<>(); for (String eachType : types) { DatabaseType[] databaseTypeSrcs = DatabaseType.values(); for (DatabaseType each : databaseTypeSrcs) { if (eachType.equalsIgnoreCase(each.name())) { - databaseTypes.add(each); + result.add(each); } } } - return databaseTypes; + return result; } }