Browse Source

Add ValueContext parameter for Converters

Change-Id: I47179b06b9e345f5a454ac1806d0bc9bcac24bcf
tags/8.0.0.alpha6
Teemu Suo-Anttila 7 years ago
parent
commit
c72beed926
40 changed files with 394 additions and 144 deletions
  1. 19
    5
      server/src/main/java/com/vaadin/data/Binder.java
  2. 2
    2
      server/src/main/java/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java
  3. 28
    14
      server/src/main/java/com/vaadin/data/util/converter/Converter.java
  4. 2
    3
      server/src/main/java/com/vaadin/data/util/converter/DateToLongConverter.java
  5. 4
    3
      server/src/main/java/com/vaadin/data/util/converter/DateToSqlDateConverter.java
  6. 3
    2
      server/src/main/java/com/vaadin/data/util/converter/StringToBigDecimalConverter.java
  7. 10
    8
      server/src/main/java/com/vaadin/data/util/converter/StringToBigIntegerConverter.java
  8. 4
    2
      server/src/main/java/com/vaadin/data/util/converter/StringToBooleanConverter.java
  9. 5
    4
      server/src/main/java/com/vaadin/data/util/converter/StringToDateConverter.java
  10. 3
    2
      server/src/main/java/com/vaadin/data/util/converter/StringToDoubleConverter.java
  11. 3
    2
      server/src/main/java/com/vaadin/data/util/converter/StringToFloatConverter.java
  12. 3
    2
      server/src/main/java/com/vaadin/data/util/converter/StringToIntegerConverter.java
  13. 3
    2
      server/src/main/java/com/vaadin/data/util/converter/StringToLongConverter.java
  14. 103
    0
      server/src/main/java/com/vaadin/data/util/converter/ValueContext.java
  15. 2
    1
      server/src/main/java/com/vaadin/ui/declarative/DesignAttributeHandler.java
  16. 13
    7
      server/src/main/java/com/vaadin/ui/declarative/DesignFormatter.java
  17. 3
    3
      server/src/main/java/com/vaadin/ui/declarative/converters/DesignDateConverter.java
  18. 3
    2
      server/src/main/java/com/vaadin/ui/declarative/converters/DesignEnumConverter.java
  19. 10
    6
      server/src/main/java/com/vaadin/ui/declarative/converters/DesignLocalDateConverter.java
  20. 3
    4
      server/src/main/java/com/vaadin/ui/declarative/converters/DesignObjectConverter.java
  21. 3
    2
      server/src/main/java/com/vaadin/ui/declarative/converters/DesignResourceConverter.java
  22. 5
    3
      server/src/main/java/com/vaadin/ui/declarative/converters/DesignShortcutActionConverter.java
  23. 3
    3
      server/src/main/java/com/vaadin/ui/declarative/converters/DesignTimeZoneConverter.java
  24. 3
    3
      server/src/main/java/com/vaadin/ui/declarative/converters/DesignToStringConverter.java
  25. 4
    3
      server/src/test/java/com/vaadin/data/BinderBookOfVaadinTest.java
  26. 3
    3
      server/src/test/java/com/vaadin/data/BinderMultiSelectTest.java
  27. 60
    0
      server/src/test/java/com/vaadin/data/ValueContextTest.java
  28. 3
    1
      server/src/test/java/com/vaadin/tests/data/converter/AbstractConverterTest.java
  29. 5
    2
      server/src/test/java/com/vaadin/tests/data/converter/AbstractStringConverterTest.java
  30. 3
    3
      server/src/test/java/com/vaadin/tests/data/converter/ConverterTest.java
  31. 4
    2
      server/src/test/java/com/vaadin/tests/data/converter/DateToLongConverterTest.java
  32. 3
    2
      server/src/test/java/com/vaadin/tests/data/converter/DateToSqlDateConverterTest.java
  33. 3
    2
      server/src/test/java/com/vaadin/tests/data/converter/StringToBigDecimalConverterTest.java
  34. 3
    2
      server/src/test/java/com/vaadin/tests/data/converter/StringToBigIntegerConverterTest.java
  35. 28
    20
      server/src/test/java/com/vaadin/tests/data/converter/StringToBooleanConverterTest.java
  36. 5
    3
      server/src/test/java/com/vaadin/tests/data/converter/StringToDateConverterTest.java
  37. 7
    3
      server/src/test/java/com/vaadin/tests/data/converter/StringToDoubleConverterTest.java
  38. 6
    3
      server/src/test/java/com/vaadin/tests/data/converter/StringToFloatConverterTest.java
  39. 7
    4
      server/src/test/java/com/vaadin/tests/data/converter/StringToIntegerConverterTest.java
  40. 10
    6
      server/src/test/java/com/vaadin/tests/data/converter/StringToLongConverterTest.java

+ 19
- 5
server/src/main/java/com/vaadin/data/Binder.java View File

@@ -34,6 +34,7 @@ import java.util.stream.Collectors;

