Add further unit tests.
authorChris Jaekl <cejaekl@yahoo.com>
Wed, 30 Dec 2015 08:49:31 +0000 (17:49 +0900)
committerChris Jaekl <cejaekl@yahoo.com>
Wed, 30 Dec 2015 08:49:31 +0000 (17:49 +0900)
prod/net/jaekl/cfb/CfbException.java
prod/net/jaekl/cfb/store/DbStore.java
prod/net/jaekl/cfb/store/StoreException.java
prod/net/jaekl/cfb/util/Util.java
test/net/jaekl/cfb/db/driver/DbDriverMock.java
test/net/jaekl/cfb/store/DbStoreTest.java

index 6b46797be126b42a05a4b9446eb3aa676227c640..b280c1d0e79fc77dfae6d6c7cb2f56c5ae0b41e3 100644 (file)
@@ -10,4 +10,8 @@ public class CfbException extends Exception {
        public CfbException(String msg) {
                super(msg);
        }
+
+       public CfbException(Throwable cause) {
+               super(cause);
+       }
 }
index e50f7403910830d798e7cd8ca1b3296f056f42bc..b17a419626a69e4afbb692aaf01157df9fe3c3da 100644 (file)
@@ -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,
index 5c0caf43405f3af851fd70a1cfee21e38d27694a..b7d75816450814da88747a968314aa76e3dd4162 100644 (file)
@@ -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);
index d1e79bdaa0742c45bb763c302d7404aaa6d44216..99ef6e846f78b9b082cfbbc7d4e8db7f94e9fae6 100644 (file)
@@ -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);
        }
        
index 86ae18c1785bcbbd454ba425573bdae0eb330a14..bb4675a2ff345fefb3b78827c45a625b0220d396 100644 (file)
@@ -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:
index f269a9cd126205bfe0a0dc2db63ff9a99fc37536..5c66c225fd38107d7e866d826bf43be269a68616 100644 (file)
@@ -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());
+               }
        }
-*/
 }