]> WPIA git - gigi.git/commitdiff
UPD: Fasten up testing
authorFelix Dörre <felix@dogcraft.de>
Mon, 22 Sep 2014 19:06:59 +0000 (21:06 +0200)
committerFelix Dörre <felix@dogcraft.de>
Mon, 22 Sep 2014 20:04:13 +0000 (22:04 +0200)
build.xml
doc/tableStructure.sql
tests/org/cacert/gigi/testUtils/ManagedTest.java
util/org/cacert/gigi/util/DatabaseManager.java

index 84d5e8399de4f2a0ab560f30bc5389650fbfbbec..83c95e10328deb6e9675ae3e31bafad7711278ba 100644 (file)
--- a/build.xml
+++ b/build.xml
        <target name="reset-db">
                <copy file="config/test.properties" tofile="config/gigi.properties"/>
                <java classname="org.cacert.gigi.util.DatabaseManager">
+                       <arg value="--test"/>
                        <classpath refid="cacert-gigi.classpath" />
                </java>
        </target>
index cf46d04279689851c1696f9b3f639fbd57b596c8..c141f0adf35f7973effb24899a41f14da7ba5205 100644 (file)
@@ -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',
index cc023239250447d610fdeac22019f8236b85c7db..dff72da839de5481e4dce2944dbf9dd22364d571 100644 (file)
@@ -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();
         }
index c97e2e864631cc67d8a4ec32eed47d76be8554b9..0dadd00aacb61f719fc689a84f56996d0d8b11f2 100644 (file)
@@ -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"));
             }
         }
     }