Change-Id: I347e6cd195b2d77ab2399667a69e4c23b0b54eb7tags/8.0.0.alpha1
import org.junit.Before; | import org.junit.Before; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import com.vaadin.tests.data.converter.ConverterFactoryTest.ConvertTo42; | |||||
import com.vaadin.v7.data.util.ObjectProperty; | import com.vaadin.v7.data.util.ObjectProperty; | ||||
import com.vaadin.v7.data.validator.RangeValidator; | import com.vaadin.v7.data.validator.RangeValidator; | ||||
import com.vaadin.v7.tests.data.converter.ConverterFactoryTest.ConvertTo42; | |||||
import com.vaadin.v7.ui.TextField; | import com.vaadin.v7.ui.TextField; | ||||
public class TextFieldWithConverterAndValidatorTest { | public class TextFieldWithConverterAndValidatorTest { |
*/ | */ | ||||
public Optional<String> getMessage(); | 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; | |||||
} | } |
} | } | ||||
} | } | ||||
@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; | |||||
} | |||||
} | |||||
} | } |
} | } | ||||
}; | }; | ||||
} | } | ||||
} | } |
/* | |||||
* 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); | |||||
} | |||||
} |
/* | |||||
* 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()); | |||||
} | |||||
} |
/* | |||||
* 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)); | |||||
} | |||||
} |
/* | |||||
* 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); | |||||
} | |||||
} |
/* | |||||
* 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; | |||||
} | |||||
} |
/* | |||||
* 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); | |||||
} | |||||
} |
/* | |||||
* 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()); | |||||
} | |||||
} |
/* | |||||
* 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()); | |||||
} | |||||
} |
Number n = convertToNumber(value, locale); | Number n = convertToNumber(value, locale); | ||||
if (n == null) { | if (n == null) { | ||||
return null; | |||||
return Result.ok(null); | |||||
} | } | ||||
int intValue = n.intValue(); | int intValue = n.intValue(); |
/* | |||||
* 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()); | |||||
} | |||||
} |
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))); | |||||
} | |||||
} |
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))); | |||||
} | |||||
} |
import java.util.Date; | import java.util.Date; | ||||
import org.junit.Assert; | |||||
import org.junit.Test; | 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 | @Test | ||||
public void testNullConversion() { | public void testNullConversion() { | ||||
Assert.assertEquals(null, | |||||
converter.convertToModel(null, Long.class, null)); | |||||
assertResult(null, getConverter().convertToModel(null, null)); | |||||
} | } | ||||
@Test | @Test | ||||
public void testValueConversion() { | public void testValueConversion() { | ||||
Date d = new Date(100, 0, 1); | Date d = new Date(100, 0, 1); | ||||
Assert.assertEquals( | |||||
assertResult( | |||||
Long.valueOf(946677600000l | Long.valueOf(946677600000l | ||||
+ (d.getTimezoneOffset() + 120) * 60 * 1000L), | + (d.getTimezoneOffset() + 120) * 60 * 1000L), | ||||
converter.convertToModel(d, Long.class, null)); | |||||
getConverter().convertToModel(d, null)); | |||||
} | } | ||||
} | } |
import java.util.Date; | import java.util.Date; | ||||
import java.util.Locale; | import java.util.Locale; | ||||
import org.junit.Assert; | |||||
import org.junit.Test; | 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 | @Test | ||||
public void testValueConversion() { | public void testValueConversion() { | ||||
Date testDate = new Date(100, 0, 1); | Date testDate = new Date(100, 0, 1); | ||||
long time = testDate.getTime(); | 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)); | |||||
} | } | ||||
} | } |
import org.junit.Assert; | import org.junit.Assert; | ||||
import org.junit.Test; | 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 | @Test | ||||
public void testEmptyStringConversion() { | public void testEmptyStringConversion() { | ||||
Assert.assertEquals(null, | |||||
converter.convertToModel("", BigDecimal.class, null)); | |||||
assertResult(null, getConverter().convertToModel("", null)); | |||||
} | } | ||||
@Test | @Test | ||||
public void testValueParsing() { | public void testValueParsing() { | ||||
BigDecimal converted = converter.convertToModel("10", BigDecimal.class, | |||||
Result<BigDecimal> converted = getConverter().convertToModel("10", | |||||
null); | null); | ||||
BigDecimal expected = new BigDecimal(10); | BigDecimal expected = new BigDecimal(10); | ||||
Assert.assertEquals(expected, converted); | |||||
assertResult(expected, converted); | |||||
} | } | ||||
@Test | @Test | ||||
BigDecimal bd = new BigDecimal(12.5); | BigDecimal bd = new BigDecimal(12.5); | ||||
String expected = "12,5"; | String expected = "12,5"; | ||||
String converted = converter.convertToPresentation(bd, String.class, | |||||
String converted = getConverter().convertToPresentation(bd, | |||||
Locale.GERMAN); | Locale.GERMAN); | ||||
Assert.assertEquals(expected, converted); | Assert.assertEquals(expected, converted); | ||||
} | } |
import java.math.BigInteger; | import java.math.BigInteger; | ||||
import java.util.Locale; | import java.util.Locale; | ||||
import org.junit.Assert; | |||||
import org.junit.Test; | 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 | @Test | ||||
public void testEmptyStringConversion() { | 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 | @Test | ||||
public void testValueParsing() { | public void testValueParsing() { | ||||
String bigInt = "1180591620717411303424"; // 2^70 > 2^63 - 1 | 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); | 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); | expected, converted); | ||||
} | } | ||||
BigInteger bd = new BigInteger("1000"); | BigInteger bd = new BigInteger("1000"); | ||||
String expected = "1.000"; | String expected = "1.000"; | ||||
String converted = converter.convertToPresentation(bd, String.class, | |||||
String converted = getConverter().convertToPresentation(bd, | |||||
Locale.GERMAN); | Locale.GERMAN); | ||||
Assert.assertEquals( | Assert.assertEquals( | ||||
"Value with specific locale was converted incorrectly", | "Value with specific locale was converted incorrectly", |
import org.junit.Assert; | import org.junit.Assert; | ||||
import org.junit.Test; | 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( | StringToBooleanConverter yesNoConverter = new StringToBooleanConverter( | ||||
"yes", "no"); | "yes", "no"); | ||||
StringToBooleanConverter localeConverter = new StringToBooleanConverter() { | StringToBooleanConverter localeConverter = new StringToBooleanConverter() { | ||||
} | } | ||||
}; | }; | ||||
@Test | |||||
public void testNullConversion() { | |||||
Assert.assertEquals(null, | |||||
converter.convertToModel(null, Boolean.class, null)); | |||||
} | |||||
@Test | @Test | ||||
public void testEmptyStringConversion() { | public void testEmptyStringConversion() { | ||||
Assert.assertEquals(null, | |||||
converter.convertToModel("", Boolean.class, null)); | |||||
assertResult(null, getConverter().convertToModel("", null)); | |||||
} | } | ||||
@Test | @Test | ||||
public void testValueConversion() { | 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 | @Test | ||||
public void testYesNoValueConversion() { | 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", | 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 | @Test | ||||
public void testLocale() { | 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)); | |||||
} | } | ||||
} | } |
import java.util.Date; | import java.util.Date; | ||||
import java.util.Locale; | import java.util.Locale; | ||||
import org.junit.Assert; | |||||
import org.junit.Test; | 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 | @Test | ||||
public void testEmptyStringConversion() { | public void testEmptyStringConversion() { | ||||
Assert.assertEquals(null, | |||||
converter.convertToModel("", Date.class, null)); | |||||
assertResult(null, getConverter().convertToModel("", null)); | |||||
} | } | ||||
@Test | @Test | ||||
public void testValueConversion() { | 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)); | |||||
} | } | ||||
} | } |
package com.vaadin.tests.data.converter; | package com.vaadin.tests.data.converter; | ||||
import org.junit.Assert; | |||||
import org.junit.Test; | 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 | @Test | ||||
public void testEmptyStringConversion() { | public void testEmptyStringConversion() { | ||||
Assert.assertEquals(null, | |||||
converter.convertToModel("", Double.class, null)); | |||||
assertResult(null, getConverter().convertToModel("", null)); | |||||
} | } | ||||
@Test | @Test | ||||
public void testValueConversion() { | 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); | |||||
} | } | ||||
} | } |
package com.vaadin.tests.data.converter; | package com.vaadin.tests.data.converter; | ||||
import org.junit.Assert; | |||||
import org.junit.Test; | 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 | @Test | ||||
public void testNullConversion() { | public void testNullConversion() { | ||||
Assert.assertEquals(null, | |||||
converter.convertToModel(null, Float.class, null)); | |||||
assertResult(null, getConverter().convertToModel(null, null)); | |||||
} | } | ||||
@Test | @Test | ||||
public void testEmptyStringConversion() { | public void testEmptyStringConversion() { | ||||
Assert.assertEquals(null, | |||||
converter.convertToModel("", Float.class, null)); | |||||
assertResult(null, getConverter().convertToModel("", null)); | |||||
} | } | ||||
@Test | @Test | ||||
public void testValueConversion() { | public void testValueConversion() { | ||||
Assert.assertEquals(Float.valueOf(10), | |||||
converter.convertToModel("10", Float.class, null)); | |||||
assertResult(Float.valueOf(10), | |||||
getConverter().convertToModel("10", null)); | |||||
} | } | ||||
} | } |
import org.junit.Assert; | import org.junit.Assert; | ||||
import org.junit.Test; | 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 | @Test | ||||
public void testEmptyStringConversion() { | public void testEmptyStringConversion() { | ||||
Assert.assertEquals(null, | |||||
converter.convertToModel("", Integer.class, null)); | |||||
assertResult(null, getConverter().convertToModel("", null)); | |||||
} | } | ||||
@Test | @Test | ||||
boolean accepted = false; | boolean accepted = false; | ||||
for (Number value : values) { | for (Number value : values) { | ||||
try { | try { | ||||
converter.convertToModel(String.format("%.0f", value), | |||||
Integer.class, null); | |||||
getConverter().convertToModel(String.format("%.0f", value), | |||||
null); | |||||
} catch (Exception e) { | |||||
accepted = true; | accepted = true; | ||||
} catch (ConversionException expected) { | |||||
} | } | ||||
} | } | ||||
Assert.assertFalse("Accepted value outside range of int", accepted); | Assert.assertFalse("Accepted value outside range of int", accepted); | ||||
@Test | @Test | ||||
public void testValueConversion() { | public void testValueConversion() { | ||||
Assert.assertEquals(Integer.valueOf(10), | |||||
converter.convertToModel("10", Integer.class, null)); | |||||
assertResult(Integer.valueOf(10), | |||||
getConverter().convertToModel("10", null)); | |||||
} | } | ||||
} | } |
package com.vaadin.tests.data.converter; | package com.vaadin.tests.data.converter; | ||||
import java.util.Locale; | |||||
import org.junit.Assert; | |||||
import org.junit.Test; | 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 | @Test | ||||
public void testEmptyStringConversion() { | public void testEmptyStringConversion() { | ||||
Assert.assertEquals(null, | |||||
converter.convertToModel("", Long.class, null)); | |||||
assertResult(null, getConverter().convertToModel("", null)); | |||||
} | } | ||||
@Test | @Test | ||||
public void testValueConversion() { | 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 | @Test | ||||
public void testExtremeLongValueConversion() { | public void testExtremeLongValueConversion() { | ||||
long l = converter.convertToModel("9223372036854775807", Long.class, | |||||
Result<Long> l = getConverter().convertToModel("9223372036854775807", | |||||
null); | 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 | @Test | ||||
public void testOutOfBoundsValueConversion() { | public void testOutOfBoundsValueConversion() { | ||||
// Long.MAX_VALUE+1 is converted to Long.MAX_VALUE | // Long.MAX_VALUE+1 is converted to Long.MAX_VALUE | ||||
long l = converter.convertToModel("9223372036854775808", Long.class, | |||||
Result<Long> l = getConverter().convertToModel("9223372036854775808", | |||||
null); | null); | ||||
Assert.assertEquals(Long.MAX_VALUE, l); | |||||
assertResult(Long.MAX_VALUE, l); | |||||
// Long.MIN_VALUE-1 is converted to Long.MIN_VALUE | // 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); | |||||
} | } | ||||
} | } |
* the License. | * the License. | ||||
*/ | */ | ||||
package com.vaadin.tests.data.converter; | |||||
package com.vaadin.v7.tests.data.converter; | |||||
import java.util.Locale; | import java.util.Locale; | ||||
* License for the specific language governing permissions and limitations under | * License for the specific language governing permissions and limitations under | ||||
* the License. | * the License. | ||||
*/ | */ | ||||
package com.vaadin.tests.data.converter; | |||||
package com.vaadin.v7.tests.data.converter; | |||||
import java.util.Locale; | import java.util.Locale; | ||||
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)); | |||||
} | |||||
} |
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)); | |||||
} | |||||
} |
* License for the specific language governing permissions and limitations under | * License for the specific language governing permissions and limitations under | ||||
* the License. | * the License. | ||||
*/ | */ | ||||
package com.vaadin.tests.data.converter; | |||||
package com.vaadin.v7.tests.data.converter; | |||||
import java.math.BigDecimal; | import java.math.BigDecimal; | ||||
import java.math.BigInteger; | import java.math.BigInteger; |
* the License. | * the License. | ||||
*/ | */ | ||||
package com.vaadin.tests.data.converter; | |||||
package com.vaadin.v7.tests.data.converter; | |||||
import java.util.Locale; | import java.util.Locale; | ||||
/* | |||||
* 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); | |||||
} | |||||
} |
/* | |||||
* 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); | |||||
} | |||||
} |
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)); | |||||
} | |||||
} |
package com.vaadin.tests.data.converter; | |||||
package com.vaadin.v7.tests.data.converter; | |||||
import org.junit.Assert; | import org.junit.Assert; | ||||
import org.junit.Test; | import org.junit.Test; |
* License for the specific language governing permissions and limitations under | * License for the specific language governing permissions and limitations under | ||||
* the License. | * the License. | ||||
*/ | */ | ||||
package com.vaadin.tests.data.converter; | |||||
package com.vaadin.v7.tests.data.converter; | |||||
import java.util.ArrayList; | import java.util.ArrayList; | ||||
import java.util.Arrays; | import java.util.Arrays; |
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)); | |||||
} | |||||
} |
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); | |||||
} | |||||
} |
package com.vaadin.tests.data.converter; | |||||
package com.vaadin.v7.tests.data.converter; | |||||
import org.junit.Assert; | import org.junit.Assert; | ||||
import org.junit.Test; | import org.junit.Test; |
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)); | |||||
} | |||||
} |
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)); | |||||
} | |||||
} |
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); | |||||
} | |||||
} |
package com.vaadin.tests.data.converter; | |||||
package com.vaadin.v7.tests.data.converter; | |||||
import org.junit.Assert; | import org.junit.Assert; | ||||
import org.junit.Test; | import org.junit.Test; |