2 * Copyright 2000-2014 Vaadin Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
16 package com.vaadin.tests.server.component.abstractselect;
18 import java.util.Collection;
19 import java.util.Collections;
21 import org.junit.Assert;
22 import org.junit.Test;
24 import com.vaadin.data.util.BeanItemContainer;
25 import com.vaadin.ui.AbstractSelect;
27 public class TestAbstractSelectValueUpdate {
30 public void removeItem_deleteItemFromUnderlyingContainer_selectValueIsUpdated() {
31 BeanItemContainer<Object> container = new BeanItemContainer<Object>(
33 Object item1 = new Object();
34 Object item2 = new Object();
35 container.addBean(item1);
36 container.addBean(item2);
37 TestSelect select = new TestSelect();
38 select.setContainerDataSource(container);
40 select.setValue(item1);
42 Assert.assertNotNull("Value is null after selection", select.getValue());
44 container.removeItem(item1);
46 Assert.assertNull("Value is not null after removal", select.getValue());
50 public void removeItem_multiselectSectionDeleteItemFromUnderlyingContainer_selectValueIsUpdated() {
51 BeanItemContainer<Object> container = new BeanItemContainer<Object>(
53 Object item1 = new Object();
54 Object item2 = new Object();
55 container.addBean(item1);
56 container.addBean(item2);
57 TestSelect select = new TestSelect();
58 select.setMultiSelect(true);
59 select.setContainerDataSource(container);
61 select.setValue(Collections.singletonList(item1));
63 checkSelectedItemsCount(select, 1);
65 container.removeItem(item1);
67 checkSelectedItemsCount(select, 0);
70 private void checkSelectedItemsCount(TestSelect select, int count) {
71 Assert.assertNotNull("Selected value is null", select.getValue());
72 Assert.assertTrue("Selected value is not a collection",
73 select.getValue() instanceof Collection);
74 Assert.assertEquals("Wrong number of selected items",
75 ((Collection<?>) select.getValue()).size(), count);
78 private class TestSelect extends AbstractSelect {