From 482a7362d423a778e71b5e7fc1a7ac346d40c802 Mon Sep 17 00:00:00 2001 From: Henri Sara Date: Fri, 20 Jul 2012 14:46:33 +0000 Subject: [PATCH] #8291, #7666 fix Table NegativeArraySizeException when table size reduced by filter or otherwise, related test application svn changeset:24018/svn branch:6.8 --- src/com/vaadin/ui/Table.java | 3 +- .../com/vaadin/tests/tickets/Ticket8291.java | 121 ++++++++++++++++++ 2 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 tests/testbench/com/vaadin/tests/tickets/Ticket8291.java diff --git a/src/com/vaadin/ui/Table.java b/src/com/vaadin/ui/Table.java index 55f3f27507..60b6122270 100644 --- a/src/com/vaadin/ui/Table.java +++ b/src/com/vaadin/ui/Table.java @@ -1479,9 +1479,10 @@ public class Table extends AbstractSelect implements Action.Container, // Collects the basic facts about the table page final int pagelen = getPageLength(); - int firstIndex = getCurrentPageFirstItemIndex(); int rows, totalRows; rows = totalRows = size(); + int firstIndex = Math + .min(getCurrentPageFirstItemIndex(), totalRows - 1); if (rows > 0 && firstIndex >= 0) { rows -= firstIndex; } diff --git a/tests/testbench/com/vaadin/tests/tickets/Ticket8291.java b/tests/testbench/com/vaadin/tests/tickets/Ticket8291.java new file mode 100644 index 0000000000..86b5db953b --- /dev/null +++ b/tests/testbench/com/vaadin/tests/tickets/Ticket8291.java @@ -0,0 +1,121 @@ +package com.vaadin.tests.tickets; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import com.vaadin.Application; +import com.vaadin.data.Container.Filter; +import com.vaadin.data.Item; +import com.vaadin.data.util.BeanItemContainer; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.HorizontalLayout; +import com.vaadin.ui.Table; +import com.vaadin.ui.Window; + +/** + * Test for #8291 and #7666: NegativeArraySizeException when Table scrolled to + * the end and its size reduced. + */ +public class Ticket8291 extends Application { + + @Override + public void init() { + setMainWindow(new Window("", new TestView())); + } + + private static class DecimateFilter implements Filter { + public boolean passesFilter(Object itemId, Item item) + throws UnsupportedOperationException { + return ((((TestObject) itemId).property3 % 10) == 0); + } + + public boolean appliesToProperty(Object propertyId) { + return true; + } + } + + private static class TestView extends HorizontalLayout { + + private Filter filter = null; + + private boolean reduceData; + + private TestView() { + final Table table = new Table(); + List data = createData(1000); + final BeanItemContainer container = new BeanItemContainer( + TestObject.class, data) { + + @Override + public int size() { + if (reduceData) { + return 100; + } else { + return super.size(); + } + } + }; + table.setContainerDataSource(container); + addComponent(table); + Button button = new Button("Click"); + button.addListener(new Button.ClickListener() { + public void buttonClick(ClickEvent event) { + reduceData = !reduceData; + table.refreshRowCache(); + } + }); + addComponent(button); + Button button2 = new Button("Filter"); + button2.addListener(new Button.ClickListener() { + public void buttonClick(ClickEvent event) { + if (filter != null) { + container.removeAllContainerFilters(); + filter = null; + } else { + filter = new DecimateFilter(); + container.addContainerFilter(filter); + } + table.refreshRowCache(); + } + }); + addComponent(button2); + } + } + + private static List createData(int count) { + ArrayList data = new ArrayList(count); + for (int i = 0; i < count; i++) { + data.add(new TestObject("string-" + i, new Date(), i)); + } + return data; + } + + public static class TestObject { + + private String property1; + private Date property2; + private Integer property3; + + public TestObject(String property1, Date property2, Integer property3) { + this.property1 = property1; + this.property2 = property2; + this.property3 = property3; + } + + public String getProperty1() { + return property1; + } + + public Date getProperty2() { + return property2; + } + + public Integer getProperty3() { + return property3; + } + + } + +} -- 2.39.5