]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fix SQLContainer indexed access and related tests (#9472)
authorHenri Sara <hesara@vaadin.com>
Tue, 4 Sep 2012 13:47:42 +0000 (16:47 +0300)
committerHenri Sara <hesara@vaadin.com>
Tue, 4 Sep 2012 13:47:42 +0000 (16:47 +0300)
server/src/com/vaadin/data/util/sqlcontainer/SQLContainer.java
server/tests/src/com/vaadin/data/util/sqlcontainer/SQLContainerTableQueryTest.java
server/tests/src/com/vaadin/data/util/sqlcontainer/SQLContainerTest.java

index 7a63e8c6c20c306bb6ef898e7941dbf740fc489f..1b479a21d8a6896fa8a4e4802552ae68741fb1b2 100644 (file)
@@ -661,7 +661,8 @@ public class SQLContainer implements Container, Container.Filterable,
             throw new IndexOutOfBoundsException("Index is negative! index="
                     + index);
         }
-
+        // make sure the size field is valid
+        updateCount();
         if (index < size) {
             if (itemIndexes.keySet().contains(index)) {
                 return itemIndexes.get(index);
@@ -671,7 +672,9 @@ public class SQLContainer implements Container, Container.Filterable,
         } else {
             // The index is in the added items
             int offset = index - size;
-            return addedItems.get(offset).getId();
+            // TODO this is very inefficient if looping - should improve
+            // getItemIds(int, int)
+            return getFilteredAddedItems().get(offset).getId();
         }
     }
 
@@ -694,7 +697,12 @@ public class SQLContainer implements Container, Container.Filterable,
 
     @Override
     public Object nextItemId(Object itemId) {
-        return getIdByIndex(indexOfId(itemId) + 1);
+        int index = indexOfId(itemId) + 1;
+        try {
+            return getIdByIndex(index);
+        } catch (IndexOutOfBoundsException e) {
+            return null;
+        }
     }
 
     /*
index 438c40823d57fa0ad0bf6713b3500246328157f6..7b3d8a4cf2248f1ce70fe85903c8e83364700f44 100644 (file)
@@ -1468,7 +1468,12 @@ public class SQLContainerTableQueryTest {
                 container.getContainerProperty(container.getIdByIndex(3),
                         "NAME").getValue());
 
-        Assert.assertNull(container.getIdByIndex(4));
+        try {
+            container.getIdByIndex(4);
+            Assert.fail("SQLContainer.getIdByIndex() returned a value for an index beyond the end of the container");
+        } catch (IndexOutOfBoundsException e) {
+            // should throw exception - item is filtered out
+        }
         Assert.assertNull(container.nextItemId(container.getIdByIndex(3)));
 
         Assert.assertFalse(container.containsId(id2));
index 6649bc16e8fafcbc43ff715497ca9d9e0775da02..0856b3c08c9b5c2d2017651ef91eaa0090cb910a 100644 (file)
@@ -2322,8 +2322,13 @@ public class SQLContainerTest {
                 container.getContainerProperty(container.getIdByIndex(3),
                         "NAME").getValue());
 
-        Assert.assertNull(container.getIdByIndex(4));
-        Assert.assertNull(container.nextItemId(container.getIdByIndex(3)));
+        try {
+            container.getIdByIndex(4);
+            Assert.fail("SQLContainer.getIdByIndex() returned a value for an index beyond the end of the container");
+        } catch (IndexOutOfBoundsException e) {
+            // should throw exception - item is filtered out
+        }
+        container.nextItemId(container.getIdByIndex(3));
 
         Assert.assertFalse(container.containsId(id2));
         Assert.assertFalse(container.getItemIds().contains(id2));