summaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/tickets/Ticket8291.java
blob: 0280db0693c98e6d4c4064766e0473a97efb1b3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.vaadin.tests.tickets;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.vaadin.data.Container.Filter;
import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.server.WrappedRequest;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.UI;
import com.vaadin.ui.Table;

/**
 * Test for #8291 and #7666: NegativeArraySizeException when Table scrolled to
 * the end and its size reduced.
 */
public class Ticket8291 extends UI {

    @Override
    public void init(WrappedRequest request) {
        setContent(new TestView());
    }

    private static class DecimateFilter implements Filter {
        @Override
        public boolean passesFilter(Object itemId, Item item)
                throws UnsupportedOperationException {
            return ((((TestObject) itemId).property3 % 10) == 0);
        }

        @Override
        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<TestObject> data = createData(1000);
            final BeanItemContainer<TestObject> container = new BeanItemContainer<TestObject>(
                    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() {
                @Override
                public void buttonClick(ClickEvent event) {
                    reduceData = !reduceData;
                    table.refreshRowCache();
                }
            });
            addComponent(button);
            Button button2 = new Button("Filter");
            button2.addListener(new Button.ClickListener() {
                @Override
                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<TestObject> createData(int count) {
        ArrayList<TestObject> data = new ArrayList<TestObject>(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;
        }

    }

}