From: Henri Sara Date: Tue, 4 Sep 2012 13:47:42 +0000 (+0300) Subject: Fix SQLContainer indexed access and related tests (#9472) X-Git-Tag: 7.0.0.beta1~195^2~1 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=e8a5f2082d854085fc9da33d5f837900ef347f4f;p=vaadin-framework.git Fix SQLContainer indexed access and related tests (#9472) --- diff --git a/server/src/com/vaadin/data/util/sqlcontainer/SQLContainer.java b/server/src/com/vaadin/data/util/sqlcontainer/SQLContainer.java index 7a63e8c6c2..1b479a21d8 100644 --- a/server/src/com/vaadin/data/util/sqlcontainer/SQLContainer.java +++ b/server/src/com/vaadin/data/util/sqlcontainer/SQLContainer.java @@ -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; + } } /* diff --git a/server/tests/src/com/vaadin/data/util/sqlcontainer/SQLContainerTableQueryTest.java b/server/tests/src/com/vaadin/data/util/sqlcontainer/SQLContainerTableQueryTest.java index 438c40823d..7b3d8a4cf2 100644 --- a/server/tests/src/com/vaadin/data/util/sqlcontainer/SQLContainerTableQueryTest.java +++ b/server/tests/src/com/vaadin/data/util/sqlcontainer/SQLContainerTableQueryTest.java @@ -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)); diff --git a/server/tests/src/com/vaadin/data/util/sqlcontainer/SQLContainerTest.java b/server/tests/src/com/vaadin/data/util/sqlcontainer/SQLContainerTest.java index 6649bc16e8..0856b3c08c 100644 --- a/server/tests/src/com/vaadin/data/util/sqlcontainer/SQLContainerTest.java +++ b/server/tests/src/com/vaadin/data/util/sqlcontainer/SQLContainerTest.java @@ -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));