aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArtur Signell <artur@vaadin.com>2016-09-05 11:26:29 +0300
committerArtur Signell <artur@vaadin.com>2016-09-05 14:12:25 +0300
commita253d8b94d88ec2f0c28ba2399262d942980ba07 (patch)
tree9a8bdb38e9c3ab8fae05f0b9592adc402ae49a4a
parent504b3c4c65c6a2ccfeeb1501eff7a29f5a3dbec3 (diff)
downloadvaadin-framework-a253d8b94d88ec2f0c28ba2399262d942980ba07.tar.gz
vaadin-framework-a253d8b94d88ec2f0c28ba2399262d942980ba07.zip
Add documentation test for custom converters
Change-Id: I84621b6b568095b1b7f86068fa992dd570f8a103
-rw-r--r--documentation/datamodel/datamodel-forms.asciidoc8
-rw-r--r--server/src/test/java/com/vaadin/data/BinderBookOfVaadinTest.java47
2 files changed, 51 insertions, 4 deletions
diff --git a/documentation/datamodel/datamodel-forms.asciidoc b/documentation/datamodel/datamodel-forms.asciidoc
index 5ec548e8be..b86cf8f672 100644
--- a/documentation/datamodel/datamodel-forms.asciidoc
+++ b/documentation/datamodel/datamodel-forms.asciidoc
@@ -282,19 +282,19 @@ Another option is to directly implement the [interfacename]#Converter# interface
----
class MyConverter implements Converter<String, Integer> {
@Override
- public Result<Integer> fromField(String fieldValue) {
+ public Result<Integer> convertToModel(String fieldValue, Locale locale) {
// Produces a converted value or an error
try {
// ok is a static helper method that creates a Result
- return ok(Integer.valueOf(fieldValue));
+ return Result.ok(Integer.valueOf(fieldValue));
} catch (NumberFormatException e) {
// error is a static helper method that creates a Result
- return error("Please enter a number");
+ return Result.error("Please enter a number");
}
}
@Override
- public String toField(Integer integer) {
+ public String convertToPresentation(Integer integer, Locale locale) {
// Converting to the field type should always succeed,
// so there is no support for returning an error Result.
return String.valueOf(integer);
diff --git a/server/src/test/java/com/vaadin/data/BinderBookOfVaadinTest.java b/server/src/test/java/com/vaadin/data/BinderBookOfVaadinTest.java
index 02a32135f6..c51d41d6a7 100644
--- a/server/src/test/java/com/vaadin/data/BinderBookOfVaadinTest.java
+++ b/server/src/test/java/com/vaadin/data/BinderBookOfVaadinTest.java
@@ -18,6 +18,7 @@ package com.vaadin.data;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
+import java.util.Locale;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Assert;
@@ -25,6 +26,7 @@ import org.junit.Before;
import org.junit.Test;
import com.vaadin.data.Binder.Binding;
+import com.vaadin.data.util.converter.Converter;
import com.vaadin.data.util.converter.StringToIntegerConverter;
import com.vaadin.data.validator.EmailValidator;
import com.vaadin.server.AbstractErrorMessage;
@@ -508,4 +510,49 @@ public class BinderBookOfVaadinTest {
Assert.assertEquals(1950, person.getYearOfBirth());
}
+ class MyConverter implements Converter<String, Integer> {
+ @Override
+ public Result<Integer> convertToModel(String fieldValue,
+ Locale locale) {
+ // Produces a converted value or an error
+ try {
+ // ok is a static helper method that creates a Result
+ return Result.ok(Integer.valueOf(fieldValue));
+ } catch (NumberFormatException e) {
+ // error is a static helper method that creates a Result
+ return Result.error("Please enter a number");
+ }
+ }
+
+ @Override
+ public String convertToPresentation(Integer integer, Locale locale) {
+ // Converting to the field type should always succeed,
+ // so there is no support for returning an error Result.
+ return String.valueOf(integer);
+ }
+ }
+
+ @Test
+ public void bindUsingCustomConverter() {
+ Binder<BookPerson> binder = new Binder<>();
+ TextField yearOfBirthField = new TextField();
+
+ // Using the converter
+ binder.forField(yearOfBirthField).withConverter(new MyConverter())
+ .bind(BookPerson::getYearOfBirth, BookPerson::setYearOfBirth);
+
+ BookPerson p = new BookPerson(1500, 12);
+ binder.bind(p);
+
+ yearOfBirthField.setValue("abc");
+ Assert.assertEquals("Please enter a number",
+ binder.validate().get(0).getMessage());
+
+ yearOfBirthField.setValue("123");
+ Assert.assertTrue(binder.validate().isEmpty());
+
+ p.setYearOfBirth(12500);
+ binder.load(p);
+ Assert.assertEquals("12500", yearOfBirthField.getValue());
+ }
}