Further unit tests: CFB and Delta
[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         volatile static CfbBundle m_bundle = null;      
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 = null;
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         CfbBundle getBundle() {
76                 CfbBundle bundle = m_bundle;
77                 if (null == bundle) {
78                         synchronized(CFB.class) {
79                                 if (null == m_bundle) {
80                                         m_bundle = bundle = CfbBundle.getInst(m_locale);
81                                 }
82                         }
83                 }
84                 return bundle;
85         }
86         
87         Options createOptions() {
88                 Options opt = new Options();
89                 
90                 opt.addOption("c",  "config",      true,  "Properties configuration file");
91                 opt.addOption("d",  "dbname",      true,  "DB name");
92                 opt.addOption(null, "drop-tables", false, "Remove database schema (drop all data)");
93                 opt.addOption("f",  "fbp",         true,  "FindBugsProject file");
94                 opt.addOption("h",  "host",        true,  "DB hostname");
95                 opt.addOption("j",  "project",     true,  "proJect name");
96                 opt.addOption("n",  "number",      true,  "Build number (version)");
97                 opt.addOption("o",  "outfile",     true,  "Output report filename");
98                 opt.addOption("p",  "pass",        true,  "DB password");
99                 opt.addOption("t",  "port",        true,  "DB port");
100                 opt.addOption("u",  "user",        true,  "DB username");
101                 
102                 return opt;
103         }
104         
105         boolean parseArgs(PrintWriter pw, String[] args) {
106                 Options opt = createOptions();
107                 
108                 try {
109                         CommandLine line = new GnuParser().parse(opt, args);
110                         if (line.hasOption("c")) {
111                                 m_configFile = new File(line.getOptionValue("c"));
112                         }
113                         if (line.hasOption("d")) {
114                                 m_config.setDbName(line.getOptionValue("d"));
115                         }
116                         if (line.hasOption("f")) {
117                                 m_fbp = new File(line.getOptionValue("f"));
118                         }
119                         if (line.hasOption("h")) {
120                                 m_config.setDbHost(line.getOptionValue("h"));
121                         }
122                         if (line.hasOption("j")) {
123                                 m_projName = line.getOptionValue("j");
124                         }
125                         if (line.hasOption("n")) {
126                                 m_buildNum = line.getOptionValue("n");
127                         }
128                         if (line.hasOption("o")) {
129                                 m_output = new File(line.getOptionValue("o"));
130                         }
131                         if (line.hasOption("p")) {
132                                 m_config.setDbPass(line.getOptionValue("p"));
133                         }
134                         m_removeSchema = line.hasOption("drop-tables");
135                         if (line.hasOption("t")) {
136                                 m_config.setDbPort(Integer.parseInt(line.getOptionValue("t")));
137                         }
138                         if (line.hasOption("u")) {
139                                 m_config.setDbUser(line.getOptionValue("u"));
140                         }
141                 } 
142                 catch (ParseException exc) {
143                         usage(pw, opt);
144                         return false;
145                 }
146                 
147                 return true;
148         }
149         
150         void usage(PrintWriter pw, Options opt) {
151                 HelpFormatter help = new HelpFormatter();
152                 help.printHelp(pw, 80, getClass().getName(), "", opt, 0, 0, "", true);
153         }
154         
155         String trans(String key) {
156                 return getBundle().get(key);
157         }
158         
159         String getenv(String varName) {
160                 // This is a separate function so that we can override it at unit test time
161                 return System.getenv(varName);
162         }
163         
164         String getProperty(String propName) {
165                 // This is a separate function so that we can override it at unit test time
166                 return System.getProperty(propName);
167         }
168         
169         File getFindBugsDir() {
170                 return (null != m_fbDir) ? m_fbDir : new File(".");
171         }
172         
173         void initArgs() {
174                 String findBugsDir = getenv("FINDBUGS_HOME");
175                 if (null != findBugsDir) {
176                         m_fbDir = new File(findBugsDir);
177                 }
178                 findBugsDir = getProperty("findbugs.home");
179                 if (null != findBugsDir) {
180                         m_fbDir = new File(findBugsDir);
181                 }
182         }
183         
184         void readConfig() throws IOException {
185                 if (null != m_configFile) {
186                         m_config.readFile(m_configFile);
187                 }
188         }
189         
190         void doMain(PrintWriter pw, String[] args) throws SQLException, IOException, XmlParseException, SAXException, TypeMismatchException {
191                 initArgs();     // read environment and system properties
192                 if ( ! parseArgs(pw, args) ) {
193                         return;
194                 }
195                 readConfig();
196
197                 File findBugsDir = getFindBugsDir();
198                 File workDir = new File(".");
199                 MessageMap messageMap = new MessageMap();
200                 messageMap.load(findBugsDir, Locale.getDefault(Category.DISPLAY));
201                 
202                 if (!ensureDbInitialized(pw, messageMap)) {
203                         return;
204                 }
205                 
206                 Analyzer analyzer = new Analyzer(messageMap);
207                 Analysis analysis = analyzer.analyze(pw, workDir, m_fbp, m_projName, m_buildNum);
208                 if (null == analysis) {
209                         pw.println(trans(CfbBundle.ANALYSIS_FAILED));
210                         return;
211                 }
212                 
213                 storeAndReport(pw, messageMap, analysis);
214         }
215
216         boolean ensureDbInitialized(PrintWriter pw, MessageMap messageMap)
217                         throws TypeMismatchException 
218         {
219                 try (Connection con = m_driver.connect(
220                                         m_config.getDbHost(), m_config.getDbPort(), 
221                                         m_config.getDbName(), 
222                                         m_config.getDbUser(), m_config.getDbPass())
223                         ) 
224                 {
225                         m_schema.setMessageMap(messageMap);
226                         
227                         if (m_removeSchema) {
228                                 m_schema.purge(con);
229                                 return false;   // do not continue execution
230                         }
231                         m_schema.ensureDbInitialized(con);
232                         messageMap.loadIds(con, m_driver);
233                 }
234                 catch (SQLException exc) {
235                         reportUnableToConnect(pw, exc);
236                         return false;   // do not continue execution
237                 }
238                 
239                 return true;    // all OK; continue execution
240         }
241
242         void storeAndReport(PrintWriter pw, MessageMap messageMap, Analysis analysis) 
243                         throws TypeMismatchException, IOException 
244         {
245                 try (
246                                 Connection con = m_driver.connect(
247                                                 m_config.getDbHost(), m_config.getDbPort(), 
248                                                 m_config.getDbName(),
249                                                 m_config.getDbUser(), m_config.getDbPass()
250                                         )
251                         )
252                 {
253                         DbStore store = new DbStore(con, m_driver, messageMap.getColl());
254                         
255                         store.put(analysis);
256                         Analysis prior = store.getPrior(analysis);
257                         Delta delta = new Delta(prior, analysis);
258
259                         HtmlReport report = new HtmlReport(getBundle(), messageMap.getColl(), delta);
260                         if (null != m_output) {
261                                 report.write(m_output);
262                         }
263                         
264                         Notifier notifier = new Notifier(getBundle(), m_config);
265                         notifier.sendEmailIfNeeded(pw, report);
266                 }
267                 catch (StoreException exc) {
268                         exc.printStackTrace(pw);
269                 }
270                 catch (SQLException exc) {
271                         reportUnableToConnect(pw, exc);
272                 }
273         }
274
275         private void reportUnableToConnect(PrintWriter pw, SQLException exc) {
276                 String cannotConnectFormat = trans(CfbBundle.CANNOT_CONNECT);
277                 String cannotConnect = MessageFormat.format(cannotConnectFormat, 
278                                                                                         m_config.getDbHost(), 
279                                                                                         ""+m_config.getDbPort(), 
280                                                                                         m_config.getDbName(), 
281                                                                                         m_config.getDbUser()
282                                                                                 );
283                 exc.printStackTrace(pw);
284                 SQLException next = exc.getNextException();
285                 while (null != next) {
286                         next.printStackTrace(pw);
287                         next = next.getNextException();
288                 }
289                 pw.println(cannotConnect);
290         }
291         
292         public static void main(String[] args) {
293                 CFB cfb = new CFB(Locale.getDefault());
294                 
295                 try (PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out, Charset.defaultCharset()))) {
296                         cfb.doMain(pw, args);
297                         pw.flush();
298                 } catch (SQLException | IOException | XmlParseException | SAXException | TypeMismatchException exc) {
299                         exc.printStackTrace();
300                 }
301         }
302
303 }