Support for null values in NestedMethodProperty was already implemented in master branch (#11435)
This changeset includes changesets made for that.
Change-Id: I10696467f792e234326075bbcdd5aad487905a7e
Merge: no
* Adds a nested container property for the container, e.g.
* "manager.address.street".
*
- * All intermediate getters must exist and must return non-null values when
- * the property value is accessed.
+ * All intermediate getters must exist and should return non-null values
+ * when the property value is accessed. If an intermediate getter returns
+ * null, a null value will be returned.
*
* @see NestedMethodProperty
*
* property to the container. The named property itself is removed from the
* model as its subproperties are added.
*
- * All intermediate getters must exist and must return non-null values when
- * the property value is accessed.
+ * All intermediate getters must exist and should return non-null values
+ * when the property value is accessed. If an intermediate getter returns
+ * null, a null value will be returned.
*
* @see NestedMethodProperty
* @see #addNestedContainerProperty(String)
}
/**
- * Adds a nested property to the item.
+ * Adds a nested property to the item. The property must not exist in the
+ * item already and must of form "field1.field2" where field2 is a field in
+ * the object referenced to by field1. If an intermediate property returns
+ * null, the property will return a null value
*
* @param nestedPropertyId
- * property id to add. This property must not exist in the item
- * already and must of of form "field1.field2" where field2 is a
- * field in the object referenced to by field1
+ * property id to add.
*/
public void addNestedProperty(String nestedPropertyId) {
addItemProperty(nestedPropertyId, new NestedMethodProperty<Object>(
* The property is specified in the dotted notation, e.g. "address.street", and
* can contain multiple levels of nesting.
*
- * When accessing the property value, all intermediate getters must return
- * non-null values.
+ * When accessing the property value, all intermediate getters must exist and
+ * should return non-null values when the property value is accessed. If an
+ * intermediate getter returns null, a null value will be returned.
*
* @see MethodProperty
*
* Constructs a nested method property for a given object instance. The
* property name is a dot separated string pointing to a nested property,
* e.g. "manager.address.street".
+ * <p>
+ * Calling getValue will return null if any intermediate getter returns null
*
* @param instance
* top-level bean to which the property applies
Object object = instance;
for (Method m : getMethods) {
object = m.invoke(object);
+ if (object == null) {
+ return null;
+ }
}
return (T) object;
} catch (final Throwable e) {
.getValue());
}
+ public void testNestedContainerPropertyWithNullBean() {
+ BeanContainer<String, NestedMethodPropertyTest.Person> container = new BeanContainer<String, NestedMethodPropertyTest.Person>(
+ NestedMethodPropertyTest.Person.class);
+ container.setBeanIdProperty("name");
+
+ container.addBean(new NestedMethodPropertyTest.Person("John", null));
+ assertTrue(container
+ .addNestedContainerProperty("address.postalCodeObject"));
+ assertTrue(container.addNestedContainerProperty("address.street"));
+ assertNull(container.getContainerProperty("John", "address.street")
+ .getValue());
+ }
+
}
.getValue());
}
+ public void testNestedContainerPropertyWithNullBean() {
+ BeanItemContainer<NestedMethodPropertyTest.Person> container = new BeanItemContainer<NestedMethodPropertyTest.Person>(
+ NestedMethodPropertyTest.Person.class);
+ NestedMethodPropertyTest.Person john = new NestedMethodPropertyTest.Person(
+ "John", null);
+ assertNotNull(container.addBean(john));
+ assertTrue(container
+ .addNestedContainerProperty("address.postalCodeObject"));
+ assertTrue(container.addNestedContainerProperty("address.street"));
+ assertNull(container.getContainerProperty(john, "address.street")
+ .getValue());
+ }
}
import java.io.ObjectOutputStream;
import java.io.Serializable;
-import junit.framework.Assert;
import junit.framework.TestCase;
+import org.junit.Assert;
+
public class NestedMethodPropertyTest extends TestCase {
public static class Address implements Serializable {
vaadin, "manager.address.street");
joonas.setAddress(null);
- try {
- streetProperty.getValue();
- fail();
- } catch (Exception e) {
- // should get exception
- }
+ assertNull(streetProperty.getValue());
vaadin.setManager(null);
- try {
- managerNameProperty.getValue();
- fail();
- } catch (Exception e) {
- // should get exception
- }
- try {
- streetProperty.getValue();
- fail();
- } catch (Exception e) {
- // should get exception
- }
+ assertNull(managerNameProperty.getValue());
+ assertNull(streetProperty.getValue());
vaadin.setManager(joonas);
Assert.assertEquals("Joonas", managerNameProperty.getValue());
+ Assert.assertNull(streetProperty.getValue());
+
}
public void testMultiLevelNestedPropertySetValue() {
Assert.assertEquals("Ruukinkatu 2-4", property2.getValue());
}
+ public void testSerializationWithIntermediateNull() throws IOException,
+ ClassNotFoundException {
+ vaadin.setManager(null);
+ NestedMethodProperty<String> streetProperty = new NestedMethodProperty<String>(
+ vaadin, "manager.address.street");
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ new ObjectOutputStream(baos).writeObject(streetProperty);
+ @SuppressWarnings("unchecked")
+ NestedMethodProperty<String> property2 = (NestedMethodProperty<String>) new ObjectInputStream(
+ new ByteArrayInputStream(baos.toByteArray())).readObject();
+
+ Assert.assertNull(property2.getValue());
+ }
+
public void testIsReadOnly() {
NestedMethodProperty<String> streetProperty = new NestedMethodProperty<String>(
vaadin, "manager.address.street");
Assert.assertEquals("John", property.getValue());
}
- public void testNestedPropertyDescriptorSerialization() throws Exception {
+ public void testSimpleNestedPropertyDescriptorSerialization()
+ throws Exception {
NestedPropertyDescriptor<Person> pd = new NestedPropertyDescriptor<Person>(
"name", Person.class);
Property<?> property = pd2.createProperty(new Person("John", null));
Assert.assertEquals("John", property.getValue());
}
+
+ public void testNestedPropertyDescriptorSerialization() throws Exception {
+ NestedPropertyDescriptor<Person> pd = new NestedPropertyDescriptor<Person>(
+ "address.street", Person.class);
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ new ObjectOutputStream(baos).writeObject(pd);
+ @SuppressWarnings("unchecked")
+ VaadinPropertyDescriptor<Person> pd2 = (VaadinPropertyDescriptor<Person>) new ObjectInputStream(
+ new ByteArrayInputStream(baos.toByteArray())).readObject();
+
+ Property<?> property = pd2.createProperty(new Person("John", null));
+ Assert.assertNull(property.getValue());
+ }
+
}