From 6941d683eb15a8ae10e0d302bcf9608be8825f8a Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Sun, 4 Sep 2016 23:35:42 +0300 Subject: [PATCH] Add error message constructor to all converters which can fail Change-Id: I3ec60effc75e22765d21e0223ee1537ffbdb29e7 --- .../AbstractStringToNumberConverter.java | 34 ++++++++++++++--- .../StringToBigDecimalConverter.java | 14 ++++++- .../StringToBigIntegerConverter.java | 22 ++++++++--- .../converter/StringToBooleanConverter.java | 20 +++++++--- .../converter/StringToDoubleConverter.java | 21 ++++++++++- .../converter/StringToFloatConverter.java | 23 ++++++++++-- .../converter/StringToIntegerConverter.java | 37 +++++++++---------- .../util/converter/StringToLongConverter.java | 21 +++++++++-- .../ui/declarative/DesignFormatter.java | 19 ++++++---- .../vaadin/data/BinderBookOfVaadinTest.java | 32 ++++++++++++++++ .../data/converter/AbstractConverterTest.java | 18 +++++++-- .../AbstractStringConverterTest.java | 32 ++++++++++++++++ .../converter/DateToLongConverterTest.java | 4 +- .../converter/DateToSqlDateConverterTest.java | 2 +- .../StringToBigDecimalConverterTest.java | 12 ++---- .../StringToBigIntegerConverterTest.java | 16 +++----- .../StringToBooleanConverterTest.java | 37 ++++++++++++------- .../converter/StringToDateConverterTest.java | 4 +- .../StringToDoubleConverterTest.java | 15 ++++++-- .../converter/StringToFloatConverterTest.java | 11 +++--- .../StringToIntegerConverterTest.java | 12 +++++- .../converter/StringToLongConverterTest.java | 17 +++++---- 22 files changed, 309 insertions(+), 114 deletions(-) create mode 100644 server/src/test/java/com/vaadin/tests/data/converter/AbstractStringConverterTest.java diff --git a/server/src/main/java/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java b/server/src/main/java/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java index bd2ea690a1..c06a9baa61 100644 --- a/server/src/main/java/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java +++ b/server/src/main/java/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java @@ -20,6 +20,8 @@ import java.text.NumberFormat; import java.text.ParsePosition; import java.util.Locale; +import com.vaadin.data.Result; + /** * A converter that converts from the number type T to {@link String} and back. * Uses the given locale and {@link NumberFormat} for formatting and parsing. @@ -35,6 +37,18 @@ import java.util.Locale; public abstract class AbstractStringToNumberConverter implements Converter { + private final String errorMessage; + + /** + * Creates a new converter instance with the given error message. + * + * @param errorMessage + * the error message to use if conversion fails + */ + protected AbstractStringToNumberConverter(String errorMessage) { + this.errorMessage = errorMessage; + } + /** * Returns the format used by {@link #convertToPresentation(Object, Locale)} * and {@link #convertToModel(Object, Locale)}. @@ -61,9 +75,9 @@ public abstract class AbstractStringToNumberConverter * The locale to use for conversion * @return The converted value */ - protected Number convertToNumber(String value, Locale locale) { + protected Result convertToNumber(String value, Locale locale) { if (value == null) { - return null; + return Result.ok(null); } // Remove leading and trailing white space @@ -74,16 +88,24 @@ public abstract class AbstractStringToNumberConverter ParsePosition parsePosition = new ParsePosition(0); Number parsedValue = getFormat(locale).parse(value, parsePosition); if (parsePosition.getIndex() != value.length()) { - throw new IllegalArgumentException( - "Could not convert '" + value + "'"); + return Result.error(getErrorMessage()); } if (parsedValue == null) { // Convert "" to null - return null; + return Result.ok(null); } - return parsedValue; + return Result.ok(parsedValue); + } + + /** + * Gets the error message to use when conversion fails. + * + * @return the error message + */ + protected String getErrorMessage() { + return errorMessage; } @Override diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToBigDecimalConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToBigDecimalConverter.java index 639523516e..af0a63fc7a 100644 --- a/server/src/main/java/com/vaadin/data/util/converter/StringToBigDecimalConverter.java +++ b/server/src/main/java/com/vaadin/data/util/converter/StringToBigDecimalConverter.java @@ -38,6 +38,17 @@ import com.vaadin.data.Result; */ public class StringToBigDecimalConverter extends AbstractStringToNumberConverter { + + /** + * Creates a new converter instance with the given error message. + * + * @param errorMessage + * the error message to use if conversion fails + */ + public StringToBigDecimalConverter(String errorMessage) { + super(errorMessage); + } + @Override protected NumberFormat getFormat(Locale locale) { NumberFormat numberFormat = super.getFormat(locale); @@ -50,7 +61,8 @@ public class StringToBigDecimalConverter @Override public Result convertToModel(String value, Locale locale) { - return Result.ok((BigDecimal) convertToNumber(value, locale)); + return convertToNumber(value, locale) + .map(number -> (BigDecimal) number); } } diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToBigIntegerConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToBigIntegerConverter.java index 58080013b6..d469db7055 100644 --- a/server/src/main/java/com/vaadin/data/util/converter/StringToBigIntegerConverter.java +++ b/server/src/main/java/com/vaadin/data/util/converter/StringToBigIntegerConverter.java @@ -39,6 +39,15 @@ import com.vaadin.data.Result; */ public class StringToBigIntegerConverter extends AbstractStringToNumberConverter { + /** + * Creates a new converter instance with the given error message. + * + * @param errorMessage + * the error message to use if conversion fails + */ + public StringToBigIntegerConverter(String errorMessage) { + super(errorMessage); + } @Override protected NumberFormat getFormat(Locale locale) { @@ -52,12 +61,13 @@ public class StringToBigIntegerConverter @Override public Result convertToModel(String value, Locale locale) { - - BigDecimal bigDecimalValue = (BigDecimal) convertToNumber(value, - locale); - - return (bigDecimalValue != null) - ? Result.ok(bigDecimalValue.toBigInteger()) : Result.ok(null); + return convertToNumber(value, locale).map(number -> { + if (number == null) { + return null; + } else { + return ((BigDecimal) number).toBigInteger(); + } + }); } } diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToBooleanConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToBooleanConverter.java index 23ce228619..7b9e1850f0 100644 --- a/server/src/main/java/com/vaadin/data/util/converter/StringToBooleanConverter.java +++ b/server/src/main/java/com/vaadin/data/util/converter/StringToBooleanConverter.java @@ -41,31 +41,39 @@ public class StringToBooleanConverter implements Converter { private final String falseString; + private String errorMessage; + /** * Creates converter with default string representations - "true" and * "false". * + * @param errorMessage + * the error message to use if conversion fails */ - public StringToBooleanConverter() { - this(Boolean.TRUE.toString(), Boolean.FALSE.toString()); + public StringToBooleanConverter(String errorMessage) { + this(errorMessage, Boolean.TRUE.toString(), Boolean.FALSE.toString()); } /** * Creates converter with custom string representation. * + * @param errorMessage + * the error message to use if conversion fails * @param falseString * string representation for false * @param trueString * string representation for true */ - public StringToBooleanConverter(String trueString, String falseString) { + public StringToBooleanConverter(String errorMessage, String trueString, + String falseString) { + this.errorMessage = errorMessage; this.trueString = trueString; this.falseString = falseString; } @Override public Result convertToModel(String value, Locale locale) { - if (value == null || value.isEmpty()) { + if (value == null) { return Result.ok(null); } @@ -76,8 +84,10 @@ public class StringToBooleanConverter implements Converter { return Result.ok(true); } else if (getFalseString(locale).equals(value)) { return Result.ok(false); + } else if (value.isEmpty()) { + return Result.ok(null); } else { - throw new IllegalArgumentException("Cannot convert " + value); + return Result.error(errorMessage); } } diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToDoubleConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToDoubleConverter.java index 374a553e8e..463a7f6b36 100644 --- a/server/src/main/java/com/vaadin/data/util/converter/StringToDoubleConverter.java +++ b/server/src/main/java/com/vaadin/data/util/converter/StringToDoubleConverter.java @@ -38,10 +38,27 @@ import com.vaadin.data.Result; public class StringToDoubleConverter extends AbstractStringToNumberConverter { + /** + * Creates a new converter instance with the given error message. + * + * @param errorMessage + * the error message to use if conversion fails + */ + public StringToDoubleConverter(String errorMessage) { + super(errorMessage); + } + @Override public Result convertToModel(String value, Locale locale) { - Number n = convertToNumber(value, locale); - return n == null ? Result.ok(null) : Result.ok(n.doubleValue()); + Result n = convertToNumber(value, locale); + + return n.map(number -> { + if (number == null) { + return null; + } else { + return number.doubleValue(); + } + }); } } diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToFloatConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToFloatConverter.java index d05374a3cc..be592665cf 100644 --- a/server/src/main/java/com/vaadin/data/util/converter/StringToFloatConverter.java +++ b/server/src/main/java/com/vaadin/data/util/converter/StringToFloatConverter.java @@ -27,10 +27,8 @@ import com.vaadin.data.Result; * parsing. *

* Leading and trailing white spaces are ignored when converting from a String. - *

*

* Override and overwrite {@link #getFormat(Locale)} to use a different format. - *

* * @author Vaadin Ltd * @since 8.0 @@ -38,10 +36,27 @@ import com.vaadin.data.Result; public class StringToFloatConverter extends AbstractStringToNumberConverter { + /** + * Creates a new converter instance with the given error message. + * + * @param errorMessage + * the error message to use if conversion fails + */ + public StringToFloatConverter(String errorMessage) { + super(errorMessage); + } + @Override public Result convertToModel(String value, Locale locale) { - Number n = convertToNumber(value, locale); - return n == null ? Result.ok(null) : Result.ok(n.floatValue()); + Result n = convertToNumber(value, locale); + + return n.map(number -> { + if (number == null) { + return null; + } else { + return number.floatValue(); + } + }); } } diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToIntegerConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToIntegerConverter.java index 7a5ee69aed..ec05647a51 100644 --- a/server/src/main/java/com/vaadin/data/util/converter/StringToIntegerConverter.java +++ b/server/src/main/java/com/vaadin/data/util/converter/StringToIntegerConverter.java @@ -35,8 +35,6 @@ import com.vaadin.data.Result; public class StringToIntegerConverter extends AbstractStringToNumberConverter { - private final String errorMessage; - /** * Creates a new converter instance with the given error message. * @@ -44,7 +42,7 @@ public class StringToIntegerConverter * the error message to use if conversion fails */ public StringToIntegerConverter(String errorMessage) { - this.errorMessage = errorMessage; + super(errorMessage); } /** @@ -66,22 +64,23 @@ public class StringToIntegerConverter @Override public Result convertToModel(String value, Locale locale) { - Number n = convertToNumber(value, locale); - - if (n == null) { - return Result.ok(null); - } - - int intValue = n.intValue(); - if (intValue == n.longValue()) { - // If the value of n is outside the range of long, the return value - // of longValue() is either Long.MIN_VALUE or Long.MAX_VALUE. The - // above comparison promotes int to long and thus does not need to - // consider wrap-around. - return Result.ok(intValue); - } else { - return Result.error(errorMessage); - } + Result n = convertToNumber(value, locale); + return n.flatMap(number -> { + if (number == null) { + return Result.ok(null); + } else { + int intValue = number.intValue(); + if (intValue == number.longValue()) { + // If the value of n is outside the range of long, the + // return value of longValue() is either Long.MIN_VALUE or + // Long.MAX_VALUE. The/ above comparison promotes int to + // long and thus does not need to consider wrap-around. + return Result.ok(intValue); + } else { + return Result.error(getErrorMessage()); + } + } + }); } } diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToLongConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToLongConverter.java index d82dec9ae9..9b7bfa4fc6 100644 --- a/server/src/main/java/com/vaadin/data/util/converter/StringToLongConverter.java +++ b/server/src/main/java/com/vaadin/data/util/converter/StringToLongConverter.java @@ -35,6 +35,16 @@ import com.vaadin.data.Result; public class StringToLongConverter extends AbstractStringToNumberConverter { + /** + * Creates a new converter instance with the given error message. + * + * @param errorMessage + * the error message to use if conversion fails + */ + public StringToLongConverter(String errorMessage) { + super(errorMessage); + } + /** * Returns the format used by {@link #convertToPresentation(Long, Locale)} * and {@link #convertToModel(String, Locale)}. @@ -53,9 +63,14 @@ public class StringToLongConverter @Override public Result convertToModel(String value, Locale locale) { - Number n = convertToNumber(value, locale); - return n == null ? Result.ok(null) : Result.ok(n.longValue()); - + Result n = convertToNumber(value, locale); + return n.map(number -> { + if (number == null) { + return null; + } else { + return number.longValue(); + } + }); } } diff --git a/server/src/main/java/com/vaadin/ui/declarative/DesignFormatter.java b/server/src/main/java/com/vaadin/ui/declarative/DesignFormatter.java index 7414c0b1e7..0fcefe0a27 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/DesignFormatter.java +++ b/server/src/main/java/com/vaadin/ui/declarative/DesignFormatter.java @@ -109,7 +109,8 @@ public class DesignFormatter implements Serializable { final DecimalFormat fmt = new DecimalFormat("0.###", symbols); fmt.setGroupingUsed(false); - Converter floatConverter = new StringToFloatConverter() { + Converter floatConverter = new StringToFloatConverter( + "Error converting value") { @Override protected NumberFormat getFormat(Locale locale) { return fmt; @@ -118,7 +119,8 @@ public class DesignFormatter implements Serializable { converterMap.put(Float.class, floatConverter); converterMap.put(float.class, floatConverter); - Converter doubleConverter = new StringToDoubleConverter() { + Converter doubleConverter = new StringToDoubleConverter( + "Error converting value") { @Override protected NumberFormat getFormat(Locale locale) { return fmt; @@ -130,12 +132,13 @@ public class DesignFormatter implements Serializable { final DecimalFormat bigDecimalFmt = new DecimalFormat("0.###", symbols); bigDecimalFmt.setGroupingUsed(false); bigDecimalFmt.setParseBigDecimal(true); - converterMap.put(BigDecimal.class, new StringToBigDecimalConverter() { - @Override - protected NumberFormat getFormat(Locale locale) { - return bigDecimalFmt; - }; - }); + converterMap.put(BigDecimal.class, + new StringToBigDecimalConverter("Error converting value") { + @Override + protected NumberFormat getFormat(Locale locale) { + return bigDecimalFmt; + }; + }); // strings do nothing converterMap.put(String.class, new Converter() { diff --git a/server/src/test/java/com/vaadin/data/BinderBookOfVaadinTest.java b/server/src/test/java/com/vaadin/data/BinderBookOfVaadinTest.java index 6ed2170fa7..02a32135f6 100644 --- a/server/src/test/java/com/vaadin/data/BinderBookOfVaadinTest.java +++ b/server/src/test/java/com/vaadin/data/BinderBookOfVaadinTest.java @@ -476,4 +476,36 @@ public class BinderBookOfVaadinTest { Assert.assertEquals("foo@bar.com", person.getEmail()); } + @Test + public void manyConvertersAndValidators() throws ValidationException { + TextField yearOfBirthField = new TextField(); + binder.forField(yearOfBirthField) + // Validator will be run with the String value of the field + .withValidator(text -> text.length() == 4, + "Doesn't look like a year") + // Converter will only be run for strings with 4 characters + .withConverter( + new StringToIntegerConverter("Must enter a number")) + // Validator will be run with the converted value + .withValidator(year -> year >= 1900 && year <= 2000, + "Person must be born in the 20th century") + .bind(BookPerson::getYearOfBirth, BookPerson::setYearOfBirth); + + yearOfBirthField.setValue("abc"); + Assert.assertEquals("Doesn't look like a year", + binder.validate().get(0).getMessage()); + yearOfBirthField.setValue("abcd"); + Assert.assertEquals("Must enter a number", + binder.validate().get(0).getMessage()); + yearOfBirthField.setValue("1200"); + Assert.assertEquals("Person must be born in the 20th century", + binder.validate().get(0).getMessage()); + + yearOfBirthField.setValue("1950"); + Assert.assertTrue(binder.validate().isEmpty()); + BookPerson person = new BookPerson(1500, 12); + binder.save(person); + Assert.assertEquals(1950, person.getYearOfBirth()); + } + } diff --git a/server/src/test/java/com/vaadin/tests/data/converter/AbstractConverterTest.java b/server/src/test/java/com/vaadin/tests/data/converter/AbstractConverterTest.java index 276b0b674c..bc9a96b311 100644 --- a/server/src/test/java/com/vaadin/tests/data/converter/AbstractConverterTest.java +++ b/server/src/test/java/com/vaadin/tests/data/converter/AbstractConverterTest.java @@ -10,16 +10,16 @@ public abstract class AbstractConverterTest { @Test public void testNullConversion() { - assertResult(null, getConverter().convertToModel(null, null)); + assertValue(null, getConverter().convertToModel(null, null)); } protected abstract Converter getConverter(); - protected void assertResult(Object object, Result result) { - assertResult(null, object, result); + protected void assertValue(Object object, Result result) { + assertValue(null, object, result); } - protected void assertResult(String error, Object object, Result result) { + protected void assertValue(String error, Object object, Result result) { Assert.assertNotNull("Result should never be null", result); Assert.assertFalse("Result is not ok", result.isError()); Assert.assertEquals(object, @@ -27,4 +27,14 @@ public abstract class AbstractConverterTest { error != null ? error : message))); } + protected void assertError(String expected, Result result) { + Assert.assertNotNull("Result should never be null", result); + Assert.assertTrue("Result should be an error", result.isError()); + Assert.assertEquals(expected, result.getMessage().get()); + } + + protected String getErrorMessage() { + return "conversion failed"; + } + } diff --git a/server/src/test/java/com/vaadin/tests/data/converter/AbstractStringConverterTest.java b/server/src/test/java/com/vaadin/tests/data/converter/AbstractStringConverterTest.java new file mode 100644 index 0000000000..bf2653da88 --- /dev/null +++ b/server/src/test/java/com/vaadin/tests/data/converter/AbstractStringConverterTest.java @@ -0,0 +1,32 @@ +package com.vaadin.tests.data.converter; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.data.Result; +import com.vaadin.data.util.converter.Converter; + +public abstract class AbstractStringConverterTest + extends AbstractConverterTest { + + @Override + protected abstract Converter getConverter(); + + @Test + public void testEmptyStringConversion() { + assertValue("Null value was converted incorrectly", null, + getConverter().convertToModel("", null)); + } + + @Test + public void testErrorMessage() { + Result result = getConverter().convertToModel("abc", null); + Assert.assertTrue(result.isError()); + Assert.assertEquals(getErrorMessage(), result.getMessage().get()); + } + + protected String getErrorMessage() { + return "conversion failed"; + } + +} diff --git a/server/src/test/java/com/vaadin/tests/data/converter/DateToLongConverterTest.java b/server/src/test/java/com/vaadin/tests/data/converter/DateToLongConverterTest.java index 3ce42f5348..d0eb336616 100644 --- a/server/src/test/java/com/vaadin/tests/data/converter/DateToLongConverterTest.java +++ b/server/src/test/java/com/vaadin/tests/data/converter/DateToLongConverterTest.java @@ -16,13 +16,13 @@ public class DateToLongConverterTest extends AbstractConverterTest { @Override @Test public void testNullConversion() { - assertResult(null, getConverter().convertToModel(null, null)); + assertValue(null, getConverter().convertToModel(null, null)); } @Test public void testValueConversion() { Date d = new Date(100, 0, 1); - assertResult( + assertValue( Long.valueOf(946677600000l + (d.getTimezoneOffset() + 120) * 60 * 1000L), getConverter().convertToModel(d, null)); diff --git a/server/src/test/java/com/vaadin/tests/data/converter/DateToSqlDateConverterTest.java b/server/src/test/java/com/vaadin/tests/data/converter/DateToSqlDateConverterTest.java index 3bc4e2d481..6264b81da1 100644 --- a/server/src/test/java/com/vaadin/tests/data/converter/DateToSqlDateConverterTest.java +++ b/server/src/test/java/com/vaadin/tests/data/converter/DateToSqlDateConverterTest.java @@ -18,7 +18,7 @@ public class DateToSqlDateConverterTest extends AbstractConverterTest { public void testValueConversion() { Date testDate = new Date(100, 0, 1); long time = testDate.getTime(); - assertResult(testDate, getConverter() + assertValue(testDate, getConverter() .convertToModel(new java.sql.Date(time), Locale.ENGLISH)); } } diff --git a/server/src/test/java/com/vaadin/tests/data/converter/StringToBigDecimalConverterTest.java b/server/src/test/java/com/vaadin/tests/data/converter/StringToBigDecimalConverterTest.java index fd5fe2416b..4481da2ead 100644 --- a/server/src/test/java/com/vaadin/tests/data/converter/StringToBigDecimalConverterTest.java +++ b/server/src/test/java/com/vaadin/tests/data/converter/StringToBigDecimalConverterTest.java @@ -24,16 +24,12 @@ import org.junit.Test; import com.vaadin.data.Result; import com.vaadin.data.util.converter.StringToBigDecimalConverter; -public class StringToBigDecimalConverterTest extends AbstractConverterTest { +public class StringToBigDecimalConverterTest + extends AbstractStringConverterTest { @Override protected StringToBigDecimalConverter getConverter() { - return new StringToBigDecimalConverter(); - } - - @Test - public void testEmptyStringConversion() { - assertResult(null, getConverter().convertToModel("", null)); + return new StringToBigDecimalConverter(getErrorMessage()); } @Test @@ -41,7 +37,7 @@ public class StringToBigDecimalConverterTest extends AbstractConverterTest { Result converted = getConverter().convertToModel("10", null); BigDecimal expected = new BigDecimal(10); - assertResult(expected, converted); + assertValue(expected, converted); } @Test diff --git a/server/src/test/java/com/vaadin/tests/data/converter/StringToBigIntegerConverterTest.java b/server/src/test/java/com/vaadin/tests/data/converter/StringToBigIntegerConverterTest.java index e537fdf8a1..56ed799bbc 100644 --- a/server/src/test/java/com/vaadin/tests/data/converter/StringToBigIntegerConverterTest.java +++ b/server/src/test/java/com/vaadin/tests/data/converter/StringToBigIntegerConverterTest.java @@ -18,24 +18,18 @@ package com.vaadin.tests.data.converter; import java.math.BigInteger; import java.util.Locale; +import org.junit.Assert; import org.junit.Test; import com.vaadin.data.Result; import com.vaadin.data.util.converter.StringToBigIntegerConverter; -import junit.framework.Assert; - -public class StringToBigIntegerConverterTest extends AbstractConverterTest { +public class StringToBigIntegerConverterTest + extends AbstractStringConverterTest { @Override protected StringToBigIntegerConverter getConverter() { - return new StringToBigIntegerConverter(); - } - - @Test - public void testEmptyStringConversion() { - assertResult("Empty value was converted incorrectly", null, - getConverter().convertToModel("", null)); + return new StringToBigIntegerConverter(getErrorMessage()); } @Test @@ -44,7 +38,7 @@ public class StringToBigIntegerConverterTest extends AbstractConverterTest { Result converted = getConverter().convertToModel(bigInt, null); BigInteger expected = new BigInteger(bigInt); - assertResult("Value bigger than max long was converted incorrectly", + assertValue("Value bigger than max long was converted incorrectly", expected, converted); } diff --git a/server/src/test/java/com/vaadin/tests/data/converter/StringToBooleanConverterTest.java b/server/src/test/java/com/vaadin/tests/data/converter/StringToBooleanConverterTest.java index d73fe5ca87..5ca24f64aa 100644 --- a/server/src/test/java/com/vaadin/tests/data/converter/StringToBooleanConverterTest.java +++ b/server/src/test/java/com/vaadin/tests/data/converter/StringToBooleanConverterTest.java @@ -9,16 +9,19 @@ import org.junit.Test; import com.vaadin.data.util.converter.StringToBooleanConverter; -public class StringToBooleanConverterTest extends AbstractConverterTest { +public class StringToBooleanConverterTest extends AbstractStringConverterTest { @Override protected StringToBooleanConverter getConverter() { - return new StringToBooleanConverter(); + return new StringToBooleanConverter(getErrorMessage()); } - StringToBooleanConverter yesNoConverter = new StringToBooleanConverter( - "yes", "no"); - StringToBooleanConverter localeConverter = new StringToBooleanConverter() { + private StringToBooleanConverter yesNoConverter = new StringToBooleanConverter( + getErrorMessage(), "yes", "no"); + private StringToBooleanConverter emptyTrueConverter = new StringToBooleanConverter( + getErrorMessage(), "", "ABSENT"); + private StringToBooleanConverter localeConverter = new StringToBooleanConverter( + getErrorMessage()) { @Override public String getFalseString(Locale locale) { Date d = new Date(3000000000000L); @@ -38,21 +41,16 @@ public class StringToBooleanConverterTest extends AbstractConverterTest { } }; - @Test - public void testEmptyStringConversion() { - assertResult(null, getConverter().convertToModel("", null)); - } - @Test public void testValueConversion() { - assertResult(true, getConverter().convertToModel("true", null)); - assertResult(false, getConverter().convertToModel("false", null)); + assertValue(true, getConverter().convertToModel("true", null)); + assertValue(false, getConverter().convertToModel("false", null)); } @Test public void testYesNoValueConversion() { - assertResult(true, yesNoConverter.convertToModel("yes", null)); - assertResult(false, yesNoConverter.convertToModel("no", null)); + assertValue(true, yesNoConverter.convertToModel("yes", null)); + assertValue(false, yesNoConverter.convertToModel("no", null)); Assert.assertEquals("yes", yesNoConverter.convertToPresentation(true, null)); @@ -60,6 +58,17 @@ public class StringToBooleanConverterTest extends AbstractConverterTest { yesNoConverter.convertToPresentation(false, null)); } + @Test + public void testEmptyTrueValueConversion() { + assertValue(true, emptyTrueConverter.convertToModel("", null)); + assertValue(false, emptyTrueConverter.convertToModel("ABSENT", null)); + + Assert.assertEquals("", + emptyTrueConverter.convertToPresentation(true, null)); + Assert.assertEquals("ABSENT", + emptyTrueConverter.convertToPresentation(false, null)); + } + @Test public void testLocale() { Assert.assertEquals("May 18, 2033", diff --git a/server/src/test/java/com/vaadin/tests/data/converter/StringToDateConverterTest.java b/server/src/test/java/com/vaadin/tests/data/converter/StringToDateConverterTest.java index b7866ca9d6..0fe2088847 100644 --- a/server/src/test/java/com/vaadin/tests/data/converter/StringToDateConverterTest.java +++ b/server/src/test/java/com/vaadin/tests/data/converter/StringToDateConverterTest.java @@ -16,12 +16,12 @@ public class StringToDateConverterTest extends AbstractConverterTest { @Test public void testEmptyStringConversion() { - assertResult(null, getConverter().convertToModel("", null)); + assertValue(null, getConverter().convertToModel("", null)); } @Test public void testValueConversion() { - assertResult(new Date(100, 0, 1), getConverter() + assertValue(new Date(100, 0, 1), getConverter() .convertToModel("Jan 1, 2000 12:00:00 AM", Locale.ENGLISH)); } } diff --git a/server/src/test/java/com/vaadin/tests/data/converter/StringToDoubleConverterTest.java b/server/src/test/java/com/vaadin/tests/data/converter/StringToDoubleConverterTest.java index 8e043e2082..28a5a7f73f 100644 --- a/server/src/test/java/com/vaadin/tests/data/converter/StringToDoubleConverterTest.java +++ b/server/src/test/java/com/vaadin/tests/data/converter/StringToDoubleConverterTest.java @@ -1,5 +1,6 @@ package com.vaadin.tests.data.converter; +import org.junit.Assert; import org.junit.Test; import com.vaadin.data.Result; @@ -9,17 +10,25 @@ public class StringToDoubleConverterTest extends AbstractConverterTest { @Override protected StringToDoubleConverter getConverter() { - return new StringToDoubleConverter(); + return new StringToDoubleConverter("Failed"); } @Test public void testEmptyStringConversion() { - assertResult(null, getConverter().convertToModel("", null)); + assertValue(null, getConverter().convertToModel("", null)); } @Test public void testValueConversion() { Result value = getConverter().convertToModel("10", null); - assertResult(10.0d, value); + assertValue(10.0d, value); } + + @Test + public void testErrorMessage() { + Result result = getConverter().convertToModel("abc", null); + Assert.assertTrue(result.isError()); + Assert.assertEquals("Failed", result.getMessage().get()); + } + } diff --git a/server/src/test/java/com/vaadin/tests/data/converter/StringToFloatConverterTest.java b/server/src/test/java/com/vaadin/tests/data/converter/StringToFloatConverterTest.java index a6292d3310..20b51deb54 100644 --- a/server/src/test/java/com/vaadin/tests/data/converter/StringToFloatConverterTest.java +++ b/server/src/test/java/com/vaadin/tests/data/converter/StringToFloatConverterTest.java @@ -4,27 +4,28 @@ import org.junit.Test; import com.vaadin.data.util.converter.StringToFloatConverter; -public class StringToFloatConverterTest extends AbstractConverterTest { +public class StringToFloatConverterTest extends AbstractStringConverterTest { @Override protected StringToFloatConverter getConverter() { - return new StringToFloatConverter(); + return new StringToFloatConverter(getErrorMessage()); } @Override @Test public void testNullConversion() { - assertResult(null, getConverter().convertToModel(null, null)); + assertValue(null, getConverter().convertToModel(null, null)); } + @Override @Test public void testEmptyStringConversion() { - assertResult(null, getConverter().convertToModel("", null)); + assertValue(null, getConverter().convertToModel("", null)); } @Test public void testValueConversion() { - assertResult(Float.valueOf(10), + assertValue(Float.valueOf(10), getConverter().convertToModel("10", null)); } } diff --git a/server/src/test/java/com/vaadin/tests/data/converter/StringToIntegerConverterTest.java b/server/src/test/java/com/vaadin/tests/data/converter/StringToIntegerConverterTest.java index daaf27838b..a215c2c785 100644 --- a/server/src/test/java/com/vaadin/tests/data/converter/StringToIntegerConverterTest.java +++ b/server/src/test/java/com/vaadin/tests/data/converter/StringToIntegerConverterTest.java @@ -3,6 +3,7 @@ package com.vaadin.tests.data.converter; import org.junit.Assert; import org.junit.Test; +import com.vaadin.data.Result; import com.vaadin.data.util.converter.StringToIntegerConverter; public class StringToIntegerConverterTest extends AbstractConverterTest { @@ -14,7 +15,7 @@ public class StringToIntegerConverterTest extends AbstractConverterTest { @Test public void testEmptyStringConversion() { - assertResult(null, getConverter().convertToModel("", null)); + assertValue(null, getConverter().convertToModel("", null)); } @Test @@ -37,7 +38,14 @@ public class StringToIntegerConverterTest extends AbstractConverterTest { @Test public void testValueConversion() { - assertResult(Integer.valueOf(10), + assertValue(Integer.valueOf(10), getConverter().convertToModel("10", null)); } + + @Test + public void testErrorMessage() { + Result result = getConverter().convertToModel("abc", null); + Assert.assertTrue(result.isError()); + Assert.assertEquals("Failed", result.getMessage().get()); + } } diff --git a/server/src/test/java/com/vaadin/tests/data/converter/StringToLongConverterTest.java b/server/src/test/java/com/vaadin/tests/data/converter/StringToLongConverterTest.java index 46afb848d3..818e0f2ce7 100644 --- a/server/src/test/java/com/vaadin/tests/data/converter/StringToLongConverterTest.java +++ b/server/src/test/java/com/vaadin/tests/data/converter/StringToLongConverterTest.java @@ -5,21 +5,22 @@ import org.junit.Test; import com.vaadin.data.Result; import com.vaadin.data.util.converter.StringToLongConverter; -public class StringToLongConverterTest extends AbstractConverterTest { +public class StringToLongConverterTest extends AbstractStringConverterTest { @Override protected StringToLongConverter getConverter() { - return new StringToLongConverter(); + return new StringToLongConverter(getErrorMessage()); } + @Override @Test public void testEmptyStringConversion() { - assertResult(null, getConverter().convertToModel("", null)); + assertValue(null, getConverter().convertToModel("", null)); } @Test public void testValueConversion() { - assertResult(Long.valueOf(10), + assertValue(Long.valueOf(10), getConverter().convertToModel("10", null)); } @@ -27,9 +28,9 @@ public class StringToLongConverterTest extends AbstractConverterTest { public void testExtremeLongValueConversion() { Result l = getConverter().convertToModel("9223372036854775807", null); - assertResult(Long.MAX_VALUE, l); + assertValue(Long.MAX_VALUE, l); l = getConverter().convertToModel("-9223372036854775808", null); - assertResult(Long.MIN_VALUE, l); + assertValue(Long.MIN_VALUE, l); } @Test @@ -37,10 +38,10 @@ public class StringToLongConverterTest extends AbstractConverterTest { // Long.MAX_VALUE+1 is converted to Long.MAX_VALUE Result l = getConverter().convertToModel("9223372036854775808", null); - assertResult(Long.MAX_VALUE, l); + assertValue(Long.MAX_VALUE, l); // Long.MIN_VALUE-1 is converted to Long.MIN_VALUE l = getConverter().convertToModel("-9223372036854775809", null); - assertResult(Long.MIN_VALUE, l); + assertValue(Long.MIN_VALUE, l); } } -- 2.39.5