X-Git-Url: http://jaekl.net/gitweb/?p=cfb.git;a=blobdiff_plain;f=test%2Fnet%2Fjaekl%2Fcfb%2Fstore%2FDbStoreTest.java;h=f269a9cd126205bfe0a0dc2db63ff9a99fc37536;hp=c4e2668448eeddd692385366131d3d44993aa0e7;hb=e8190c8189a5270ada70aaa478409db6dbf1efae;hpb=6032315cf7e150bcda88c9d6e6b35fc244217d54 diff --git a/test/net/jaekl/cfb/store/DbStoreTest.java b/test/net/jaekl/cfb/store/DbStoreTest.java index c4e2668..f269a9c 100644 --- a/test/net/jaekl/cfb/store/DbStoreTest.java +++ b/test/net/jaekl/cfb/store/DbStoreTest.java @@ -1,11 +1,17 @@ package net.jaekl.cfb.store; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.io.ByteArrayInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.nio.charset.Charset; import java.sql.SQLException; import java.util.Date; @@ -15,15 +21,103 @@ import net.jaekl.cfb.db.CfbSchema; import net.jaekl.cfb.db.TypeMismatchException; import net.jaekl.cfb.db.driver.ConnectionMock; import net.jaekl.cfb.db.driver.DbDriverMock; +import net.jaekl.cfb.util.Command; +import net.jaekl.cfb.xml.BugCollection; import net.jaekl.cfb.xml.MessagesXmlData; -import net.jaekl.cfb.xml.messages.MessageCollection; import org.junit.Before; import org.junit.Test; import org.xml.sax.InputSource; import org.xml.sax.SAXException; -public class DbStoreTest { +public class DbStoreTest { + private static final String BUG_COLLECTION_XML = "" + + "" + + "" + + "/data/prog/findbugs-3.0.1/lib/junit.jar" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + ""; + + private static final String UNKNOWN_BUG_CATEGORY_XML = "" + + "" + + "" + + "/data/prog/findbugs-3.0.1/lib/junit.jar" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + ""; + + private static final String UNKNOWN_BUG_PATTERN_XML = "" + + "" + + "" + + "/data/prog/findbugs-3.0.1/lib/junit.jar" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + ""; + private DbStore m_store; @Before @@ -39,8 +133,7 @@ public class DbStoreTest { schema.setMessageMap(msgMap); schema.ensureDbInitialized(con); - MessageCollection msgColl = new MessageCollection(); - m_store = new DbStore(con, driver, msgColl); + m_store = new DbStore(con, driver, msgMap.getColl()); } @Test @@ -60,9 +153,59 @@ public class DbStoreTest { actual = m_store.getPrior(current); assertNull(actual); } + + private BugCollection parseBugCollection(String xml) throws IOException, SAXException + { + Charset utf8 = Charset.forName(Command.UTF_8); + + try ( ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes(utf8))) + { + InputSource inputSource = new InputSource(bais); + Analysis analysis = new Analysis(null, null); + analysis.parse(inputSource); + + assertNotNull(analysis); + + BugCollection bugColl = analysis.getBugCollection(); + return bugColl; + } + } + + @Test + public void testPut_null() throws StoreException, SQLException, TypeMismatchException + { + // Adding null should return false, with no exception thrown + boolean result = m_store.put(null); + assertFalse(result); + } + + @Test + public void testPut_withUnknownPatternOrCategory() throws SQLException, TypeMismatchException, IOException, SAXException + { + final String[] data = { UNKNOWN_BUG_PATTERN_XML, UNKNOWN_BUG_CATEGORY_XML}; + String projName = "ProjectName"; + String firstVersion = "1.0.1"; + Date firstStart = new Date(100); + Date firstEnd = new Date(200); + Analysis firstAnalysis = new Analysis(projName, firstVersion); + firstAnalysis.setStart(firstStart); + firstAnalysis.setEnd(firstEnd); + + for (String xml : data) { + firstAnalysis.setBugCollection(parseBugCollection(xml)); + + try { + m_store.put(firstAnalysis); + fail("Should have thrown an exception\n" + xml); + } + catch (StoreException exc) { + // This is the success path + } + } + } @Test - public void testPut_thenGetPrior() throws SQLException, TypeMismatchException { + public void testPut_thenGetPrior() throws SQLException, TypeMismatchException, IOException, SAXException, StoreException { String projName = "ProjectName"; String firstVersion = "1.0.1"; Date firstStart = new Date(100); @@ -70,10 +213,13 @@ public class DbStoreTest { Analysis firstAnalysis = new Analysis(projName, firstVersion); firstAnalysis.setStart(firstStart); firstAnalysis.setEnd(firstEnd); + firstAnalysis.setBugCollection(parseBugCollection(BUG_COLLECTION_XML)); + // Adding a valid Analysis object should return true boolean result = m_store.put(firstAnalysis); assertTrue(result); + // Create a second Analysis object String secondVersion = "1.0.2"; Date secondStart = new Date(2300); Date secondEnd = new Date(2400); @@ -81,6 +227,7 @@ public class DbStoreTest { secondAnalysis.setStart(secondStart); secondAnalysis.setEnd(secondEnd); + // Retrieve the first Analysis object Analysis priorAnalysis = m_store.getPrior(secondAnalysis); assertNotNull(priorAnalysis); assertEquals(firstAnalysis.getProjectName(), priorAnalysis.getProjectName());