changeset 1300:e12ff00f2846

Fix a badly specified API in ThreadDAO Reviewed-by: vanaltj Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-November/008627.html
author Omair Majid <omajid@redhat.com>
date Tue, 05 Nov 2013 15:15:17 -0500
parents 02a031166d98
children ea2a5aedd25a
files thread/collector/src/main/java/com/redhat/thermostat/thread/dao/ThreadDao.java thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/ThreadDaoImpl.java thread/collector/src/test/java/com/redhat/thermostat/thread/dao/impl/ThreadDaoImplTest.java
diffstat 3 files changed, 33 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/ThreadDao.java	Mon Nov 04 16:14:47 2013 -0500
+++ b/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/ThreadDao.java	Tue Nov 05 15:15:17 2013 -0500
@@ -124,8 +124,15 @@
 
     /** Save the specified thread info */
     void saveThreadInfo(ThreadInfoData info);
-    /** Get the time interval for the entire data */
+
+    /**
+     * Get the time interval for the entire data
+     *
+     * @returns a {@link Range} of timestamps (in milliseconds) or null if there
+     * is no valid data.
+     */
     Range<Long> getThreadInfoTimeRange(VmRef ref);
+
     /** Get the thread info data with a timestamp greated than the one specified */
     List<ThreadInfoData> loadThreadInfo(VmRef ref, long since);
     /** Get the thread info data with a timestamp in the given time range (inclusive) */
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/ThreadDaoImpl.java	Mon Nov 04 16:14:47 2013 -0500
+++ b/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/ThreadDaoImpl.java	Tue Nov 05 15:15:17 2013 -0500
@@ -320,6 +320,10 @@
 
         stmt = prepareQuery(THREAD_INFO, QUERY_OLDEST_THREAD_INFO, ref);
         ThreadInfoData oldestData = getFirstResult(stmt);
+        if (oldestData == null) {
+            return null;
+        }
+
         long oldestTimeStamp = oldestData.getTimeStamp();
 
         stmt = prepareQuery(THREAD_INFO, QUERY_LATEST_THREAD_INFO, ref);
--- a/thread/collector/src/test/java/com/redhat/thermostat/thread/dao/impl/ThreadDaoImplTest.java	Mon Nov 04 16:14:47 2013 -0500
+++ b/thread/collector/src/test/java/com/redhat/thermostat/thread/dao/impl/ThreadDaoImplTest.java	Tue Nov 05 15:15:17 2013 -0500
@@ -38,6 +38,7 @@
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
@@ -421,6 +422,26 @@
     }
 
     @Test
+    public void testThreadInfoTimeRangeWithNoDataAvailable() throws Exception {
+        Cursor<ThreadInfoData> oldestThreadQueryCursor = mock(Cursor.class);
+        when(oldestThreadQueryCursor.hasNext()).thenReturn(false);
+        when(oldestThreadQueryCursor.next()).thenThrow(IllegalStateException.class);
+
+        PreparedStatement<ThreadInfoData> oldestThreadQueryStatement = mock(PreparedStatement.class);
+        when(oldestThreadQueryStatement.executeQuery()).thenReturn(oldestThreadQueryCursor);
+
+        Storage storage = mock(Storage.class);
+        when(storage.prepareStatement(any(StatementDescriptor.class)))
+            .thenReturn(oldestThreadQueryStatement);
+
+        ThreadDaoImpl dao = new ThreadDaoImpl(storage);
+
+        Range<Long> result = dao.getThreadInfoTimeRange(vmRef);
+
+        assertNull(result);
+    }
+
+    @Test
     public void testThreadInfoTimeRange() throws DescriptorParsingException, StatementExecutionException {
         final long OLDEST_TIMESTAMP = 0;
         final long LATEST_TIMESTAMP = 1999;