import com.vaadin.data.util.converter.Converter;
import com.vaadin.data.util.converter.StringToIntegerConverter;
import com.vaadin.data.util.converter.ValueContext;
import com.vaadin.event.EventRouter;
import com.vaadin.server.ErrorMessage;
import com.vaadin.server.UserError;
@@ -563,10 +564,23 @@ public class Binder<BEAN> implements Serializable {
private ValidationStatus<TARGET> doValidation() {
FIELDVALUE fieldValue = field.getValue();
Result<TARGET> dataValue = converterValidatorChain
.convertToModel(fieldValue, findLocale());
.convertToModel(fieldValue, createValueContext());
return new ValidationStatus<>(this, dataValue);
}

/**
* Creates a value context from the current state of the binding and its
* field.
*
* @return the value context
*/
protected ValueContext createValueContext() {
if (field instanceof Component) {
return new ValueContext((Component) field);
}
return new ValueContext(findLocale());
}

private void unbind() {
onValueChange.remove();
}
@@ -584,8 +598,8 @@ public class Binder<BEAN> implements Serializable {
}

private FIELDVALUE convertDataToFieldType(BEAN bean) {
return converterValidatorChain
.convertToPresentation(getter.apply(bean), findLocale());
return converterValidatorChain.convertToPresentation(
getter.apply(bean), createValueContext());
}

/**
@@ -661,7 +675,7 @@ public class Binder<BEAN> implements Serializable {
}

@Override
public Result<T> convertToModel(T value, Locale locale) {
public Result<T> convertToModel(T value, ValueContext context) {
Result<? super T> validationResult = validator.apply(value);
if (validationResult.isError()) {
return Result.error(validationResult.getMessage().get());
@@ -671,7 +685,7 @@ public class Binder<BEAN> implements Serializable {
}

@Override
public T convertToPresentation(T value, Locale locale) {
public T convertToPresentation(T value, ValueContext context) {
return value;
}


+ 2
- 2
server/src/main/java/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java View File

@@ -109,12 +109,12 @@ public abstract class AbstractStringToNumberConverter<T>
}

@Override
public String convertToPresentation(T value, Locale locale) {
public String convertToPresentation(T value, ValueContext context) {
if (value == null) {
return null;
}

return getFormat(locale).format(value);
return getFormat(context.getLocale().orElse(null)).format(value);
}

}

+ 28
- 14
server/src/main/java/com/vaadin/data/util/converter/Converter.java View File

@@ -45,11 +45,17 @@ public interface Converter<PRESENTATION, MODEL> extends Serializable {
*
* @param value
* The value to convert. Can be null
* @param locale
* The locale to use for conversion. Can be null.
* @param context
* The value context for the conversion.
* @return The converted value compatible with the source type
*/
public Result<MODEL> convertToModel(PRESENTATION value, Locale locale);
public Result<MODEL> convertToModel(PRESENTATION value,
ValueContext context);

default public Result<MODEL> convertToModel(PRESENTATION value,
Locale locale) {
return convertToModel(value, new ValueContext(locale));
}

/**
* Converts the given value from presentation type to model type.
@@ -58,11 +64,17 @@ public interface Converter<PRESENTATION, MODEL> extends Serializable {
*
* @param value
* The value to convert. Can be null
* @param locale
* The locale to use for conversion. Can be null.
* @param context
* The value context for the conversion.
* @return The converted value compatible with the source type
*/
public PRESENTATION convertToPresentation(MODEL value, Locale locale);
public PRESENTATION convertToPresentation(MODEL value,
ValueContext context);

default public PRESENTATION convertToPresentation(MODEL value,
Locale locale) {
return convertToPresentation(value, new ValueContext());
}

/**
* Returns a converter that returns its input as-is in both directions.
@@ -124,12 +136,12 @@ public interface Converter<PRESENTATION, MODEL> extends Serializable {
return new Converter<P, M>() {

@Override
public Result<M> convertToModel(P value, Locale locale) {
public Result<M> convertToModel(P value, ValueContext context) {
return toModel.apply(value);
}

@Override
public P convertToPresentation(M value, Locale locale) {
public P convertToPresentation(M value, ValueContext context) {
return toPresentation.apply(value);
}
};
@@ -157,16 +169,18 @@ public interface Converter<PRESENTATION, MODEL> extends Serializable {
Converter<MODEL, T> other) {
return new Converter<PRESENTATION, T>() {
@Override
public Result<T> convertToModel(PRESENTATION value, Locale locale) {
public Result<T> convertToModel(PRESENTATION value,
ValueContext context) {
Result<MODEL> model = Converter.this.convertToModel(value,
locale);
return model.flatMap(v -> other.convertToModel(v, locale));
context);
return model.flatMap(v -> other.convertToModel(v, context));
}

@Override
public PRESENTATION convertToPresentation(T value, Locale locale) {
MODEL model = other.convertToPresentation(value, locale);
return Converter.this.convertToPresentation(model, locale);
public PRESENTATION convertToPresentation(T value,
ValueContext context) {
MODEL model = other.convertToPresentation(value, context);
return Converter.this.convertToPresentation(model, context);
}
};
}

+ 2
- 3
server/src/main/java/com/vaadin/data/util/converter/DateToLongConverter.java View File

@@ -17,7 +17,6 @@
package com.vaadin.data.util.converter;

import java.util.Date;
import java.util.Locale;

import com.vaadin.data.Result;

@@ -30,7 +29,7 @@ import com.vaadin.data.Result;
public class DateToLongConverter implements Converter<Date, Long> {

@Override
public Result<Long> convertToModel(Date value, Locale locale) {
public Result<Long> convertToModel(Date value, ValueContext context) {
if (value == null) {
return Result.ok(null);
}
@@ -39,7 +38,7 @@ public class DateToLongConverter implements Converter<Date, Long> {
}

@Override
public Date convertToPresentation(Long value, Locale locale) {
public Date convertToPresentation(Long value, ValueContext context) {
if (value == null) {
return null;
}

+ 4
- 3
server/src/main/java/com/vaadin/data/util/converter/DateToSqlDateConverter.java View File

@@ -20,7 +20,6 @@
package com.vaadin.data.util.converter;

import java.util.Date;
import java.util.Locale;

import com.vaadin.data.Result;

@@ -37,7 +36,8 @@ import com.vaadin.data.Result;
public class DateToSqlDateConverter implements Converter<Date, java.sql.Date> {

@Override
public Result<java.sql.Date> convertToModel(Date value, Locale locale) {
public Result<java.sql.Date> convertToModel(Date value,
ValueContext context) {
if (value == null) {
return Result.ok(null);
}
@@ -46,7 +46,8 @@ public class DateToSqlDateConverter implements Converter<Date, java.sql.Date> {
}

@Override
public Date convertToPresentation(java.sql.Date value, Locale locale) {
public Date convertToPresentation(java.sql.Date value,
ValueContext context) {
if (value == null) {
return null;
}

+ 3
- 2
server/src/main/java/com/vaadin/data/util/converter/StringToBigDecimalConverter.java View File

@@ -60,8 +60,9 @@ public class StringToBigDecimalConverter
}

@Override
public Result<BigDecimal> convertToModel(String value, Locale locale) {
return convertToNumber(value, locale)
public Result<BigDecimal> convertToModel(String value,
ValueContext context) {
return convertToNumber(value, context.getLocale().orElse(null))
.map(number -> (BigDecimal) number);
}


+ 10
- 8
server/src/main/java/com/vaadin/data/util/converter/StringToBigIntegerConverter.java View File

@@ -60,14 +60,16 @@ public class StringToBigIntegerConverter
}

@Override
public Result<BigInteger> convertToModel(String value, Locale locale) {
return convertToNumber(value, locale).map(number -> {
if (number == null) {
return null;
} else {
return ((BigDecimal) number).toBigInteger();
}
});
public Result<BigInteger> convertToModel(String value,
ValueContext context) {
return convertToNumber(value, context.getLocale().orElse(null))
.map(number -> {
if (number == null) {
return null;
} else {
return ((BigDecimal) number).toBigInteger();
}
});
}

}

+ 4
- 2
server/src/main/java/com/vaadin/data/util/converter/StringToBooleanConverter.java View File

@@ -72,7 +72,7 @@ public class StringToBooleanConverter implements Converter<String, Boolean> {
}

@Override
public Result<Boolean> convertToModel(String value, Locale locale) {
public Result<Boolean> convertToModel(String value, ValueContext context) {
if (value == null) {
return Result.ok(null);
}
@@ -80,6 +80,7 @@ public class StringToBooleanConverter implements Converter<String, Boolean> {
// Remove leading and trailing white space
value = value.trim();

Locale locale = context.getLocale().orElse(null);
if (getTrueString(locale).equals(value)) {
return Result.ok(true);
} else if (getFalseString(locale).equals(value)) {
@@ -92,10 +93,11 @@ public class StringToBooleanConverter implements Converter<String, Boolean> {
}

@Override
public String convertToPresentation(Boolean value, Locale locale) {
public String convertToPresentation(Boolean value, ValueContext context) {
if (value == null) {
return null;
}
Locale locale = context.getLocale().orElse(null);
if (value) {
return getTrueString(locale);
} else {

+ 5
- 4
server/src/main/java/com/vaadin/data/util/converter/StringToDateConverter.java View File

@@ -58,7 +58,7 @@ public class StringToDateConverter implements Converter<String, Date> {
}

@Override
public Result<Date> convertToModel(String value, Locale locale) {
public Result<Date> convertToModel(String value, ValueContext context) {
if (value == null) {
return Result.ok(null);
}
@@ -67,7 +67,8 @@ public class StringToDateConverter implements Converter<String, Date> {
value = value.trim();

ParsePosition parsePosition = new ParsePosition(0);
Date parsedValue = getFormat(locale).parse(value, parsePosition);
Date parsedValue = getFormat(context.getLocale().orElse(null))
.parse(value, parsePosition);
if (parsePosition.getIndex() != value.length()) {
return Result.error("Could not convert '" + value);
}
@@ -76,12 +77,12 @@ public class StringToDateConverter implements Converter<String, Date> {
}

@Override
public String convertToPresentation(Date value, Locale locale) {
public String convertToPresentation(Date value, ValueContext context) {
if (value == null) {
return null;
}

return getFormat(locale).format(value);
return getFormat(context.getLocale().orElse(null)).format(value);
}

}

+ 3
- 2
server/src/main/java/com/vaadin/data/util/converter/StringToDoubleConverter.java View File

@@ -49,8 +49,9 @@ public class StringToDoubleConverter
}

@Override
public Result<Double> convertToModel(String value, Locale locale) {
Result<Number> n = convertToNumber(value, locale);
public Result<Double> convertToModel(String value, ValueContext context) {
Result<Number> n = convertToNumber(value,
context.getLocale().orElse(null));

return n.map(number -> {
if (number == null) {

+ 3
- 2
server/src/main/java/com/vaadin/data/util/converter/StringToFloatConverter.java View File

@@ -47,8 +47,9 @@ public class StringToFloatConverter
}

@Override
public Result<Float> convertToModel(String value, Locale locale) {
Result<Number> n = convertToNumber(value, locale);
public Result<Float> convertToModel(String value, ValueContext context) {
Result<Number> n = convertToNumber(value,
context.getLocale().orElse(null));

return n.map(number -> {
if (number == null) {

+ 3
- 2
server/src/main/java/com/vaadin/data/util/converter/StringToIntegerConverter.java View File

@@ -63,8 +63,9 @@ public class StringToIntegerConverter
}

@Override
public Result<Integer> convertToModel(String value, Locale locale) {
Result<Number> n = convertToNumber(value, locale);
public Result<Integer> convertToModel(String value, ValueContext context) {
Result<Number> n = convertToNumber(value,
context.getLocale().orElse(null));
return n.flatMap(number -> {
if (number == null) {
return Result.ok(null);

+ 3
- 2
server/src/main/java/com/vaadin/data/util/converter/StringToLongConverter.java View File

@@ -62,8 +62,9 @@ public class StringToLongConverter
}

@Override
public Result<Long> convertToModel(String value, Locale locale) {
Result<Number> n = convertToNumber(value, locale);
public Result<Long> convertToModel(String value, ValueContext context) {
Result<Number> n = convertToNumber(value,
context.getLocale().orElse(null));
return n.map(number -> {
if (number == null) {
return null;

+ 103
- 0
server/src/main/java/com/vaadin/data/util/converter/ValueContext.java View File

@@ -0,0 +1,103 @@
/*
* Copyright 2000-2016 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.data.util.converter;

import java.io.Serializable;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;

import com.vaadin.ui.Component;
import com.vaadin.ui.UI;

/**
* Value context for {@code Converter}s. Contains relevant information for
* converting values.
*
* @author Vaadin Ltd.
* @since
*/
public class ValueContext implements Serializable {

private final Component component;
private final Locale locale;

/**
* Constructor for {@code ValueContext} without a {@code Locale}.
*/
public ValueContext() {
this.locale = null;
this.component = null;
}

/**
* Constructor for {@code ValueContext} without a {@code Component}.
*
* @param locale
* The locale used with conversion. Can be null.
*/
public ValueContext(Locale locale) {
this.component = null;
this.locale = locale;
}

/**
* Constructor for {@code ValueContext}.
*
* @param component
* The component related to current value. Can be null.
*/
public ValueContext(Component component) {
Objects.requireNonNull(component,
"Component can't be null in ValueContext construction");
this.component = component;
this.locale = findLocale();
}

private Locale findLocale() {
Locale l = null;
if (component != null) {
l = component.getLocale();
}
if (l == null && UI.getCurrent() != null) {
l = UI.getCurrent().getLocale();
}
if (l == null) {
l = Locale.getDefault();
}
return l;
}

/**
* Returns an {@code Optional} for the {@code Component} related to value
* conversion.
*
* @return the optional of component
*/
public Optional<Component> getComponent() {
return Optional.ofNullable(component);
}

/**
* Returns an {@code Optional} for the {@code Locale} used in the value
* conversion.
*
* @return the optional of locale
*/
public Optional<Locale> getLocale() {
return Optional.ofNullable(locale);
}
}

+ 2
- 1
server/src/main/java/com/vaadin/ui/declarative/DesignAttributeHandler.java View File

@@ -37,6 +37,7 @@ import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;

import com.vaadin.data.util.converter.Converter;
import com.vaadin.data.util.converter.ValueContext;
import com.vaadin.shared.ui.AlignmentInfo;
import com.vaadin.shared.util.SharedUtil;
import com.vaadin.ui.Alignment;
@@ -398,7 +399,7 @@ public class DesignAttributeHandler implements Serializable {
Converter<String, Object> converter = (Converter<String, Object>) getFormatter()
.findConverterFor(sourceType);
if (converter != null) {
return converter.convertToPresentation(value, null);
return converter.convertToPresentation(value, new ValueContext());
} else {
return value.toString();
}

+ 13
- 7
server/src/main/java/com/vaadin/ui/declarative/DesignFormatter.java View File

@@ -36,6 +36,7 @@ import com.vaadin.data.util.converter.Converter;
import com.vaadin.data.util.converter.StringToBigDecimalConverter;
import com.vaadin.data.util.converter.StringToDoubleConverter;
import com.vaadin.data.util.converter.StringToFloatConverter;
import com.vaadin.data.util.converter.ValueContext;
import com.vaadin.event.ShortcutAction;
import com.vaadin.server.Resource;
import com.vaadin.ui.declarative.converters.DesignDateConverter;
@@ -88,12 +89,14 @@ public class DesignFormatter implements Serializable {
Converter<String, Boolean> booleanConverter = new Converter<String, Boolean>() {

@Override
public Result<Boolean> convertToModel(String value, Locale locale) {
public Result<Boolean> convertToModel(String value,
ValueContext context) {
return Result.ok(!value.equalsIgnoreCase("false"));
}

@Override
public String convertToPresentation(Boolean value, Locale locale) {
public String convertToPresentation(Boolean value,
ValueContext context) {
if (value.booleanValue()) {
return "";
} else {
@@ -146,12 +149,14 @@ public class DesignFormatter implements Serializable {
converterMap.put(String.class, new Converter<String, String>() {

@Override
public Result<String> convertToModel(String value, Locale locale) {
public Result<String> convertToModel(String value,
ValueContext context) {
return Result.ok(value);
}

@Override
public String convertToPresentation(String value, Locale locale) {
public String convertToPresentation(String value,
ValueContext context) {
return value;
}

@@ -163,7 +168,7 @@ public class DesignFormatter implements Serializable {

@Override
public Result<Character> convertToModel(String value,
Locale locale) {
ValueContext context) {
return Result.ok(value.charAt(0));
}

@@ -226,7 +231,8 @@ public class DesignFormatter implements Serializable {
public <T> T parse(String value, Class<? extends T> type) {
Converter<String, T> converter = findConverterFor(type);
if (converter != null) {
Result<T> result = converter.convertToModel(value, null);
Result<T> result = converter.convertToModel(value,
new ValueContext());
return result.getOrThrow(msg -> new IllegalArgumentException(msg));
} else {
return null;
@@ -262,7 +268,7 @@ public class DesignFormatter implements Serializable {
} else {
Converter<String, Object> converter = findConverterFor(
object.getClass());
return converter.convertToPresentation(object, null);
return converter.convertToPresentation(object, new ValueContext());
}
}


+ 3
- 3
server/src/main/java/com/vaadin/ui/declarative/converters/DesignDateConverter.java View File

@@ -18,10 +18,10 @@ package com.vaadin.ui.declarative.converters;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import com.vaadin.data.Result;
import com.vaadin.data.util.converter.Converter;
import com.vaadin.data.util.converter.ValueContext;
import com.vaadin.ui.declarative.DesignAttributeHandler;

/**
@@ -34,7 +34,7 @@ import com.vaadin.ui.declarative.DesignAttributeHandler;
public class DesignDateConverter implements Converter<String, Date> {

@Override
public Result<Date> convertToModel(String value, Locale locale) {
public Result<Date> convertToModel(String value, ValueContext context) {
for (String pattern : new String[] { "yyyy-MM-dd HH:mm:ssZ",
"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd HH",
"yyyy-MM-dd", "yyyy-MM", "yyyy" }) {
@@ -48,7 +48,7 @@ public class DesignDateConverter implements Converter<String, Date> {
}

@Override
public String convertToPresentation(Date value, Locale locale) {
public String convertToPresentation(Date value, ValueContext context) {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ").format(value);
}


+ 3
- 2
server/src/main/java/com/vaadin/ui/declarative/converters/DesignEnumConverter.java View File

@@ -19,6 +19,7 @@ import java.util.Locale;

import com.vaadin.data.Result;
import com.vaadin.data.util.converter.Converter;
import com.vaadin.data.util.converter.ValueContext;
import com.vaadin.ui.declarative.DesignAttributeHandler;

/**
@@ -46,7 +47,7 @@ public class DesignEnumConverter<T extends Enum>

@SuppressWarnings("unchecked")
@Override
public Result<T> convertToModel(String value, Locale locale) {
public Result<T> convertToModel(String value, ValueContext context) {
if (value == null || value.trim().equals("")) {
return Result.ok(null);
}
@@ -60,7 +61,7 @@ public class DesignEnumConverter<T extends Enum>
}

@Override
public String convertToPresentation(T value, Locale locale) {
public String convertToPresentation(T value, ValueContext context) {
if (value == null) {
return null;
}

+ 10
- 6
server/src/main/java/com/vaadin/ui/declarative/converters/DesignLocalDateConverter.java View File

@@ -22,6 +22,7 @@ import java.util.Locale;

import com.vaadin.data.Result;
import com.vaadin.data.util.converter.Converter;
import com.vaadin.data.util.converter.ValueContext;
import com.vaadin.ui.declarative.DesignAttributeHandler;

/**
@@ -34,12 +35,13 @@ import com.vaadin.ui.declarative.DesignAttributeHandler;
public class DesignLocalDateConverter implements Converter<String, LocalDate> {

@Override
public Result<LocalDate> convertToModel(String value, Locale locale) {
public Result<LocalDate> convertToModel(String value,
ValueContext context) {
for (String pattern : new String[] { "yyyy-MM-dd", "yyyy-MM",
"yyyy" }) {
try {
Locale effectiveLocale = locale == null ? Locale.ENGLISH
: locale;
Locale effectiveLocale = context.getLocale()
.orElse(Locale.ENGLISH);
LocalDate date = DateTimeFormatter
.ofPattern(pattern, effectiveLocale)
.parse(value, LocalDate::from);
@@ -52,9 +54,11 @@ public class DesignLocalDateConverter implements Converter<String, LocalDate> {
}

@Override
public String convertToPresentation(LocalDate value, Locale locale) {
return DateTimeFormatter.ofPattern("yyyy-MM-dd",
locale == null ? Locale.ENGLISH : locale).format(value);
public String convertToPresentation(LocalDate value, ValueContext context) {
return DateTimeFormatter
.ofPattern("yyyy-MM-dd",
context.getLocale().orElse(Locale.ENGLISH))
.format(value);
}

}

+ 3
- 4
server/src/main/java/com/vaadin/ui/declarative/converters/DesignObjectConverter.java View File

@@ -15,10 +15,9 @@
*/
package com.vaadin.ui.declarative.converters;

import java.util.Locale;

import com.vaadin.data.Result;
import com.vaadin.data.util.converter.Converter;
import com.vaadin.data.util.converter.ValueContext;
import com.vaadin.ui.declarative.DesignAttributeHandler;

/**
@@ -31,12 +30,12 @@ import com.vaadin.ui.declarative.DesignAttributeHandler;
public class DesignObjectConverter implements Converter<String, Object> {

@Override
public Result<Object> convertToModel(String value, Locale locale) {
public Result<Object> convertToModel(String value, ValueContext context) {
return Result.ok(value);
}

@Override
public String convertToPresentation(Object value, Locale locale) {
public String convertToPresentation(Object value, ValueContext context) {
if (value == null) {
return null;
}

+ 3
- 2
server/src/main/java/com/vaadin/ui/declarative/converters/DesignResourceConverter.java View File

@@ -23,6 +23,7 @@ import java.util.Map;

import com.vaadin.data.Result;
import com.vaadin.data.util.converter.Converter;
import com.vaadin.data.util.converter.ValueContext;
import com.vaadin.server.ExternalResource;
import com.vaadin.server.FileResource;
import com.vaadin.server.FontAwesome;
@@ -44,7 +45,7 @@ import com.vaadin.ui.declarative.DesignAttributeHandler;
public class DesignResourceConverter implements Converter<String, Resource> {

@Override
public Result<Resource> convertToModel(String value, Locale locale) {
public Result<Resource> convertToModel(String value, ValueContext context) {
if (!value.contains("://")) {
// assume it'is "file://" protocol, one that is used to access a
// file on a given path on the server, this will later be striped
@@ -63,7 +64,7 @@ public class DesignResourceConverter implements Converter<String, Resource> {
}

@Override
public String convertToPresentation(Resource value, Locale locale) {
public String convertToPresentation(Resource value, ValueContext context) {
ResourceConverterByProtocol byType = ResourceConverterByProtocol
.byType(value.getClass());
if (byType != null) {

+ 5
- 3
server/src/main/java/com/vaadin/ui/declarative/converters/DesignShortcutActionConverter.java View File

@@ -17,12 +17,12 @@ package com.vaadin.ui.declarative.converters;

import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;

import com.vaadin.data.Result;
import com.vaadin.data.util.converter.Converter;
import com.vaadin.data.util.converter.ValueContext;
import com.vaadin.event.ShortcutAction;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.event.ShortcutAction.ModifierKey;
@@ -121,7 +121,8 @@ public class DesignShortcutActionConverter
}

@Override
public Result<ShortcutAction> convertToModel(String value, Locale locale) {
public Result<ShortcutAction> convertToModel(String value,
ValueContext context) {
if (value.length() == 0) {
return Result.ok(null);
}
@@ -159,7 +160,8 @@ public class DesignShortcutActionConverter
}

@Override
public String convertToPresentation(ShortcutAction value, Locale locale) {
public String convertToPresentation(ShortcutAction value,
ValueContext context) {
StringBuilder sb = new StringBuilder();
// handle modifiers
if (value.getModifiers() != null) {

+ 3
- 3
server/src/main/java/com/vaadin/ui/declarative/converters/DesignTimeZoneConverter.java View File

@@ -15,11 +15,11 @@
*/
package com.vaadin.ui.declarative.converters;

import java.util.Locale;
import java.util.TimeZone;

import com.vaadin.data.Result;
import com.vaadin.data.util.converter.Converter;
import com.vaadin.data.util.converter.ValueContext;
import com.vaadin.ui.declarative.DesignAttributeHandler;

/**
@@ -32,7 +32,7 @@ import com.vaadin.ui.declarative.DesignAttributeHandler;
public class DesignTimeZoneConverter implements Converter<String, TimeZone> {

@Override
public Result<TimeZone> convertToModel(String value, Locale locale) {
public Result<TimeZone> convertToModel(String value, ValueContext context) {
if (value == null || value.isEmpty()) {
return Result.ok(null);
}
@@ -41,7 +41,7 @@ public class DesignTimeZoneConverter implements Converter<String, TimeZone> {
}

@Override
public String convertToPresentation(TimeZone value, Locale locale) {
public String convertToPresentation(TimeZone value, ValueContext context) {
if (value == null) {
return "";
} else {

+ 3
- 3
server/src/main/java/com/vaadin/ui/declarative/converters/DesignToStringConverter.java View File

@@ -16,10 +16,10 @@
package com.vaadin.ui.declarative.converters;

import java.lang.reflect.InvocationTargetException;
import java.util.Locale;

import com.vaadin.data.Result;
import com.vaadin.data.util.converter.Converter;
import com.vaadin.data.util.converter.ValueContext;
import com.vaadin.ui.declarative.DesignAttributeHandler;

/**
@@ -72,7 +72,7 @@ public class DesignToStringConverter<TYPE> implements Converter<String, TYPE> {
}

@Override
public Result<TYPE> convertToModel(String value, Locale locale) {
public Result<TYPE> convertToModel(String value, ValueContext context) {
try {
return Result.ok(type
.cast(type.getMethod(this.staticMethodName, String.class)
@@ -85,7 +85,7 @@ public class DesignToStringConverter<TYPE> implements Converter<String, TYPE> {
}

@Override
public String convertToPresentation(TYPE value, Locale locale) {
public String convertToPresentation(TYPE value, ValueContext context) {
if (value == null) {
return NULL_VALUE_REPRESENTATION;
} else {

+ 4
- 3
server/src/test/java/com/vaadin/data/BinderBookOfVaadinTest.java View File

@@ -17,7 +17,6 @@ package com.vaadin.data;

import java.time.LocalDate;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
@@ -31,6 +30,7 @@ import com.vaadin.data.Binder.Binding;
import com.vaadin.data.ValidationStatus.Status;
import com.vaadin.data.util.converter.Converter;
import com.vaadin.data.util.converter.StringToIntegerConverter;
import com.vaadin.data.util.converter.ValueContext;
import com.vaadin.data.validator.EmailValidator;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.server.AbstractErrorMessage;
@@ -537,7 +537,7 @@ public class BinderBookOfVaadinTest {
class MyConverter implements Converter<String, Integer> {
@Override
public Result<Integer> convertToModel(String fieldValue,
Locale locale) {
ValueContext context) {
// Produces a converted value or an error
try {
// ok is a static helper method that creates a Result
@@ -549,7 +549,8 @@ public class BinderBookOfVaadinTest {
}

@Override
public String convertToPresentation(Integer integer, Locale locale) {
public String convertToPresentation(Integer integer,
ValueContext context) {
// Converting to the field type should always succeed,
// so there is no support for returning an error Result.
return String.valueOf(integer);

+ 3
- 3
server/src/test/java/com/vaadin/data/BinderMultiSelectTest.java View File

@@ -20,7 +20,6 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.Collections;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
@@ -30,6 +29,7 @@ import org.junit.Before;
import org.junit.Test;

import com.vaadin.data.util.converter.Converter;
import com.vaadin.data.util.converter.ValueContext;
import com.vaadin.tests.data.bean.BeanWithEnums;
import com.vaadin.tests.data.bean.TestEnum;
import com.vaadin.ui.CheckBoxGroup;
@@ -40,14 +40,14 @@ public class BinderMultiSelectTest
implements Converter<Set<TestEnum>, String> {
@Override
public Result<String> convertToModel(Set<TestEnum> value,
Locale locale) {
ValueContext context) {
return Result.ok(value.stream().map(TestEnum::name)
.collect(Collectors.joining(",")));
}

@Override
public Set<TestEnum> convertToPresentation(String value,
Locale locale) {
ValueContext context) {
return Stream.of(value.split(","))
.filter(string -> !string.isEmpty()).map(TestEnum::valueOf)
.collect(Collectors.toSet());

+ 60
- 0
server/src/test/java/com/vaadin/data/ValueContextTest.java View File

@@ -0,0 +1,60 @@
package com.vaadin.data;

import java.util.Locale;
import java.util.Objects;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.vaadin.data.util.converter.ValueContext;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;

public class ValueContextTest extends UI {

private static final Locale UI_LOCALE = Locale.GERMAN;
private static final Locale COMPONENT_LOCALE = Locale.FRENCH;
private TextField textField;

@Test
public void locale_from_component() {
textField.setLocale(COMPONENT_LOCALE);
ValueContext fromComponent = new ValueContext(textField);
Locale locale = fromComponent.getLocale().orElse(null);
Objects.requireNonNull(locale);
Assert.assertEquals("Unexpected locale from component",
COMPONENT_LOCALE, locale);
}

@Test
public void locale_from_ui() {
ValueContext fromComponent = new ValueContext(textField);
Locale locale = fromComponent.getLocale().orElse(null);
Objects.requireNonNull(locale);
Assert.assertEquals("Unexpected locale from component", UI_LOCALE,
locale);
}

@Test
public void default_locale() {
setLocale(null);
ValueContext fromComponent = new ValueContext(textField);
Locale locale = fromComponent.getLocale().orElse(null);
Objects.requireNonNull(locale);
Assert.assertEquals("Unexpected locale from component",
Locale.getDefault(), locale);
}

@Before
public void setUp() {
setLocale(UI_LOCALE);
textField = new TextField();
setContent(textField);
}

@Override
public void init(VaadinRequest request) {
}
}

+ 3
- 1
server/src/test/java/com/vaadin/tests/data/converter/AbstractConverterTest.java View File

@@ -5,12 +5,14 @@ import org.junit.Test;

import com.vaadin.data.Result;
import com.vaadin.data.util.converter.Converter;
import com.vaadin.data.util.converter.ValueContext;

public abstract class AbstractConverterTest {

@Test
public void testNullConversion() {
assertValue(null, getConverter().convertToModel(null, null));
assertValue(null,
getConverter().convertToModel(null, new ValueContext()));
}

protected abstract Converter<?, ?> getConverter();

+ 5
- 2
server/src/test/java/com/vaadin/tests/data/converter/AbstractStringConverterTest.java View File

@@ -5,6 +5,7 @@ import org.junit.Test;

import com.vaadin.data.Result;
import com.vaadin.data.util.converter.Converter;
import com.vaadin.data.util.converter.ValueContext;

public abstract class AbstractStringConverterTest
extends AbstractConverterTest {
@@ -15,16 +16,18 @@ public abstract class AbstractStringConverterTest
@Test
public void testEmptyStringConversion() {
assertValue("Null value was converted incorrectly", null,
getConverter().convertToModel("", null));
getConverter().convertToModel("", new ValueContext()));
}

@Test
public void testErrorMessage() {
Result<?> result = getConverter().convertToModel("abc", null);
Result<?> result = getConverter().convertToModel("abc",
new ValueContext());
Assert.assertTrue(result.isError());
Assert.assertEquals(getErrorMessage(), result.getMessage().get());
}

@Override
protected String getErrorMessage() {
return "conversion failed";
}

+ 3
- 3
server/src/test/java/com/vaadin/tests/data/converter/ConverterTest.java View File

@@ -7,6 +7,7 @@ import org.junit.Test;

import com.vaadin.data.Result;
import com.vaadin.data.util.converter.Converter;
import com.vaadin.data.util.converter.ValueContext;

public class ConverterTest {

@@ -26,10 +27,9 @@ public class ConverterTest {
@Test
public void basicConversion() {
Assert.assertEquals("presentation-123",
converter.convertToPresentation("123", null));
converter.convertToPresentation("123", new ValueContext()));
Assert.assertEquals("123",
converter.convertToModel("presentation-123", null)
converter.convertToModel("presentation-123", new ValueContext())
.getOrThrow(msg -> new AssertionError(msg)));
}

}

+ 4
- 2
server/src/test/java/com/vaadin/tests/data/converter/DateToLongConverterTest.java View File

@@ -5,6 +5,7 @@ import java.util.Date;
import org.junit.Test;

import com.vaadin.data.util.converter.DateToLongConverter;
import com.vaadin.data.util.converter.ValueContext;

public class DateToLongConverterTest extends AbstractConverterTest {

@@ -16,7 +17,8 @@ public class DateToLongConverterTest extends AbstractConverterTest {
@Override
@Test
public void testNullConversion() {
assertValue(null, getConverter().convertToModel(null, null));
assertValue(null,
getConverter().convertToModel(null, new ValueContext()));
}

@Test
@@ -25,6 +27,6 @@ public class DateToLongConverterTest extends AbstractConverterTest {
assertValue(
Long.valueOf(946677600000l
+ (d.getTimezoneOffset() + 120) * 60 * 1000L),
getConverter().convertToModel(d, null));
getConverter().convertToModel(d, new ValueContext()));
}
}

+ 3
- 2
server/src/test/java/com/vaadin/tests/data/converter/DateToSqlDateConverterTest.java View File

@@ -6,6 +6,7 @@ import java.util.Locale;
import org.junit.Test;

import com.vaadin.data.util.converter.DateToSqlDateConverter;
import com.vaadin.data.util.converter.ValueContext;

public class DateToSqlDateConverterTest extends AbstractConverterTest {

@@ -18,7 +19,7 @@ public class DateToSqlDateConverterTest extends AbstractConverterTest {
public void testValueConversion() {
Date testDate = new Date(100, 0, 1);
long time = testDate.getTime();
assertValue(testDate, getConverter()
.convertToModel(new java.sql.Date(time), Locale.ENGLISH));
assertValue(testDate, getConverter().convertToModel(
new java.sql.Date(time), new ValueContext(Locale.ENGLISH)));
}
}

+ 3
- 2
server/src/test/java/com/vaadin/tests/data/converter/StringToBigDecimalConverterTest.java View File

@@ -23,6 +23,7 @@ import org.junit.Test;

import com.vaadin.data.Result;
import com.vaadin.data.util.converter.StringToBigDecimalConverter;
import com.vaadin.data.util.converter.ValueContext;

public class StringToBigDecimalConverterTest
extends AbstractStringConverterTest {
@@ -35,7 +36,7 @@ public class StringToBigDecimalConverterTest
@Test
public void testValueParsing() {
Result<BigDecimal> converted = getConverter().convertToModel("10",
null);
new ValueContext());
BigDecimal expected = new BigDecimal(10);
assertValue(expected, converted);
}
@@ -46,7 +47,7 @@ public class StringToBigDecimalConverterTest
String expected = "12,5";

String converted = getConverter().convertToPresentation(bd,
Locale.GERMAN);
new ValueContext(Locale.GERMAN));
Assert.assertEquals(expected, converted);
}
}

+ 3
- 2
server/src/test/java/com/vaadin/tests/data/converter/StringToBigIntegerConverterTest.java View File

@@ -23,6 +23,7 @@ import org.junit.Test;

import com.vaadin.data.Result;
import com.vaadin.data.util.converter.StringToBigIntegerConverter;
import com.vaadin.data.util.converter.ValueContext;

public class StringToBigIntegerConverterTest
extends AbstractStringConverterTest {
@@ -36,7 +37,7 @@ public class StringToBigIntegerConverterTest
public void testValueParsing() {
String bigInt = "1180591620717411303424"; // 2^70 > 2^63 - 1
Result<BigInteger> converted = getConverter().convertToModel(bigInt,
null);
new ValueContext());
BigInteger expected = new BigInteger(bigInt);
assertValue("Value bigger than max long was converted incorrectly",
expected, converted);
@@ -48,7 +49,7 @@ public class StringToBigIntegerConverterTest
String expected = "1.000";

String converted = getConverter().convertToPresentation(bd,
Locale.GERMAN);
new ValueContext(Locale.GERMAN));
Assert.assertEquals(
"Value with specific locale was converted incorrectly",
expected, converted);

+ 28
- 20
server/src/test/java/com/vaadin/tests/data/converter/StringToBooleanConverterTest.java View File

@@ -8,6 +8,7 @@ import org.junit.Assert;
import org.junit.Test;

import com.vaadin.data.util.converter.StringToBooleanConverter;
import com.vaadin.data.util.converter.ValueContext;

public class StringToBooleanConverterTest extends AbstractStringConverterTest {

@@ -43,42 +44,49 @@ public class StringToBooleanConverterTest extends AbstractStringConverterTest {

@Test
public void testValueConversion() {
assertValue(true, getConverter().convertToModel("true", null));
assertValue(false, getConverter().convertToModel("false", null));
assertValue(true,
getConverter().convertToModel("true", new ValueContext()));
assertValue(false,
getConverter().convertToModel("false", new ValueContext()));
}

@Test
public void testYesNoValueConversion() {
assertValue(true, yesNoConverter.convertToModel("yes", null));
assertValue(false, yesNoConverter.convertToModel("no", null));
assertValue(true,
yesNoConverter.convertToModel("yes", new ValueContext()));
assertValue(false,
yesNoConverter.convertToModel("no", new ValueContext()));

Assert.assertEquals("yes",
yesNoConverter.convertToPresentation(true, null));
Assert.assertEquals("no",
yesNoConverter.convertToPresentation(false, null));
yesNoConverter.convertToPresentation(true, new ValueContext()));
Assert.assertEquals("no", yesNoConverter.convertToPresentation(false,
new ValueContext()));
}

@Test
public void testEmptyTrueValueConversion() {
assertValue(true, emptyTrueConverter.convertToModel("", null));
assertValue(false, emptyTrueConverter.convertToModel("ABSENT", null));
assertValue(true,
emptyTrueConverter.convertToModel("", new ValueContext()));
assertValue(false, emptyTrueConverter.convertToModel("ABSENT",
new ValueContext()));

Assert.assertEquals("",
emptyTrueConverter.convertToPresentation(true, null));
Assert.assertEquals("ABSENT",
emptyTrueConverter.convertToPresentation(false, null));
Assert.assertEquals("", emptyTrueConverter.convertToPresentation(true,
new ValueContext()));
Assert.assertEquals("ABSENT", emptyTrueConverter
.convertToPresentation(false, new ValueContext()));
}

@Test
public void testLocale() {
Assert.assertEquals("May 18, 2033",
localeConverter.convertToPresentation(true, Locale.US));
Assert.assertEquals("January 24, 2065",
localeConverter.convertToPresentation(false, Locale.US));
Assert.assertEquals("May 18, 2033", localeConverter
.convertToPresentation(true, new ValueContext(Locale.US)));
Assert.assertEquals("January 24, 2065", localeConverter
.convertToPresentation(false, new ValueContext(Locale.US)));

Assert.assertEquals("18. Mai 2033",
localeConverter.convertToPresentation(true, Locale.GERMANY));
Assert.assertEquals("18. Mai 2033", localeConverter
.convertToPresentation(true, new ValueContext(Locale.GERMANY)));
Assert.assertEquals("24. Januar 2065",
localeConverter.convertToPresentation(false, Locale.GERMANY));
localeConverter.convertToPresentation(false,
new ValueContext(Locale.GERMANY)));
}
}

+ 5
- 3
server/src/test/java/com/vaadin/tests/data/converter/StringToDateConverterTest.java View File

@@ -6,6 +6,7 @@ import java.util.Locale;
import org.junit.Test;

import com.vaadin.data.util.converter.StringToDateConverter;
import com.vaadin.data.util.converter.ValueContext;

public class StringToDateConverterTest extends AbstractConverterTest {

@@ -16,12 +17,13 @@ public class StringToDateConverterTest extends AbstractConverterTest {

@Test
public void testEmptyStringConversion() {
assertValue(null, getConverter().convertToModel("", null));
assertValue(null,
getConverter().convertToModel("", new ValueContext()));
}

@Test
public void testValueConversion() {
assertValue(new Date(100, 0, 1), getConverter()
.convertToModel("Jan 1, 2000 12:00:00 AM", Locale.ENGLISH));
assertValue(new Date(100, 0, 1), getConverter().convertToModel(
"Jan 1, 2000 12:00:00 AM", new ValueContext(Locale.ENGLISH)));
}
}

+ 7
- 3
server/src/test/java/com/vaadin/tests/data/converter/StringToDoubleConverterTest.java View File

@@ -5,6 +5,7 @@ import org.junit.Test;

import com.vaadin.data.Result;
import com.vaadin.data.util.converter.StringToDoubleConverter;
import com.vaadin.data.util.converter.ValueContext;

public class StringToDoubleConverterTest extends AbstractConverterTest {

@@ -15,18 +16,21 @@ public class StringToDoubleConverterTest extends AbstractConverterTest {

@Test
public void testEmptyStringConversion() {
assertValue(null, getConverter().convertToModel("", null));
assertValue(null,
getConverter().convertToModel("", new ValueContext()));
}

@Test
public void testValueConversion() {
Result<Double> value = getConverter().convertToModel("10", null);
Result<Double> value = getConverter().convertToModel("10",
new ValueContext());
assertValue(10.0d, value);
}

@Test
public void testErrorMessage() {
Result<Double> result = getConverter().convertToModel("abc", null);
Result<Double> result = getConverter().convertToModel("abc",
new ValueContext());
Assert.assertTrue(result.isError());
Assert.assertEquals("Failed", result.getMessage().get());
}

+ 6
- 3
server/src/test/java/com/vaadin/tests/data/converter/StringToFloatConverterTest.java View File

@@ -3,6 +3,7 @@ package com.vaadin.tests.data.converter;
import org.junit.Test;

import com.vaadin.data.util.converter.StringToFloatConverter;
import com.vaadin.data.util.converter.ValueContext;

public class StringToFloatConverterTest extends AbstractStringConverterTest {

@@ -14,18 +15,20 @@ public class StringToFloatConverterTest extends AbstractStringConverterTest {
@Override
@Test
public void testNullConversion() {
assertValue(null, getConverter().convertToModel(null, null));
assertValue(null,
getConverter().convertToModel(null, new ValueContext()));
}

@Override
@Test
public void testEmptyStringConversion() {
assertValue(null, getConverter().convertToModel("", null));
assertValue(null,
getConverter().convertToModel("", new ValueContext()));
}

@Test
public void testValueConversion() {
assertValue(Float.valueOf(10),
getConverter().convertToModel("10", null));
getConverter().convertToModel("10", new ValueContext()));
}
}

+ 7
- 4
server/src/test/java/com/vaadin/tests/data/converter/StringToIntegerConverterTest.java View File

@@ -5,6 +5,7 @@ import org.junit.Test;

import com.vaadin.data.Result;
import com.vaadin.data.util.converter.StringToIntegerConverter;
import com.vaadin.data.util.converter.ValueContext;

public class StringToIntegerConverterTest extends AbstractConverterTest {

@@ -15,7 +16,8 @@ public class StringToIntegerConverterTest extends AbstractConverterTest {

@Test
public void testEmptyStringConversion() {
assertValue(null, getConverter().convertToModel("", null));
assertValue(null,
getConverter().convertToModel("", new ValueContext()));
}

@Test
@@ -28,7 +30,7 @@ public class StringToIntegerConverterTest extends AbstractConverterTest {
for (Number value : values) {
try {
getConverter().convertToModel(String.format("%.0f", value),
null);
new ValueContext());
} catch (Exception e) {
accepted = true;
}
@@ -39,12 +41,13 @@ public class StringToIntegerConverterTest extends AbstractConverterTest {
@Test
public void testValueConversion() {
assertValue(Integer.valueOf(10),
getConverter().convertToModel("10", null));
getConverter().convertToModel("10", new ValueContext()));
}

@Test
public void testErrorMessage() {
Result<Integer> result = getConverter().convertToModel("abc", null);
Result<Integer> result = getConverter().convertToModel("abc",
new ValueContext());
Assert.assertTrue(result.isError());
Assert.assertEquals("Failed", result.getMessage().get());
}

+ 10
- 6
server/src/test/java/com/vaadin/tests/data/converter/StringToLongConverterTest.java View File

@@ -4,6 +4,7 @@ import org.junit.Test;

import com.vaadin.data.Result;
import com.vaadin.data.util.converter.StringToLongConverter;
import com.vaadin.data.util.converter.ValueContext;

public class StringToLongConverterTest extends AbstractStringConverterTest {

@@ -15,21 +16,23 @@ public class StringToLongConverterTest extends AbstractStringConverterTest {
@Override
@Test
public void testEmptyStringConversion() {
assertValue(null, getConverter().convertToModel("", null));
assertValue(null,
getConverter().convertToModel("", new ValueContext()));
}

@Test
public void testValueConversion() {
assertValue(Long.valueOf(10),
getConverter().convertToModel("10", null));
getConverter().convertToModel("10", new ValueContext()));
}

@Test
public void testExtremeLongValueConversion() {
Result<Long> l = getConverter().convertToModel("9223372036854775807",
null);
new ValueContext());
assertValue(Long.MAX_VALUE, l);
l = getConverter().convertToModel("-9223372036854775808", null);
l = getConverter().convertToModel("-9223372036854775808",
new ValueContext());
assertValue(Long.MIN_VALUE, l);
}

@@ -37,10 +40,11 @@ public class StringToLongConverterTest extends AbstractStringConverterTest {
public void testOutOfBoundsValueConversion() {
// Long.MAX_VALUE+1 is converted to Long.MAX_VALUE
Result<Long> l = getConverter().convertToModel("9223372036854775808",
null);
new ValueContext());
assertValue(Long.MAX_VALUE, l);
// Long.MIN_VALUE-1 is converted to Long.MIN_VALUE
l = getConverter().convertToModel("-9223372036854775809", null);
l = getConverter().convertToModel("-9223372036854775809",
new ValueContext());
assertValue(Long.MIN_VALUE, l);

}

Loading…
Cancel
Save