diff options
author | Artur Signell <artur@vaadin.com> | 2016-08-23 23:06:05 +0300 |
---|---|---|
committer | Artur Signell <artur@vaadin.com> | 2016-08-24 22:12:42 +0300 |
commit | 38b825cf892b0877faa4c4d3b9f2ad7d7b878bd0 (patch) | |
tree | 2cae495f2db79650714c578781184da4fae0531d | |
parent | 08450203e750bfd4b7a6b2e55842f185aef2b8e5 (diff) | |
download | vaadin-framework-38b825cf892b0877faa4c4d3b9f2ad7d7b878bd0.tar.gz vaadin-framework-38b825cf892b0877faa4c4d3b9f2ad7d7b878bd0.zip |
Convert Vaadin 7 converters to Vaadin 8 converters
Change-Id: I347e6cd195b2d77ab2399667a69e4c23b0b54eb7
44 files changed, 1287 insertions, 188 deletions
diff --git a/compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/textfield/TextFieldWithConverterAndValidatorTest.java b/compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/textfield/TextFieldWithConverterAndValidatorTest.java index 9c5020490d..2116b7395d 100644 --- a/compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/textfield/TextFieldWithConverterAndValidatorTest.java +++ b/compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/textfield/TextFieldWithConverterAndValidatorTest.java @@ -3,9 +3,9 @@ package com.vaadin.v7.tests.server.component.textfield; import org.junit.Before; import org.junit.Test; -import com.vaadin.tests.data.converter.ConverterFactoryTest.ConvertTo42; import com.vaadin.v7.data.util.ObjectProperty; import com.vaadin.v7.data.validator.RangeValidator; +import com.vaadin.v7.tests.data.converter.ConverterFactoryTest.ConvertTo42; import com.vaadin.v7.ui.TextField; public class TextFieldWithConverterAndValidatorTest { diff --git a/server/src/main/java/com/vaadin/data/Result.java b/server/src/main/java/com/vaadin/data/Result.java index 07179851e4..7fb25bf9af 100644 --- a/server/src/main/java/com/vaadin/data/Result.java +++ b/server/src/main/java/com/vaadin/data/Result.java @@ -169,4 +169,19 @@ public interface Result<R> extends Serializable { */ public Optional<String> getMessage(); + /** + * Return the value, if the result denotes success, otherwise throw an + * exception to be created by the provided supplier. + * + * @param <X> + * Type of the exception to be thrown + * @param exceptionProvider + * The provider which will return the exception to be thrown + * based on the given error message + * @return the value + * @throws X + * if this result denotes an error + */ + public <X extends Throwable> R getOrThrow( + Function<String, ? extends X> exceptionProvider) throws X; } diff --git a/server/src/main/java/com/vaadin/data/SimpleResult.java b/server/src/main/java/com/vaadin/data/SimpleResult.java index 56c786942d..935fb545e3 100644 --- a/server/src/main/java/com/vaadin/data/SimpleResult.java +++ b/server/src/main/java/com/vaadin/data/SimpleResult.java @@ -94,4 +94,16 @@ class SimpleResult<R> implements Result<R> { } } + @Override + public <X extends Throwable> R getOrThrow( + Function<String, ? extends X> exceptionSupplier) throws X { + Objects.requireNonNull(exceptionSupplier, + "Exception supplier cannot be null"); + if (isError()) { + throw exceptionSupplier.apply(message); + } else { + return 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 345f8df5bf..97985af9ca 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 @@ -170,4 +170,5 @@ public interface Converter<PRESENTATION, MODEL> extends Serializable { } }; } + } 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 new file mode 100644 index 0000000000..2fff4915ee --- /dev/null +++ b/server/src/main/java/com/vaadin/data/util/converter/DateToLongConverter.java @@ -0,0 +1,50 @@ +/* + * 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.util.Date; +import java.util.Locale; + +import com.vaadin.data.Result; + +/** + * A converter that converts from {@link Long} to {@link Date} and back. + * + * @author Vaadin Ltd + * @since 8.0 + */ +public class DateToLongConverter implements Converter<Date, Long> { + + @Override + public Result<Long> convertToModel(Date value, Locale locale) { + if (value == null) { + return Result.ok(null); + } + + return Result.ok(value.getTime()); + } + + @Override + public Date convertToPresentation(Long value, Locale locale) { + if (value == null) { + return null; + } + + return new Date(value); + } + +} 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 new file mode 100644 index 0000000000..5695e4bf9d --- /dev/null +++ b/server/src/main/java/com/vaadin/data/util/converter/DateToSqlDateConverter.java @@ -0,0 +1,57 @@ +/* + * 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.util.Date; +import java.util.Locale; + +import com.vaadin.data.Result; + +/** + * Converter for handling conversion between {@link java.util.Date} and + * {@link java.sql.Date}. This is used when a PopupDateField or InlineDateField + * is connected to a java.sql.Date property. Note that information (time + * information) is lost when converting from {@link java.util.Date} to + * {@link java.sql.Date}. + * + * @since 8.0 + * @author Vaadin Ltd + */ +public class DateToSqlDateConverter implements Converter<Date, java.sql.Date> { + + @Override + public Result<java.sql.Date> convertToModel(Date value, Locale locale) { + if (value == null) { + return Result.ok(null); + } + + return Result.ok(new java.sql.Date(value.getTime())); + } + + @Override + public Date convertToPresentation(java.sql.Date value, Locale locale) { + if (value == null) { + return null; + } + + return new Date(value.getTime()); + } + +} 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 new file mode 100644 index 0000000000..639523516e --- /dev/null +++ b/server/src/main/java/com/vaadin/data/util/converter/StringToBigDecimalConverter.java @@ -0,0 +1,56 @@ +/* + * 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.math.BigDecimal; +import java.text.DecimalFormat; +import java.text.NumberFormat; +import java.util.Locale; + +import com.vaadin.data.Result; + +/** + * A converter that converts from {@link String} to {@link BigDecimal} and back. + * Uses the given locale and a {@link NumberFormat} instance for formatting and + * parsing. + * <p> + * Leading and trailing white spaces are ignored when converting from a String. + * </p> + * <p> + * Override and overwrite {@link #getFormat(Locale)} to use a different format. + * </p> + * + * @author Vaadin Ltd + * @since 8.0 + */ +public class StringToBigDecimalConverter + extends AbstractStringToNumberConverter<BigDecimal> { + @Override + protected NumberFormat getFormat(Locale locale) { + NumberFormat numberFormat = super.getFormat(locale); + if (numberFormat instanceof DecimalFormat) { + ((DecimalFormat) numberFormat).setParseBigDecimal(true); + } + + return numberFormat; + } + + @Override + public Result<BigDecimal> convertToModel(String value, Locale locale) { + return Result.ok((BigDecimal) convertToNumber(value, locale)); + } + +} 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 new file mode 100644 index 0000000000..58080013b6 --- /dev/null +++ b/server/src/main/java/com/vaadin/data/util/converter/StringToBigIntegerConverter.java @@ -0,0 +1,63 @@ +/* + * 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.math.BigDecimal; +import java.math.BigInteger; +import java.text.DecimalFormat; +import java.text.NumberFormat; +import java.util.Locale; + +import com.vaadin.data.Result; + +/** + * A converter that converts from {@link String} to {@link BigInteger} and back. + * Uses the given locale and a {@link NumberFormat} instance for formatting and + * parsing. + * <p> + * Leading and trailing white spaces are ignored when converting from a String. + * </p> + * <p> + * Override and overwrite {@link #getFormat(Locale)} to use a different format. + * </p> + * + * @author Vaadin Ltd + * @since 8.0 + */ +public class StringToBigIntegerConverter + extends AbstractStringToNumberConverter<BigInteger> { + + @Override + protected NumberFormat getFormat(Locale locale) { + NumberFormat numberFormat = super.getFormat(locale); + if (numberFormat instanceof DecimalFormat) { + ((DecimalFormat) numberFormat).setParseBigDecimal(true); + } + + return numberFormat; + } + + @Override + public Result<BigInteger> convertToModel(String value, Locale locale) { + + BigDecimal bigDecimalValue = (BigDecimal) convertToNumber(value, + locale); + + return (bigDecimalValue != null) + ? Result.ok(bigDecimalValue.toBigInteger()) : Result.ok(null); + } + +} 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 new file mode 100644 index 0000000000..23ce228619 --- /dev/null +++ b/server/src/main/java/com/vaadin/data/util/converter/StringToBooleanConverter.java @@ -0,0 +1,120 @@ +/* + * 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.util.Locale; + +import com.vaadin.data.Result; + +/** + * A converter that converts from {@link String} to {@link Boolean} and back. + * The String representation is given by {@link Boolean#toString()} or provided + * in constructor {@link #StringToBooleanConverter(String, String)}. + * <p> + * Leading and trailing white spaces are ignored when converting from a String. + * </p> + * <p> + * For language-dependent representation, subclasses should overwrite + * {@link #getFalseString(Locale)} and {@link #getTrueString(Locale)} + * </p> + * + * @author Vaadin Ltd + * @since 8.0 + */ +public class StringToBooleanConverter implements Converter<String, Boolean> { + + private final String trueString; + + private final String falseString; + + /** + * Creates converter with default string representations - "true" and + * "false". + * + */ + public StringToBooleanConverter() { + this(Boolean.TRUE.toString(), Boolean.FALSE.toString()); + } + + /** + * Creates converter with custom string representation. + * + * @param falseString + * string representation for <code>false</code> + * @param trueString + * string representation for <code>true</code> + */ + public StringToBooleanConverter(String trueString, String falseString) { + this.trueString = trueString; + this.falseString = falseString; + } + + @Override + public Result<Boolean> convertToModel(String value, Locale locale) { + if (value == null || value.isEmpty()) { + return Result.ok(null); + } + + // Remove leading and trailing white space + value = value.trim(); + + if (getTrueString(locale).equals(value)) { + return Result.ok(true); + } else if (getFalseString(locale).equals(value)) { + return Result.ok(false); + } else { + throw new IllegalArgumentException("Cannot convert " + value); + } + } + + @Override + public String convertToPresentation(Boolean value, Locale locale) { + if (value == null) { + return null; + } + if (value) { + return getTrueString(locale); + } else { + return getFalseString(locale); + } + } + + /** + * Gets the locale-depended string representation for false. Default is + * locale-independent value provided by {@link #getFalseString()} + * + * @param locale + * to be used + * @return the string representation for false + */ + protected String getFalseString(Locale locale) { + return falseString; + } + + /** + * Gets the locale-depended string representation for true. Default is + * locale-independent value provided by {@link #getTrueString()} + * + * @param locale + * to be used + * @return the string representation for true + */ + protected String getTrueString(Locale locale) { + return trueString; + } + +} 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 new file mode 100644 index 0000000000..2e3e8ab04a --- /dev/null +++ b/server/src/main/java/com/vaadin/data/util/converter/StringToDateConverter.java @@ -0,0 +1,87 @@ +/* + * 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.text.DateFormat; +import java.text.ParsePosition; +import java.util.Date; +import java.util.Locale; + +import com.vaadin.data.Result; + +/** + * A converter that converts from {@link Date} to {@link String} and back. Uses + * the given locale and {@link DateFormat} for formatting and parsing. + * <p> + * Leading and trailing white spaces are ignored when converting from a String. + * </p> + * <p> + * Override and overwrite {@link #getFormat(Locale)} to use a different format. + * </p> + * + * @author Vaadin Ltd + * @since 8.0 + */ +public class StringToDateConverter implements Converter<String, Date> { + + /** + * Returns the format used by {@link #convertToPresentation(Date, Locale)} + * and {@link #convertToModel(String, Locale)}. + * + * @param locale + * The locale to use + * @return A DateFormat instance + */ + protected DateFormat getFormat(Locale locale) { + if (locale == null) { + locale = Locale.getDefault(); + } + + DateFormat format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, + DateFormat.MEDIUM, locale); + format.setLenient(false); + return format; + } + + @Override + public Result<Date> convertToModel(String value, Locale locale) { + if (value == null) { + return Result.ok(null); + } + + // Remove leading and trailing white space + value = value.trim(); + + ParsePosition parsePosition = new ParsePosition(0); + Date parsedValue = getFormat(locale).parse(value, parsePosition); + if (parsePosition.getIndex() != value.length()) { + return Result.error("Could not convert '" + value); + } + + return Result.ok(parsedValue); + } + + @Override + public String convertToPresentation(Date value, Locale locale) { + if (value == null) { + return null; + } + + return getFormat(locale).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 new file mode 100644 index 0000000000..374a553e8e --- /dev/null +++ b/server/src/main/java/com/vaadin/data/util/converter/StringToDoubleConverter.java @@ -0,0 +1,47 @@ +/* + * 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.text.NumberFormat; +import java.util.Locale; + +import com.vaadin.data.Result; + +/** + * A converter that converts from {@link String} to {@link Double} and back. + * Uses the given locale and a {@link NumberFormat} instance for formatting and + * parsing. + * <p> + * Leading and trailing white spaces are ignored when converting from a String. + * </p> + * <p> + * Override and overwrite {@link #getFormat(Locale)} to use a different format. + * </p> + * + * @author Vaadin Ltd + * @since 8.0 + */ +public class StringToDoubleConverter + extends AbstractStringToNumberConverter<Double> { + + @Override + public Result<Double> convertToModel(String value, Locale locale) { + Number n = convertToNumber(value, locale); + return n == null ? Result.ok(null) : Result.ok(n.doubleValue()); + } + +} diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToFloatConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToFloatConverter.java new file mode 100644 index 0000000000..d05374a3cc --- /dev/null +++ b/server/src/main/java/com/vaadin/data/util/converter/StringToFloatConverter.java @@ -0,0 +1,47 @@ +/* + * 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.text.NumberFormat; +import java.util.Locale; + +import com.vaadin.data.Result; + +/** + * A converter that converts from {@link String} to {@link Float} and back. Uses + * the given locale and a {@link NumberFormat} instance for formatting and + * parsing. + * <p> + * Leading and trailing white spaces are ignored when converting from a String. + * </p> + * <p> + * Override and overwrite {@link #getFormat(Locale)} to use a different format. + * </p> + * + * @author Vaadin Ltd + * @since 8.0 + */ +public class StringToFloatConverter + extends AbstractStringToNumberConverter<Float> { + + @Override + public Result<Float> convertToModel(String value, Locale locale) { + Number n = convertToNumber(value, locale); + return n == null ? Result.ok(null) : Result.ok(n.floatValue()); + } + +} diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToIntegerConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToIntegerConverter.java index 16dcd832dd..7a5ee69aed 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 @@ -69,7 +69,7 @@ public class StringToIntegerConverter Number n = convertToNumber(value, locale); if (n == null) { - return null; + return Result.ok(null); } int intValue = n.intValue(); 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 new file mode 100644 index 0000000000..d82dec9ae9 --- /dev/null +++ b/server/src/main/java/com/vaadin/data/util/converter/StringToLongConverter.java @@ -0,0 +1,61 @@ +/* + * 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.text.NumberFormat; +import java.util.Locale; + +import com.vaadin.data.Result; + +/** + * A converter that converts from {@link String} to {@link Long} and back. Uses + * the given locale and a {@link NumberFormat} instance for formatting and + * parsing. + * <p> + * Override and overwrite {@link #getFormat(Locale)} to use a different format. + * </p> + * + * @author Vaadin Ltd + * @since 8.0 + */ +public class StringToLongConverter + extends AbstractStringToNumberConverter<Long> { + + /** + * Returns the format used by {@link #convertToPresentation(Long, Locale)} + * and {@link #convertToModel(String, Locale)}. + * + * @param locale + * The locale to use + * @return A NumberFormat instance + */ + @Override + protected NumberFormat getFormat(Locale locale) { + if (locale == null) { + locale = Locale.getDefault(); + } + return NumberFormat.getIntegerInstance(locale); + } + + @Override + public Result<Long> convertToModel(String value, Locale locale) { + Number n = convertToNumber(value, locale); + return n == null ? Result.ok(null) : Result.ok(n.longValue()); + + } + +} 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 new file mode 100644 index 0000000000..276b0b674c --- /dev/null +++ b/server/src/test/java/com/vaadin/tests/data/converter/AbstractConverterTest.java @@ -0,0 +1,30 @@ +package com.vaadin.tests.data.converter; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.data.Result; +import com.vaadin.data.util.converter.Converter; + +public abstract class AbstractConverterTest { + + @Test + public void testNullConversion() { + assertResult(null, getConverter().convertToModel(null, null)); + } + + protected abstract Converter<?, ?> getConverter(); + + protected void assertResult(Object object, Result<?> result) { + assertResult(null, object, result); + } + + protected void assertResult(String error, Object object, Result<?> result) { + Assert.assertNotNull("Result should never be null", result); + Assert.assertFalse("Result is not ok", result.isError()); + Assert.assertEquals(object, + result.getOrThrow(message -> new AssertionError( + error != null ? error : message))); + } + +} 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 new file mode 100644 index 0000000000..a3d52b0520 --- /dev/null +++ b/server/src/test/java/com/vaadin/tests/data/converter/ConverterTest.java @@ -0,0 +1,35 @@ +package com.vaadin.tests.data.converter; + +import java.util.function.Function; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.data.Result; +import com.vaadin.data.util.converter.Converter; + +public class ConverterTest { + + Function<String, Result<String>> toModel = presentation -> { + if (presentation.startsWith("presentation-")) { + return Result.ok(presentation.substring("presentation-".length())); + } else { + return Result.error("invalid prefix: " + presentation); + } + }; + + Function<String, String> toPresentation = model -> "presentation-" + model; + + Converter<String, String> converter = Converter.from(toModel, + toPresentation); + + @Test + public void basicConversion() { + Assert.assertEquals("presentation-123", + converter.convertToPresentation("123", null)); + Assert.assertEquals("123", + converter.convertToModel("presentation-123", null) + .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 e8932b9600..3ce42f5348 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 @@ -2,27 +2,29 @@ package com.vaadin.tests.data.converter; import java.util.Date; -import org.junit.Assert; import org.junit.Test; -import com.vaadin.v7.data.util.converter.DateToLongConverter; +import com.vaadin.data.util.converter.DateToLongConverter; -public class DateToLongConverterTest { +public class DateToLongConverterTest extends AbstractConverterTest { - DateToLongConverter converter = new DateToLongConverter(); + @Override + protected DateToLongConverter getConverter() { + return new DateToLongConverter(); + } + @Override @Test public void testNullConversion() { - Assert.assertEquals(null, - converter.convertToModel(null, Long.class, null)); + assertResult(null, getConverter().convertToModel(null, null)); } @Test public void testValueConversion() { Date d = new Date(100, 0, 1); - Assert.assertEquals( + assertResult( Long.valueOf(946677600000l + (d.getTimezoneOffset() + 120) * 60 * 1000L), - converter.convertToModel(d, Long.class, null)); + getConverter().convertToModel(d, null)); } } diff --git a/server/src/test/java/com/vaadin/tests/data/converter/DateToSqlDateConverterTest.java b/server/src/test/java/com/vaadin/tests/data/converter/DateToSqlDateConverterTest.java index 607d71b70e..3bc4e2d481 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 @@ -3,26 +3,22 @@ package com.vaadin.tests.data.converter; import java.util.Date; import java.util.Locale; -import org.junit.Assert; import org.junit.Test; -import com.vaadin.v7.data.util.converter.DateToSqlDateConverter; +import com.vaadin.data.util.converter.DateToSqlDateConverter; -public class DateToSqlDateConverterTest { +public class DateToSqlDateConverterTest extends AbstractConverterTest { - DateToSqlDateConverter converter = new DateToSqlDateConverter(); - - @Test - public void testNullConversion() { - Assert.assertEquals(null, - converter.convertToModel(null, java.sql.Date.class, null)); + @Override + protected DateToSqlDateConverter getConverter() { + return new DateToSqlDateConverter(); } @Test public void testValueConversion() { Date testDate = new Date(100, 0, 1); long time = testDate.getTime(); - Assert.assertEquals(testDate, converter.convertToModel( - new java.sql.Date(time), java.sql.Date.class, Locale.ENGLISH)); + assertResult(testDate, getConverter() + .convertToModel(new java.sql.Date(time), Locale.ENGLISH)); } } diff --git a/server/src/test/java/com/vaadin/tests/data/converter/StringToBigDecimalConverterTest.java b/server/src/test/java/com/vaadin/tests/data/converter/StringToBigDecimalConverterTest.java index ff57849bf8..fd5fe2416b 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 @@ -21,30 +21,27 @@ import java.util.Locale; import org.junit.Assert; import org.junit.Test; -import com.vaadin.v7.data.util.converter.StringToBigDecimalConverter; +import com.vaadin.data.Result; +import com.vaadin.data.util.converter.StringToBigDecimalConverter; -public class StringToBigDecimalConverterTest { +public class StringToBigDecimalConverterTest extends AbstractConverterTest { - StringToBigDecimalConverter converter = new StringToBigDecimalConverter(); - - @Test - public void testNullConversion() { - Assert.assertEquals(null, - converter.convertToModel(null, BigDecimal.class, null)); + @Override + protected StringToBigDecimalConverter getConverter() { + return new StringToBigDecimalConverter(); } @Test public void testEmptyStringConversion() { - Assert.assertEquals(null, - converter.convertToModel("", BigDecimal.class, null)); + assertResult(null, getConverter().convertToModel("", null)); } @Test public void testValueParsing() { - BigDecimal converted = converter.convertToModel("10", BigDecimal.class, + Result<BigDecimal> converted = getConverter().convertToModel("10", null); BigDecimal expected = new BigDecimal(10); - Assert.assertEquals(expected, converted); + assertResult(expected, converted); } @Test @@ -52,7 +49,7 @@ public class StringToBigDecimalConverterTest { BigDecimal bd = new BigDecimal(12.5); String expected = "12,5"; - String converted = converter.convertToPresentation(bd, String.class, + String converted = getConverter().convertToPresentation(bd, 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 db0edfce71..e537fdf8a1 100644 --- a/server/src/test/java/com/vaadin/tests/data/converter/StringToBigIntegerConverterTest.java +++ b/server/src/test/java/com/vaadin/tests/data/converter/StringToBigIntegerConverterTest.java @@ -18,35 +18,33 @@ package com.vaadin.tests.data.converter; import java.math.BigInteger; import java.util.Locale; -import org.junit.Assert; import org.junit.Test; -import com.vaadin.v7.data.util.converter.StringToBigIntegerConverter; +import com.vaadin.data.Result; +import com.vaadin.data.util.converter.StringToBigIntegerConverter; -public class StringToBigIntegerConverterTest { +import junit.framework.Assert; - StringToBigIntegerConverter converter = new StringToBigIntegerConverter(); +public class StringToBigIntegerConverterTest extends AbstractConverterTest { - @Test - public void testNullConversion() { - Assert.assertEquals("Null value was converted incorrectly", null, - converter.convertToModel(null, BigInteger.class, null)); + @Override + protected StringToBigIntegerConverter getConverter() { + return new StringToBigIntegerConverter(); } @Test public void testEmptyStringConversion() { - Assert.assertEquals("Empty value was converted incorrectly", null, - converter.convertToModel("", BigInteger.class, null)); + assertResult("Empty value was converted incorrectly", null, + getConverter().convertToModel("", null)); } @Test public void testValueParsing() { String bigInt = "1180591620717411303424"; // 2^70 > 2^63 - 1 - BigInteger converted = converter.convertToModel(bigInt, - BigInteger.class, null); + Result<BigInteger> converted = getConverter().convertToModel(bigInt, + null); BigInteger expected = new BigInteger(bigInt); - Assert.assertEquals( - "Value bigger than max long was converted incorrectly", + assertResult("Value bigger than max long was converted incorrectly", expected, converted); } @@ -55,7 +53,7 @@ public class StringToBigIntegerConverterTest { BigInteger bd = new BigInteger("1000"); String expected = "1.000"; - String converted = converter.convertToPresentation(bd, String.class, + String converted = getConverter().convertToPresentation(bd, Locale.GERMAN); Assert.assertEquals( "Value with specific locale was converted incorrectly", 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 9889036f7d..d73fe5ca87 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 @@ -7,11 +7,15 @@ import java.util.Locale; import org.junit.Assert; import org.junit.Test; -import com.vaadin.v7.data.util.converter.StringToBooleanConverter; +import com.vaadin.data.util.converter.StringToBooleanConverter; -public class StringToBooleanConverterTest { +public class StringToBooleanConverterTest extends AbstractConverterTest { + + @Override + protected StringToBooleanConverter getConverter() { + return new StringToBooleanConverter(); + } - StringToBooleanConverter converter = new StringToBooleanConverter(); StringToBooleanConverter yesNoConverter = new StringToBooleanConverter( "yes", "no"); StringToBooleanConverter localeConverter = new StringToBooleanConverter() { @@ -35,48 +39,37 @@ public class StringToBooleanConverterTest { }; @Test - public void testNullConversion() { - Assert.assertEquals(null, - converter.convertToModel(null, Boolean.class, null)); - } - - @Test public void testEmptyStringConversion() { - Assert.assertEquals(null, - converter.convertToModel("", Boolean.class, null)); + assertResult(null, getConverter().convertToModel("", null)); } @Test public void testValueConversion() { - Assert.assertTrue( - converter.convertToModel("true", Boolean.class, null)); - Assert.assertFalse( - converter.convertToModel("false", Boolean.class, null)); + assertResult(true, getConverter().convertToModel("true", null)); + assertResult(false, getConverter().convertToModel("false", null)); } @Test public void testYesNoValueConversion() { - Assert.assertTrue( - yesNoConverter.convertToModel("yes", Boolean.class, null)); - Assert.assertFalse( - yesNoConverter.convertToModel("no", Boolean.class, null)); + assertResult(true, yesNoConverter.convertToModel("yes", null)); + assertResult(false, yesNoConverter.convertToModel("no", null)); Assert.assertEquals("yes", - yesNoConverter.convertToPresentation(true, String.class, null)); - Assert.assertEquals("no", yesNoConverter.convertToPresentation(false, - String.class, null)); + yesNoConverter.convertToPresentation(true, null)); + Assert.assertEquals("no", + yesNoConverter.convertToPresentation(false, null)); } @Test public void testLocale() { - Assert.assertEquals("May 18, 2033", localeConverter - .convertToPresentation(true, String.class, Locale.US)); - Assert.assertEquals("January 24, 2065", localeConverter - .convertToPresentation(false, String.class, Locale.US)); + Assert.assertEquals("May 18, 2033", + localeConverter.convertToPresentation(true, Locale.US)); + Assert.assertEquals("January 24, 2065", + localeConverter.convertToPresentation(false, Locale.US)); - Assert.assertEquals("18. Mai 2033", localeConverter - .convertToPresentation(true, String.class, Locale.GERMANY)); - Assert.assertEquals("24. Januar 2065", localeConverter - .convertToPresentation(false, String.class, Locale.GERMANY)); + Assert.assertEquals("18. Mai 2033", + localeConverter.convertToPresentation(true, Locale.GERMANY)); + Assert.assertEquals("24. Januar 2065", + localeConverter.convertToPresentation(false, 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 b6f8214f36..b7866ca9d6 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 @@ -3,30 +3,25 @@ package com.vaadin.tests.data.converter; import java.util.Date; import java.util.Locale; -import org.junit.Assert; import org.junit.Test; -import com.vaadin.v7.data.util.converter.StringToDateConverter; +import com.vaadin.data.util.converter.StringToDateConverter; -public class StringToDateConverterTest { +public class StringToDateConverterTest extends AbstractConverterTest { - StringToDateConverter converter = new StringToDateConverter(); - - @Test - public void testNullConversion() { - Assert.assertEquals(null, - converter.convertToModel(null, Date.class, null)); + @Override + protected StringToDateConverter getConverter() { + return new StringToDateConverter(); } @Test public void testEmptyStringConversion() { - Assert.assertEquals(null, - converter.convertToModel("", Date.class, null)); + assertResult(null, getConverter().convertToModel("", null)); } @Test public void testValueConversion() { - Assert.assertEquals(new Date(100, 0, 1), converter.convertToModel( - "Jan 1, 2000 12:00:00 AM", Date.class, Locale.ENGLISH)); + assertResult(new Date(100, 0, 1), getConverter() + .convertToModel("Jan 1, 2000 12:00:00 AM", Locale.ENGLISH)); } } diff --git a/server/src/test/java/com/vaadin/tests/data/converter/StringToDoubleConverterTest.java b/server/src/test/java/com/vaadin/tests/data/converter/StringToDoubleConverterTest.java index d3e9831b4b..8e043e2082 100644 --- a/server/src/test/java/com/vaadin/tests/data/converter/StringToDoubleConverterTest.java +++ b/server/src/test/java/com/vaadin/tests/data/converter/StringToDoubleConverterTest.java @@ -1,29 +1,25 @@ package com.vaadin.tests.data.converter; -import org.junit.Assert; import org.junit.Test; -import com.vaadin.v7.data.util.converter.StringToDoubleConverter; +import com.vaadin.data.Result; +import com.vaadin.data.util.converter.StringToDoubleConverter; -public class StringToDoubleConverterTest { +public class StringToDoubleConverterTest extends AbstractConverterTest { - StringToDoubleConverter converter = new StringToDoubleConverter(); - - @Test - public void testNullConversion() { - Assert.assertEquals(null, - converter.convertToModel(null, Double.class, null)); + @Override + protected StringToDoubleConverter getConverter() { + return new StringToDoubleConverter(); } @Test public void testEmptyStringConversion() { - Assert.assertEquals(null, - converter.convertToModel("", Double.class, null)); + assertResult(null, getConverter().convertToModel("", null)); } @Test public void testValueConversion() { - Double value = converter.convertToModel("10", Double.class, null); - Assert.assertEquals(10.0d, value, 0.01d); + Result<Double> value = getConverter().convertToModel("10", null); + assertResult(10.0d, value); } } 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 e6768e30d9..a6292d3310 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 @@ -1,29 +1,30 @@ package com.vaadin.tests.data.converter; -import org.junit.Assert; import org.junit.Test; -import com.vaadin.v7.data.util.converter.StringToFloatConverter; +import com.vaadin.data.util.converter.StringToFloatConverter; -public class StringToFloatConverterTest { +public class StringToFloatConverterTest extends AbstractConverterTest { - StringToFloatConverter converter = new StringToFloatConverter(); + @Override + protected StringToFloatConverter getConverter() { + return new StringToFloatConverter(); + } + @Override @Test public void testNullConversion() { - Assert.assertEquals(null, - converter.convertToModel(null, Float.class, null)); + assertResult(null, getConverter().convertToModel(null, null)); } @Test public void testEmptyStringConversion() { - Assert.assertEquals(null, - converter.convertToModel("", Float.class, null)); + assertResult(null, getConverter().convertToModel("", null)); } @Test public void testValueConversion() { - Assert.assertEquals(Float.valueOf(10), - converter.convertToModel("10", Float.class, null)); + assertResult(Float.valueOf(10), + getConverter().convertToModel("10", null)); } } diff --git a/server/src/test/java/com/vaadin/tests/data/converter/StringToIntegerConverterTest.java b/server/src/test/java/com/vaadin/tests/data/converter/StringToIntegerConverterTest.java index 3d1f6985d2..daaf27838b 100644 --- a/server/src/test/java/com/vaadin/tests/data/converter/StringToIntegerConverterTest.java +++ b/server/src/test/java/com/vaadin/tests/data/converter/StringToIntegerConverterTest.java @@ -3,23 +3,18 @@ package com.vaadin.tests.data.converter; import org.junit.Assert; import org.junit.Test; -import com.vaadin.v7.data.util.converter.Converter.ConversionException; -import com.vaadin.v7.data.util.converter.StringToIntegerConverter; +import com.vaadin.data.util.converter.StringToIntegerConverter; -public class StringToIntegerConverterTest { +public class StringToIntegerConverterTest extends AbstractConverterTest { - StringToIntegerConverter converter = new StringToIntegerConverter(); - - @Test - public void testNullConversion() { - Assert.assertEquals(null, - converter.convertToModel(null, Integer.class, null)); + @Override + protected StringToIntegerConverter getConverter() { + return new StringToIntegerConverter("Failed"); } @Test public void testEmptyStringConversion() { - Assert.assertEquals(null, - converter.convertToModel("", Integer.class, null)); + assertResult(null, getConverter().convertToModel("", null)); } @Test @@ -31,10 +26,10 @@ public class StringToIntegerConverterTest { boolean accepted = false; for (Number value : values) { try { - converter.convertToModel(String.format("%.0f", value), - Integer.class, null); + getConverter().convertToModel(String.format("%.0f", value), + null); + } catch (Exception e) { accepted = true; - } catch (ConversionException expected) { } } Assert.assertFalse("Accepted value outside range of int", accepted); @@ -42,7 +37,7 @@ public class StringToIntegerConverterTest { @Test public void testValueConversion() { - Assert.assertEquals(Integer.valueOf(10), - converter.convertToModel("10", Integer.class, null)); + assertResult(Integer.valueOf(10), + getConverter().convertToModel("10", null)); } } 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 77f1167769..46afb848d3 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 @@ -1,78 +1,46 @@ package com.vaadin.tests.data.converter; -import java.util.Locale; - -import org.junit.Assert; import org.junit.Test; -import com.vaadin.v7.data.util.converter.Converter; -import com.vaadin.v7.data.util.converter.ReverseConverter; -import com.vaadin.v7.data.util.converter.StringToLongConverter; - -public class StringToLongConverterTest { - - StringToLongConverter converter = new StringToLongConverter(); - Converter<Long, String> reverseConverter = new ReverseConverter<Long, String>( - converter); +import com.vaadin.data.Result; +import com.vaadin.data.util.converter.StringToLongConverter; - @Test - public void testNullConversion() { - Assert.assertEquals(null, - converter.convertToModel(null, Long.class, null)); - } +public class StringToLongConverterTest extends AbstractConverterTest { - @Test - public void testReverseNullConversion() { - Assert.assertEquals(null, - reverseConverter.convertToModel(null, String.class, null)); + @Override + protected StringToLongConverter getConverter() { + return new StringToLongConverter(); } @Test public void testEmptyStringConversion() { - Assert.assertEquals(null, - converter.convertToModel("", Long.class, null)); + assertResult(null, getConverter().convertToModel("", null)); } @Test public void testValueConversion() { - Assert.assertEquals(Long.valueOf(10), - converter.convertToModel("10", Long.class, null)); - } - - @Test - public void testReverseValueConversion() { - Assert.assertEquals( - reverseConverter.convertToModel(10L, String.class, null), "10"); + assertResult(Long.valueOf(10), + getConverter().convertToModel("10", null)); } @Test public void testExtremeLongValueConversion() { - long l = converter.convertToModel("9223372036854775807", Long.class, + Result<Long> l = getConverter().convertToModel("9223372036854775807", null); - Assert.assertEquals(Long.MAX_VALUE, l); - l = converter.convertToModel("-9223372036854775808", Long.class, null); - Assert.assertEquals(Long.MIN_VALUE, l); - } - - @Test - public void testExtremeReverseLongValueConversion() { - String str = reverseConverter.convertToModel(Long.MAX_VALUE, - String.class, Locale.ENGLISH); - Assert.assertEquals("9,223,372,036,854,775,807", str); - str = reverseConverter.convertToModel(Long.MIN_VALUE, String.class, - Locale.ENGLISH); - Assert.assertEquals("-9,223,372,036,854,775,808", str); + assertResult(Long.MAX_VALUE, l); + l = getConverter().convertToModel("-9223372036854775808", null); + assertResult(Long.MIN_VALUE, l); } @Test public void testOutOfBoundsValueConversion() { // Long.MAX_VALUE+1 is converted to Long.MAX_VALUE - long l = converter.convertToModel("9223372036854775808", Long.class, + Result<Long> l = getConverter().convertToModel("9223372036854775808", null); - Assert.assertEquals(Long.MAX_VALUE, l); + assertResult(Long.MAX_VALUE, l); // Long.MIN_VALUE-1 is converted to Long.MIN_VALUE - l = converter.convertToModel("-9223372036854775809", Long.class, null); - Assert.assertEquals(Long.MIN_VALUE, l); + l = getConverter().convertToModel("-9223372036854775809", null); + assertResult(Long.MIN_VALUE, l); } } diff --git a/server/src/test/java/com/vaadin/tests/data/converter/AnyEnumToStringConverterTest.java b/server/src/test/java/com/vaadin/v7/tests/data/converter/AnyEnumToStringConverterTest.java index 7b5773b070..4f0c286d2d 100644 --- a/server/src/test/java/com/vaadin/tests/data/converter/AnyEnumToStringConverterTest.java +++ b/server/src/test/java/com/vaadin/v7/tests/data/converter/AnyEnumToStringConverterTest.java @@ -14,7 +14,7 @@ * the License. */ -package com.vaadin.tests.data.converter; +package com.vaadin.v7.tests.data.converter; import java.util.Locale; diff --git a/server/src/test/java/com/vaadin/tests/data/converter/ConverterFactoryTest.java b/server/src/test/java/com/vaadin/v7/tests/data/converter/ConverterFactoryTest.java index be3bf0db97..1f3a64cbb7 100644 --- a/server/src/test/java/com/vaadin/tests/data/converter/ConverterFactoryTest.java +++ b/server/src/test/java/com/vaadin/v7/tests/data/converter/ConverterFactoryTest.java @@ -13,7 +13,7 @@ * License for the specific language governing permissions and limitations under * the License. */ -package com.vaadin.tests.data.converter; +package com.vaadin.v7.tests.data.converter; import java.util.Locale; diff --git a/server/src/test/java/com/vaadin/v7/tests/data/converter/DateToLongConverterTest.java b/server/src/test/java/com/vaadin/v7/tests/data/converter/DateToLongConverterTest.java new file mode 100644 index 0000000000..6300ed9479 --- /dev/null +++ b/server/src/test/java/com/vaadin/v7/tests/data/converter/DateToLongConverterTest.java @@ -0,0 +1,28 @@ +package com.vaadin.v7.tests.data.converter; + +import java.util.Date; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.v7.data.util.converter.DateToLongConverter; + +public class DateToLongConverterTest { + + DateToLongConverter converter = new DateToLongConverter(); + + @Test + public void testNullConversion() { + Assert.assertEquals(null, + converter.convertToModel(null, Long.class, null)); + } + + @Test + public void testValueConversion() { + Date d = new Date(100, 0, 1); + Assert.assertEquals( + Long.valueOf(946677600000l + + (d.getTimezoneOffset() + 120) * 60 * 1000L), + converter.convertToModel(d, Long.class, null)); + } +} diff --git a/server/src/test/java/com/vaadin/v7/tests/data/converter/DateToSqlDateConverterTest.java b/server/src/test/java/com/vaadin/v7/tests/data/converter/DateToSqlDateConverterTest.java new file mode 100644 index 0000000000..35a8c5dbec --- /dev/null +++ b/server/src/test/java/com/vaadin/v7/tests/data/converter/DateToSqlDateConverterTest.java @@ -0,0 +1,28 @@ +package com.vaadin.v7.tests.data.converter; + +import java.util.Date; +import java.util.Locale; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.v7.data.util.converter.DateToSqlDateConverter; + +public class DateToSqlDateConverterTest { + + DateToSqlDateConverter converter = new DateToSqlDateConverter(); + + @Test + public void testNullConversion() { + Assert.assertEquals(null, + converter.convertToModel(null, java.sql.Date.class, null)); + } + + @Test + public void testValueConversion() { + Date testDate = new Date(100, 0, 1); + long time = testDate.getTime(); + Assert.assertEquals(testDate, converter.convertToModel( + new java.sql.Date(time), java.sql.Date.class, Locale.ENGLISH)); + } +} diff --git a/server/src/test/java/com/vaadin/tests/data/converter/DefaultConverterFactoryTest.java b/server/src/test/java/com/vaadin/v7/tests/data/converter/DefaultConverterFactoryTest.java index 518ca1936f..a157884376 100644 --- a/server/src/test/java/com/vaadin/tests/data/converter/DefaultConverterFactoryTest.java +++ b/server/src/test/java/com/vaadin/v7/tests/data/converter/DefaultConverterFactoryTest.java @@ -13,7 +13,7 @@ * License for the specific language governing permissions and limitations under * the License. */ -package com.vaadin.tests.data.converter; +package com.vaadin.v7.tests.data.converter; import java.math.BigDecimal; import java.math.BigInteger; diff --git a/server/src/test/java/com/vaadin/tests/data/converter/SpecificEnumToStringConverterTest.java b/server/src/test/java/com/vaadin/v7/tests/data/converter/SpecificEnumToStringConverterTest.java index 627ab7943d..10b4a0065e 100644 --- a/server/src/test/java/com/vaadin/tests/data/converter/SpecificEnumToStringConverterTest.java +++ b/server/src/test/java/com/vaadin/v7/tests/data/converter/SpecificEnumToStringConverterTest.java @@ -14,7 +14,7 @@ * the License. */ -package com.vaadin.tests.data.converter; +package com.vaadin.v7.tests.data.converter; import java.util.Locale; diff --git a/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToBigDecimalConverterTest.java b/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToBigDecimalConverterTest.java new file mode 100644 index 0000000000..00e384e594 --- /dev/null +++ b/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToBigDecimalConverterTest.java @@ -0,0 +1,59 @@ +/* + * 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.v7.tests.data.converter; + +import java.math.BigDecimal; +import java.util.Locale; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.v7.data.util.converter.StringToBigDecimalConverter; + +public class StringToBigDecimalConverterTest { + + StringToBigDecimalConverter converter = new StringToBigDecimalConverter(); + + @Test + public void testNullConversion() { + Assert.assertEquals(null, + converter.convertToModel(null, BigDecimal.class, null)); + } + + @Test + public void testEmptyStringConversion() { + Assert.assertEquals(null, + converter.convertToModel("", BigDecimal.class, null)); + } + + @Test + public void testValueParsing() { + BigDecimal converted = converter.convertToModel("10", BigDecimal.class, + null); + BigDecimal expected = new BigDecimal(10); + Assert.assertEquals(expected, converted); + } + + @Test + public void testValueFormatting() { + BigDecimal bd = new BigDecimal(12.5); + String expected = "12,5"; + + String converted = converter.convertToPresentation(bd, String.class, + Locale.GERMAN); + Assert.assertEquals(expected, converted); + } +} diff --git a/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToBigIntegerConverterTest.java b/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToBigIntegerConverterTest.java new file mode 100644 index 0000000000..f613950c71 --- /dev/null +++ b/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToBigIntegerConverterTest.java @@ -0,0 +1,64 @@ +/* + * 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.v7.tests.data.converter; + +import java.math.BigInteger; +import java.util.Locale; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.v7.data.util.converter.StringToBigIntegerConverter; + +public class StringToBigIntegerConverterTest { + + StringToBigIntegerConverter converter = new StringToBigIntegerConverter(); + + @Test + public void testNullConversion() { + Assert.assertEquals("Null value was converted incorrectly", null, + converter.convertToModel(null, BigInteger.class, null)); + } + + @Test + public void testEmptyStringConversion() { + Assert.assertEquals("Empty value was converted incorrectly", null, + converter.convertToModel("", BigInteger.class, null)); + } + + @Test + public void testValueParsing() { + String bigInt = "1180591620717411303424"; // 2^70 > 2^63 - 1 + BigInteger converted = converter.convertToModel(bigInt, + BigInteger.class, null); + BigInteger expected = new BigInteger(bigInt); + Assert.assertEquals( + "Value bigger than max long was converted incorrectly", + expected, converted); + } + + @Test + public void testValueFormatting() { + BigInteger bd = new BigInteger("1000"); + String expected = "1.000"; + + String converted = converter.convertToPresentation(bd, String.class, + Locale.GERMAN); + Assert.assertEquals( + "Value with specific locale was converted incorrectly", + expected, converted); + } +} diff --git a/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToBooleanConverterTest.java b/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToBooleanConverterTest.java new file mode 100644 index 0000000000..9f94a4185e --- /dev/null +++ b/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToBooleanConverterTest.java @@ -0,0 +1,82 @@ +package com.vaadin.v7.tests.data.converter; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.v7.data.util.converter.StringToBooleanConverter; + +public class StringToBooleanConverterTest { + + StringToBooleanConverter converter = new StringToBooleanConverter(); + StringToBooleanConverter yesNoConverter = new StringToBooleanConverter( + "yes", "no"); + StringToBooleanConverter localeConverter = new StringToBooleanConverter() { + @Override + public String getFalseString(Locale locale) { + Date d = new Date(3000000000000L); + return SimpleDateFormat + .getDateInstance(SimpleDateFormat.LONG, locale) + .format(d.getTime() + + (d.getTimezoneOffset() + 120) * 60 * 1000L); + } + + @Override + public String getTrueString(Locale locale) { + Date d = new Date(2000000000000L); + return SimpleDateFormat + .getDateInstance(SimpleDateFormat.LONG, locale) + .format(d.getTime() + + (d.getTimezoneOffset() + 120) * 60 * 1000L); + } + }; + + @Test + public void testNullConversion() { + Assert.assertEquals(null, + converter.convertToModel(null, Boolean.class, null)); + } + + @Test + public void testEmptyStringConversion() { + Assert.assertEquals(null, + converter.convertToModel("", Boolean.class, null)); + } + + @Test + public void testValueConversion() { + Assert.assertTrue( + converter.convertToModel("true", Boolean.class, null)); + Assert.assertFalse( + converter.convertToModel("false", Boolean.class, null)); + } + + @Test + public void testYesNoValueConversion() { + Assert.assertTrue( + yesNoConverter.convertToModel("yes", Boolean.class, null)); + Assert.assertFalse( + yesNoConverter.convertToModel("no", Boolean.class, null)); + + Assert.assertEquals("yes", + yesNoConverter.convertToPresentation(true, String.class, null)); + Assert.assertEquals("no", yesNoConverter.convertToPresentation(false, + String.class, null)); + } + + @Test + public void testLocale() { + Assert.assertEquals("May 18, 2033", localeConverter + .convertToPresentation(true, String.class, Locale.US)); + Assert.assertEquals("January 24, 2065", localeConverter + .convertToPresentation(false, String.class, Locale.US)); + + Assert.assertEquals("18. Mai 2033", localeConverter + .convertToPresentation(true, String.class, Locale.GERMANY)); + Assert.assertEquals("24. Januar 2065", localeConverter + .convertToPresentation(false, String.class, Locale.GERMANY)); + } +} diff --git a/server/src/test/java/com/vaadin/tests/data/converter/StringToByteConverterTest.java b/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToByteConverterTest.java index a28d16797f..1ffc7a020e 100644 --- a/server/src/test/java/com/vaadin/tests/data/converter/StringToByteConverterTest.java +++ b/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToByteConverterTest.java @@ -1,4 +1,4 @@ -package com.vaadin.tests.data.converter; +package com.vaadin.v7.tests.data.converter; import org.junit.Assert; import org.junit.Test; diff --git a/server/src/test/java/com/vaadin/tests/data/converter/StringToCollectionConverterTest.java b/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToCollectionConverterTest.java index d21f6c5803..f6dc22face 100644 --- a/server/src/test/java/com/vaadin/tests/data/converter/StringToCollectionConverterTest.java +++ b/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToCollectionConverterTest.java @@ -13,7 +13,7 @@ * License for the specific language governing permissions and limitations under * the License. */ -package com.vaadin.tests.data.converter; +package com.vaadin.v7.tests.data.converter; import java.util.ArrayList; import java.util.Arrays; diff --git a/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToDateConverterTest.java b/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToDateConverterTest.java new file mode 100644 index 0000000000..c778e9e731 --- /dev/null +++ b/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToDateConverterTest.java @@ -0,0 +1,32 @@ +package com.vaadin.v7.tests.data.converter; + +import java.util.Date; +import java.util.Locale; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.v7.data.util.converter.StringToDateConverter; + +public class StringToDateConverterTest { + + StringToDateConverter converter = new StringToDateConverter(); + + @Test + public void testNullConversion() { + Assert.assertEquals(null, + converter.convertToModel(null, Date.class, null)); + } + + @Test + public void testEmptyStringConversion() { + Assert.assertEquals(null, + converter.convertToModel("", Date.class, null)); + } + + @Test + public void testValueConversion() { + Assert.assertEquals(new Date(100, 0, 1), converter.convertToModel( + "Jan 1, 2000 12:00:00 AM", Date.class, Locale.ENGLISH)); + } +} diff --git a/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToDoubleConverterTest.java b/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToDoubleConverterTest.java new file mode 100644 index 0000000000..a0891ecefe --- /dev/null +++ b/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToDoubleConverterTest.java @@ -0,0 +1,29 @@ +package com.vaadin.v7.tests.data.converter; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.v7.data.util.converter.StringToDoubleConverter; + +public class StringToDoubleConverterTest { + + StringToDoubleConverter converter = new StringToDoubleConverter(); + + @Test + public void testNullConversion() { + Assert.assertEquals(null, + converter.convertToModel(null, Double.class, null)); + } + + @Test + public void testEmptyStringConversion() { + Assert.assertEquals(null, + converter.convertToModel("", Double.class, null)); + } + + @Test + public void testValueConversion() { + Double value = converter.convertToModel("10", Double.class, null); + Assert.assertEquals(10.0d, value, 0.01d); + } +} diff --git a/server/src/test/java/com/vaadin/tests/data/converter/StringToEnumConverterTest.java b/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToEnumConverterTest.java index 4e7131e956..a86b8b82e0 100644 --- a/server/src/test/java/com/vaadin/tests/data/converter/StringToEnumConverterTest.java +++ b/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToEnumConverterTest.java @@ -1,4 +1,4 @@ -package com.vaadin.tests.data.converter; +package com.vaadin.v7.tests.data.converter; import org.junit.Assert; import org.junit.Test; diff --git a/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToFloatConverterTest.java b/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToFloatConverterTest.java new file mode 100644 index 0000000000..70c408b808 --- /dev/null +++ b/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToFloatConverterTest.java @@ -0,0 +1,29 @@ +package com.vaadin.v7.tests.data.converter; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.v7.data.util.converter.StringToFloatConverter; + +public class StringToFloatConverterTest { + + StringToFloatConverter converter = new StringToFloatConverter(); + + @Test + public void testNullConversion() { + Assert.assertEquals(null, + converter.convertToModel(null, Float.class, null)); + } + + @Test + public void testEmptyStringConversion() { + Assert.assertEquals(null, + converter.convertToModel("", Float.class, null)); + } + + @Test + public void testValueConversion() { + Assert.assertEquals(Float.valueOf(10), + converter.convertToModel("10", Float.class, null)); + } +} diff --git a/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToIntegerConverterTest.java b/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToIntegerConverterTest.java new file mode 100644 index 0000000000..0cc8e646dc --- /dev/null +++ b/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToIntegerConverterTest.java @@ -0,0 +1,48 @@ +package com.vaadin.v7.tests.data.converter; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.v7.data.util.converter.Converter.ConversionException; +import com.vaadin.v7.data.util.converter.StringToIntegerConverter; + +public class StringToIntegerConverterTest { + + StringToIntegerConverter converter = new StringToIntegerConverter(); + + @Test + public void testNullConversion() { + Assert.assertEquals(null, + converter.convertToModel(null, Integer.class, null)); + } + + @Test + public void testEmptyStringConversion() { + Assert.assertEquals(null, + converter.convertToModel("", Integer.class, null)); + } + + @Test + public void testValueOutOfRange() { + Double[] values = new Double[] { Integer.MAX_VALUE * 2.0, + Integer.MIN_VALUE * 2.0, Long.MAX_VALUE * 2.0, + Long.MIN_VALUE * 2.0 }; + + boolean accepted = false; + for (Number value : values) { + try { + converter.convertToModel(String.format("%.0f", value), + Integer.class, null); + accepted = true; + } catch (ConversionException expected) { + } + } + Assert.assertFalse("Accepted value outside range of int", accepted); + } + + @Test + public void testValueConversion() { + Assert.assertEquals(Integer.valueOf(10), + converter.convertToModel("10", Integer.class, null)); + } +} diff --git a/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToLongConverterTest.java b/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToLongConverterTest.java new file mode 100644 index 0000000000..5e5b9f1d86 --- /dev/null +++ b/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToLongConverterTest.java @@ -0,0 +1,78 @@ +package com.vaadin.v7.tests.data.converter; + +import java.util.Locale; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.v7.data.util.converter.Converter; +import com.vaadin.v7.data.util.converter.ReverseConverter; +import com.vaadin.v7.data.util.converter.StringToLongConverter; + +public class StringToLongConverterTest { + + StringToLongConverter converter = new StringToLongConverter(); + Converter<Long, String> reverseConverter = new ReverseConverter<Long, String>( + converter); + + @Test + public void testNullConversion() { + Assert.assertEquals(null, + converter.convertToModel(null, Long.class, null)); + } + + @Test + public void testReverseNullConversion() { + Assert.assertEquals(null, + reverseConverter.convertToModel(null, String.class, null)); + } + + @Test + public void testEmptyStringConversion() { + Assert.assertEquals(null, + converter.convertToModel("", Long.class, null)); + } + + @Test + public void testValueConversion() { + Assert.assertEquals(Long.valueOf(10), + converter.convertToModel("10", Long.class, null)); + } + + @Test + public void testReverseValueConversion() { + Assert.assertEquals( + reverseConverter.convertToModel(10L, String.class, null), "10"); + } + + @Test + public void testExtremeLongValueConversion() { + long l = converter.convertToModel("9223372036854775807", Long.class, + null); + Assert.assertEquals(Long.MAX_VALUE, l); + l = converter.convertToModel("-9223372036854775808", Long.class, null); + Assert.assertEquals(Long.MIN_VALUE, l); + } + + @Test + public void testExtremeReverseLongValueConversion() { + String str = reverseConverter.convertToModel(Long.MAX_VALUE, + String.class, Locale.ENGLISH); + Assert.assertEquals("9,223,372,036,854,775,807", str); + str = reverseConverter.convertToModel(Long.MIN_VALUE, String.class, + Locale.ENGLISH); + Assert.assertEquals("-9,223,372,036,854,775,808", str); + } + + @Test + public void testOutOfBoundsValueConversion() { + // Long.MAX_VALUE+1 is converted to Long.MAX_VALUE + long l = converter.convertToModel("9223372036854775808", Long.class, + null); + Assert.assertEquals(Long.MAX_VALUE, l); + // Long.MIN_VALUE-1 is converted to Long.MIN_VALUE + l = converter.convertToModel("-9223372036854775809", Long.class, null); + Assert.assertEquals(Long.MIN_VALUE, l); + + } +} diff --git a/server/src/test/java/com/vaadin/tests/data/converter/StringToShortConverterTest.java b/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToShortConverterTest.java index ca5c257f80..a2a7332fd1 100644 --- a/server/src/test/java/com/vaadin/tests/data/converter/StringToShortConverterTest.java +++ b/server/src/test/java/com/vaadin/v7/tests/data/converter/StringToShortConverterTest.java @@ -1,4 +1,4 @@ -package com.vaadin.tests.data.converter; +package com.vaadin.v7.tests.data.converter; import org.junit.Assert; import org.junit.Test; |