]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add null check to FieldGroup.bind. (#14729)
authorSauli Tähkäpää <sauli@vaadin.com>
Fri, 26 Sep 2014 10:41:12 +0000 (13:41 +0300)
committerAnna Koskinen <anna@vaadin.com>
Wed, 5 Nov 2014 15:53:48 +0000 (17:53 +0200)
Change-Id: I56ee44f34307d76c8c98ca3346feed8e7ee2f72e

server/src/com/vaadin/data/fieldgroup/FieldGroup.java
server/tests/src/com/vaadin/data/fieldgroup/FieldGroupTests.java [new file with mode: 0644]

index 6f3a67a92a69393f3134db51d5ed942399c20a63..5a4e8775542e91aff59b55bdec5fac7da6ad8291 100644 (file)
@@ -245,15 +245,13 @@ public class FieldGroup implements Serializable {
      * @param propertyId
      *            The propertyId to bind to the field
      * @throws BindException
-     *             If the property id is already bound to another field by this
-     *             field binder
+     *             If the field is null or the property id is already bound to
+     *             another field by this field binder
      */
     public void bind(Field<?> field, Object propertyId) throws BindException {
-        if (propertyIdToField.containsKey(propertyId)
-                && propertyIdToField.get(propertyId) != field) {
-            throw new BindException("Property id " + propertyId
-                    + " is already bound to another field");
-        }
+        throwIfFieldIsNull(field, propertyId);
+        throwIfPropertyIdAlreadyBound(field, propertyId);
+
         fieldToPropertyId.put(field, propertyId);
         propertyIdToField.put(propertyId, field);
         if (itemDataSource == null) {
@@ -265,6 +263,23 @@ public class FieldGroup implements Serializable {
         configureField(field);
     }
 
+    private void throwIfFieldIsNull(Field<?> field, Object propertyId) {
+        if (field == null) {
+            throw new BindException(
+                    String.format(
+                            "Cannot bind property id '%s' to a null field.",
+                            propertyId));
+        }
+    }
+
+    private void throwIfPropertyIdAlreadyBound(Field<?> field, Object propertyId) {
+        if (propertyIdToField.containsKey(propertyId)
+                && propertyIdToField.get(propertyId) != field) {
+            throw new BindException("Property id " + propertyId
+                    + " is already bound to another field");
+        }
+    }
+
     private <T> Property.Transactional<T> wrapInTransactionalProperty(
             Property<T> itemProperty) {
         return new TransactionalPropertyWrapper<T>(itemProperty);
diff --git a/server/tests/src/com/vaadin/data/fieldgroup/FieldGroupTests.java b/server/tests/src/com/vaadin/data/fieldgroup/FieldGroupTests.java
new file mode 100644 (file)
index 0000000..eb8f21c
--- /dev/null
@@ -0,0 +1,40 @@
+package com.vaadin.data.fieldgroup;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import static org.mockito.Mockito.mock;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import com.vaadin.ui.Field;
+
+public class FieldGroupTests {
+
+    private FieldGroup sut;
+    private Field field;
+
+    @Before
+    public void setup() {
+        sut = new FieldGroup();
+        field = mock(Field.class);
+    }
+
+    @Test
+    public void fieldIsBound() {
+        sut.bind(field, "foobar");
+
+        assertThat(sut.getField("foobar"), is(field));
+    }
+
+    @Test(expected = FieldGroup.BindException.class)
+    public void cannotBindToAlreadyBoundProperty() {
+        sut.bind(field, "foobar");
+        sut.bind(mock(Field.class), "foobar");
+    }
+
+    @Test(expected = FieldGroup.BindException.class)
+    public void cannotBindNullField() {
+        sut.bind(null, "foobar");
+    }
+}