Change-Id: I6562c537d9e5a0745eb67bc613123a265578ae00tags/7.1.0
@@ -65,7 +65,8 @@ public abstract class AbstractStringToNumberConverter<T> implements | |||
* If there was a problem converting the value | |||
* @since 7.1 | |||
*/ | |||
protected Number convertToNumber(String value, Locale locale) | |||
protected Number convertToNumber(String value, | |||
Class<? extends Number> targetType, Locale locale) | |||
throws ConversionException { | |||
if (value == null) { | |||
return null; | |||
@@ -98,7 +99,8 @@ public abstract class AbstractStringToNumberConverter<T> implements | |||
* .Object, java.util.Locale) | |||
*/ | |||
@Override | |||
public String convertToPresentation(T value, Locale locale) | |||
public String convertToPresentation(T value, | |||
Class<? extends String> targetType, Locale locale) | |||
throws ConversionException { | |||
if (value == null) { | |||
return null; |
@@ -22,10 +22,10 @@ import java.util.Locale; | |||
/** | |||
* Interface that implements conversion between a model and a presentation type. | |||
* <p> | |||
* Typically {@link #convertToPresentation(Object, Locale)} and | |||
* {@link #convertToModel(Object, Locale)} should be symmetric so that chaining | |||
* these together returns the original result for all input but this is not a | |||
* requirement. | |||
* Typically {@link #convertToPresentation(Object, Class, Locale)} and | |||
* {@link #convertToModel(Object, Class, Locale)} should be symmetric so that | |||
* chaining these together returns the original result for all input but this is | |||
* not a requirement. | |||
* </p> | |||
* <p> | |||
* Converters must not have any side effects (never update UI from inside a | |||
@@ -55,19 +55,23 @@ public interface Converter<PRESENTATION, MODEL> extends Serializable { | |||
* A converter can optionally use locale to do the conversion. | |||
* </p> | |||
* A converter should in most cases be symmetric so chaining | |||
* {@link #convertToPresentation(Object, Locale)} and | |||
* {@link #convertToModel(Object, Locale)} should return the original value. | |||
* {@link #convertToPresentation(Object, Class, Locale)} and | |||
* {@link #convertToModel(Object, Class, Locale)} should return the original | |||
* value. | |||
* | |||
* @param value | |||
* The value to convert, compatible with the target type. Can be | |||
* null | |||
* @param targetType | |||
* The requested type of the return value | |||
* @param locale | |||
* The locale to use for conversion. Can be null. | |||
* @return The converted value compatible with the source type | |||
* @throws ConversionException | |||
* If the value could not be converted | |||
*/ | |||
public MODEL convertToModel(PRESENTATION value, Locale locale) | |||
public MODEL convertToModel(PRESENTATION value, | |||
Class<? extends MODEL> targetType, Locale locale) | |||
throws ConversionException; | |||
/** | |||
@@ -76,26 +80,30 @@ public interface Converter<PRESENTATION, MODEL> extends Serializable { | |||
* A converter can optionally use locale to do the conversion. | |||
* </p> | |||
* A converter should in most cases be symmetric so chaining | |||
* {@link #convertToPresentation(Object, Locale)} and | |||
* {@link #convertToModel(Object, Locale)} should return the original value. | |||
* {@link #convertToPresentation(Object, Class, Locale)} and | |||
* {@link #convertToModel(Object, Class, Locale)} should return the original | |||
* value. | |||
* | |||
* @param value | |||
* The value to convert, compatible with the target type. Can be | |||
* null | |||
* @param targetType | |||
* The requested type of the return value | |||
* @param locale | |||
* The locale to use for conversion. Can be null. | |||
* @return The converted value compatible with the source type | |||
* @throws ConversionException | |||
* If the value could not be converted | |||
*/ | |||
public PRESENTATION convertToPresentation(MODEL value, Locale locale) | |||
public PRESENTATION convertToPresentation(MODEL value, | |||
Class<? extends PRESENTATION> targetType, Locale locale) | |||
throws ConversionException; | |||
/** | |||
* The source type of the converter. | |||
* | |||
* Values of this type can be passed to | |||
* {@link #convertToPresentation(Object, Locale)}. | |||
* {@link #convertToPresentation(Object, Class, Locale)}. | |||
* | |||
* @return The source type | |||
*/ | |||
@@ -105,7 +113,7 @@ public interface Converter<PRESENTATION, MODEL> extends Serializable { | |||
* The target type of the converter. | |||
* | |||
* Values of this type can be passed to | |||
* {@link #convertToModel(Object, Locale)}. | |||
* {@link #convertToModel(Object, Class, Locale)}. | |||
* | |||
* @return The target type | |||
*/ | |||
@@ -113,8 +121,9 @@ public interface Converter<PRESENTATION, MODEL> extends Serializable { | |||
/** | |||
* An exception that signals that the value passed to | |||
* {@link Converter#convertToPresentation(Object, Locale)} or | |||
* {@link Converter#convertToModel(Object, Locale)} could not be converted. | |||
* {@link Converter#convertToPresentation(Object, Class, Locale)} or | |||
* {@link Converter#convertToModel(Object, Class, Locale)} could not be | |||
* converted. | |||
* | |||
* @author Vaadin Ltd | |||
* @since 7.0 |
@@ -86,7 +86,17 @@ public class ConverterUtil implements Serializable { | |||
Converter<PRESENTATIONTYPE, MODELTYPE> converter, Locale locale) | |||
throws Converter.ConversionException { | |||
if (converter != null) { | |||
return converter.convertToPresentation(modelValue, locale); | |||
PRESENTATIONTYPE presentation = converter.convertToPresentation( | |||
modelValue, presentationType, locale); | |||
if (!presentationType.isInstance(presentation)) { | |||
throw new Converter.ConversionException( | |||
"Converter returned an object of type " | |||
+ presentation.getClass().getName() | |||
+ " when expecting " | |||
+ presentationType.getName()); | |||
} | |||
return presentation; | |||
} | |||
if (modelValue == null) { | |||
return null; | |||
@@ -123,7 +133,17 @@ public class ConverterUtil implements Serializable { | |||
* If there is a converter, always use it. It must convert or throw | |||
* an exception. | |||
*/ | |||
return converter.convertToModel(presentationValue, locale); | |||
MODELTYPE model = converter.convertToModel(presentationValue, | |||
modelType, locale); | |||
if (!modelType.isInstance(model)) { | |||
throw new Converter.ConversionException( | |||
"Converter returned an object of type " | |||
+ model.getClass().getName() | |||
+ " when expecting " + modelType.getName()); | |||
} | |||
return model; | |||
} | |||
if (presentationValue == null) { |
@@ -32,10 +32,11 @@ public class DateToLongConverter implements Converter<Date, Long> { | |||
* | |||
* @see | |||
* com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, | |||
* java.util.Locale) | |||
* java.lang.Class, java.util.Locale) | |||
*/ | |||
@Override | |||
public Long convertToModel(Date value, Locale locale) { | |||
public Long convertToModel(Date value, Class<? extends Long> targetType, | |||
Locale locale) { | |||
if (value == null) { | |||
return null; | |||
} | |||
@@ -48,10 +49,16 @@ public class DateToLongConverter implements Converter<Date, Long> { | |||
* | |||
* @see | |||
* com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang | |||
* .Object, java.util.Locale) | |||
* .Object, java.lang.Class, java.util.Locale) | |||
*/ | |||
@Override | |||
public Date convertToPresentation(Long value, Locale locale) { | |||
public Date convertToPresentation(Long value, | |||
Class<? extends Date> targetType, Locale locale) { | |||
if (targetType != getPresentationType()) { | |||
throw new ConversionException("Converter only supports " | |||
+ getPresentationType().getName() + " (targetType was " | |||
+ targetType.getName() + ")"); | |||
} | |||
if (value == null) { | |||
return null; | |||
} |
@@ -35,14 +35,27 @@ import java.util.Locale; | |||
public class DateToSqlDateConverter implements Converter<Date, java.sql.Date> { | |||
@Override | |||
public java.sql.Date convertToModel(Date value, Locale locale) | |||
public java.sql.Date convertToModel(Date value, | |||
Class<? extends java.sql.Date> targetType, Locale locale) | |||
throws ConversionException { | |||
if (targetType != getModelType()) { | |||
throw new ConversionException("Converter only supports " | |||
+ getModelType().getName() + " (targetType was " | |||
+ targetType.getName() + ")"); | |||
} | |||
return new java.sql.Date(value.getTime()); | |||
} | |||
@Override | |||
public Date convertToPresentation(java.sql.Date value, Locale locale) | |||
public Date convertToPresentation(java.sql.Date value, | |||
Class<? extends Date> targetType, Locale locale) | |||
throws ConversionException { | |||
if (targetType != getPresentationType()) { | |||
throw new ConversionException("Converter only supports " | |||
+ getPresentationType().getName() + " (targetType was " | |||
+ targetType.getName() + ")"); | |||
} | |||
return new Date(value.getTime()); | |||
} | |||
@@ -53,9 +53,10 @@ public class ReverseConverter<PRESENTATION, MODEL> implements | |||
* .lang.Object, java.util.Locale) | |||
*/ | |||
@Override | |||
public MODEL convertToModel(PRESENTATION value, Locale locale) | |||
public MODEL convertToModel(PRESENTATION value, | |||
Class<? extends MODEL> targetType, Locale locale) | |||
throws com.vaadin.data.util.converter.Converter.ConversionException { | |||
return realConverter.convertToPresentation(value, locale); | |||
return realConverter.convertToPresentation(value, targetType, locale); | |||
} | |||
/* | |||
@@ -66,9 +67,10 @@ public class ReverseConverter<PRESENTATION, MODEL> implements | |||
* .Object, java.util.Locale) | |||
*/ | |||
@Override | |||
public PRESENTATION convertToPresentation(MODEL value, Locale locale) | |||
public PRESENTATION convertToPresentation(MODEL value, | |||
Class<? extends PRESENTATION> targetType, Locale locale) | |||
throws com.vaadin.data.util.converter.Converter.ConversionException { | |||
return realConverter.convertToModel(value, locale); | |||
return realConverter.convertToModel(value, targetType, locale); | |||
} | |||
/* |
@@ -35,10 +35,11 @@ public class StringToBooleanConverter implements Converter<String, Boolean> { | |||
* | |||
* @see | |||
* com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, | |||
* java.util.Locale) | |||
* java.lang.Class, java.util.Locale) | |||
*/ | |||
@Override | |||
public Boolean convertToModel(String value, Locale locale) | |||
public Boolean convertToModel(String value, | |||
Class<? extends Boolean> targetType, Locale locale) | |||
throws ConversionException { | |||
if (value == null || value.isEmpty()) { | |||
return null; | |||
@@ -80,10 +81,11 @@ public class StringToBooleanConverter implements Converter<String, Boolean> { | |||
* | |||
* @see | |||
* com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang | |||
* .Object, java.util.Locale) | |||
* .Object, java.lang.Class, java.util.Locale) | |||
*/ | |||
@Override | |||
public String convertToPresentation(Boolean value, Locale locale) | |||
public String convertToPresentation(Boolean value, | |||
Class<? extends String> targetType, Locale locale) | |||
throws ConversionException { | |||
if (value == null) { | |||
return null; |
@@ -37,8 +37,9 @@ import java.util.Locale; | |||
public class StringToDateConverter implements Converter<String, Date> { | |||
/** | |||
* Returns the format used by {@link #convertToPresentation(Date, Locale)} | |||
* and {@link #convertToModel(String, Locale)}. | |||
* Returns the format used by | |||
* {@link #convertToPresentation(Date, Class,Locale)} and | |||
* {@link #convertToModel(String, Class, Locale)}. | |||
* | |||
* @param locale | |||
* The locale to use | |||
@@ -60,11 +61,18 @@ public class StringToDateConverter implements Converter<String, Date> { | |||
* | |||
* @see | |||
* com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, | |||
* java.util.Locale) | |||
* java.lang.Class, java.util.Locale) | |||
*/ | |||
@Override | |||
public Date convertToModel(String value, Locale locale) | |||
public Date convertToModel(String value, Class<? extends Date> targetType, | |||
Locale locale) | |||
throws com.vaadin.data.util.converter.Converter.ConversionException { | |||
if (targetType != getModelType()) { | |||
throw new ConversionException("Converter only supports " | |||
+ getModelType().getName() + " (targetType was " | |||
+ targetType.getName() + ")"); | |||
} | |||
if (value == null) { | |||
return null; | |||
} | |||
@@ -87,10 +95,11 @@ public class StringToDateConverter implements Converter<String, Date> { | |||
* | |||
* @see | |||
* com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang | |||
* .Object, java.util.Locale) | |||
* .Object, java.lang.Class, java.util.Locale) | |||
*/ | |||
@Override | |||
public String convertToPresentation(Date value, Locale locale) | |||
public String convertToPresentation(Date value, | |||
Class<? extends String> targetType, Locale locale) | |||
throws com.vaadin.data.util.converter.Converter.ConversionException { | |||
if (value == null) { | |||
return null; |
@@ -44,9 +44,10 @@ public class StringToDoubleConverter extends | |||
* java.util.Locale) | |||
*/ | |||
@Override | |||
public Double convertToModel(String value, Locale locale) | |||
public Double convertToModel(String value, | |||
Class<? extends Double> targetType, Locale locale) | |||
throws ConversionException { | |||
Number n = convertToNumber(value, locale); | |||
Number n = convertToNumber(value, targetType, locale); | |||
return n == null ? null : n.doubleValue(); | |||
} | |||
@@ -44,9 +44,10 @@ public class StringToFloatConverter extends | |||
* java.util.Locale) | |||
*/ | |||
@Override | |||
public Float convertToModel(String value, Locale locale) | |||
public Float convertToModel(String value, | |||
Class<? extends Float> targetType, Locale locale) | |||
throws ConversionException { | |||
Number n = convertToNumber(value, locale); | |||
Number n = convertToNumber(value, targetType, locale); | |||
return n == null ? null : n.floatValue(); | |||
} | |||
@@ -35,8 +35,8 @@ public class StringToIntegerConverter extends | |||
/** | |||
* Returns the format used by | |||
* {@link #convertToPresentation(Integer, Locale)} and | |||
* {@link #convertToModel(String, Locale)}. | |||
* {@link #convertToPresentation(Integer, Class, Locale)} and | |||
* {@link #convertToModel(String, Class, Locale)} | |||
* | |||
* @param locale | |||
* The locale to use | |||
@@ -55,12 +55,13 @@ public class StringToIntegerConverter extends | |||
* | |||
* @see | |||
* com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, | |||
* java.util.Locale) | |||
* java.lang.Class, java.util.Locale) | |||
*/ | |||
@Override | |||
public Integer convertToModel(String value, Locale locale) | |||
public Integer convertToModel(String value, | |||
Class<? extends Integer> targetType, Locale locale) | |||
throws ConversionException { | |||
Number n = convertToNumber(value, locale); | |||
Number n = convertToNumber(value, targetType, locale); | |||
return n == null ? null : n.intValue(); | |||
} |
@@ -37,12 +37,19 @@ public class StringToNumberConverter extends | |||
* | |||
* @see | |||
* com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, | |||
* java.util.Locale) | |||
* java.lang.Class, java.util.Locale) | |||
*/ | |||
@Override | |||
public Number convertToModel(String value, Locale locale) | |||
public Number convertToModel(String value, | |||
Class<? extends Number> targetType, Locale locale) | |||
throws ConversionException { | |||
return convertToNumber(value, locale); | |||
if (targetType != getModelType()) { | |||
throw new ConversionException("Converter only supports " | |||
+ getModelType().getName() + " (targetType was " | |||
+ targetType.getName() + ")"); | |||
} | |||
return convertToNumber(value, targetType, locale); | |||
} | |||
/* |
@@ -740,13 +740,7 @@ public abstract class AbstractField<T> extends AbstractComponent implements | |||
*/ | |||
private Object convertToModel(T fieldValue, Locale locale) | |||
throws Converter.ConversionException { | |||
Class<?> modelType = null; | |||
Property pd = getPropertyDataSource(); | |||
if (pd != null) { | |||
modelType = pd.getType(); | |||
} else if (getConverter() != null) { | |||
modelType = getConverter().getModelType(); | |||
} | |||
Class<?> modelType = getModelType(); | |||
try { | |||
return ConverterUtil.convertToModel(fieldValue, | |||
(Class<Object>) modelType, getConverter(), locale); | |||
@@ -755,6 +749,24 @@ public abstract class AbstractField<T> extends AbstractComponent implements | |||
} | |||
} | |||
/** | |||
* Retrieves the type of the currently used data model. If the field has no | |||
* data source then the model type of the converter is used. | |||
* | |||
* @since 7.1 | |||
* @return The type of the currently used data model or null if no data | |||
* source or converter is set. | |||
*/ | |||
protected Class<?> getModelType() { | |||
Property<?> pd = getPropertyDataSource(); | |||
if (pd != null) { | |||
return pd.getType(); | |||
} else if (getConverter() != null) { | |||
return getConverter().getModelType(); | |||
} | |||
return null; | |||
} | |||
/** | |||
* Returns the conversion error with {0} replaced by the data source type | |||
* and {1} replaced by the exception (localized) message. | |||
@@ -935,7 +947,7 @@ public abstract class AbstractField<T> extends AbstractComponent implements | |||
if (getConverter() != null) { | |||
try { | |||
valueToValidate = getConverter().convertToModel(fieldValue, | |||
getLocale()); | |||
getModelType(), getLocale()); | |||
} catch (ConversionException e) { | |||
throw new InvalidValueException(getConversionError( | |||
getConverter().getModelType(), e)); |
@@ -4011,7 +4011,8 @@ public class Table extends AbstractSelect implements Action.Container, | |||
} | |||
Object value = property.getValue(); | |||
if (converter != null) { | |||
return converter.convertToPresentation(value, getLocale()); | |||
return converter.convertToPresentation(value, String.class, | |||
getLocale()); | |||
} | |||
return (null != value) ? value.toString() : ""; | |||
} |
@@ -30,13 +30,15 @@ public class ConverterFactory extends TestCase { | |||
public static class ConvertTo42 implements Converter<String, Integer> { | |||
@Override | |||
public Integer convertToModel(String value, Locale locale) | |||
public Integer convertToModel(String value, | |||
Class<? extends Integer> targetType, Locale locale) | |||
throws com.vaadin.data.util.converter.Converter.ConversionException { | |||
return 42; | |||
} | |||
@Override | |||
public String convertToPresentation(Integer value, Locale locale) | |||
public String convertToPresentation(Integer value, | |||
Class<? extends String> targetType, Locale locale) | |||
throws com.vaadin.data.util.converter.Converter.ConversionException { | |||
return "42"; | |||
} |
@@ -33,14 +33,12 @@ public class TestAnyEnumToStringConverter { | |||
public class AnyEnumToStringConverter implements Converter<Enum, String> { | |||
private Class<? extends Enum>[] enumClass; | |||
public AnyEnumToStringConverter(Class<? extends Enum>... enumClass) { | |||
this.enumClass = enumClass; | |||
public AnyEnumToStringConverter() { | |||
} | |||
@Override | |||
public String convertToModel(Enum value, Locale locale) | |||
public String convertToModel(Enum value, | |||
Class<? extends String> targetType, Locale locale) | |||
throws com.vaadin.data.util.converter.Converter.ConversionException { | |||
if (value == null) { | |||
return null; | |||
@@ -50,16 +48,15 @@ public class TestAnyEnumToStringConverter { | |||
} | |||
@Override | |||
public Enum convertToPresentation(String value, Locale locale) | |||
public Enum convertToPresentation(String value, | |||
Class<? extends Enum> targetType, Locale locale) | |||
throws com.vaadin.data.util.converter.Converter.ConversionException { | |||
if (value == null) { | |||
return null; | |||
} | |||
for (Class<? extends Enum> candidate : enumClass) { | |||
for (Enum e : candidate.getEnumConstants()) { | |||
if (e.toString().equals(value)) { | |||
return e; | |||
} | |||
for (Enum e : targetType.getEnumConstants()) { | |||
if (e.toString().equals(value)) { | |||
return e; | |||
} | |||
} | |||
@@ -82,29 +79,29 @@ public class TestAnyEnumToStringConverter { | |||
@Before | |||
public void setup() { | |||
converter = new AnyEnumToStringConverter(TestEnum.class, | |||
AnotherTestEnum.class); | |||
converter = new AnyEnumToStringConverter(); | |||
} | |||
@Test | |||
public void nullConversion() { | |||
Assert.assertEquals(null, converter.convertToModel(null, null)); | |||
Assert.assertEquals(null, converter.convertToModel(null, null, null)); | |||
} | |||
@Test | |||
public void enumToStringConversion() { | |||
Assert.assertEquals(TestEnum.TWO.toString(), | |||
converter.convertToModel(TestEnum.TWO, null)); | |||
Assert.assertEquals(AnotherTestEnum.TWO.toString(), | |||
converter.convertToModel(AnotherTestEnum.TWO, null)); | |||
converter.convertToModel(TestEnum.TWO, String.class, null)); | |||
Assert.assertEquals(AnotherTestEnum.TWO.toString(), converter | |||
.convertToModel(AnotherTestEnum.TWO, String.class, null)); | |||
} | |||
@Test | |||
public void stringToEnumConversion() { | |||
Assert.assertEquals(TestEnum.TWO, | |||
converter.convertToPresentation(TestEnum.TWO.toString(), null)); | |||
Assert.assertEquals(TestEnum.TWO, converter.convertToPresentation( | |||
TestEnum.TWO.toString(), TestEnum.class, null)); | |||
Assert.assertEquals(AnotherTestEnum.TWO, converter | |||
.convertToPresentation(AnotherTestEnum.TWO.toString(), null)); | |||
.convertToPresentation(AnotherTestEnum.TWO.toString(), | |||
AnotherTestEnum.class, null)); | |||
} | |||
@Test |
@@ -11,11 +11,11 @@ public class TestDateToLongConverter extends TestCase { | |||
DateToLongConverter converter = new DateToLongConverter(); | |||
public void testNullConversion() { | |||
assertEquals(null, converter.convertToModel(null, null)); | |||
assertEquals(null, converter.convertToModel(null, Long.class, null)); | |||
} | |||
public void testValueConversion() { | |||
assertEquals(Long.valueOf(946677600000l), | |||
converter.convertToModel(new Date(100, 0, 1), null)); | |||
converter.convertToModel(new Date(100, 0, 1), Long.class, null)); | |||
} | |||
} |
@@ -41,7 +41,8 @@ public class TestSpecificEnumToStringConverter { | |||
} | |||
@Override | |||
public String convertToModel(Enum value, Locale locale) | |||
public String convertToModel(Enum value, | |||
Class<? extends String> targetType, Locale locale) | |||
throws com.vaadin.data.util.converter.Converter.ConversionException { | |||
if (value == null) { | |||
return null; | |||
@@ -51,7 +52,8 @@ public class TestSpecificEnumToStringConverter { | |||
} | |||
@Override | |||
public Enum convertToPresentation(String value, Locale locale) | |||
public Enum convertToPresentation(String value, | |||
Class<? extends Enum> targetType, Locale locale) | |||
throws com.vaadin.data.util.converter.Converter.ConversionException { | |||
if (value == null) { | |||
return null; | |||
@@ -90,19 +92,21 @@ public class TestSpecificEnumToStringConverter { | |||
@Test | |||
public void nullConversion() { | |||
Assert.assertEquals(null, testEnumConverter.convertToModel(null, null)); | |||
Assert.assertEquals(null, | |||
testEnumConverter.convertToModel(null, null, null)); | |||
} | |||
@Test | |||
public void enumToStringConversion() { | |||
Assert.assertEquals(TestEnum.TWO.toString(), | |||
testEnumConverter.convertToModel(TestEnum.TWO, null)); | |||
Assert.assertEquals(TestEnum.TWO.toString(), testEnumConverter | |||
.convertToModel(TestEnum.TWO, String.class, null)); | |||
} | |||
@Test | |||
public void stringToEnumConversion() { | |||
Assert.assertEquals(TestEnum.TWO, testEnumConverter | |||
.convertToPresentation(TestEnum.TWO.toString(), null)); | |||
.convertToPresentation(TestEnum.TWO.toString(), TestEnum.class, | |||
null)); | |||
} | |||
@Test |
@@ -9,15 +9,15 @@ public class TestStringToBooleanConverter extends TestCase { | |||
StringToBooleanConverter converter = new StringToBooleanConverter(); | |||
public void testNullConversion() { | |||
assertEquals(null, converter.convertToModel(null, null)); | |||
assertEquals(null, converter.convertToModel(null, null, null)); | |||
} | |||
public void testEmptyStringConversion() { | |||
assertEquals(null, converter.convertToModel("", null)); | |||
assertEquals(null, converter.convertToModel("", Boolean.class, null)); | |||
} | |||
public void testValueConversion() { | |||
assertTrue(converter.convertToModel("true", null)); | |||
assertFalse(converter.convertToModel("false", null)); | |||
assertTrue(converter.convertToModel("true", Boolean.class, null)); | |||
assertFalse(converter.convertToModel("false", Boolean.class, null)); | |||
} | |||
} |
@@ -12,15 +12,15 @@ public class TestStringToDateConverter extends TestCase { | |||
StringToDateConverter converter = new StringToDateConverter(); | |||
public void testNullConversion() { | |||
assertEquals(null, converter.convertToModel(null, null)); | |||
assertEquals(null, converter.convertToModel(null, null, null)); | |||
} | |||
public void testEmptyStringConversion() { | |||
assertEquals(null, converter.convertToModel("", null)); | |||
assertEquals(null, converter.convertToModel("", Date.class, null)); | |||
} | |||
public void testValueConversion() { | |||
assertEquals(new Date(100, 0, 1), converter.convertToModel( | |||
"Jan 1, 2000 12:00:00 AM", Locale.ENGLISH)); | |||
"Jan 1, 2000 12:00:00 AM", Date.class, Locale.ENGLISH)); | |||
} | |||
} |
@@ -9,14 +9,14 @@ public class TestStringToDoubleConverter extends TestCase { | |||
StringToDoubleConverter converter = new StringToDoubleConverter(); | |||
public void testNullConversion() { | |||
assertEquals(null, converter.convertToModel(null, null)); | |||
assertEquals(null, converter.convertToModel(null, null, null)); | |||
} | |||
public void testEmptyStringConversion() { | |||
assertEquals(null, converter.convertToModel("", null)); | |||
assertEquals(null, converter.convertToModel("", Double.class, null)); | |||
} | |||
public void testValueConversion() { | |||
assertEquals(10.0, converter.convertToModel("10", null)); | |||
assertEquals(10.0, converter.convertToModel("10", Double.class, null)); | |||
} | |||
} |
@@ -9,14 +9,15 @@ public class TestStringToFloatConverter extends TestCase { | |||
StringToFloatConverter converter = new StringToFloatConverter(); | |||
public void testNullConversion() { | |||
assertEquals(null, converter.convertToModel(null, null)); | |||
assertEquals(null, converter.convertToModel(null, null, null)); | |||
} | |||
public void testEmptyStringConversion() { | |||
assertEquals(null, converter.convertToModel("", null)); | |||
assertEquals(null, converter.convertToModel("", Float.class, null)); | |||
} | |||
public void testValueConversion() { | |||
assertEquals(Float.valueOf(10), converter.convertToModel("10", null)); | |||
assertEquals(Float.valueOf(10), | |||
converter.convertToModel("10", Float.class, null)); | |||
} | |||
} |
@@ -9,14 +9,15 @@ public class TestStringToIntegerConverter extends TestCase { | |||
StringToIntegerConverter converter = new StringToIntegerConverter(); | |||
public void testNullConversion() { | |||
assertEquals(null, converter.convertToModel(null, null)); | |||
assertEquals(null, converter.convertToModel(null, null, null)); | |||
} | |||
public void testEmptyStringConversion() { | |||
assertEquals(null, converter.convertToModel("", null)); | |||
assertEquals(null, converter.convertToModel("", Integer.class, null)); | |||
} | |||
public void testValueConversion() { | |||
assertEquals(Integer.valueOf(10), converter.convertToModel("10", null)); | |||
assertEquals(Integer.valueOf(10), | |||
converter.convertToModel("10", Integer.class, null)); | |||
} | |||
} |
@@ -9,15 +9,16 @@ public class TestStringToNumberConverter extends TestCase { | |||
StringToNumberConverter converter = new StringToNumberConverter(); | |||
public void testNullConversion() { | |||
assertEquals(null, converter.convertToModel(null, null)); | |||
assertEquals(null, converter.convertToModel(null, null, null)); | |||
} | |||
public void testEmptyStringConversion() { | |||
assertEquals(null, converter.convertToModel("", null)); | |||
assertEquals(null, converter.convertToModel("", Number.class, null)); | |||
} | |||
public void testValueConversion() { | |||
assertEquals(Long.valueOf(10), converter.convertToModel("10", null)); | |||
assertEquals(10.5, converter.convertToModel("10.5", null)); | |||
assertEquals(Long.valueOf(10), | |||
converter.convertToModel("10", Number.class, null)); | |||
assertEquals(10.5, converter.convertToModel("10.5", Number.class, null)); | |||
} | |||
} |
@@ -85,12 +85,14 @@ public class AbsFieldValueConversions extends TestCase { | |||
tf.setConverter(new Converter<String, String>() { | |||
@Override | |||
public String convertToModel(String value, Locale locale) { | |||
public String convertToModel(String value, | |||
Class<? extends String> targetType, Locale locale) { | |||
return value; | |||
} | |||
@Override | |||
public String convertToPresentation(String value, Locale locale) { | |||
public String convertToPresentation(String value, | |||
Class<? extends String> targetType, Locale locale) { | |||
return value; | |||
} | |||
@@ -150,7 +152,8 @@ public class AbsFieldValueConversions extends TestCase { | |||
cb.setConverter(new Converter<Boolean, Boolean>() { | |||
@Override | |||
public Boolean convertToModel(Boolean value, Locale locale) { | |||
public Boolean convertToModel(Boolean value, | |||
Class<? extends Boolean> targetType, Locale locale) { | |||
// value from a CheckBox should never be null as long as it is | |||
// not set to null (handled by conversion below). | |||
assertNotNull(value); | |||
@@ -158,7 +161,8 @@ public class AbsFieldValueConversions extends TestCase { | |||
} | |||
@Override | |||
public Boolean convertToPresentation(Boolean value, Locale locale) { | |||
public Boolean convertToPresentation(Boolean value, | |||
Class<? extends Boolean> targetType, Locale locale) { | |||
// Datamodel -> field | |||
if (value == null) { | |||
return false; | |||
@@ -184,7 +188,7 @@ public class AbsFieldValueConversions extends TestCase { | |||
assertEquals(Boolean.FALSE, property.getValue()); | |||
assertEquals(Boolean.FALSE, cb.getValue()); | |||
Boolean newDmValue = cb.getConverter().convertToPresentation( | |||
cb.getValue(), new Locale("fi", "FI")); | |||
cb.getValue(), Boolean.class, new Locale("fi", "FI")); | |||
assertEquals(Boolean.FALSE, newDmValue); | |||
// FIXME: Should be able to set to false here to cause datamodel to be |
@@ -8,7 +8,8 @@ public class Vaadin6ImplicitDoubleConverter implements | |||
Converter<String, Double> { | |||
@Override | |||
public Double convertToModel(String value, Locale locale) | |||
public Double convertToModel(String value, | |||
Class<? extends Double> targetType, Locale locale) | |||
throws com.vaadin.data.util.converter.Converter.ConversionException { | |||
if (null == value) { | |||
return null; | |||
@@ -17,7 +18,8 @@ public class Vaadin6ImplicitDoubleConverter implements | |||
} | |||
@Override | |||
public String convertToPresentation(Double value, Locale locale) | |||
public String convertToPresentation(Double value, | |||
Class<? extends String> targetType, Locale locale) | |||
throws com.vaadin.data.util.converter.Converter.ConversionException { | |||
if (value == null) { | |||
return null; |
@@ -150,14 +150,16 @@ public class DoublesInTable extends TestBase { | |||
t.setConverter("sex", new Converter<String, Sex>() { | |||
@Override | |||
public Sex convertToModel(String value, Locale locale) | |||
public Sex convertToModel(String value, | |||
Class<? extends Sex> targetType, Locale locale) | |||
throws com.vaadin.data.util.converter.Converter.ConversionException { | |||
// not used in this test - Table only converts to presentation | |||
return null; | |||
} | |||
@Override | |||
public String convertToPresentation(Sex value, Locale locale) | |||
public String convertToPresentation(Sex value, | |||
Class<? extends String> targetType, Locale locale) | |||
throws com.vaadin.data.util.converter.Converter.ConversionException { | |||
if (value == null) { | |||
value = Sex.UNKNOWN; | |||
@@ -178,14 +180,16 @@ public class DoublesInTable extends TestBase { | |||
t.setConverter("deceased", new Converter<String, Boolean>() { | |||
@Override | |||
public Boolean convertToModel(String value, Locale locale) { | |||
public Boolean convertToModel(String value, | |||
Class<? extends Boolean> targetType, Locale locale) { | |||
// not used in this test - Table only converts from source to | |||
// target | |||
return null; | |||
} | |||
@Override | |||
public String convertToPresentation(Boolean value, Locale locale) { | |||
public String convertToPresentation(Boolean value, | |||
Class<? extends String> targetType, Locale locale) { | |||
if (value == null || value) { | |||
return "YES, DEAD!"; | |||
} else { | |||
@@ -206,7 +210,8 @@ public class DoublesInTable extends TestBase { | |||
t.setConverter("age", new Converter<String, Integer>() { | |||
@Override | |||
public Integer convertToModel(String value, Locale locale) | |||
public Integer convertToModel(String value, | |||
Class<? extends Integer> targetType, Locale locale) | |||
throws com.vaadin.data.util.converter.Converter.ConversionException { | |||
// not used in this test - Table only converts from source to | |||
// target | |||
@@ -214,7 +219,8 @@ public class DoublesInTable extends TestBase { | |||
} | |||
@Override | |||
public String convertToPresentation(Integer value, Locale locale) | |||
public String convertToPresentation(Integer value, | |||
Class<? extends String> targetType, Locale locale) | |||
throws com.vaadin.data.util.converter.Converter.ConversionException { | |||
if (value == null) { | |||
return null; | |||
@@ -243,14 +249,16 @@ public class DoublesInTable extends TestBase { | |||
t.setConverter("address", new Converter<String, Address>() { | |||
@Override | |||
public Address convertToModel(String value, Locale locale) | |||
public Address convertToModel(String value, | |||
Class<? extends Address> targetType, Locale locale) | |||
throws ConversionException { | |||
// not used in this test - Table only converts to presentation | |||
return null; | |||
} | |||
@Override | |||
public String convertToPresentation(Address value, Locale locale) | |||
public String convertToPresentation(Address value, | |||
Class<? extends String> targetType, Locale locale) | |||
throws ConversionException { | |||
return value.getStreetAddress() + ", " + value.getCity() + " (" | |||
+ value.getCountry() + ")"; |
@@ -29,14 +29,16 @@ public class TableWithCustomConverterFactory extends AbstractTestUI { | |||
Converter<String, Integer> { | |||
@Override | |||
public Integer convertToModel(String value, Locale locale) | |||
public Integer convertToModel(String value, | |||
Class<? extends Integer> targetType, Locale locale) | |||
throws com.vaadin.data.util.converter.Converter.ConversionException { | |||
// TODO Auto-generated method stub | |||
return null; | |||
} | |||
@Override | |||
public String convertToPresentation(Integer value, Locale locale) | |||
public String convertToPresentation(Integer value, | |||
Class<? extends String> targetType, Locale locale) | |||
throws com.vaadin.data.util.converter.Converter.ConversionException { | |||
return "Integer: " + value; | |||
} |
@@ -28,7 +28,8 @@ public class ConverterThatEnforcesAFormat extends TestBase { | |||
+ "). Two-way conversion gives: " | |||
+ tf.getConverter().convertToPresentation( | |||
tf.getConverter().convertToModel(tf.getValue(), | |||
tf.getLocale()), tf.getLocale()) + ")"); | |||
Double.class, tf.getLocale()), | |||
String.class, tf.getLocale()) + ")"); | |||
} | |||
}); | |||
tf.setImmediate(true); |
@@ -52,8 +52,8 @@ public class StringMyTypeConverter extends AbstractTestUI { | |||
class StringToNameConverter implements Converter<String, Name> { | |||
@Override | |||
public Name convertToModel(String text, Locale locale) | |||
throws ConversionException { | |||
public Name convertToModel(String text, Class<? extends Name> targetType, | |||
Locale locale) throws ConversionException { | |||
if (text == null) { | |||
return null; | |||
} | |||
@@ -66,7 +66,8 @@ class StringToNameConverter implements Converter<String, Name> { | |||
} | |||
@Override | |||
public String convertToPresentation(Name name, Locale locale) | |||
public String convertToPresentation(Name name, | |||
Class<? extends String> targetType, Locale locale) | |||
throws ConversionException { | |||
if (name == null) { | |||
return null; |