]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add asserts checking for negative container sizes (#14232)
authorLeif Åstrand <leif@vaadin.com>
Tue, 15 Jul 2014 13:11:40 +0000 (16:11 +0300)
committerBogdan Udrescu <bogdan@vaadin.com>
Wed, 23 Jul 2014 11:11:52 +0000 (14:11 +0300)
Change-Id: I5b6298be367e4fe820320a5e3fd6bf5aaa7e2047

server/src/com/vaadin/data/util/ContainerHierarchicalWrapper.java
server/src/com/vaadin/data/util/ContainerOrderedWrapper.java
server/src/com/vaadin/ui/AbstractSelect.java
server/src/com/vaadin/ui/ComboBox.java
server/src/com/vaadin/ui/components/calendar/ContainerEventProvider.java
server/tests/src/com/vaadin/data/util/ContainerSizeAssertTest.java [new file with mode: 0644]

index eafd3573bce85558fdb81f142f88774d96f52824..0bfec339574d9be02864997aab84c379535928e1 100644 (file)
@@ -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;
     }
 
     /*
index 483753da8864568b8b8e2c27c319418d7078d487..4bb4e4c1b2fa21dd7b5a3b7a0920ff3217c6ef9a 100644 (file)
@@ -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
index b8db32990609b78b4d53c83d862cd9ddb1487a61..b083db3183214e48b16cc98c07da3f08d62cb646 100644 (file)
@@ -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;
     }
 
     /**
index 048726dc843cff41d398ec216ef5147fa2769a32..5367505c566fc29415b3e8dda2d66d80257e5b6e 100644 (file)
@@ -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,
index 7c19395df2947f56cd1a75de36f9ff6b3f4d8835..a8804caedb3b673b69abfa5f55e300ba0ff6955d 100644 (file)
@@ -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 (file)
index 0000000..04fd8d3
--- /dev/null
@@ -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;
+            }
+        };
+    }
+}