summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorArtur <artur@vaadin.com>2017-05-19 13:05:11 +0300
committerHenri Sara <henri.sara@gmail.com>2017-05-19 13:05:11 +0300
commitdfe4e3bf6f66b075c96ac2b0f30c0e79bd04ee6d (patch)
treead588425dbf4f79aa977435256ef61e3768dc45a /server
parent9b72cbfa113c48f2c396055f8ff04970d2ae3aa1 (diff)
downloadvaadin-framework-dfe4e3bf6f66b075c96ac2b0f30c0e79bd04ee6d.tar.gz
vaadin-framework-dfe4e3bf6f66b075c96ac2b0f30c0e79bd04ee6d.zip
Add a method for getting bound fields from Binder (#9365)
Diffstat (limited to 'server')
-rw-r--r--server/src/main/java/com/vaadin/data/Binder.java10
-rw-r--r--server/src/test/java/com/vaadin/data/BinderTest.java37
2 files changed, 36 insertions, 11 deletions
diff --git a/server/src/main/java/com/vaadin/data/Binder.java b/server/src/main/java/com/vaadin/data/Binder.java
index 56bb9a6656..676030ec22 100644
--- a/server/src/main/java/com/vaadin/data/Binder.java
+++ b/server/src/main/java/com/vaadin/data/Binder.java
@@ -2361,4 +2361,14 @@ public class Binder<BEAN> implements Serializable {
private <V> void fireValueChangeEvent(ValueChangeEvent<V> event) {
getEventRouter().fireEvent(event);
}
+
+ /**
+ * Returns the fields this binder has been bound to.
+ *
+ * @return the fields with bindings
+ * @since
+ */
+ public Stream<HasValue<?>> getFields() {
+ return bindings.stream().map(Binding::getField);
+ }
}
diff --git a/server/src/test/java/com/vaadin/data/BinderTest.java b/server/src/test/java/com/vaadin/data/BinderTest.java
index ee92c86f58..89392be3e3 100644
--- a/server/src/test/java/com/vaadin/data/BinderTest.java
+++ b/server/src/test/java/com/vaadin/data/BinderTest.java
@@ -10,6 +10,7 @@ import static org.junit.Assert.assertTrue;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.Stream;
import org.junit.Assert;
import org.junit.Before;
@@ -50,7 +51,7 @@ public class BinderTest extends BinderTestBase<Binder<Person>, Person> {
.withConverter(new StringToIntegerConverter(""))
.bind(Person::getAge, Person::setAge);
binder.setBean(item);
- assertEquals("No name field value","Johannes", nameField.getValue());
+ assertEquals("No name field value", "Johannes", nameField.getValue());
assertEquals("No age field value", "32", ageField.getValue());
binder.setBean(null);
@@ -67,7 +68,7 @@ public class BinderTest extends BinderTestBase<Binder<Person>, Person> {
.bind(Person::getAge, Person::setAge);
binder.readBean(item);
- assertEquals("No name field value","Johannes", nameField.getValue());
+ assertEquals("No name field value", "Johannes", nameField.getValue());
assertEquals("No age field value", "32", ageField.getValue());
binder.readBean(null);
@@ -88,7 +89,7 @@ public class BinderTest extends BinderTestBase<Binder<Person>, Person> {
binder.setBean(null);
- assertEquals("ReadOnly field not empty","", nameField.getValue());
+ assertEquals("ReadOnly field not empty", "", nameField.getValue());
}
@Test
@@ -586,15 +587,13 @@ public class BinderTest extends BinderTestBase<Binder<Person>, Person> {
@Test
public void isValidTest_bound_binder() {
binder.forField(nameField)
- .withValidator(
- Validator.from(
- name -> !name.equals("fail field validation"),
- ""))
+ .withValidator(Validator.from(
+ name -> !name.equals("fail field validation"), ""))
.bind(Person::getFirstName, Person::setFirstName);
- binder.withValidator(
- Validator.from(person -> !person.getFirstName()
- .equals("fail bean validation"), ""));
+ binder.withValidator(Validator.from(
+ person -> !person.getFirstName().equals("fail bean validation"),
+ ""));
binder.setBean(item);
@@ -635,4 +634,20 @@ public class BinderTest extends BinderTestBase<Binder<Person>, Person> {
""));
binder.isValid();
}
-} \ No newline at end of file
+
+ @Test
+ public void getFields_returnsFields() {
+ Assert.assertEquals(0, binder.getFields().count());
+ binder.forField(nameField).bind(Person::getFirstName,
+ Person::setFirstName);
+ assertStreamEquals(Stream.of(nameField), binder.getFields());
+ binder.forField(ageField)
+ .withConverter(new StringToIntegerConverter(""))
+ .bind(Person::getAge, Person::setAge);
+ assertStreamEquals(Stream.of(nameField, ageField), binder.getFields());
+ }
+
+ private void assertStreamEquals(Stream<?> s1, Stream<?> s2) {
+ Assert.assertArrayEquals(s1.toArray(), s2.toArray());
+ }
+}