From 0d64798ee0a9c2091119335e0c6182f61c601173 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Felix=20D=C3=B6rre?= Date: Mon, 22 Sep 2014 21:06:59 +0200 Subject: [PATCH] UPD: Fasten up testing --- build.xml | 1 + doc/tableStructure.sql | 2 +- .../cacert/gigi/testUtils/ManagedTest.java | 3 +- .../org/cacert/gigi/util/DatabaseManager.java | 43 ++++++++++++++++--- 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/build.xml b/build.xml index 84d5e839..83c95e10 100644 --- a/build.xml +++ b/build.xml @@ -154,6 +154,7 @@ + diff --git a/doc/tableStructure.sql b/doc/tableStructure.sql index cf46d042..c141f0ad 100644 --- a/doc/tableStructure.sql +++ b/doc/tableStructure.sql @@ -128,7 +128,7 @@ CREATE TABLE `certs` ( `memid` int(11) NOT NULL DEFAULT '0', `serial` varchar(50) NOT NULL DEFAULT '', `CN` varchar(255) NOT NULL DEFAULT '', - `subject` text NOT NULL, + `subject` varchar(1024) NOT NULL, `keytype` char(2) NOT NULL DEFAULT 'NS', `codesign` tinyint(1) NOT NULL DEFAULT '0', `md` enum('md5','sha1','sha256','sha512') NOT NULL DEFAULT 'sha512', diff --git a/tests/org/cacert/gigi/testUtils/ManagedTest.java b/tests/org/cacert/gigi/testUtils/ManagedTest.java index cc023239..dff72da8 100644 --- a/tests/org/cacert/gigi/testUtils/ManagedTest.java +++ b/tests/org/cacert/gigi/testUtils/ManagedTest.java @@ -48,6 +48,7 @@ import org.cacert.gigi.testUtils.TestEmailReciever.TestMail; import org.cacert.gigi.util.DatabaseManager; import org.cacert.gigi.util.ServerConstants; import org.cacert.gigi.util.SimpleSigner; +import org.cacert.gigi.util.DatabaseManager.ImportType; import org.junit.After; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -149,7 +150,7 @@ public class ManagedTest extends ConfiguredTest { try { DatabaseManager.run(new String[] { testProps.getProperty("sql.driver"), testProps.getProperty("sql.url"), testProps.getProperty("sql.user"), testProps.getProperty("sql.password") - }, true); + }, ImportType.TRUNCATE); } catch (ClassNotFoundException e) { e.printStackTrace(); } diff --git a/util/org/cacert/gigi/util/DatabaseManager.java b/util/org/cacert/gigi/util/DatabaseManager.java index c97e2e86..0dadd00a 100644 --- a/util/org/cacert/gigi/util/DatabaseManager.java +++ b/util/org/cacert/gigi/util/DatabaseManager.java @@ -19,6 +19,13 @@ public class DatabaseManager { } public static void main(String[] args) throws SQLException, ClassNotFoundException, IOException { + boolean test = false; + if (args.length >= 1 && args[0].equals("--test")) { + test = true; + String[] ne = new String[args.length - 1]; + System.arraycopy(args, 1, ne, 0, ne.length); + args = ne; + } if (args.length == 0) { Properties p = new Properties(); p.load(new FileReader("config/gigi.properties")); @@ -30,10 +37,25 @@ public class DatabaseManager { System.err.println("Usage: com.mysql.jdbc.Driver jdbc:mysql://localhost/cacert user password"); return; } - run(args, false); + run(args, test ? ImportType.TEST : ImportType.PRODUCTION); + } + + public static enum ImportType { + /** + * Execute Script as-as + */ + PRODUCTION, + /** + * Execute Script, but changing Engine=InnoDB to Engine=Memory + */ + TEST, + /** + * Execute INSERT statements as-is, and TRUNCATE instead of DROPPING + */ + TRUNCATE } - public static void run(String[] args, boolean truncate) throws ClassNotFoundException, SQLException, IOException { + public static void run(String[] args, ImportType truncate) throws ClassNotFoundException, SQLException, IOException { Class.forName(args[0]); Connection conn = DriverManager.getConnection(args[1], args[2], args[3]); conn.setAutoCommit(false); @@ -41,26 +63,33 @@ public class DatabaseManager { addFile(stmt, new File("doc/tableStructure.sql"), truncate); File localData = new File("doc/sampleData.sql"); if (localData.exists()) { - addFile(stmt, localData, false); + addFile(stmt, localData, ImportType.PRODUCTION); } stmt.executeBatch(); conn.commit(); stmt.close(); } - private static void addFile(Statement stmt, File f, boolean truncate) throws IOException, SQLException { + private static void addFile(Statement stmt, File f, ImportType type) throws IOException, SQLException { String sql = readFile(f); sql = sql.replaceAll("--[^\n]+\n", "\n"); String[] stmts = sql.split(";"); Pattern p = Pattern.compile("\\s*DROP TABLE IF EXISTS `([^`]+)`"); for (String string : stmts) { Matcher m = p.matcher(string); - if (m.matches()) { + string = string.trim(); + if (string.equals("")) { + continue; + } + if (m.matches() && type == ImportType.TRUNCATE) { String sql2 = "TRUNCATE `" + m.group(1) + "`"; stmt.addBatch(sql2); + continue; } - if ( !string.trim().equals("") && ( !truncate || string.contains("INSERT"))) { - stmt.addBatch(string.replace("ENGINE=Memory", "")); + if (type == ImportType.PRODUCTION || string.startsWith("INSERT")) { + stmt.addBatch(string); + } else if (type == ImportType.TEST) { + stmt.addBatch(string.replace("ENGINE=InnoDB", "ENGINE=Memory")); } } } -- 2.39.2