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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
/*
* 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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Before;
import org.junit.Test;
import com.vaadin.data.Container.Filter;
import com.vaadin.data.Container.Indexed;
import com.vaadin.data.Container.ItemSetChangeEvent;
import com.vaadin.data.Container.ItemSetChangeListener;
import com.vaadin.data.Container.PropertySetChangeEvent;
import com.vaadin.data.Container.PropertySetChangeListener;
import com.vaadin.data.Item;
import com.vaadin.data.sort.SortOrder;
import com.vaadin.data.util.filter.Compare;
import com.vaadin.data.util.filter.UnsupportedFilterException;
public class GeneratedPropertyContainerTest {
GeneratedPropertyContainer container;
Indexed wrappedContainer;
private static double MILES_CONVERSION = 0.6214d;
private class GeneratedPropertyListener implements
PropertySetChangeListener {
private int callCount = 0;
public int getCallCount() {
return callCount;
}
@Override
public void containerPropertySetChange(PropertySetChangeEvent event) {
++callCount;
assertEquals(
"Container for event was not GeneratedPropertyContainer",
event.getContainer(), container);
}
}
private class GeneratedItemSetListener implements ItemSetChangeListener {
private int callCount = 0;
public int getCallCount() {
return callCount;
}
@Override
public void containerItemSetChange(ItemSetChangeEvent event) {
++callCount;
assertEquals(
"Container for event was not GeneratedPropertyContainer",
event.getContainer(), container);
}
}
@Before
public void setUp() {
container = new GeneratedPropertyContainer(createContainer());
}
@Test
public void testSimpleGeneratedProperty() {
container.addGeneratedProperty("hello",
new PropertyValueGenerator<String>() {
@Override
public String getValue(Item item, Object itemId,
Object propertyId) {
return "Hello World!";
}
@Override
public Class<String> getType() {
return String.class;
}
});
Object itemId = container.addItem();
assertEquals("Expected value not in item.", container.getItem(itemId)
.getItemProperty("hello").getValue(), "Hello World!");
}
@Test
public void testSortableProperties() {
container.addGeneratedProperty("baz",
new PropertyValueGenerator<String>() {
@Override
public String getValue(Item item, Object itemId,
Object propertyId) {
return item.getItemProperty("foo").getValue() + " "
+ item.getItemProperty("bar").getValue();
}
@Override
public Class<String> getType() {
return String.class;
}
@Override
public SortOrder[] getSortProperties(SortOrder order) {
SortOrder[] sortOrder = new SortOrder[1];
sortOrder[0] = new SortOrder("bar", order
.getDirection());
return sortOrder;
}
});
container.sort(new Object[] { "baz" }, new boolean[] { true });
assertEquals("foo 0", container.getItem(container.getIdByIndex(0))
.getItemProperty("baz").getValue());
container.sort(new Object[] { "baz" }, new boolean[] { false });
assertEquals("foo 10", container.getItem(container.getIdByIndex(0))
.getItemProperty("baz").getValue());
}
@Test
public void testOverrideSortableProperties() {
assertTrue(container.getSortableContainerPropertyIds().contains("bar"));
container.addGeneratedProperty("bar",
new PropertyValueGenerator<String>() {
@Override
public String getValue(Item item, Object itemId,
Object propertyId) {
return item.getItemProperty("foo").getValue() + " "
+ item.getItemProperty("bar").getValue();
}
@Override
public Class<String> getType() {
return String.class;
}
});
assertFalse(container.getSortableContainerPropertyIds().contains("bar"));
}
@Test
public void testFilterByMiles() {
container.addGeneratedProperty("miles",
new PropertyValueGenerator<Double>() {
@Override
public Double getValue(Item item, Object itemId,
Object propertyId) {
return (Double) item.getItemProperty("km").getValue()
* MILES_CONVERSION;
}
@Override
public Class<Double> getType() {
return Double.class;
}
@Override
public Filter modifyFilter(Filter filter)
throws UnsupportedFilterException {
if (filter instanceof Compare.LessOrEqual) {
Double value = (Double) ((Compare.LessOrEqual) filter)
.getValue();
value = value / MILES_CONVERSION;
return new Compare.LessOrEqual("km", value);
}
return super.modifyFilter(filter);
}
});
for (Object itemId : container.getItemIds()) {
Item item = container.getItem(itemId);
Double km = (Double) item.getItemProperty("km").getValue();
Double miles = (Double) item.getItemProperty("miles").getValue();
assertTrue(miles.equals(km * MILES_CONVERSION));
}
Filter filter = new Compare.LessOrEqual("miles", MILES_CONVERSION);
container.addContainerFilter(filter);
for (Object itemId : container.getItemIds()) {
Item item = container.getItem(itemId);
assertTrue("Item did not pass original filter.",
filter.passesFilter(itemId, item));
}
assertTrue(container.getContainerFilters().contains(filter));
container.removeContainerFilter(filter);
assertFalse(container.getContainerFilters().contains(filter));
boolean allPass = true;
for (Object itemId : container.getItemIds()) {
Item item = container.getItem(itemId);
if (!filter.passesFilter(itemId, item)) {
allPass = false;
}
}
if (allPass) {
fail("Removing filter did not introduce any previous filtered items");
}
}
@Test
public void testPropertySetChangeNotifier() {
GeneratedPropertyListener listener = new GeneratedPropertyListener();
GeneratedPropertyListener removedListener = new GeneratedPropertyListener();
container.addPropertySetChangeListener(listener);
container.addPropertySetChangeListener(removedListener);
container.addGeneratedProperty("foo",
new PropertyValueGenerator<String>() {
@Override
public String getValue(Item item, Object itemId,
Object propertyId) {
return "";
}
@Override
public Class<String> getType() {
return String.class;
}
});
// Adding property to wrapped container should cause an event
wrappedContainer.addContainerProperty("baz", String.class, "");
container.removePropertySetChangeListener(removedListener);
container.removeGeneratedProperty("foo");
assertEquals("Listener was not called correctly.", 3,
listener.getCallCount());
assertEquals("Removed listener was not called correctly.", 2,
removedListener.getCallCount());
}
@Test
public void testItemSetChangeNotifier() {
GeneratedItemSetListener listener = new GeneratedItemSetListener();
container.addItemSetChangeListener(listener);
container.sort(new Object[] { "foo" }, new boolean[] { true });
container.sort(new Object[] { "foo" }, new boolean[] { false });
assertEquals("Listener was not called correctly.", 2,
listener.getCallCount());
}
@Test
public void testRemoveProperty() {
container.removeContainerProperty("foo");
assertFalse("Container contained removed property", container
.getContainerPropertyIds().contains("foo"));
assertTrue("Wrapped container did not contain removed property",
wrappedContainer.getContainerPropertyIds().contains("foo"));
assertFalse(container.getItem(container.firstItemId())
.getItemPropertyIds().contains("foo"));
container.addContainerProperty("foo", null, null);
assertTrue("Container did not contain returned property", container
.getContainerPropertyIds().contains("foo"));
}
private Indexed createContainer() {
wrappedContainer = new IndexedContainer();
wrappedContainer.addContainerProperty("foo", String.class, "foo");
wrappedContainer.addContainerProperty("bar", Integer.class, 0);
// km contains double values from 0.0 to 2.0
wrappedContainer.addContainerProperty("km", Double.class, 0);
for (int i = 0; i <= 10; ++i) {
Object itemId = wrappedContainer.addItem();
Item item = wrappedContainer.getItem(itemId);
item.getItemProperty("foo").setValue("foo");
item.getItemProperty("bar").setValue(i);
item.getItemProperty("km").setValue(i / 5.0d);
}
return wrappedContainer;
}
}
|