From e8a5f2082d854085fc9da33d5f837900ef347f4f Mon Sep 17 00:00:00 2001 From: Henri Sara Date: Tue, 4 Sep 2012 16:47:42 +0300 Subject: [PATCH] Fix SQLContainer indexed access and related tests (#9472) --- .../data/util/sqlcontainer/SQLContainer.java | 14 +++++++++++--- .../sqlcontainer/SQLContainerTableQueryTest.java | 7 ++++++- .../data/util/sqlcontainer/SQLContainerTest.java | 9 +++++++-- 3 files changed, 24 insertions(+), 6 deletions(-) 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)); -- 2.39.5