]> source.dussan.org Git - vaadin-framework.git/commitdiff
Allow BeanFieldGroup.setItemDataSource() method accept null (#14731).
authorDenis Anisimov <denis@vaadin.com>
Mon, 29 Sep 2014 17:40:36 +0000 (20:40 +0300)
committerVaadin Code Review <review@vaadin.com>
Tue, 2 Dec 2014 18:01:12 +0000 (18:01 +0000)
Change-Id: I7a451f428f0aa3ea7fc322cac6b0e62d830d7987

server/src/com/vaadin/data/fieldgroup/BeanFieldGroup.java
server/src/com/vaadin/data/util/BeanItem.java
server/src/com/vaadin/data/util/MethodProperty.java
server/tests/src/com/vaadin/tests/server/component/fieldgroup/BeanFieldGroupTest.java

index e5d53b759df56526cfbbec91f5b7f11526609315..1f3ee36d58b9e110f0d95015cfd6d758953ad8e9 100644 (file)
@@ -130,16 +130,17 @@ public class BeanFieldGroup<T> extends FieldGroup {
      *            The bean to use as data source.
      */
     public void setItemDataSource(T bean) {
-        setItemDataSource(new BeanItem(bean));
+        setItemDataSource(new BeanItem<T>(bean, beanType));
     }
 
     @Override
     public void setItemDataSource(Item item) {
-        if (!(item instanceof BeanItem)) {
+        if (item == null || (item instanceof BeanItem)) {
+            super.setItemDataSource(item);
+        } else {
             throw new RuntimeException(getClass().getSimpleName()
                     + " only supports BeanItems as item data source");
         }
-        super.setItemDataSource(item);
     }
 
     @Override
index 12d9b23d0af5ef2279364fe93bad0ca023f7bb04..64f30261c27c786997d0e091d2516b4bc6457b9e 100644 (file)
@@ -62,7 +62,30 @@ public class BeanItem<BT> extends PropertysetItem {
      * 
      */
     public BeanItem(BT bean) {
-        this(bean, getPropertyDescriptors((Class<BT>) bean.getClass()));
+        this(bean, (Class<BT>) bean.getClass());
+    }
+
+    /**
+     * <p>
+     * Creates a new instance of <code>BeanItem</code> and adds all properties
+     * of a Java Bean to it. The properties are identified by their respective
+     * bean names.
+     * </p>
+     * 
+     * <p>
+     * Note : This version only supports introspectable bean properties and
+     * their getter and setter methods. Stand-alone <code>is</code> and
+     * <code>are</code> methods are not supported.
+     * </p>
+     * 
+     * @param bean
+     *            the Java Bean to copy properties from.
+     * @param beanClass
+     *            class of the {@code bean}
+     * 
+     */
+    public BeanItem(BT bean, Class<BT> beanClass) {
+        this(bean, getPropertyDescriptors(beanClass));
     }
 
     /**
index 5e6b731571af24a63353c0a837afc421bd0b9eef..853f68b711b4f2be391c597ec1b60be6ad52a70a 100644 (file)
@@ -132,7 +132,7 @@ public class MethodProperty<T> extends AbstractProperty<T> {
             setArguments(getArgs, setArgs, setArgumentIndex);
             String name = (String) in.readObject();
             Class<?>[] paramTypes = SerializerHelper.readClassArray(in);
-            if (name != null) {
+            if (instance != null && name != null) {
                 setMethod = instance.getClass().getMethod(name, paramTypes);
             } else {
                 setMethod = null;
@@ -140,7 +140,7 @@ public class MethodProperty<T> extends AbstractProperty<T> {
 
             name = (String) in.readObject();
             paramTypes = SerializerHelper.readClassArray(in);
-            if (name != null) {
+            if (instance != null && name != null) {
                 getMethod = instance.getClass().getMethod(name, paramTypes);
             } else {
                 getMethod = null;
@@ -589,7 +589,11 @@ public class MethodProperty<T> extends AbstractProperty<T> {
     @Override
     public T getValue() {
         try {
-            return (T) getMethod.invoke(instance, getArgs);
+            if (instance == null) {
+                return null;
+            } else {
+                return (T) getMethod.invoke(instance, getArgs);
+            }
         } catch (final Throwable e) {
             throw new MethodException(this, e);
         }
index 112d36d8843cd6fabce58d6ec0ab4c56f1babbba..965fb4947923b5bcde6a59e63fdffd4b75f214ec 100644 (file)
@@ -2,12 +2,16 @@ package com.vaadin.tests.server.component.fieldgroup;
 
 import static org.junit.Assert.assertEquals;
 
+import java.util.Collection;
+
 import org.junit.Assert;
 import org.junit.Test;
 
+import com.vaadin.data.Item;
 import com.vaadin.data.fieldgroup.BeanFieldGroup;
 import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
 import com.vaadin.data.fieldgroup.PropertyId;
+import com.vaadin.data.util.BeanItem;
 import com.vaadin.ui.Field;
 import com.vaadin.ui.TextField;
 
@@ -133,4 +137,31 @@ public class BeanFieldGroupTest {
         assertEquals(bean.nestedBean.hello, helloField.getValue().toString());
     }
 
+    @Test
+    public void setDataSource_nullBean_nullBeanIsSetInDataSource() {
+        BeanFieldGroup<MyBean> group = new BeanFieldGroup<MyBean>(MyBean.class);
+
+        group.setItemDataSource((MyBean) null);
+
+        BeanItem<MyBean> dataSource = group.getItemDataSource();
+        Assert.assertNotNull("Data source is null for null bean", dataSource);
+
+        Collection<?> itemPropertyIds = dataSource.getItemPropertyIds();
+        Assert.assertEquals("Unexpected number of properties", 3,
+                itemPropertyIds.size());
+        for (Object id : itemPropertyIds) {
+            Assert.assertNull("Value for property " + id + " is not null",
+                    dataSource.getItemProperty(id).getValue());
+        }
+    }
+
+    @Test
+    public void setDataSource_nullItem_nullDataSourceIsSet() {
+        BeanFieldGroup<MyBean> group = new BeanFieldGroup<MyBean>(MyBean.class);
+
+        group.setItemDataSource((Item) null);
+        BeanItem<MyBean> dataSource = group.getItemDataSource();
+        Assert.assertNull("Group returns not null data source", dataSource);
+    }
+
 }