3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertNotNull;
6 import static org.junit.Assert.assertTrue;
8 import java.io.ByteArrayInputStream;
9 import java.io.ByteArrayOutputStream;
10 import java.io.FileNotFoundException;
11 import java.io.IOException;
12 import java.io.OutputStreamWriter;
13 import java.io.PrintWriter;
14 import java.io.StringWriter;
15 import java.nio.charset.Charset;
16 import java.sql.Connection;
17 import java.sql.SQLException;
18 import java.util.Locale;
20 import net.jaekl.cfb.analyze.Analysis;
21 import net.jaekl.cfb.analyze.FBMsgFileNotFoundException;
22 import net.jaekl.cfb.analyze.MessageMap;
23 import net.jaekl.cfb.db.CfbSchema;
24 import net.jaekl.cfb.db.Column;
25 import net.jaekl.cfb.db.Condition;
26 import net.jaekl.cfb.db.Operation;
27 import net.jaekl.cfb.db.Row;
28 import net.jaekl.cfb.db.Table;
29 import net.jaekl.cfb.db.TypeMismatchException;
30 import net.jaekl.cfb.db.driver.DbDriverMock;
31 import net.jaekl.cfb.util.Command;
32 import net.jaekl.cfb.util.EnvMock;
33 import net.jaekl.cfb.xml.MessagesXmlData;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.xml.sax.InputSource;
38 import org.xml.sax.SAXException;
40 public class CFBTest {
41 private CFBMock m_cfb;
42 private DbDriverMock m_driver;
44 private static final String BUG_COLLECTION_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
45 + "<BugCollection version=\"2.0.3\" sequence=\"0\" timestamp=\"1425655198000\" analysisTimestamp=\"1451305502231\" release=\"\">"
46 + "<Project projectName=\"JUnit\">"
47 + "<Jar>/data/prog/findbugs-3.0.1/lib/junit.jar</Jar>"
49 + "<BugInstance type=\"VO_VOLATILE_INCREMENT\" priority=\"2\" abbrev=\"VO\" category=\"MT_CORRECTNESS\">"
50 + "<Class classname=\"junit.extensions.ActiveTestSuite\">"
51 + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
53 + "<Method classname=\"junit.extensions.ActiveTestSuite\" name=\"runFinished\" signature=\"()V\" isStatic=\"false\">"
54 + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" start=\"67\" end=\"69\" startBytecode=\"0\" endBytecode=\"64\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
56 + "<Field classname=\"junit.extensions.ActiveTestSuite\" name=\"fActiveTestDeathCount\" signature=\"I\" isStatic=\"false\">"
57 + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
59 + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" start=\"67\" end=\"67\" startBytecode=\"7\" endBytecode=\"7\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
61 + "<BugInstance type=\"DM_NUMBER_CTOR\" priority=\"2\" abbrev=\"Bx\" category=\"PERFORMANCE\">"
62 + "<Class classname=\"junit.framework.Assert\">"
63 + "<SourceLine classname=\"junit.framework.Assert\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
65 + "<Method classname=\"junit.framework.Assert\" name=\"assertEquals\" signature=\"(Ljava/lang/String;BB)V\" isStatic=\"true\">"
66 + "<SourceLine classname=\"junit.framework.Assert\" start=\"156\" end=\"157\" startBytecode=\"0\" endBytecode=\"86\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
68 + "<Method classname=\"java.lang.Byte\" name=\"<init>\" signature=\"(B)V\" isStatic=\"false\" role=\"METHOD_CALLED\">"
69 + "<SourceLine classname=\"java.lang.Byte\" start=\"307\" end=\"309\" startBytecode=\"0\" endBytecode=\"41\" sourcefile=\"Byte.java\" sourcepath=\"java/lang/Byte.java\"/>"
71 + "<Method classname=\"java.lang.Byte\" name=\"valueOf\" signature=\"(B)Ljava/lang/Byte;\" isStatic=\"true\" role=\"SHOULD_CALL\">"
72 + "<SourceLine classname=\"java.lang.Byte\" start=\"87\" end=\"87\" startBytecode=\"0\" endBytecode=\"33\" sourcefile=\"Byte.java\" sourcepath=\"java/lang/Byte.java\"/>"
74 + "<SourceLine classname=\"junit.framework.Assert\" start=\"156\" end=\"156\" startBytecode=\"6\" endBytecode=\"6\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
76 + "<BugInstance type=\"DM_NUMBER_CTOR\" priority=\"2\" abbrev=\"Bx\" category=\"PERFORMANCE\">"
77 + "<Class classname=\"junit.framework.Assert\">"
78 + "<SourceLine classname=\"junit.framework.Assert\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
80 + "<Method classname=\"junit.framework.Assert\" name=\"assertEquals\" signature=\"(Ljava/lang/String;CC)V\" isStatic=\"true\">"
81 + "<SourceLine classname=\"junit.framework.Assert\" start=\"169\" end=\"170\" startBytecode=\"0\" endBytecode=\"86\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
83 + "<Method classname=\"java.lang.Character\" name=\"<init>\" signature=\"(C)V\" isStatic=\"false\" role=\"METHOD_CALLED\">"
84 + "<SourceLine classname=\"java.lang.Character\" start=\"2056\" end=\"2058\" startBytecode=\"0\" endBytecode=\"41\" sourcefile=\"Character.java\" sourcepath=\"java/lang/Character.java\"/>"
86 + "<Method classname=\"java.lang.Character\" name=\"valueOf\" signature=\"(C)Ljava/lang/Character;\" isStatic=\"true\" role=\"SHOULD_CALL\">"
87 + "<SourceLine classname=\"java.lang.Character\" start=\"2085\" end=\"2088\" startBytecode=\"0\" endBytecode=\"52\" sourcefile=\"Character.java\" sourcepath=\"java/lang/Character.java\"/>"
89 + "<SourceLine classname=\"junit.framework.Assert\" start=\"169\" end=\"169\" startBytecode=\"6\" endBytecode=\"6\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
93 private Analysis analysisFromXml(String xml, String projectName, String version)
94 throws FileNotFoundException, IOException, SAXException
96 Analysis analysis = new Analysis(projectName, version);
97 ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes(Command.UTF_8));
98 InputSource inputSource = new InputSource(bais);
99 analysis.parse(inputSource);
107 m_driver = new DbDriverMock();
108 m_cfb = new CFBMock(Locale.getDefault(), m_driver);
112 public void testParseArgs_noParams() throws IOException
114 StringWriter sw = new StringWriter();
115 PrintWriter pw = new PrintWriter(sw);
118 boolean result = m_cfb.parseArgs(pw, args);
123 assertEquals("[must.specify.fbp.file]\n[invoke.with.help.for.help]\n", sw.toString());
127 public void testParseArgs_dropTables() throws IOException
129 StringWriter sw = new StringWriter();
130 PrintWriter pw = new PrintWriter(sw);
131 String[] args = { "--drop-tables" };
133 boolean result = m_cfb.parseArgs(pw, args);
138 assertEquals("", sw.toString());
142 public void testParseArgs_invalidParam() throws IOException
144 StringWriter sw = new StringWriter();
145 PrintWriter pw = new PrintWriter(sw);
146 String[] args = {"--xyzzyaoeuidhtnsl"};
148 boolean result = m_cfb.parseArgs(pw, args);
151 String actual = sw.toString();
154 assertTrue(actual.contains("usage"));
155 assertTrue(actual.contains("--help"));
159 public void testStoreAndReport_noPrior() throws IOException, SQLException, SAXException, TypeMismatchException
161 final String PROJECT_NAME = "ProjectName";
162 final String VERSION = "1.0.1";
165 ByteArrayOutputStream baos = new ByteArrayOutputStream();
166 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, Charset.forName(Command.UTF_8)));
169 MessageMap msgMap = new MessageMap();
170 msgMap.parse(new InputSource(new ByteArrayInputStream(MessagesXmlData.XML.getBytes(Command.UTF_8))));
172 Analysis analysis = analysisFromXml(BUG_COLLECTION_XML, PROJECT_NAME, VERSION);
174 m_cfb.ensureDbInitialized(pw, msgMap);
175 m_cfb.storeAndReport(pw, msgMap, analysis);
178 Connection con = m_driver.connect("host", 1234, "dbName", "dbUser", "dbPass");
179 Column[] columns = { CfbSchema.RUNID, CfbSchema.PROJNAME, CfbSchema.VERSION };
180 Table[] tables = { CfbSchema.RUNS };
181 Condition[] conditions = { new Condition(CfbSchema.RUNID, Operation.NOT_NULL) };
183 Row row = m_driver.selectExactlyOne(con, columns, tables, conditions);
185 assertEquals(PROJECT_NAME, row.getValue(1));
186 assertEquals(VERSION, row.getValue(2));
190 public void testTrans()
193 { "hello", new Object[] {}, "[hello]" },
194 { "hello", new Object[] {"world"}, "[hello][world]"}
197 for (Object[] datum : data) {
198 String key = (String)datum[0];
199 Object[] params = (Object[])datum[1];
200 String expected = (String)datum[2];
201 String actual = m_cfb.trans(key, params);
202 assertEquals(expected, actual);
207 public void testReportException_FBMsgFileNotFoundException() throws IOException
211 "/home/fred/findbugs3.0.1", "messages.xml",
212 "[cannot.load.fbmsg.file][messages.xml]\n[findbugs.home.is.set.to][FINDBUGS_HOME][/home/fred/findbugs3.0.1]\n"
215 null, "messages.xml",
216 "[cannot.load.fbmsg.file][messages.xml]\n[findbugs.home.is.not.set][FINDBUGS_HOME]\n"
219 null, null, "[cannot.load.fbmsg.file][null]\n[findbugs.home.is.not.set][FINDBUGS_HOME]\n"
224 EnvMock envMock = EnvMock.mock_putInstance();
226 for (String[] datum : data)
228 String fbHome = datum[0];
229 String filename = datum[1];
230 String expected = datum[2];
233 StringWriter sw = new StringWriter();
234 PrintWriter pw = new PrintWriter(sw);
237 envMock.mock_putEnv(CFB.FINDBUGS_HOME, fbHome);
238 FBMsgFileNotFoundException exc = new FBMsgFileNotFoundException(filename);
239 m_cfb.reportException(pw, exc);
244 String actual = sw.toString();
245 boolean pass = actual.endsWith(expected);
247 System.out.println("Expected:\n" + expected + "\nActual:\n" + actual);
254 EnvMock.mock_resetInstance();