1 package net.jaekl.cfb.store;
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.assertNull;
7 import static org.junit.Assert.assertTrue;
8 import static org.junit.Assert.fail;
10 import java.io.ByteArrayInputStream;
11 import java.io.FileNotFoundException;
12 import java.io.IOException;
13 import java.io.UnsupportedEncodingException;
14 import java.nio.charset.Charset;
15 import java.sql.SQLException;
16 import java.util.Date;
18 import net.jaekl.cfb.analyze.Analysis;
19 import net.jaekl.cfb.analyze.MessageMap;
20 import net.jaekl.cfb.db.CfbSchema;
21 import net.jaekl.cfb.db.TypeMismatchException;
22 import net.jaekl.cfb.db.driver.ConnectionMock;
23 import net.jaekl.cfb.db.driver.DbDriverMock;
24 import net.jaekl.cfb.util.Command;
25 import net.jaekl.cfb.xml.BugCollection;
26 import net.jaekl.cfb.xml.MessagesXmlData;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.xml.sax.InputSource;
31 import org.xml.sax.SAXException;
33 public class DbStoreTest {
34 private static final String BUG_COLLECTION_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
35 + "<BugCollection version=\"2.0.3\" sequence=\"0\" timestamp=\"1425655198000\" analysisTimestamp=\"1451305502231\" release=\"\">"
36 + "<Project projectName=\"JUnit\">"
37 + "<Jar>/data/prog/findbugs-3.0.1/lib/junit.jar</Jar>"
39 + "<BugInstance type=\"VO_VOLATILE_INCREMENT\" priority=\"2\" abbrev=\"VO\" category=\"MT_CORRECTNESS\">"
40 + "<Class classname=\"junit.extensions.ActiveTestSuite\">"
41 + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
43 + "<Method classname=\"junit.extensions.ActiveTestSuite\" name=\"runFinished\" signature=\"()V\" isStatic=\"false\">"
44 + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" start=\"67\" end=\"69\" startBytecode=\"0\" endBytecode=\"64\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
46 + "<Field classname=\"junit.extensions.ActiveTestSuite\" name=\"fActiveTestDeathCount\" signature=\"I\" isStatic=\"false\">"
47 + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
49 + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" start=\"67\" end=\"67\" startBytecode=\"7\" endBytecode=\"7\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
51 + "<BugInstance type=\"DM_NUMBER_CTOR\" priority=\"2\" abbrev=\"Bx\" category=\"PERFORMANCE\">"
52 + "<Class classname=\"junit.framework.Assert\">"
53 + "<SourceLine classname=\"junit.framework.Assert\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
55 + "<Method classname=\"junit.framework.Assert\" name=\"assertEquals\" signature=\"(Ljava/lang/String;BB)V\" isStatic=\"true\">"
56 + "<SourceLine classname=\"junit.framework.Assert\" start=\"156\" end=\"157\" startBytecode=\"0\" endBytecode=\"86\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
58 + "<Method classname=\"java.lang.Byte\" name=\"<init>\" signature=\"(B)V\" isStatic=\"false\" role=\"METHOD_CALLED\">"
59 + "<SourceLine classname=\"java.lang.Byte\" start=\"307\" end=\"309\" startBytecode=\"0\" endBytecode=\"41\" sourcefile=\"Byte.java\" sourcepath=\"java/lang/Byte.java\"/>"
61 + "<Method classname=\"java.lang.Byte\" name=\"valueOf\" signature=\"(B)Ljava/lang/Byte;\" isStatic=\"true\" role=\"SHOULD_CALL\">"
62 + "<SourceLine classname=\"java.lang.Byte\" start=\"87\" end=\"87\" startBytecode=\"0\" endBytecode=\"33\" sourcefile=\"Byte.java\" sourcepath=\"java/lang/Byte.java\"/>"
64 + "<SourceLine classname=\"junit.framework.Assert\" start=\"156\" end=\"156\" startBytecode=\"6\" endBytecode=\"6\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
66 + "<BugInstance type=\"DM_NUMBER_CTOR\" priority=\"2\" abbrev=\"Bx\" category=\"PERFORMANCE\">"
67 + "<Class classname=\"junit.framework.Assert\">"
68 + "<SourceLine classname=\"junit.framework.Assert\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
70 + "<Method classname=\"junit.framework.Assert\" name=\"assertEquals\" signature=\"(Ljava/lang/String;CC)V\" isStatic=\"true\">"
71 + "<SourceLine classname=\"junit.framework.Assert\" start=\"169\" end=\"170\" startBytecode=\"0\" endBytecode=\"86\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
73 + "<Method classname=\"java.lang.Character\" name=\"<init>\" signature=\"(C)V\" isStatic=\"false\" role=\"METHOD_CALLED\">"
74 + "<SourceLine classname=\"java.lang.Character\" start=\"2056\" end=\"2058\" startBytecode=\"0\" endBytecode=\"41\" sourcefile=\"Character.java\" sourcepath=\"java/lang/Character.java\"/>"
76 + "<Method classname=\"java.lang.Character\" name=\"valueOf\" signature=\"(C)Ljava/lang/Character;\" isStatic=\"true\" role=\"SHOULD_CALL\">"
77 + "<SourceLine classname=\"java.lang.Character\" start=\"2085\" end=\"2088\" startBytecode=\"0\" endBytecode=\"52\" sourcefile=\"Character.java\" sourcepath=\"java/lang/Character.java\"/>"
79 + "<SourceLine classname=\"junit.framework.Assert\" start=\"169\" end=\"169\" startBytecode=\"6\" endBytecode=\"6\" sourcefile=\"Assert.java\" sourcepath=\"junit/framework/Assert.java\"/>"
83 private static final String UNKNOWN_BUG_CATEGORY_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
84 + "<BugCollection version=\"2.0.3\" sequence=\"0\" timestamp=\"1425655198000\" analysisTimestamp=\"1451305502231\" release=\"\">"
85 + "<Project projectName=\"JUnit\">"
86 + "<Jar>/data/prog/findbugs-3.0.1/lib/junit.jar</Jar>"
88 + "<BugInstance type=\"VO_VOLATILE_INCREMENT\" priority=\"2\" abbrev=\"VO\" category=\"NON_EXTANT\">"
89 + "<Class classname=\"junit.extensions.ActiveTestSuite\">"
90 + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
92 + "<Method classname=\"junit.extensions.ActiveTestSuite\" name=\"runFinished\" signature=\"()V\" isStatic=\"false\">"
93 + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" start=\"67\" end=\"69\" startBytecode=\"0\" endBytecode=\"64\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
95 + "<Field classname=\"junit.extensions.ActiveTestSuite\" name=\"fActiveTestDeathCount\" signature=\"I\" isStatic=\"false\">"
96 + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
98 + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" start=\"67\" end=\"67\" startBytecode=\"7\" endBytecode=\"7\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
100 + "</BugCollection>";
102 private static final String UNKNOWN_BUG_PATTERN_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
103 + "<BugCollection version=\"2.0.3\" sequence=\"0\" timestamp=\"1425655198000\" analysisTimestamp=\"1451305502231\" release=\"\">"
104 + "<Project projectName=\"JUnit\">"
105 + "<Jar>/data/prog/findbugs-3.0.1/lib/junit.jar</Jar>"
107 + "<BugInstance type=\"DN_DOES_NOT_EXIST\" priority=\"2\" abbrev=\"DN\" category=\"MT_CORRECTNESS\">"
108 + "<Class classname=\"junit.extensions.ActiveTestSuite\">"
109 + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
111 + "<Method classname=\"junit.extensions.ActiveTestSuite\" name=\"runFinished\" signature=\"()V\" isStatic=\"false\">"
112 + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" start=\"67\" end=\"69\" startBytecode=\"0\" endBytecode=\"64\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
114 + "<Field classname=\"junit.extensions.ActiveTestSuite\" name=\"fActiveTestDeathCount\" signature=\"I\" isStatic=\"false\">"
115 + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
117 + "<SourceLine classname=\"junit.extensions.ActiveTestSuite\" start=\"67\" end=\"67\" startBytecode=\"7\" endBytecode=\"7\" sourcefile=\"ActiveTestSuite.java\" sourcepath=\"junit/extensions/ActiveTestSuite.java\"/>"
119 + "</BugCollection>";
121 private DbStore m_store;
124 public void setUp() throws SQLException, FileNotFoundException, UnsupportedEncodingException, IOException, SAXException
126 MessageMap msgMap = new MessageMap();
127 msgMap.parse(new InputSource(new ByteArrayInputStream(MessagesXmlData.XML.getBytes("UTF-8"))));
129 DbDriverMock driver = new DbDriverMock();
130 CfbSchema schema = new CfbSchema(driver);
131 ConnectionMock con = new ConnectionMock();
133 schema.setMessageMap(msgMap);
134 schema.ensureDbInitialized(con);
136 m_store = new DbStore(con, driver, msgMap.getColl());
140 public void testGetPrior_withNoEntries() throws SQLException, TypeMismatchException {
141 // First test: getPrior(null) should return null
142 Analysis actual = m_store.getPrior(null);
145 // Second test: getPrior(current) with no data in the DB should return null
146 String projName = "ProjectName";
147 String version = "1.2.3";
148 Date start = new Date(1234567890);
149 Date end = new Date(1234567900);
150 Analysis current = new Analysis(projName, version);
151 current.setStart(start);
153 actual = m_store.getPrior(current);
157 private BugCollection parseBugCollection(String xml) throws IOException, SAXException
159 Charset utf8 = Charset.forName(Command.UTF_8);
161 try ( ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes(utf8)))
163 InputSource inputSource = new InputSource(bais);
164 Analysis analysis = new Analysis(null, null);
165 analysis.parse(inputSource);
167 assertNotNull(analysis);
169 BugCollection bugColl = analysis.getBugCollection();
175 public void testPut_null() throws StoreException, SQLException, TypeMismatchException
177 // Adding null should return false, with no exception thrown
178 boolean result = m_store.put(null);
183 public void testPut_withUnknownPatternOrCategory() throws SQLException, TypeMismatchException, IOException, SAXException
185 final String[] data = { UNKNOWN_BUG_PATTERN_XML, UNKNOWN_BUG_CATEGORY_XML};
186 String projName = "ProjectName";
187 String firstVersion = "1.0.1";
188 Date firstStart = new Date(100);
189 Date firstEnd = new Date(200);
190 Analysis firstAnalysis = new Analysis(projName, firstVersion);
191 firstAnalysis.setStart(firstStart);
192 firstAnalysis.setEnd(firstEnd);
194 for (String xml : data) {
195 firstAnalysis.setBugCollection(parseBugCollection(xml));
198 m_store.put(firstAnalysis);
199 fail("Should have thrown an exception\n" + xml);
201 catch (StoreException exc) {
202 // This is the success path
208 public void testPut_thenGetPrior() throws SQLException, TypeMismatchException, IOException, SAXException, StoreException {
209 String projName = "ProjectName";
210 String firstVersion = "1.0.1";
211 Date firstStart = new Date(100);
212 Date firstEnd = new Date(200);
213 Analysis firstAnalysis = new Analysis(projName, firstVersion);
214 firstAnalysis.setStart(firstStart);
215 firstAnalysis.setEnd(firstEnd);
216 firstAnalysis.setBugCollection(parseBugCollection(BUG_COLLECTION_XML));
218 // Adding a valid Analysis object should return true
219 boolean result = m_store.put(firstAnalysis);
222 // Create a second Analysis object
223 String secondVersion = "1.0.2";
224 Date secondStart = new Date(2300);
225 Date secondEnd = new Date(2400);
226 Analysis secondAnalysis = new Analysis(projName, secondVersion);
227 secondAnalysis.setStart(secondStart);
228 secondAnalysis.setEnd(secondEnd);
230 // Retrieve the first Analysis object
231 Analysis priorAnalysis = m_store.getPrior(secondAnalysis);
232 assertNotNull(priorAnalysis);
233 assertEquals(firstAnalysis.getProjectName(), priorAnalysis.getProjectName());
234 assertEquals(firstAnalysis.getBuildNumber(), priorAnalysis.getBuildNumber());
235 assertEquals(firstAnalysis.getStart(), priorAnalysis.getStart());
236 assertEquals(firstAnalysis.getEnd(), priorAnalysis.getEnd());
237 assertEquals(firstAnalysis.getBugCollection(), priorAnalysis.getBugCollection());
241 public void testGetBugType() {
242 fail("Not yet implemented");
246 public void testGetCategoryName() {
247 fail("Not yet implemented");
251 public void testGetLoc() {
252 fail("Not yet implemented");
256 public void testGetLocId() {
257 fail("Not yet implemented");
261 public void testFindLocId() {
262 fail("Not yet implemented");
266 public void testStoreLoc() {
267 fail("Not yet implemented");
271 public void testGetVarIdBugInstance() {
272 fail("Not yet implemented");
276 public void testGetVar() {
277 fail("Not yet implemented");
281 public void testGetVarIdLocalVariable() {
282 fail("Not yet implemented");
286 public void testFindVarId() {
287 fail("Not yet implemented");
291 public void testStoreVar() {
292 fail("Not yet implemented");
296 public void testGetPriorId() {
297 fail("Not yet implemented");
301 public void testGetAnalysis() {
302 fail("Not yet implemented");
306 public void testGetBugCollection() {
307 fail("Not yet implemented");