Add unit tests. Make DbStore handle cases where the bug type or category
[cfb.git] / prod / net / jaekl / cfb / CFB.java
index 178dc2fe139b686a417b2d7c96fdb5a7088608a0..639a73198088ffcf7e9335286dbeb4932512ad0a 100644 (file)
@@ -9,7 +9,9 @@ package net.jaekl.cfb;
 
 import java.io.File;
 import java.io.IOException;
+import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
+import java.nio.charset.Charset;
 import java.sql.Connection;
 import java.sql.SQLException;
 import java.text.MessageFormat;
@@ -27,6 +29,7 @@ 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;
+import net.jaekl.cfb.store.StoreException;
 import net.jaekl.qd.xml.XmlParseException;
 
 import org.apache.commons.cli.CommandLine;
@@ -46,13 +49,9 @@ public class CFB {
        
        // Command-line parameters
        File m_configFile;
-       String m_dbName; // db name
        File m_fbp;             // FindBugsProject file
        File m_fbDir;   // Directory where FindBugs is installed
-       String m_host;  // db host
-       int m_port;             // db port
-       String m_user;  // db user
-       String m_pass;  // db password
+       String m_projName; // project (module) name
        String m_buildNum; // build number (version)
        boolean m_removeSchema; // purge DB schema
        File m_output;  // File to which we should write our output (report)
@@ -65,13 +64,9 @@ public class CFB {
                m_config = new Config();
                
                m_configFile = new File("config.properties");
-               m_dbName = "CFB";
                m_fbp    = null;
                m_fbDir  = null;
-               m_host = "localhost";
-               m_port = 5432;
-               m_pass = "";
-               m_user = "user";
+               m_projName = null;
                m_buildNum = null;
                m_removeSchema = false;
                m_output = null;
@@ -85,6 +80,7 @@ public class CFB {
                opt.addOption(null, "drop-tables", false, "Remove database schema (drop all data)");
                opt.addOption("f",  "fbp",         true,  "FindBugsProject file");
                opt.addOption("h",  "host",        true,  "DB hostname");
+               opt.addOption("j",  "project",     true,  "proJect name");
                opt.addOption("n",  "number",      true,  "Build number (version)");
                opt.addOption("o",  "outfile",     true,  "Output report filename");
                opt.addOption("p",  "pass",        true,  "DB password");
@@ -103,13 +99,16 @@ public class CFB {
                                m_configFile = new File(line.getOptionValue("c"));
                        }
                        if (line.hasOption("d")) {
-                               m_dbName = line.getOptionValue("d");
+                               m_config.setDbName(line.getOptionValue("d"));
                        }
                        if (line.hasOption("f")) {
                                m_fbp = new File(line.getOptionValue("f"));
                        }
                        if (line.hasOption("h")) {
-                               m_host = line.getOptionValue("h");
+                               m_config.setDbHost(line.getOptionValue("h"));
+                       }
+                       if (line.hasOption("j")) {
+                               m_projName = line.getOptionValue("j");
                        }
                        if (line.hasOption("n")) {
                                m_buildNum = line.getOptionValue("n");
@@ -118,14 +117,14 @@ public class CFB {
                                m_output = new File(line.getOptionValue("o"));
                        }
                        if (line.hasOption("p")) {
-                               m_pass = line.getOptionValue("p");
+                               m_config.setDbPass(line.getOptionValue("p"));
                        }
                        m_removeSchema = line.hasOption("drop-tables");
                        if (line.hasOption("t")) {
-                               m_port = Integer.parseInt(line.getOptionValue("t"));
+                               m_config.setDbPort(Integer.parseInt(line.getOptionValue("t")));
                        }
                        if (line.hasOption("u")) {
-                               m_user = line.getOptionValue("u");
+                               m_config.setDbUser(line.getOptionValue("u"));
                        }
                } 
                catch (ParseException exc) {
@@ -188,7 +187,12 @@ public class CFB {
                MessageMap messageMap = new MessageMap();
                messageMap.load(findBugsDir, Locale.getDefault(Category.DISPLAY));
                
-               try (Connection con = m_driver.connect(m_host, m_port, m_dbName, m_user, m_pass)) {
+               try (Connection con = m_driver.connect(
+                                       m_config.getDbHost(), m_config.getDbPort(), 
+                                       m_config.getDbName(), 
+                                       m_config.getDbUser(), m_config.getDbPass())
+                       ) 
+               {
                        m_schema.setMessageMap(messageMap);
                        
                        if (m_removeSchema) {
@@ -204,13 +208,20 @@ public class CFB {
                }
                
                Analyzer analyzer = new Analyzer(messageMap);
-               Analysis analysis = analyzer.analyze(pw, workDir, m_fbp, m_buildNum);
+               Analysis analysis = analyzer.analyze(pw, workDir, m_fbp, m_projName, m_buildNum);
                if (null == analysis) {
                        pw.println(trans(CfbBundle.ANALYSIS_FAILED));
                        return;
                }
                
-               try (Connection con = m_driver.connect(m_host, m_port, m_dbName, m_user, m_pass)) {
+               try (
+                               Connection con = m_driver.connect(
+                                               m_config.getDbHost(), m_config.getDbPort(), 
+                                               m_config.getDbName(),
+                                               m_config.getDbUser(), m_config.getDbPass()
+                                       )
+                       )
+               {
                        DbStore store = new DbStore(con, m_driver, messageMap.getColl());
                        
                        store.put(analysis);
@@ -225,6 +236,10 @@ public class CFB {
                        Notifier notifier = new Notifier(m_bundle, m_config);
                        notifier.sendEmailIfNeeded(pw, report);
                }
+               catch (StoreException exc) {
+                       exc.printStackTrace(pw);
+                       return;
+               }
                catch (SQLException exc) {
                        reportUnableToConnect(pw, exc);
                        return;
@@ -233,7 +248,12 @@ public class CFB {
 
        private void reportUnableToConnect(PrintWriter pw, SQLException exc) {
                String cannotConnectFormat = trans(CfbBundle.CANNOT_CONNECT);
-               String cannotConnect = MessageFormat.format(cannotConnectFormat, m_host, ""+m_port, m_dbName, m_user);
+               String cannotConnect = MessageFormat.format(cannotConnectFormat, 
+                                                                                       m_config.getDbHost(), 
+                                                                                       ""+m_config.getDbPort(), 
+                                                                                       m_config.getDbName(), 
+                                                                                       m_config.getDbUser()
+                                                                               );
                exc.printStackTrace(pw);
                SQLException next = exc.getNextException();
                while (null != next) {
@@ -246,7 +266,7 @@ public class CFB {
        public static void main(String[] args) {
                CFB cfb = new CFB(Locale.getDefault());
                
-               try (PrintWriter pw = new PrintWriter(System.out)){
+               try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out, Charset.defaultCharset()))) {
                        cfb.doMain(pw, args);
                        pw.flush();
                } catch (SQLException | IOException | XmlParseException | SAXException | TypeMismatchException exc) {