diff options
Diffstat (limited to 'server/src/test/java/com')
4 files changed, 279 insertions, 25 deletions
diff --git a/server/src/test/java/com/vaadin/data/BinderMultiSelectTest.java b/server/src/test/java/com/vaadin/data/BinderMultiSelectTest.java new file mode 100644 index 0000000000..93f4ccdac4 --- /dev/null +++ b/server/src/test/java/com/vaadin/data/BinderMultiSelectTest.java @@ -0,0 +1,180 @@ +/* + * Copyright 2000-2016 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; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.Collections; +import java.util.Locale; +import java.util.Set; +import java.util.concurrent.atomic.AtomicReference; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.junit.Before; +import org.junit.Test; + +import com.vaadin.data.util.converter.Converter; +import com.vaadin.tests.data.bean.BeanWithEnums; +import com.vaadin.tests.data.bean.TestEnum; +import com.vaadin.ui.CheckBoxGroup; + +public class BinderMultiSelectTest + extends BinderTestBase<Binder<BeanWithEnums>, BeanWithEnums> { + public class TestEnumSetToStringConverter + implements Converter<Set<TestEnum>, String> { + @Override + public Result<String> convertToModel(Set<TestEnum> value, + Locale locale) { + return Result.ok(value.stream().map(TestEnum::name) + .collect(Collectors.joining(","))); + } + + @Override + public Set<TestEnum> convertToPresentation(String value, + Locale locale) { + return Stream.of(value.split(",")) + .filter(string -> !string.isEmpty()).map(TestEnum::valueOf) + .collect(Collectors.toSet()); + } + } + + private Binder<AtomicReference<String>> converterBinder = new Binder<>(); + + private CheckBoxGroup<TestEnum> select; + + @Before + public void setUp() { + binder = new Binder<>(); + item = new BeanWithEnums(); + select = new CheckBoxGroup<>(); + select.setItems(TestEnum.values()); + + converterBinder.forSelect(select) + .withConverter(new TestEnumSetToStringConverter()) + .bind(AtomicReference<String>::get, + AtomicReference<String>::set); + } + + @Test + public void beanBound_bindSelectByShortcut_selectionUpdated() { + item.setEnums(Collections.singleton(TestEnum.ONE)); + binder.bind(item); + binder.bind(select, BeanWithEnums::getEnums, BeanWithEnums::setEnums); + + assertEquals(Collections.singleton(TestEnum.ONE), + select.getSelectedItems()); + } + + @Test + public void beanBound_bindSelect_selectionUpdated() { + item.setEnums(Collections.singleton(TestEnum.TWO)); + binder.bind(item); + binder.forSelect(select).bind(BeanWithEnums::getEnums, + BeanWithEnums::setEnums); + + assertEquals(Collections.singleton(TestEnum.TWO), + select.getSelectedItems()); + } + + @Test + public void selectBound_bindBeanWithoutEnums_selectedItemNotPresent() { + bindEnum(); + + assertTrue(select.getSelectedItems().isEmpty()); + } + + @Test + public void selectBound_bindBean_selectionUpdated() { + item.setEnums(Collections.singleton(TestEnum.ONE)); + bindEnum(); + + assertEquals(Collections.singleton(TestEnum.ONE), + select.getSelectedItems()); + } + + @Test + public void bound_setSelection_beanValueUpdated() { + bindEnum(); + + select.select(TestEnum.TWO); + + assertEquals(Collections.singleton(TestEnum.TWO), item.getEnums()); + } + + @Test + public void bound_deselect_beanValueUpdatedToNull() { + item.setEnums(Collections.singleton(TestEnum.ONE)); + bindEnum(); + + select.deselect(TestEnum.ONE); + + assertTrue(item.getEnums().isEmpty()); + } + + @Test + public void unbound_changeSelection_beanValueNotUpdated() { + item.setEnums(Collections.singleton(TestEnum.ONE)); + bindEnum(); + binder.unbind(); + + select.select(TestEnum.TWO); + + assertEquals(Collections.singleton(TestEnum.ONE), item.getEnums()); + } + + @Test + public void withConverter_load_selectUpdated() { + converterBinder.load(new AtomicReference<>("TWO")); + + assertEquals(Collections.singleton(TestEnum.TWO), + select.getSelectionModel().getSelectedItems()); + } + + @Test + public void withConverter_save_referenceUpdated() { + select.select(TestEnum.ONE); + select.select(TestEnum.TWO); + + AtomicReference<String> reference = new AtomicReference<>(""); + converterBinder.saveIfValid(reference); + + assertEquals("ONE,TWO", reference.get()); + } + + @Test + public void withValidator_validate_validatorUsed() { + binder.forSelect(select) + .withValidator(selection -> selection.size() % 2 == 1, + "Must select odd number of items") + .bind(BeanWithEnums::getEnums, BeanWithEnums::setEnums); + binder.bind(item); + + assertFalse(binder.validate().isOk()); + + select.select(TestEnum.TWO); + + assertTrue(binder.validate().isOk()); + } + + protected void bindEnum() { + binder.forSelect(select).bind(BeanWithEnums::getEnums, + BeanWithEnums::setEnums); + binder.bind(item); + } +} diff --git a/server/src/test/java/com/vaadin/tests/data/bean/BeanWithEnums.java b/server/src/test/java/com/vaadin/tests/data/bean/BeanWithEnums.java new file mode 100644 index 0000000000..b0b612c200 --- /dev/null +++ b/server/src/test/java/com/vaadin/tests/data/bean/BeanWithEnums.java @@ -0,0 +1,31 @@ +/* + * Copyright 2000-2016 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.tests.data.bean; + +import java.util.HashSet; +import java.util.Set; + +public class BeanWithEnums { + private Set<TestEnum> enums = new HashSet<>(); + + public Set<TestEnum> getEnums() { + return enums; + } + + public void setEnums(Set<TestEnum> enums) { + this.enums = enums; + } +} diff --git a/server/src/test/java/com/vaadin/ui/CheckBoxGroupTest.java b/server/src/test/java/com/vaadin/ui/CheckBoxGroupTest.java index 0ad1801e8b..192dcb3d52 100644 --- a/server/src/test/java/com/vaadin/ui/CheckBoxGroupTest.java +++ b/server/src/test/java/com/vaadin/ui/CheckBoxGroupTest.java @@ -17,22 +17,31 @@ package com.vaadin.ui; import java.util.ArrayList; import java.util.Arrays; +import java.util.concurrent.atomic.AtomicInteger; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; import com.vaadin.server.data.DataSource; import com.vaadin.shared.data.selection.SelectionModel.Multi; +import com.vaadin.shared.data.selection.SelectionServerRpc; public class CheckBoxGroupTest { - @Test - public void stableSelectionOrder() { - CheckBoxGroup<String> checkBoxGroup = new CheckBoxGroup<>(); + private CheckBoxGroup<String> checkBoxGroup; + private Multi<String> selectionModel; + + @Before + public void setUp() { + checkBoxGroup = new CheckBoxGroup<>(); // Intentional deviation from upcoming selection order checkBoxGroup .setDataSource(DataSource.create("Third", "Second", "First")); - Multi<String> selectionModel = checkBoxGroup.getSelectionModel(); + selectionModel = checkBoxGroup.getSelectionModel(); + } + @Test + public void stableSelectionOrder() { selectionModel.select("First"); selectionModel.select("Second"); selectionModel.select("Third"); @@ -46,6 +55,48 @@ public class CheckBoxGroupTest { assertSelectionOrder(selectionModel, "Second", "Third", "First"); } + @Test + public void apiSelectionChange_notUserOriginated() { + AtomicInteger listenerCount = new AtomicInteger(0); + + checkBoxGroup.addSelectionListener(event -> { + listenerCount.incrementAndGet(); + Assert.assertFalse(event.isUserOriginated()); + }); + + checkBoxGroup.select("First"); + checkBoxGroup.select("Second"); + + checkBoxGroup.deselect("Second"); + checkBoxGroup.getSelectionModel().deselectAll(); + + Assert.assertEquals(4, listenerCount.get()); + } + + @Test + public void rpcSelectionChange_userOriginated() { + AtomicInteger listenerCount = new AtomicInteger(0); + + checkBoxGroup.addSelectionListener(event -> { + listenerCount.incrementAndGet(); + Assert.assertTrue(event.isUserOriginated()); + }); + + SelectionServerRpc rpc = ComponentTest.getRpcProxy(checkBoxGroup, + SelectionServerRpc.class); + + rpc.select(getItemKey("First")); + rpc.select(getItemKey("Second")); + rpc.deselect(getItemKey("Second")); + + Assert.assertEquals(3, listenerCount.get()); + } + + private String getItemKey(String dataObject) { + return checkBoxGroup.getDataCommunicator().getKeyMapper() + .key(dataObject); + } + private static void assertSelectionOrder(Multi<String> selectionModel, String... selectionOrder) { Assert.assertEquals(Arrays.asList(selectionOrder), diff --git a/server/src/test/java/com/vaadin/ui/ComponentTest.java b/server/src/test/java/com/vaadin/ui/ComponentTest.java index 8cd9afa776..cc58dbbf93 100644 --- a/server/src/test/java/com/vaadin/ui/ComponentTest.java +++ b/server/src/test/java/com/vaadin/ui/ComponentTest.java @@ -15,12 +15,10 @@ */ package com.vaadin.ui; -import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; -import java.lang.reflect.Proxy; import com.vaadin.server.ClientConnector; -import com.vaadin.server.ServerRpcMethodInvocation; +import com.vaadin.server.ServerRpcManager; import com.vaadin.shared.communication.ServerRpc; /** @@ -67,32 +65,26 @@ public class ComponentTest { } /** - * Gets a proxy object which invokes ServerRpc methods. + * Gets the server rpc handler registered for a component. * * @param component * the component which listens to the RPC * @param serverRpcClass * the server RPC class - * @return a proxy which can be used to invoke RPC methods + * @return the server RPC handler */ - @SuppressWarnings("unchecked") public static <T extends ServerRpc> T getRpcProxy(Component component, Class<T> serverRpcClass) { - return (T) Proxy.newProxyInstance(component.getClass().getClassLoader(), - new Class[] { serverRpcClass }, new InvocationHandler() { - - @Override - public Object invoke(Object proxy, Method method, - Object[] args) throws Throwable { - ServerRpcMethodInvocation invocation = new ServerRpcMethodInvocation( - component.getConnectorId(), serverRpcClass, - method.getName(), args.length); - invocation.setParameters(args); - component.getRpcManager(serverRpcClass.getName()) - .applyInvocation(invocation); - return null; - } - }); + try { + ServerRpcManager<?> rpcManager = component + .getRpcManager(serverRpcClass.getName()); + Method method = ServerRpcManager.class + .getDeclaredMethod("getImplementation"); + method.setAccessible(true); + return serverRpcClass.cast(method.invoke(rpcManager)); + } catch (ReflectiveOperationException e) { + throw new RuntimeException(e); + } } } |