summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorLeif Åstrand <leif@vaadin.com>2014-07-15 16:11:40 +0300
committerVaadin Code Review <review@vaadin.com>2014-07-17 08:18:18 +0000
commit2c1d7bdbc2e284264b485fbcf7d9f3d1ef3180f2 (patch)
tree1efca4a2aee81b76f5a9df587e910507bd3be067 /server
parentb8255a873283b610844b7dc9a535d2a0cd144844 (diff)
downloadvaadin-framework-2c1d7bdbc2e284264b485fbcf7d9f3d1ef3180f2.tar.gz
vaadin-framework-2c1d7bdbc2e284264b485fbcf7d9f3d1ef3180f2.zip
Add asserts checking for negative container sizes (#14232)
Change-Id: I5b6298be367e4fe820320a5e3fd6bf5aaa7e2047
Diffstat (limited to 'server')
-rw-r--r--server/src/com/vaadin/data/util/ContainerHierarchicalWrapper.java4
-rw-r--r--server/src/com/vaadin/data/util/ContainerOrderedWrapper.java1
-rw-r--r--server/src/com/vaadin/ui/AbstractSelect.java4
-rw-r--r--server/src/com/vaadin/ui/ComboBox.java2
-rw-r--r--server/src/com/vaadin/ui/components/calendar/ContainerEventProvider.java1
-rw-r--r--server/tests/src/com/vaadin/data/util/ContainerSizeAssertTest.java59
6 files changed, 69 insertions, 2 deletions
diff --git a/server/src/com/vaadin/data/util/ContainerHierarchicalWrapper.java b/server/src/com/vaadin/data/util/ContainerHierarchicalWrapper.java
index eafd3573bc..0bfec33957 100644
--- a/server/src/com/vaadin/data/util/ContainerHierarchicalWrapper.java
+++ b/server/src/com/vaadin/data/util/ContainerHierarchicalWrapper.java
@@ -701,7 +701,9 @@ public class ContainerHierarchicalWrapper implements Container.Hierarchical,
*/
@Override
public int size() {
- return container.size();
+ int size = container.size();
+ assert size >= 0;
+ return size;
}
/*
diff --git a/server/src/com/vaadin/data/util/ContainerOrderedWrapper.java b/server/src/com/vaadin/data/util/ContainerOrderedWrapper.java
index 483753da88..4bb4e4c1b2 100644
--- a/server/src/com/vaadin/data/util/ContainerOrderedWrapper.java
+++ b/server/src/com/vaadin/data/util/ContainerOrderedWrapper.java
@@ -494,6 +494,7 @@ public class ContainerOrderedWrapper implements Container.Ordered,
@Override
public int size() {
int newSize = container.size();
+ assert newSize >= 0;
if (lastKnownSize != -1 && newSize != lastKnownSize
&& !(container instanceof Container.ItemSetChangeNotifier)) {
// Update the internal cache when the size of the container changes
diff --git a/server/src/com/vaadin/ui/AbstractSelect.java b/server/src/com/vaadin/ui/AbstractSelect.java
index b8db329906..b083db3183 100644
--- a/server/src/com/vaadin/ui/AbstractSelect.java
+++ b/server/src/com/vaadin/ui/AbstractSelect.java
@@ -759,7 +759,9 @@ public abstract class AbstractSelect extends AbstractField<Object> implements
*/
@Override
public int size() {
- return items.size();
+ int size = items.size();
+ assert size >= 0;
+ return size;
}
/**
diff --git a/server/src/com/vaadin/ui/ComboBox.java b/server/src/com/vaadin/ui/ComboBox.java
index 048726dc84..5367505c56 100644
--- a/server/src/com/vaadin/ui/ComboBox.java
+++ b/server/src/com/vaadin/ui/ComboBox.java
@@ -354,6 +354,7 @@ public class ComboBox extends AbstractSelect implements
if (pageLength == 0) {
// no paging: return all items
filteredSize = container.size();
+ assert filteredSize >= 0;
return new ArrayList<Object>(container.getItemIds());
}
@@ -391,6 +392,7 @@ public class ComboBox extends AbstractSelect implements
}
filteredSize = container.size();
+ assert filteredSize >= 0;
currentPage = adjustCurrentPage(currentPage, needNullSelectOption,
indexToEnsureInView, filteredSize);
int first = getFirstItemIndexOnCurrentPage(needNullSelectOption,
diff --git a/server/src/com/vaadin/ui/components/calendar/ContainerEventProvider.java b/server/src/com/vaadin/ui/components/calendar/ContainerEventProvider.java
index 7c19395df2..a8804caedb 100644
--- a/server/src/com/vaadin/ui/components/calendar/ContainerEventProvider.java
+++ b/server/src/com/vaadin/ui/components/calendar/ContainerEventProvider.java
@@ -262,6 +262,7 @@ public class ContainerEventProvider implements CalendarEditableEventProvider,
private int[] getFirstAndLastEventIndex(Date start, Date end) {
int startIndex = 0;
int size = container.size();
+ assert size >= 0;
int endIndex = size - 1;
if (start != null) {
diff --git a/server/tests/src/com/vaadin/data/util/ContainerSizeAssertTest.java b/server/tests/src/com/vaadin/data/util/ContainerSizeAssertTest.java
new file mode 100644
index 0000000000..04fd8d3cd1
--- /dev/null
+++ b/server/tests/src/com/vaadin/data/util/ContainerSizeAssertTest.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2000-2014 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.data.util;
+
+import org.junit.Test;
+
+import com.vaadin.data.Container;
+import com.vaadin.ui.Table;
+
+public class ContainerSizeAssertTest {
+
+ @Test(expected = AssertionError.class)
+ public void testNegativeSizeAssert() {
+ Table table = createAttachedTable();
+
+ table.setContainerDataSource(createNegativeSizeContainer());
+ }
+
+ @Test
+ public void testZeroSizeNoAssert() {
+ Table table = createAttachedTable();
+
+ table.setContainerDataSource(new IndexedContainer());
+ }
+
+ private Container createNegativeSizeContainer() {
+ return new IndexedContainer() {
+ @Override
+ public int size() {
+ return -1;
+ }
+ };
+ }
+
+ private Table createAttachedTable() {
+ return new Table() {
+ private boolean initialized = true;
+
+ @Override
+ public boolean isAttached() {
+ // This returns false until the super constructor has finished
+ return initialized;
+ }
+ };
+ }
+}