From: Chris Jaekl Date: Wed, 30 Dec 2015 08:49:31 +0000 (+0900) Subject: Add further unit tests. X-Git-Url: http://jaekl.net/gitweb/?p=cfb.git;a=commitdiff_plain;h=a1378c84c773511e4ffe99fb419da67af188aff7 Add further unit tests. --- diff --git a/prod/net/jaekl/cfb/CfbException.java b/prod/net/jaekl/cfb/CfbException.java index 6b46797..b280c1d 100644 --- a/prod/net/jaekl/cfb/CfbException.java +++ b/prod/net/jaekl/cfb/CfbException.java @@ -10,4 +10,8 @@ public class CfbException extends Exception { public CfbException(String msg) { super(msg); } + + public CfbException(Throwable cause) { + super(cause); + } } diff --git a/prod/net/jaekl/cfb/store/DbStore.java b/prod/net/jaekl/cfb/store/DbStore.java index e50f740..b17a419 100644 --- a/prod/net/jaekl/cfb/store/DbStore.java +++ b/prod/net/jaekl/cfb/store/DbStore.java @@ -32,7 +32,7 @@ public class DbStore { m_msgColl = msgColl; } - public Analysis getPrior(Analysis analysis) throws SQLException, TypeMismatchException { + public Analysis getPrior(Analysis analysis) throws SQLException, TypeMismatchException, StoreException { if (null == analysis) { return null; } @@ -127,7 +127,7 @@ public class DbStore { } - Location getLoc(Long locId) throws SQLException, TypeMismatchException + Location getLoc(Long locId) throws TypeMismatchException, StoreException { if (null == locId) { return null; @@ -137,16 +137,21 @@ public class DbStore { Table[] tables = { CfbSchema.LOCATIONS }; Condition[] conditions = { new Condition(CfbSchema.LOCID, locId, Operation.EQUAL) }; - Row row = m_driver.selectExactlyOne(m_con, columns, tables, conditions); - - String className = row.getString(0); - String methodName = row.getString(1); - String methodRole = row.getString(2); - Integer startLine = row.getInt(3); - Integer endLine = row.getInt(4); - - Location loc = new Location(locId, className, methodName, methodRole, startLine, endLine); - return loc; + try { + Row row = m_driver.selectExactlyOne(m_con, columns, tables, conditions); + + String className = row.getString(0); + String methodName = row.getString(1); + String methodRole = row.getString(2); + Integer startLine = row.getInt(3); + Integer endLine = row.getInt(4); + + Location loc = new Location(locId, className, methodName, methodRole, startLine, endLine); + return loc; + } + catch (SQLException exc) { + throw new StoreException(exc, StoreException.Type.INVALID_LOC_ID, ""+locId); + } } Long getLocId(Location loc) throws SQLException, TypeMismatchException @@ -311,7 +316,7 @@ public class DbStore { return rows.get(0).getLong(0); } - Analysis getAnalysis(Long analysisId) throws SQLException, TypeMismatchException + Analysis getAnalysis(Long analysisId) throws SQLException, TypeMismatchException, StoreException { Column[] columns = { CfbSchema.PROJNAME, CfbSchema.VERSION, CfbSchema.STARTTIME, CfbSchema.ENDTIME }; Table[] tables = { CfbSchema.RUNS }; @@ -339,7 +344,7 @@ public class DbStore { return prior; } - BugCollection getBugCollection(Long runId) throws SQLException, TypeMismatchException + BugCollection getBugCollection(Long runId) throws SQLException, TypeMismatchException, StoreException { Column[] columns = { CfbSchema.FOUNDID, diff --git a/prod/net/jaekl/cfb/store/StoreException.java b/prod/net/jaekl/cfb/store/StoreException.java index 5c0caf4..b7d7581 100644 --- a/prod/net/jaekl/cfb/store/StoreException.java +++ b/prod/net/jaekl/cfb/store/StoreException.java @@ -7,7 +7,8 @@ import net.jaekl.cfb.CfbException; public class StoreException extends CfbException { public enum Type { UNKNOWN_PATTERN, // bug pattern type is not found in the message collection - UNKNOWN_CATEGORY // bug category is not found in the message collection + UNKNOWN_CATEGORY, // bug category is not found in the message collection + INVALID_LOC_ID // the specified location ID is not found in the database } private Type m_type; @@ -22,6 +23,15 @@ public class StoreException extends CfbException { m_info = info; } + public StoreException(Throwable cause, Type type, String... info) { + super(cause); + + m_type = type; + m_info = info; + } + + public Type getType() { return m_type; } + @Override public String toString() { return "" + getClass().getName() + ": " + m_type + ": " + Arrays.toString(m_info); diff --git a/prod/net/jaekl/cfb/util/Util.java b/prod/net/jaekl/cfb/util/Util.java index d1e79bd..99ef6e8 100644 --- a/prod/net/jaekl/cfb/util/Util.java +++ b/prod/net/jaekl/cfb/util/Util.java @@ -47,6 +47,14 @@ public class Util { return (a == b); } + if ((a instanceof Number) && (b instanceof Number)) { + Number aNum = (Number)a; + Number bNum = (Number)b; + + return ( (aNum.longValue() == bNum.longValue()) + || (aNum.doubleValue() == bNum.doubleValue()) ); + } + return a.equals(b); } diff --git a/test/net/jaekl/cfb/db/driver/DbDriverMock.java b/test/net/jaekl/cfb/db/driver/DbDriverMock.java index 86ae18c..bb4675a 100644 --- a/test/net/jaekl/cfb/db/driver/DbDriverMock.java +++ b/test/net/jaekl/cfb/db/driver/DbDriverMock.java @@ -21,6 +21,7 @@ import net.jaekl.cfb.db.Sort; import net.jaekl.cfb.db.Table; import net.jaekl.cfb.db.TableMock; import net.jaekl.cfb.db.TypeMismatchException; +import net.jaekl.cfb.util.Util; public class DbDriverMock extends DbDriver { @@ -194,7 +195,7 @@ public class DbDriverMock extends DbDriver { if (condition.getColumn().equals(col)) { switch(condition.getOperation()) { case EQUAL: - return (condition.getValue().equals(row.getValue(idx))); + return Util.objsAreEqual(condition.getValue(), row.getValue(idx)); case GREATER_THAN: return (aLessThanB(condition.getValue(), row.getValue(idx))); case LESS_THAN: diff --git a/test/net/jaekl/cfb/store/DbStoreTest.java b/test/net/jaekl/cfb/store/DbStoreTest.java index f269a9c..5c66c22 100644 --- a/test/net/jaekl/cfb/store/DbStoreTest.java +++ b/test/net/jaekl/cfb/store/DbStoreTest.java @@ -137,7 +137,7 @@ public class DbStoreTest { } @Test - public void testGetPrior_withNoEntries() throws SQLException, TypeMismatchException { + public void testGetPrior_withNoEntries() throws SQLException, TypeMismatchException, StoreException { // First test: getPrior(null) should return null Analysis actual = m_store.getPrior(null); assertNull(actual); @@ -236,75 +236,42 @@ public class DbStoreTest { assertEquals(firstAnalysis.getEnd(), priorAnalysis.getEnd()); assertEquals(firstAnalysis.getBugCollection(), priorAnalysis.getBugCollection()); } -/* - @Test - public void testGetBugType() { - fail("Not yet implemented"); - } - - @Test - public void testGetCategoryName() { - fail("Not yet implemented"); - } - - @Test - public void testGetLoc() { - fail("Not yet implemented"); - } - - @Test - public void testGetLocId() { - fail("Not yet implemented"); - } - - @Test - public void testFindLocId() { - fail("Not yet implemented"); - } - - @Test - public void testStoreLoc() { - fail("Not yet implemented"); - } @Test - public void testGetVarIdBugInstance() { - fail("Not yet implemented"); + public void testGetLocId_nullReturnsNull() throws SQLException, TypeMismatchException { + Long locId = m_store.getLocId(null); + assertNull(locId); } @Test - public void testGetVar() { - fail("Not yet implemented"); - } - - @Test - public void testGetVarIdLocalVariable() { - fail("Not yet implemented"); - } - - @Test - public void testFindVarId() { - fail("Not yet implemented"); - } - - @Test - public void testStoreVar() { - fail("Not yet implemented"); - } - - @Test - public void testGetPriorId() { - fail("Not yet implemented"); + public void testGetLocId_notFoundIsStored() throws SQLException, TypeMismatchException { + Location loc = new Location(1234567890L, + "ThisClassDoesNotExist", + "thisMethodDoesNotExist", + "INVALID_METHOD_ROLE", + 0, 90909); + Long locId = m_store.getLocId(loc); + assertNotNull(locId); + assertTrue(locId.longValue() > 0); + + Long secondLocId = m_store.getLocId(loc); + assertEquals(locId, secondLocId); } @Test - public void testGetAnalysis() { - fail("Not yet implemented"); + public void testGetLoc_nullReturnsNull() throws SQLException, TypeMismatchException, StoreException { + Location loc = m_store.getLoc(null); + assertNull(loc); } - + @Test - public void testGetBugCollection() { - fail("Not yet implemented"); + public void testGetLoc_invalidId() throws SQLException, TypeMismatchException { + try { + m_store.getLoc(Long.valueOf(-3)); + fail("Should have thrown a StoreException"); + } + catch (StoreException exc) { + assertEquals(StoreException.Type.INVALID_LOC_ID, exc.getType()); + } } -*/ }