X-Git-Url: http://jaekl.net/gitweb/?p=cfb.git;a=blobdiff_plain;f=test%2Fnet%2Fjaekl%2Fcfb%2FCFBTest.java;fp=test%2Fnet%2Fjaekl%2Fcfb%2FCFBTest.java;h=de10fe7206675d49aca12856ac166456620958e0;hp=0000000000000000000000000000000000000000;hb=bedb0c5b72bcbbbcc2b8d11575ad99489aef6853;hpb=e39cfda923b6edce0fb80211cc927a7534abba10 diff --git a/test/net/jaekl/cfb/CFBTest.java b/test/net/jaekl/cfb/CFBTest.java new file mode 100644 index 0000000..de10fe7 --- /dev/null +++ b/test/net/jaekl/cfb/CFBTest.java @@ -0,0 +1,136 @@ +package net.jaekl.cfb; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.nio.charset.Charset; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.Locale; + +import net.jaekl.cfb.analyze.Analysis; +import net.jaekl.cfb.analyze.MessageMap; +import net.jaekl.cfb.db.CfbSchema; +import net.jaekl.cfb.db.Column; +import net.jaekl.cfb.db.Condition; +import net.jaekl.cfb.db.Operation; +import net.jaekl.cfb.db.Row; +import net.jaekl.cfb.db.Table; +import net.jaekl.cfb.db.TypeMismatchException; +import net.jaekl.cfb.db.driver.DbDriverMock; +import net.jaekl.cfb.util.Command; +import net.jaekl.cfb.xml.MessagesXmlData; + +import org.junit.Before; +import org.junit.Test; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +public class CFBTest { + private CFBMock m_cfb; + private DbDriverMock m_driver; + + private static final String BUG_COLLECTION_XML = "" + + "" + + "" + + "/data/prog/findbugs-3.0.1/lib/junit.jar" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + ""; + + private Analysis analysisFromXml(String xml, String projectName, String version) + throws FileNotFoundException, IOException, SAXException + { + Analysis analysis = new Analysis(projectName, version); + ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes(Command.UTF_8)); + InputSource inputSource = new InputSource(bais); + analysis.parse(inputSource); + + return analysis; + } + + @Before + public void setUp() + { + m_driver = new DbDriverMock(); + m_cfb = new CFBMock(Locale.getDefault(), m_driver); + } + + @Test + public void testStoreAndReport_noPrior() throws IOException, SQLException, SAXException, TypeMismatchException + { + final String PROJECT_NAME = "ProjectName"; + final String VERSION = "1.0.1"; + + try ( + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, Charset.forName(Command.UTF_8))); + ) + { + MessageMap msgMap = new MessageMap(); + msgMap.parse(new InputSource(new ByteArrayInputStream(MessagesXmlData.XML.getBytes(Command.UTF_8)))); + + Analysis analysis = analysisFromXml(BUG_COLLECTION_XML, PROJECT_NAME, VERSION); + + m_cfb.ensureDbInitialized(pw, msgMap); + m_cfb.storeAndReport(pw, msgMap, analysis); + } + + Connection con = m_driver.connect("host", 1234, "dbName", "dbUser", "dbPass"); + Column[] columns = { CfbSchema.RUNID, CfbSchema.PROJNAME, CfbSchema.VERSION }; + Table[] tables = { CfbSchema.RUNS }; + Condition[] conditions = { new Condition(CfbSchema.RUNID, Operation.NOT_NULL) }; + + Row row = m_driver.selectExactlyOne(con, columns, tables, conditions); + assertNotNull(row); + assertEquals(PROJECT_NAME, row.getValue(1)); + assertEquals(VERSION, row.getValue(2)); + } +}