Add some unit testing of the CfbSchema.
[cfb.git] / prod / net / jaekl / cfb / CFB.java
index f229ce1d7eae767674bfdb268ec9f377a55c4767..81c30ea7a39ac86aaa9ee90838775953ecc74189 100644 (file)
@@ -20,6 +20,7 @@ import net.jaekl.cfb.analyze.Analysis;
 import net.jaekl.cfb.analyze.Analyzer;
 import net.jaekl.cfb.analyze.MessageMap;
 import net.jaekl.cfb.db.CfbSchema;
+import net.jaekl.cfb.db.TypeMismatchException;
 import net.jaekl.cfb.db.driver.DbDriver;
 import net.jaekl.cfb.db.driver.PostgresqlDriver;
 import net.jaekl.cfb.store.DbStore;
@@ -47,6 +48,7 @@ public class CFB {
        String m_user;  // db user
        String m_pass;  // db password
        String m_buildNum; // build number (version)
+       boolean m_removeSchema; // purge DB schema
        
        CFB(Locale locale) {
                m_driver = new PostgresqlDriver();
@@ -62,6 +64,7 @@ public class CFB {
                m_pass = "";
                m_user = "user";
                m_buildNum = null;
+               m_removeSchema = false;
        }
        
        Options createOptions() {
@@ -72,6 +75,7 @@ public class CFB {
                opt.addOption("h", "host",   true, "DB hostname");
                opt.addOption("n", "number", true, "Build number (version)");
                opt.addOption("p", "pass",   true, "DB password");
+               opt.addOption("r", "remove", false, "Remove database schema (drop all data)");
                opt.addOption("t", "port",   true, "DB port");
                opt.addOption("u", "user",   true, "DB username");
                
@@ -98,6 +102,7 @@ public class CFB {
                        if (line.hasOption("p")) {
                                m_pass = line.getOptionValue("p");
                        }
+                       m_removeSchema = line.hasOption("r");
                        if (line.hasOption("t")) {
                                m_port = Integer.parseInt(line.getOptionValue("t"));
                        }
@@ -147,7 +152,7 @@ public class CFB {
                }
        } 
        
-       void doMain(PrintWriter pw, String[] args) throws SQLException, IOException, XmlParseException, SAXException {
+       void doMain(PrintWriter pw, String[] args) throws SQLException, IOException, XmlParseException, SAXException, TypeMismatchException {
                initArgs();     // read environment and system properties
                if ( ! parseArgs(pw, args) ) {
                        return;
@@ -160,7 +165,13 @@ public class CFB {
                
                try (Connection con = m_driver.connect(m_host, m_port, m_dbName, m_user, m_pass)) {
                        m_schema.setMessageMap(messageMap);
-                       m_schema.ensureDbInitialized(con);      
+                       
+                       if (m_removeSchema) {
+                               m_schema.purge(con);
+                               return;
+                       }
+                       m_schema.ensureDbInitialized(con);
+                       messageMap.loadIds(con, m_driver);
                }
                catch (SQLException exc) {
                        reportUnableToConnect(pw, exc);
@@ -175,7 +186,7 @@ public class CFB {
                }
                
                try (Connection con = m_driver.connect(m_host, m_port, m_dbName, m_user, m_pass)) {
-                       DbStore store = new DbStore(con);
+                       DbStore store = new DbStore(con, m_driver, messageMap.getColl());
                        
                        store.put(analysis);
                }
@@ -189,6 +200,11 @@ public class CFB {
                String cannotConnectFormat = trans(CfbBundle.CANNOT_CONNECT);
                String cannotConnect = MessageFormat.format(cannotConnectFormat, m_host, ""+m_port, m_dbName, m_user);
                exc.printStackTrace(pw);
+               SQLException next = exc.getNextException();
+               while (null != next) {
+                       next.printStackTrace(pw);
+                       next = next.getNextException();
+               }
                pw.println(cannotConnect);
        }
        
@@ -198,7 +214,7 @@ public class CFB {
                try (PrintWriter pw = new PrintWriter(System.out)){
                        cfb.doMain(pw, args);
                        pw.flush();
-               } catch (SQLException | IOException | XmlParseException | SAXException exc) {
+               } catch (SQLException | IOException | XmlParseException | SAXException | TypeMismatchException exc) {
                        exc.printStackTrace();
                }
        }