3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
6 import java.io.ByteArrayInputStream;
7 import java.io.ByteArrayOutputStream;
8 import java.io.FileNotFoundException;
9 import java.io.IOException;
10 import java.io.OutputStreamWriter;
11 import java.io.PrintWriter;
12 import java.nio.charset.Charset;
13 import java.sql.Connection;
14 import java.sql.SQLException;
15 import java.util.Locale;
17 import net.jaekl.cfb.analyze.Analysis;
18 import net.jaekl.cfb.analyze.MessageMap;
19 import net.jaekl.cfb.db.CfbSchema;
20 import net.jaekl.cfb.db.Column;
21 import net.jaekl.cfb.db.Condition;
22 import net.jaekl.cfb.db.Operation;
23 import net.jaekl.cfb.db.Row;
24 import net.jaekl.cfb.db.Table;
25 import net.jaekl.cfb.db.TypeMismatchException;
26 import net.jaekl.cfb.db.driver.DbDriverMock;
27 import net.jaekl.cfb.util.Command;
28 import net.jaekl.cfb.xml.MessagesXmlData;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.xml.sax.InputSource;
33 import org.xml.sax.SAXException;
35 public class CFBTest {
36 private CFBMock m_cfb;
37 private DbDriverMock m_driver;
39 private static final String BUG_COLLECTION_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
40 + "<BugCollection version=\"2.0.3\" sequence=\"0\" timestamp=\"1425655198000\" analysisTimestamp=\"1451305502231\" release=\"\">"
41 + "<Project projectName=\"JUnit\">"
42 + "<Jar>/data/prog/findbugs-3.0.1/lib/junit.jar</Jar>"
44 + "<BugInstance type=\"VO_VOLATILE_INCREMENT\" priority=\"2\" abbrev=\"VO\" category=\"MT_CORRECTNESS\">"
45 + "<Class classname=\"junit.extensions.ActiveTestSuite\">"
46 + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
48 + "<Method classname=\"junit.extensions.ActiveTestSuite\" name=\"runFinished\" signature=\"()V\" isStatic=\"false\">"
49 + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" start=\"67\" end=\"69\" startBytecode=\"0\" endBytecode=\"64\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
51 + "<Field classname=\"junit.extensions.ActiveTestSuite\" name=\"fActiveTestDeathCount\" signature=\"I\" isStatic=\"false\">"
52 + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
54 + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" start=\"67\" end=\"67\" startBytecode=\"7\" endBytecode=\"7\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
56 + "<BugInstance type=\"DM_NUMBER_CTOR\" priority=\"2\" abbrev=\"Bx\" category=\"PERFORMANCE\">"
57 + "<Class classname=\"junit.framework.Assert\">"
58 + "<SourceLine classname=\"junit.framework.Assert\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
60 + "<Method classname=\"junit.framework.Assert\" name=\"assertEquals\" signature=\"(Ljava/lang/String;BB)V\" isStatic=\"true\">"
61 + "<SourceLine classname=\"junit.framework.Assert\" start=\"156\" end=\"157\" startBytecode=\"0\" endBytecode=\"86\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
63 + "<Method classname=\"java.lang.Byte\" name=\"<init>\" signature=\"(B)V\" isStatic=\"false\" role=\"METHOD_CALLED\">"
64 + "<SourceLine classname=\"java.lang.Byte\" start=\"307\" end=\"309\" startBytecode=\"0\" endBytecode=\"41\" sourcefile=\"Byte.java\" sourcepath=\"java/lang/Byte.java\"/>"
66 + "<Method classname=\"java.lang.Byte\" name=\"valueOf\" signature=\"(B)Ljava/lang/Byte;\" isStatic=\"true\" role=\"SHOULD_CALL\">"
67 + "<SourceLine classname=\"java.lang.Byte\" start=\"87\" end=\"87\" startBytecode=\"0\" endBytecode=\"33\" sourcefile=\"Byte.java\" sourcepath=\"java/lang/Byte.java\"/>"
69 + "<SourceLine classname=\"junit.framework.Assert\" start=\"156\" end=\"156\" startBytecode=\"6\" endBytecode=\"6\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
71 + "<BugInstance type=\"DM_NUMBER_CTOR\" priority=\"2\" abbrev=\"Bx\" category=\"PERFORMANCE\">"
72 + "<Class classname=\"junit.framework.Assert\">"
73 + "<SourceLine classname=\"junit.framework.Assert\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
75 + "<Method classname=\"junit.framework.Assert\" name=\"assertEquals\" signature=\"(Ljava/lang/String;CC)V\" isStatic=\"true\">"
76 + "<SourceLine classname=\"junit.framework.Assert\" start=\"169\" end=\"170\" startBytecode=\"0\" endBytecode=\"86\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
78 + "<Method classname=\"java.lang.Character\" name=\"<init>\" signature=\"(C)V\" isStatic=\"false\" role=\"METHOD_CALLED\">"
79 + "<SourceLine classname=\"java.lang.Character\" start=\"2056\" end=\"2058\" startBytecode=\"0\" endBytecode=\"41\" sourcefile=\"Character.java\" sourcepath=\"java/lang/Character.java\"/>"
81 + "<Method classname=\"java.lang.Character\" name=\"valueOf\" signature=\"(C)Ljava/lang/Character;\" isStatic=\"true\" role=\"SHOULD_CALL\">"
82 + "<SourceLine classname=\"java.lang.Character\" start=\"2085\" end=\"2088\" startBytecode=\"0\" endBytecode=\"52\" sourcefile=\"Character.java\" sourcepath=\"java/lang/Character.java\"/>"
84 + "<SourceLine classname=\"junit.framework.Assert\" start=\"169\" end=\"169\" startBytecode=\"6\" endBytecode=\"6\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
88 private Analysis analysisFromXml(String xml, String projectName, String version)
89 throws FileNotFoundException, IOException, SAXException
91 Analysis analysis = new Analysis(projectName, version);
92 ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes(Command.UTF_8));
93 InputSource inputSource = new InputSource(bais);
94 analysis.parse(inputSource);
102 m_driver = new DbDriverMock();
103 m_cfb = new CFBMock(Locale.getDefault(), m_driver);
107 public void testStoreAndReport_noPrior() throws IOException, SQLException, SAXException, TypeMismatchException
109 final String PROJECT_NAME = "ProjectName";
110 final String VERSION = "1.0.1";
113 ByteArrayOutputStream baos = new ByteArrayOutputStream();
114 PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, Charset.forName(Command.UTF_8)));
117 MessageMap msgMap = new MessageMap();
118 msgMap.parse(new InputSource(new ByteArrayInputStream(MessagesXmlData.XML.getBytes(Command.UTF_8))));
120 Analysis analysis = analysisFromXml(BUG_COLLECTION_XML, PROJECT_NAME, VERSION);
122 m_cfb.ensureDbInitialized(pw, msgMap);
123 m_cfb.storeAndReport(pw, msgMap, analysis);
126 Connection con = m_driver.connect("host", 1234, "dbName", "dbUser", "dbPass");
127 Column[] columns = { CfbSchema.RUNID, CfbSchema.PROJNAME, CfbSchema.VERSION };
128 Table[] tables = { CfbSchema.RUNS };
129 Condition[] conditions = { new Condition(CfbSchema.RUNID, Operation.NOT_NULL) };
131 Row row = m_driver.selectExactlyOne(con, columns, tables, conditions);
133 assertEquals(PROJECT_NAME, row.getValue(1));
134 assertEquals(VERSION, row.getValue(2));