Add unit tests for Config.java.
[cfb.git] / prod / net / jaekl / cfb / Config.java
1 package net.jaekl.cfb;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.PrintWriter;
7 import java.util.ArrayList;
8 import java.util.Arrays;
9 import java.util.List;
10 import java.util.Properties;
11
12 import net.jaekl.qd.util.FileIO;
13
14 import org.apache.commons.cli.CommandLine;
15 import org.apache.commons.cli.GnuParser;
16 import org.apache.commons.cli.Options;
17 import org.apache.commons.cli.ParseException;
18
19 public class Config {
20         private static final String FINDBUGS_HOME = "FindBugsHome";
21         private static final String MAIL_FROM = "mail.from";
22         private static final String MAIL_SMTP_HOST = "mail.smtp.host";
23         private static final String NOTIFY = "notify";
24         
25         File m_configProperties;
26
27         String m_dbName; // db name
28         File m_fbp;             // FindBugsProject file
29         File m_fbDir;   // Directory where FindBugs is installed
30         String m_host;  // db host
31         int m_port;             // db port
32         String m_user;  // db user
33         String m_pass;  // db password
34         String m_buildNum; // build number (version)
35         boolean m_removeSchema; // purge DB schema
36         File m_output;  // File to which we should write our output (report)
37         
38         String       m_mailFrom;
39         String       m_mailSmtpHost;
40         List<String> m_notify;
41         
42         Options m_options;
43         
44         public Config() {
45                 m_dbName = "CFB";
46                 m_fbp    = null;
47                 m_fbDir  = null;
48                 m_host = "localhost";
49                 m_port = 5432;
50                 m_pass = "";
51                 m_user = "user";
52                 m_buildNum = null;
53                 m_removeSchema = false;
54                 m_output = null;
55                 
56                 m_mailFrom = "findbugs@localhost";
57                 m_mailSmtpHost = "localhost";
58                 m_notify = new ArrayList<String>();
59                 
60                 m_options = createOptions();
61         }
62         
63         public String getMailFrom() { return m_mailFrom; }
64         public String getMailSmtpHost() { return m_mailSmtpHost; }
65         public ArrayList<String> getNotify() { return new ArrayList<String>(m_notify); }
66         
67         public void readFile(File configProperties) throws IOException
68         {
69                 Properties props = new Properties();
70                 InputStream is = null;
71                 
72                 try {
73                         is = FileIO.getInst().openInput(configProperties);
74                         props.load(is);
75                 }
76                 finally {
77                         if (null != is) {
78                                 is.close();
79                         }
80                 }
81                 
82                 if (props.containsKey(FINDBUGS_HOME)) {
83                         m_fbDir = new File(props.getProperty(FINDBUGS_HOME));
84                 }
85                 if (props.containsKey(MAIL_FROM)) {
86                         m_mailFrom = props.getProperty(MAIL_FROM);
87                 }
88                 if (props.containsKey(MAIL_SMTP_HOST)) {
89                         m_mailSmtpHost = props.getProperty(MAIL_SMTP_HOST);
90                 }
91                 if (props.containsKey(NOTIFY)) {
92                         String[] addresses = props.getProperty(NOTIFY).split(",");
93                         m_notify = Arrays.asList(addresses);
94                 }
95         }
96         
97         public Options createOptions() {
98                 Options opt = new Options();
99                 
100                 opt.addOption("c",  "config",      true,  "Properties configuration file");
101                 opt.addOption("d",  "dbname",      true,  "DB name");
102                 opt.addOption(null, "drop-tables", false, "Remove database schema (drop all data)");
103                 opt.addOption("f",  "fbp",         true,  "FindBugsProject file");
104                 opt.addOption("h",  "host",        true,  "DB hostname");
105                 opt.addOption("n",  "number",      true,  "Build number (version)");
106                 opt.addOption("o",  "outfile",     true,  "Output report filename");
107                 opt.addOption("p",  "pass",        true,  "DB password");
108                 opt.addOption("t",  "port",        true,  "DB port");
109                 opt.addOption("u",  "user",        true,  "DB username");
110                 
111                 return opt;
112         }
113         
114         public void parseArgs(PrintWriter pw, String[] args) throws ParseException
115         {
116                 assert(null != m_options);
117                 
118                 CommandLine line = new GnuParser().parse(m_options, args);
119                 if (line.hasOption("c")) {
120                         m_configProperties = new File(line.getOptionValue("c"));
121                 }
122                 if (line.hasOption("d")) {
123                         m_dbName = line.getOptionValue("d");
124                 }
125                 if (line.hasOption("f")) {
126                         m_fbp = new File(line.getOptionValue("f"));
127                 }
128                 if (line.hasOption("h")) {
129                         m_host = line.getOptionValue("h");
130                 }
131                 if (line.hasOption("n")) {
132                         m_buildNum = line.getOptionValue("n");
133                 }
134                 if (line.hasOption("o")) {
135                         m_output = new File(line.getOptionValue("o"));
136                 }
137                 if (line.hasOption("p")) {
138                         m_pass = line.getOptionValue("p");
139                 }
140                 m_removeSchema = line.hasOption("drop-tables");
141                 if (line.hasOption("t")) {
142                         m_port = Integer.parseInt(line.getOptionValue("t"));
143                 }
144                 if (line.hasOption("u")) {
145                         m_user = line.getOptionValue("u");
146                 }
147         }
148 }