]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fix IndexOutOfBoundsException in RpcDataProviderExtension (#15443)
authorJohannes Dahlström <johannesd@vaadin.com>
Tue, 13 Jan 2015 10:21:53 +0000 (12:21 +0200)
committerTeemu Suo-Anttila <teemusa@vaadin.com>
Wed, 14 Jan 2015 10:26:35 +0000 (10:26 +0000)
Change-Id: I5688e369bd6247afe0c8ed381964445dfc2c1ec1

server/src/com/vaadin/data/RpcDataProviderExtension.java
uitest/src/com/vaadin/tests/components/grid/basicfeatures/server/GridEditorTest.java
uitest/src/com/vaadin/tests/components/grid/basicfeatures/server/GridRowAddRemoveTest.java
uitest/src/com/vaadin/tests/components/grid/basicfeatures/server/GridStaticSectionComponentTest.java
uitest/src/com/vaadin/tests/components/grid/basicfeatures/server/LoadingIndicatorTest.java

index 2e10ccfef1ecde7d785146d9b97752a46ef96c16..2e6b4a8414c2e520e7aacecfa297d74982de4ace 100644 (file)
@@ -96,15 +96,14 @@ public class RpcDataProviderExtension extends AbstractExtension {
             // private implementation
         }
 
-        void preActiveRowsChange(Range newActiveRange, int firstNewIndex,
-                List<?> itemIds) {
+        void setActiveRange(Range newActiveRange) {
             final Range[] removed = activeRange.partitionWith(newActiveRange);
             final Range[] added = newActiveRange.partitionWith(activeRange);
 
             removeActiveRows(removed[0]);
             removeActiveRows(removed[2]);
-            addActiveRows(added[0], firstNewIndex, itemIds);
-            addActiveRows(added[2], firstNewIndex, itemIds);
+            addActiveRows(added[0]);
+            addActiveRows(added[2]);
 
             activeRange = newActiveRange;
         }
@@ -121,10 +120,17 @@ public class RpcDataProviderExtension extends AbstractExtension {
             }
         }
 
-        private void addActiveRows(final Range added, int firstNewIndex,
-                List<?> newItemIds) {
+        private void addActiveRows(Range added) {
+            if (added.isEmpty()) {
+                // Some container.getItemIds() implementations just might be
+                // expensive even for an empty range, so bail out early
+                return;
+            }
 
-            for (int i = added.getStart(); i < added.getEnd(); i++) {
+            List<?> newItemIds = container.getItemIds(added.getStart(),
+                    added.length());
+
+            for (int i = 0; i < newItemIds.size(); i++) {
 
                 /*
                  * We might be in a situation we have an index <-> itemId entry
@@ -137,8 +143,8 @@ public class RpcDataProviderExtension extends AbstractExtension {
                  * if-state. But it sounds too stupid (and most often too
                  * insignificant) to try out.
                  */
-                final Integer ii = Integer.valueOf(i);
-                if (indexToItemId.containsKey(ii)) {
+                final Integer index = Integer.valueOf(i + added.getStart());
+                if (indexToItemId.containsKey(index)) {
                     continue;
                 }
 
@@ -149,11 +155,11 @@ public class RpcDataProviderExtension extends AbstractExtension {
                  * In that case, we only want to add an index for that entry,
                  * and not overwrite the key.
                  */
-                final Object itemId = newItemIds.get(i - firstNewIndex);
+                final Object itemId = newItemIds.get(i);
                 if (!itemIdToKey.containsKey(itemId)) {
                     itemIdToKey.put(itemId, nextKey());
                 }
-                indexToItemId.forcePut(ii, itemId);
+                indexToItemId.forcePut(index, itemId);
             }
         }
 
@@ -757,10 +763,9 @@ public class RpcDataProviderExtension extends AbstractExtension {
             active = active.combineWith(cached);
         }
 
-        List<?> itemIds = RpcDataProviderExtension.this.container.getItemIds(
-                firstRowToPush, numberOfRows);
-        keyMapper.preActiveRowsChange(active, firstRowToPush, itemIds);
+        keyMapper.setActiveRange(active);
 
+        List<?> itemIds = container.getItemIds(firstRowToPush, numberOfRows);
         JsonArray rows = Json.createArray();
         for (int i = 0; i < itemIds.size(); ++i) {
             rows.set(i, getRowData(getGrid().getColumns(), itemIds.get(i)));
index 35b2fc24fed2a7e32c48572624ba82b7203328da..74c3c0db35c8dd51e28d66ef407f344024435d10 100644 (file)
@@ -59,8 +59,7 @@ public class GridEditorTest extends GridBasicFeaturesTest {
         selectMenuPath("Component", "Editor", "Enabled");
         selectMenuPath("Component", "Editor", "Edit item 5");
         assertEditorClosed();
-        boolean thrown = getLogRow(0).startsWith(
-                "5. Exception occured, java.lang.IllegalStateException");
+        boolean thrown = logContainsText("Exception occured, java.lang.IllegalStateException");
         assertTrue("IllegalStateException thrown", thrown);
     }
 
@@ -69,8 +68,7 @@ public class GridEditorTest extends GridBasicFeaturesTest {
         selectMenuPath("Component", "Editor", "Edit item 5");
         selectMenuPath("Component", "Editor", "Enabled");
         assertEditorOpen();
-        boolean thrown = getLogRow(0).startsWith(
-                "5. Exception occured, java.lang.IllegalStateException");
+        boolean thrown = logContainsText("Exception occured, java.lang.IllegalStateException");
         assertTrue("IllegalStateException thrown", thrown);
     }
 
index 28c9e441dcb6995a40b5ff1895b1326ea8a061fb..8535efb9ef22279f48a23c7512dc613f0b62912f 100644 (file)
@@ -30,9 +30,9 @@ public class GridRowAddRemoveTest extends GridBasicFeaturesTest {
         selectMenuPath("Component", "Body rows", "Remove all rows");
         selectMenuPath("Component", "Body rows", "Add 18 rows");
 
-        Assert.assertEquals(
+        Assert.assertTrue(
                 "All added rows should be fetched in the same round trip.",
-                "2. Requested items 0 - 18", getLogRow(0));
+                logContainsText("Requested items 0 - 18"));
     }
 
     @Test
index bf1d1329aa5ae7014f60f4726ae0ef87afc7269a..2fbaa58cabd3cdbfc7a54e183304f7a91100ecb0 100644 (file)
@@ -35,7 +35,8 @@ public class GridStaticSectionComponentTest extends GridBasicFeaturesTest {
 
         getGridElement().$(ButtonElement.class).first().click();
 
-        assertEquals("3. Button clicked!", getLogRow(0));
+        assertTrue("Button click should be logged",
+                logContainsText("Button clicked!"));
     }
 
     @Test
@@ -49,7 +50,8 @@ public class GridStaticSectionComponentTest extends GridBasicFeaturesTest {
 
         getGridElement().$(ButtonElement.class).first().click();
 
-        assertEquals("5. Button clicked!", getLogRow(0));
+        assertTrue("Button click should be logged",
+                logContainsText("Button clicked!"));
     }
 
     @Test
index 1f16efdd399f0200879e93e408c711cfdaf7e94e..b122eb02e998281181380a615299b5b341c1971b 100644 (file)
@@ -68,8 +68,9 @@ public class LoadingIndicatorTest extends GridBasicFeaturesTest {
         Thread.sleep(2000);
 
         String firstLogRow = getLogRow(0);
-        Assert.assertTrue("Last log message was not the fourth message: "
-                + firstLogRow, firstLogRow.startsWith("4. Requested items"));
+        Assert.assertTrue(
+                "Last log message should be number 6: " + firstLogRow,
+                firstLogRow.startsWith("6. Requested items"));
     }
 
     private boolean isLoadingIndicatorVisible() {