From: Teemu Suo-Anttila Date: Thu, 20 Oct 2016 08:36:45 +0000 (+0300) Subject: Add ValueContext parameter for Converters X-Git-Tag: 8.0.0.alpha6~53 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c72beed9267bcf7d2693fbab87c31904319b1e84;p=vaadin-framework.git Add ValueContext parameter for Converters Change-Id: I47179b06b9e345f5a454ac1806d0bc9bcac24bcf --- diff --git a/server/src/main/java/com/vaadin/data/Binder.java b/server/src/main/java/com/vaadin/data/Binder.java index 8356a03dda..5493712cec 100644 --- a/server/src/main/java/com/vaadin/data/Binder.java +++ b/server/src/main/java/com/vaadin/data/Binder.java @@ -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 implements Serializable { private ValidationStatus doValidation() { FIELDVALUE fieldValue = field.getValue(); Result 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 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 implements Serializable { } @Override - public Result convertToModel(T value, Locale locale) { + public Result convertToModel(T value, ValueContext context) { Result validationResult = validator.apply(value); if (validationResult.isError()) { return Result.error(validationResult.getMessage().get()); @@ -671,7 +685,7 @@ public class Binder implements Serializable { } @Override - public T convertToPresentation(T value, Locale locale) { + public T convertToPresentation(T value, ValueContext context) { return value; } 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 c06a9baa61..e7f4476f66 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 @@ -109,12 +109,12 @@ public abstract class AbstractStringToNumberConverter } @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); } } diff --git a/server/src/main/java/com/vaadin/data/util/converter/Converter.java b/server/src/main/java/com/vaadin/data/util/converter/Converter.java index 97985af9ca..c25c567773 100644 --- a/server/src/main/java/com/vaadin/data/util/converter/Converter.java +++ b/server/src/main/java/com/vaadin/data/util/converter/Converter.java @@ -45,11 +45,17 @@ public interface Converter 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 convertToModel(PRESENTATION value, Locale locale); + public Result convertToModel(PRESENTATION value, + ValueContext context); + + default public Result 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 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 extends Serializable { return new Converter() { @Override - public Result convertToModel(P value, Locale locale) { + public Result 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 extends Serializable { Converter other) { return new Converter() { @Override - public Result convertToModel(PRESENTATION value, Locale locale) { + public Result convertToModel(PRESENTATION value, + ValueContext context) { Result 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); } }; } diff --git a/server/src/main/java/com/vaadin/data/util/converter/DateToLongConverter.java b/server/src/main/java/com/vaadin/data/util/converter/DateToLongConverter.java index 2fff4915ee..bd51181f5e 100644 --- a/server/src/main/java/com/vaadin/data/util/converter/DateToLongConverter.java +++ b/server/src/main/java/com/vaadin/data/util/converter/DateToLongConverter.java @@ -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 { @Override - public Result convertToModel(Date value, Locale locale) { + public Result convertToModel(Date value, ValueContext context) { if (value == null) { return Result.ok(null); } @@ -39,7 +38,7 @@ public class DateToLongConverter implements Converter { } @Override - public Date convertToPresentation(Long value, Locale locale) { + public Date convertToPresentation(Long value, ValueContext context) { if (value == null) { return null; } diff --git a/server/src/main/java/com/vaadin/data/util/converter/DateToSqlDateConverter.java b/server/src/main/java/com/vaadin/data/util/converter/DateToSqlDateConverter.java index 5695e4bf9d..1fb7584e90 100644 --- a/server/src/main/java/com/vaadin/data/util/converter/DateToSqlDateConverter.java +++ b/server/src/main/java/com/vaadin/data/util/converter/DateToSqlDateConverter.java @@ -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 { @Override - public Result convertToModel(Date value, Locale locale) { + public Result convertToModel(Date value, + ValueContext context) { if (value == null) { return Result.ok(null); } @@ -46,7 +46,8 @@ public class DateToSqlDateConverter implements Converter { } @Override - public Date convertToPresentation(java.sql.Date value, Locale locale) { + public Date convertToPresentation(java.sql.Date value, + ValueContext context) { if (value == null) { return null; } 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 af0a63fc7a..d30540d120 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 @@ -60,8 +60,9 @@ public class StringToBigDecimalConverter } @Override - public Result convertToModel(String value, Locale locale) { - return convertToNumber(value, locale) + public Result convertToModel(String value, + ValueContext context) { + return convertToNumber(value, context.getLocale().orElse(null)) .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 d469db7055..186beb9d4b 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 @@ -60,14 +60,16 @@ public class StringToBigIntegerConverter } @Override - public Result convertToModel(String value, Locale locale) { - return convertToNumber(value, locale).map(number -> { - if (number == null) { - return null; - } else { - return ((BigDecimal) number).toBigInteger(); - } - }); + public Result convertToModel(String value, + ValueContext context) { + return convertToNumber(value, context.getLocale().orElse(null)) + .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 7b9e1850f0..38b27f8b50 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 @@ -72,7 +72,7 @@ public class StringToBooleanConverter implements Converter { } @Override - public Result convertToModel(String value, Locale locale) { + public Result convertToModel(String value, ValueContext context) { if (value == null) { return Result.ok(null); } @@ -80,6 +80,7 @@ public class StringToBooleanConverter implements Converter { // 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 { } @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 { diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToDateConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToDateConverter.java index 2e3e8ab04a..d8a036c002 100644 --- a/server/src/main/java/com/vaadin/data/util/converter/StringToDateConverter.java +++ b/server/src/main/java/com/vaadin/data/util/converter/StringToDateConverter.java @@ -58,7 +58,7 @@ public class StringToDateConverter implements Converter { } @Override - public Result convertToModel(String value, Locale locale) { + public Result convertToModel(String value, ValueContext context) { if (value == null) { return Result.ok(null); } @@ -67,7 +67,8 @@ public class StringToDateConverter implements Converter { 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 { } @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); } } 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 463a7f6b36..52bf9509c9 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 @@ -49,8 +49,9 @@ public class StringToDoubleConverter } @Override - public Result convertToModel(String value, Locale locale) { - Result n = convertToNumber(value, locale); + public Result convertToModel(String value, ValueContext context) { + Result n = convertToNumber(value, + context.getLocale().orElse(null)); return n.map(number -> { if (number == null) { 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 be592665cf..aeeeffcb8b 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 @@ -47,8 +47,9 @@ public class StringToFloatConverter } @Override - public Result convertToModel(String value, Locale locale) { - Result n = convertToNumber(value, locale); + public Result convertToModel(String value, ValueContext context) { + Result n = convertToNumber(value, + context.getLocale().orElse(null)); return n.map(number -> { if (number == null) { 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 ec05647a51..1c98e54c6e 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 @@ -63,8 +63,9 @@ public class StringToIntegerConverter } @Override - public Result convertToModel(String value, Locale locale) { - Result n = convertToNumber(value, locale); + public Result convertToModel(String value, ValueContext context) { + Result n = convertToNumber(value, + context.getLocale().orElse(null)); return n.flatMap(number -> { if (number == null) { return Result.ok(null); 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 9b7bfa4fc6..444e7ede8e 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 @@ -62,8 +62,9 @@ public class StringToLongConverter } @Override - public Result convertToModel(String value, Locale locale) { - Result n = convertToNumber(value, locale); + public Result convertToModel(String value, ValueContext context) { + Result n = convertToNumber(value, + context.getLocale().orElse(null)); return n.map(number -> { if (number == null) { return null; diff --git a/server/src/main/java/com/vaadin/data/util/converter/ValueContext.java b/server/src/main/java/com/vaadin/data/util/converter/ValueContext.java new file mode 100644 index 0000000000..a8fdf296f5 --- /dev/null +++ b/server/src/main/java/com/vaadin/data/util/converter/ValueContext.java @@ -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 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 getLocale() { + return Optional.ofNullable(locale); + } +} diff --git a/server/src/main/java/com/vaadin/ui/declarative/DesignAttributeHandler.java b/server/src/main/java/com/vaadin/ui/declarative/DesignAttributeHandler.java index 4290620051..4d655e0cbf 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/DesignAttributeHandler.java +++ b/server/src/main/java/com/vaadin/ui/declarative/DesignAttributeHandler.java @@ -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 converter = (Converter) getFormatter() .findConverterFor(sourceType); if (converter != null) { - return converter.convertToPresentation(value, null); + return converter.convertToPresentation(value, new ValueContext()); } else { return value.toString(); } 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 64430470d4..78c3886dd6 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/DesignFormatter.java +++ b/server/src/main/java/com/vaadin/ui/declarative/DesignFormatter.java @@ -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 booleanConverter = new Converter() { @Override - public Result convertToModel(String value, Locale locale) { + public Result 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() { @Override - public Result convertToModel(String value, Locale locale) { + public Result 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 convertToModel(String value, - Locale locale) { + ValueContext context) { return Result.ok(value.charAt(0)); } @@ -226,7 +231,8 @@ public class DesignFormatter implements Serializable { public T parse(String value, Class type) { Converter converter = findConverterFor(type); if (converter != null) { - Result result = converter.convertToModel(value, null); + Result 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 converter = findConverterFor( object.getClass()); - return converter.convertToPresentation(object, null); + return converter.convertToPresentation(object, new ValueContext()); } } diff --git a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignDateConverter.java b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignDateConverter.java index 9f97ea6ac9..9ccdea4ba3 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignDateConverter.java +++ b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignDateConverter.java @@ -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 { @Override - public Result convertToModel(String value, Locale locale) { + public Result 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 { } @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); } diff --git a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignEnumConverter.java b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignEnumConverter.java index 5a2c3f340c..d5b3c9a9cd 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignEnumConverter.java +++ b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignEnumConverter.java @@ -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 @SuppressWarnings("unchecked") @Override - public Result convertToModel(String value, Locale locale) { + public Result convertToModel(String value, ValueContext context) { if (value == null || value.trim().equals("")) { return Result.ok(null); } @@ -60,7 +61,7 @@ public class DesignEnumConverter } @Override - public String convertToPresentation(T value, Locale locale) { + public String convertToPresentation(T value, ValueContext context) { if (value == null) { return null; } diff --git a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignLocalDateConverter.java b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignLocalDateConverter.java index 188516b431..59301ef386 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignLocalDateConverter.java +++ b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignLocalDateConverter.java @@ -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 { @Override - public Result convertToModel(String value, Locale locale) { + public Result 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 { } @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); } } diff --git a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignObjectConverter.java b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignObjectConverter.java index d8fcfbad71..cf1c1ef69a 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignObjectConverter.java +++ b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignObjectConverter.java @@ -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 { @Override - public Result convertToModel(String value, Locale locale) { + public Result 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; } diff --git a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignResourceConverter.java b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignResourceConverter.java index 1efc382fcc..ef6e10ef5c 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignResourceConverter.java +++ b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignResourceConverter.java @@ -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 { @Override - public Result convertToModel(String value, Locale locale) { + public Result 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 { } @Override - public String convertToPresentation(Resource value, Locale locale) { + public String convertToPresentation(Resource value, ValueContext context) { ResourceConverterByProtocol byType = ResourceConverterByProtocol .byType(value.getClass()); if (byType != null) { diff --git a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignShortcutActionConverter.java b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignShortcutActionConverter.java index 2468c29c8c..4856a0db8f 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignShortcutActionConverter.java +++ b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignShortcutActionConverter.java @@ -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 convertToModel(String value, Locale locale) { + public Result 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) { diff --git a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignTimeZoneConverter.java b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignTimeZoneConverter.java index d0b2622015..3fe034e5fd 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignTimeZoneConverter.java +++ b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignTimeZoneConverter.java @@ -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 { @Override - public Result convertToModel(String value, Locale locale) { + public Result convertToModel(String value, ValueContext context) { if (value == null || value.isEmpty()) { return Result.ok(null); } @@ -41,7 +41,7 @@ public class DesignTimeZoneConverter implements Converter { } @Override - public String convertToPresentation(TimeZone value, Locale locale) { + public String convertToPresentation(TimeZone value, ValueContext context) { if (value == null) { return ""; } else { diff --git a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignToStringConverter.java b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignToStringConverter.java index d1564e0b8f..fa81b36bb7 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/converters/DesignToStringConverter.java +++ b/server/src/main/java/com/vaadin/ui/declarative/converters/DesignToStringConverter.java @@ -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 implements Converter { } @Override - public Result convertToModel(String value, Locale locale) { + public Result convertToModel(String value, ValueContext context) { try { return Result.ok(type .cast(type.getMethod(this.staticMethodName, String.class) @@ -85,7 +85,7 @@ public class DesignToStringConverter implements Converter { } @Override - public String convertToPresentation(TYPE value, Locale locale) { + public String convertToPresentation(TYPE value, ValueContext context) { if (value == null) { return NULL_VALUE_REPRESENTATION; } else { diff --git a/server/src/test/java/com/vaadin/data/BinderBookOfVaadinTest.java b/server/src/test/java/com/vaadin/data/BinderBookOfVaadinTest.java index f80f0781c2..d9750b989a 100644 --- a/server/src/test/java/com/vaadin/data/BinderBookOfVaadinTest.java +++ b/server/src/test/java/com/vaadin/data/BinderBookOfVaadinTest.java @@ -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 { @Override public Result 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); diff --git a/server/src/test/java/com/vaadin/data/BinderMultiSelectTest.java b/server/src/test/java/com/vaadin/data/BinderMultiSelectTest.java index 09e9b7e8ce..318cb4e3d9 100644 --- a/server/src/test/java/com/vaadin/data/BinderMultiSelectTest.java +++ b/server/src/test/java/com/vaadin/data/BinderMultiSelectTest.java @@ -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, String> { @Override public Result convertToModel(Set value, - Locale locale) { + ValueContext context) { return Result.ok(value.stream().map(TestEnum::name) .collect(Collectors.joining(","))); } @Override public Set convertToPresentation(String value, - Locale locale) { + ValueContext context) { return Stream.of(value.split(",")) .filter(string -> !string.isEmpty()).map(TestEnum::valueOf) .collect(Collectors.toSet()); diff --git a/server/src/test/java/com/vaadin/data/ValueContextTest.java b/server/src/test/java/com/vaadin/data/ValueContextTest.java new file mode 100644 index 0000000000..9fce2996d9 --- /dev/null +++ b/server/src/test/java/com/vaadin/data/ValueContextTest.java @@ -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) { + } +} 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 bc9a96b311..6b25f0a4e7 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 @@ -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(); 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 index bf2653da88..b4cffaf325 100644 --- a/server/src/test/java/com/vaadin/tests/data/converter/AbstractStringConverterTest.java +++ b/server/src/test/java/com/vaadin/tests/data/converter/AbstractStringConverterTest.java @@ -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"; } diff --git a/server/src/test/java/com/vaadin/tests/data/converter/ConverterTest.java b/server/src/test/java/com/vaadin/tests/data/converter/ConverterTest.java index a3d52b0520..95f200bdf6 100644 --- a/server/src/test/java/com/vaadin/tests/data/converter/ConverterTest.java +++ b/server/src/test/java/com/vaadin/tests/data/converter/ConverterTest.java @@ -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))); } - } 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 d0eb336616..75fc42dbb7 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 @@ -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())); } } 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 6264b81da1..c1ecf4657b 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 @@ -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))); } } 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 4481da2ead..2178cc561e 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 @@ -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 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); } } 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 56ed799bbc..3e7afd4f55 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 @@ -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 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); 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 5ca24f64aa..4de05b9f09 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 @@ -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))); } } 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 0fe2088847..5319813c00 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 @@ -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))); } } 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 28a5a7f73f..ba807e5782 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 @@ -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 value = getConverter().convertToModel("10", null); + Result value = getConverter().convertToModel("10", + new ValueContext()); assertValue(10.0d, value); } @Test public void testErrorMessage() { - Result result = getConverter().convertToModel("abc", null); + Result result = getConverter().convertToModel("abc", + new ValueContext()); 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 20b51deb54..8bdf651ede 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 @@ -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())); } } 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 a215c2c785..62fe699395 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 @@ -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 result = getConverter().convertToModel("abc", null); + Result result = getConverter().convertToModel("abc", + new ValueContext()); 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 818e0f2ce7..0acd463a26 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 @@ -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 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 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); }