Add unit tests. Make DbStore handle cases where the bug type or category
[cfb.git] / prod / net / jaekl / cfb / CFB.java
1 package net.jaekl.cfb;
2
3 // Comparative FindBugs
4 // 
5 // Tool to compare successive runs of FindBugs, 
6 // flagging the change from one run to the next.
7 // 
8 // Copyright (C) 2015 Christian Jaekl
9
10 import java.io.File;
11 import java.io.IOException;
12 import java.io.OutputStreamWriter;
13 import java.io.PrintWriter;
14 import java.nio.charset.Charset;
15 import java.sql.Connection;
16 import java.sql.SQLException;
17 import java.text.MessageFormat;
18 import java.util.Locale;
19 import java.util.Locale.Category;
20
21 import net.jaekl.cfb.analyze.Analysis;
22 import net.jaekl.cfb.analyze.Analyzer;
23 import net.jaekl.cfb.analyze.Delta;
24 import net.jaekl.cfb.analyze.HtmlReport;
25 import net.jaekl.cfb.analyze.MessageMap;
26 import net.jaekl.cfb.analyze.Notifier;
27 import net.jaekl.cfb.db.CfbSchema;
28 import net.jaekl.cfb.db.TypeMismatchException;
29 import net.jaekl.cfb.db.driver.DbDriver;
30 import net.jaekl.cfb.db.driver.PostgresqlDriver;
31 import net.jaekl.cfb.store.DbStore;
32 import net.jaekl.cfb.store.StoreException;
33 import net.jaekl.qd.xml.XmlParseException;
34
35 import org.apache.commons.cli.CommandLine;
36 import org.apache.commons.cli.GnuParser;
37 import org.apache.commons.cli.HelpFormatter;
38 import org.apache.commons.cli.Options;
39 import org.apache.commons.cli.ParseException;
40 import org.xml.sax.SAXException;
41
42 public class CFB {
43         DbDriver m_driver;
44         CfbSchema m_schema;
45         CfbBundle m_bundle;     
46         Locale m_locale;
47         
48         Config m_config;
49         
50         // Command-line parameters
51         File m_configFile;
52         File m_fbp;             // FindBugsProject file
53         File m_fbDir;   // Directory where FindBugs is installed
54         String m_projName; // project (module) name
55         String m_buildNum; // build number (version)
56         boolean m_removeSchema; // purge DB schema
57         File m_output;  // File to which we should write our output (report)
58         
59         CFB(Locale locale) {
60                 m_driver = new PostgresqlDriver();
61                 m_schema = new CfbSchema(m_driver);
62                 m_locale = locale;
63                 m_bundle = CfbBundle.getInst(m_locale);
64                 m_config = new Config();
65                 
66                 m_configFile = new File("config.properties");
67                 m_fbp    = null;
68                 m_fbDir  = null;
69                 m_projName = null;
70                 m_buildNum = null;
71                 m_removeSchema = false;
72                 m_output = null;
73         }
74         
75         Options createOptions() {
76                 Options opt = new Options();
77                 
78                 opt.addOption("c",  "config",      true,  "Properties configuration file");
79                 opt.addOption("d",  "dbname",      true,  "DB name");
80                 opt.addOption(null, "drop-tables", false, "Remove database schema (drop all data)");
81                 opt.addOption("f",  "fbp",         true,  "FindBugsProject file");
82                 opt.addOption("h",  "host",        true,  "DB hostname");
83                 opt.addOption("j",  "project",     true,  "proJect name");
84                 opt.addOption("n",  "number",      true,  "Build number (version)");
85                 opt.addOption("o",  "outfile",     true,  "Output report filename");
86                 opt.addOption("p",  "pass",        true,  "DB password");
87                 opt.addOption("t",  "port",        true,  "DB port");
88                 opt.addOption("u",  "user",        true,  "DB username");
89                 
90                 return opt;
91         }
92         
93         boolean parseArgs(PrintWriter pw, String[] args) {
94                 Options opt = createOptions();
95                 
96                 try {
97                         CommandLine line = new GnuParser().parse(opt, args);
98                         if (line.hasOption("c")) {
99                                 m_configFile = new File(line.getOptionValue("c"));
100                         }
101                         if (line.hasOption("d")) {
102                                 m_config.setDbName(line.getOptionValue("d"));
103                         }
104                         if (line.hasOption("f")) {
105                                 m_fbp = new File(line.getOptionValue("f"));
106                         }
107                         if (line.hasOption("h")) {
108                                 m_config.setDbHost(line.getOptionValue("h"));
109                         }
110                         if (line.hasOption("j")) {
111                                 m_projName = line.getOptionValue("j");
112                         }
113                         if (line.hasOption("n")) {
114                                 m_buildNum = line.getOptionValue("n");
115                         }
116                         if (line.hasOption("o")) {
117                                 m_output = new File(line.getOptionValue("o"));
118                         }
119                         if (line.hasOption("p")) {
120                                 m_config.setDbPass(line.getOptionValue("p"));
121                         }
122                         m_removeSchema = line.hasOption("drop-tables");
123                         if (line.hasOption("t")) {
124                                 m_config.setDbPort(Integer.parseInt(line.getOptionValue("t")));
125                         }
126                         if (line.hasOption("u")) {
127                                 m_config.setDbUser(line.getOptionValue("u"));
128                         }
129                 } 
130                 catch (ParseException exc) {
131                         usage(pw, opt);
132                         return false;
133                 }
134                 
135                 return true;
136         }
137         
138         void usage(PrintWriter pw, Options opt) {
139                 HelpFormatter help = new HelpFormatter();
140                 help.printHelp(pw, 80, getClass().getName(), "", opt, 0, 0, "", true);
141         }
142         
143         String trans(String key) {
144                 return m_bundle.get(key);
145         }
146         
147         String getenv(String varName) {
148                 // This is a separate function so that we can override it at unit test time
149                 return System.getenv(varName);
150         }
151         
152         String getProperty(String propName) {
153                 // This is a separate function so that we can override it at unit test time
154                 return System.getProperty(propName);
155         }
156         
157         File getFindBugsDir() {
158                 return (null != m_fbDir) ? m_fbDir : new File(".");
159         }
160         
161         void initArgs() {
162                 String findBugsDir = getenv("FINDBUGS_HOME");
163                 if (null != findBugsDir) {
164                         m_fbDir = new File(findBugsDir);
165                 }
166                 findBugsDir = getProperty("findbugs.home");
167                 if (null != findBugsDir) {
168                         m_fbDir = new File(findBugsDir);
169                 }
170         }
171         
172         void readConfig() throws IOException {
173                 if (null != m_configFile) {
174                         m_config.readFile(m_configFile);
175                 }
176         }
177         
178         void doMain(PrintWriter pw, String[] args) throws SQLException, IOException, XmlParseException, SAXException, TypeMismatchException {
179                 initArgs();     // read environment and system properties
180                 if ( ! parseArgs(pw, args) ) {
181                         return;
182                 }
183                 readConfig();
184
185                 File findBugsDir = getFindBugsDir();
186                 File workDir = new File(".");
187                 MessageMap messageMap = new MessageMap();
188                 messageMap.load(findBugsDir, Locale.getDefault(Category.DISPLAY));
189                 
190                 try (Connection con = m_driver.connect(
191                                         m_config.getDbHost(), m_config.getDbPort(), 
192                                         m_config.getDbName(), 
193                                         m_config.getDbUser(), m_config.getDbPass())
194                         ) 
195                 {
196                         m_schema.setMessageMap(messageMap);
197                         
198                         if (m_removeSchema) {
199                                 m_schema.purge(con);
200                                 return;
201                         }
202                         m_schema.ensureDbInitialized(con);
203                         messageMap.loadIds(con, m_driver);
204                 }
205                 catch (SQLException exc) {
206                         reportUnableToConnect(pw, exc);
207                         return;
208                 }
209                 
210                 Analyzer analyzer = new Analyzer(messageMap);
211                 Analysis analysis = analyzer.analyze(pw, workDir, m_fbp, m_projName, m_buildNum);
212                 if (null == analysis) {
213                         pw.println(trans(CfbBundle.ANALYSIS_FAILED));
214                         return;
215                 }
216                 
217                 try (
218                                 Connection con = m_driver.connect(
219                                                 m_config.getDbHost(), m_config.getDbPort(), 
220                                                 m_config.getDbName(),
221                                                 m_config.getDbUser(), m_config.getDbPass()
222                                         )
223                         )
224                 {
225                         DbStore store = new DbStore(con, m_driver, messageMap.getColl());
226                         
227                         store.put(analysis);
228                         Analysis prior = store.getPrior(analysis);
229                         Delta delta = new Delta(prior, analysis);
230
231                         HtmlReport report = new HtmlReport(m_bundle, messageMap.getColl(), delta);
232                         if (null != m_output) {
233                                 report.write(m_output);
234                         }
235                         
236                         Notifier notifier = new Notifier(m_bundle, m_config);
237                         notifier.sendEmailIfNeeded(pw, report);
238                 }
239                 catch (StoreException exc) {
240                         exc.printStackTrace(pw);
241                         return;
242                 }
243                 catch (SQLException exc) {
244                         reportUnableToConnect(pw, exc);
245                         return;
246                 }
247         }
248
249         private void reportUnableToConnect(PrintWriter pw, SQLException exc) {
250                 String cannotConnectFormat = trans(CfbBundle.CANNOT_CONNECT);
251                 String cannotConnect = MessageFormat.format(cannotConnectFormat, 
252                                                                                         m_config.getDbHost(), 
253                                                                                         ""+m_config.getDbPort(), 
254                                                                                         m_config.getDbName(), 
255                                                                                         m_config.getDbUser()
256                                                                                 );
257                 exc.printStackTrace(pw);
258                 SQLException next = exc.getNextException();
259                 while (null != next) {
260                         next.printStackTrace(pw);
261                         next = next.getNextException();
262                 }
263                 pw.println(cannotConnect);
264         }
265         
266         public static void main(String[] args) {
267                 CFB cfb = new CFB(Locale.getDefault());
268                 
269                 try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out, Charset.defaultCharset()))) {
270                         cfb.doMain(pw, args);
271                         pw.flush();
272                 } catch (SQLException | IOException | XmlParseException | SAXException | TypeMismatchException exc) {
273                         exc.printStackTrace();
274                 }
275         }
276
277 }