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;
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();
}
}
private FIELDVALUE convertDataToFieldType(BEAN bean) {
- return converterValidatorChain
- .convertToPresentation(getter.apply(bean), findLocale());
+ return converterValidatorChain.convertToPresentation(
+ getter.apply(bean), createValueContext());
}
/**
}
@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());
}
@Override
- public T convertToPresentation(T value, Locale locale) {
+ public T convertToPresentation(T value, ValueContext context) {
return value;
}
}
@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);
}
}
*
* @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.
*
* @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.
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);
}
};
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);
}
};
}
package com.vaadin.data.util.converter;
import java.util.Date;
-import java.util.Locale;
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);
}
}
@Override
- public Date convertToPresentation(Long value, Locale locale) {
+ public Date convertToPresentation(Long value, ValueContext context) {
if (value == null) {
return null;
}
package com.vaadin.data.util.converter;
import java.util.Date;
-import java.util.Locale;
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);
}
}
@Override
- public Date convertToPresentation(java.sql.Date value, Locale locale) {
+ public Date convertToPresentation(java.sql.Date value,
+ ValueContext context) {
if (value == null) {
return null;
}
}
@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);
}
}
@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();
+ }
+ });
}
}
}
@Override
- public Result<Boolean> convertToModel(String value, Locale locale) {
+ public Result<Boolean> convertToModel(String value, ValueContext context) {
if (value == null) {
return Result.ok(null);
}
// 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)) {
}
@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 {
}
@Override
- public Result<Date> convertToModel(String value, Locale locale) {
+ public Result<Date> convertToModel(String value, ValueContext context) {
if (value == null) {
return Result.ok(null);
}
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);
}
}
@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);
}
}
}
@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) {
}
@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) {
}
@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);
}
@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;
--- /dev/null
+/*
+ * 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);
+ }
+}
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;
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();
}
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;
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 {
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;
}
@Override
public Result<Character> convertToModel(String value,
- Locale locale) {
+ ValueContext context) {
return Result.ok(value.charAt(0));
}
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;
} else {
Converter<String, Object> converter = findConverterFor(
object.getClass());
- return converter.convertToPresentation(object, null);
+ return converter.convertToPresentation(object, new ValueContext());
}
}
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;
/**
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" }) {
}
@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);
}
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;
/**
@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);
}
}
@Override
- public String convertToPresentation(T value, Locale locale) {
+ public String convertToPresentation(T value, ValueContext context) {
if (value == null) {
return null;
}
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;
/**
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);
}
@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);
}
}
*/
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;
/**
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;
}
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;
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
}
@Override
- public String convertToPresentation(Resource value, Locale locale) {
+ public String convertToPresentation(Resource value, ValueContext context) {
ResourceConverterByProtocol byType = ResourceConverterByProtocol
.byType(value.getClass());
if (byType != null) {
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;
}
@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);
}
}
@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) {
*/
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;
/**
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);
}
}
@Override
- public String convertToPresentation(TimeZone value, Locale locale) {
+ public String convertToPresentation(TimeZone value, ValueContext context) {
if (value == null) {
return "";
} else {
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;
/**
}
@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)
}
@Override
- public String convertToPresentation(TYPE value, Locale locale) {
+ public String convertToPresentation(TYPE value, ValueContext context) {
if (value == null) {
return NULL_VALUE_REPRESENTATION;
} else {
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;
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;
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
}
@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);
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;
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;
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());
--- /dev/null
+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) {
+ }
+}
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();
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 {
@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";
}
import com.vaadin.data.Result;
import com.vaadin.data.util.converter.Converter;
+import com.vaadin.data.util.converter.ValueContext;
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)));
}
-
}
import org.junit.Test;
import com.vaadin.data.util.converter.DateToLongConverter;
+import com.vaadin.data.util.converter.ValueContext;
public class DateToLongConverterTest extends AbstractConverterTest {
@Override
@Test
public void testNullConversion() {
- assertValue(null, getConverter().convertToModel(null, null));
+ assertValue(null,
+ getConverter().convertToModel(null, new ValueContext()));
}
@Test
assertValue(
Long.valueOf(946677600000l
+ (d.getTimezoneOffset() + 120) * 60 * 1000L),
- getConverter().convertToModel(d, null));
+ getConverter().convertToModel(d, new ValueContext()));
}
}
import org.junit.Test;
import com.vaadin.data.util.converter.DateToSqlDateConverter;
+import com.vaadin.data.util.converter.ValueContext;
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)));
}
}
import com.vaadin.data.Result;
import com.vaadin.data.util.converter.StringToBigDecimalConverter;
+import com.vaadin.data.util.converter.ValueContext;
public class StringToBigDecimalConverterTest
extends AbstractStringConverterTest {
@Test
public void testValueParsing() {
Result<BigDecimal> converted = getConverter().convertToModel("10",
- null);
+ new ValueContext());
BigDecimal expected = new BigDecimal(10);
assertValue(expected, converted);
}
String expected = "12,5";
String converted = getConverter().convertToPresentation(bd,
- Locale.GERMAN);
+ new ValueContext(Locale.GERMAN));
Assert.assertEquals(expected, converted);
}
}
import com.vaadin.data.Result;
import com.vaadin.data.util.converter.StringToBigIntegerConverter;
+import com.vaadin.data.util.converter.ValueContext;
public class StringToBigIntegerConverterTest
extends AbstractStringConverterTest {
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);
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);
import org.junit.Test;
import com.vaadin.data.util.converter.StringToBooleanConverter;
+import com.vaadin.data.util.converter.ValueContext;
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)));
}
}
import org.junit.Test;
import com.vaadin.data.util.converter.StringToDateConverter;
+import com.vaadin.data.util.converter.ValueContext;
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)));
}
}
import com.vaadin.data.Result;
import com.vaadin.data.util.converter.StringToDoubleConverter;
+import com.vaadin.data.util.converter.ValueContext;
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());
}
import org.junit.Test;
import com.vaadin.data.util.converter.StringToFloatConverter;
+import com.vaadin.data.util.converter.ValueContext;
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()));
}
}
import com.vaadin.data.Result;
import com.vaadin.data.util.converter.StringToIntegerConverter;
+import com.vaadin.data.util.converter.ValueContext;
public class StringToIntegerConverterTest extends AbstractConverterTest {
@Test
public void testEmptyStringConversion() {
- assertValue(null, getConverter().convertToModel("", null));
+ assertValue(null,
+ getConverter().convertToModel("", new ValueContext()));
}
@Test
for (Number value : values) {
try {
getConverter().convertToModel(String.format("%.0f", value),
- null);
+ new ValueContext());
} catch (Exception e) {
accepted = true;
}
@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());
}
import com.vaadin.data.Result;
import com.vaadin.data.util.converter.StringToLongConverter;
+import com.vaadin.data.util.converter.ValueContext;
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);
}
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);
}