Change-Id: I347e6cd195b2d77ab2399667a69e4c23b0b54eb7tags/8.0.0.alpha1
@@ -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 { |
@@ -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; | |||
} |
@@ -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; | |||
} | |||
} | |||
} |
@@ -170,4 +170,5 @@ public interface Converter<PRESENTATION, MODEL> extends Serializable { | |||
} | |||
}; | |||
} | |||
} |
@@ -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); | |||
} | |||
} |
@@ -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()); | |||
} | |||
} |
@@ -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)); | |||
} | |||
} |
@@ -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); | |||
} | |||
} |
@@ -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; | |||
} | |||
} |
@@ -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); | |||
} | |||
} |
@@ -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()); | |||
} | |||
} |
@@ -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()); | |||
} | |||
} |
@@ -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(); |
@@ -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()); | |||
} | |||
} |
@@ -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))); | |||
} | |||
} |
@@ -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))); | |||
} | |||
} |
@@ -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)); | |||
} | |||
} |
@@ -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)); | |||
} | |||
} |
@@ -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); | |||
} |
@@ -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", |
@@ -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() { | |||
@@ -34,49 +38,38 @@ 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)); | |||
} | |||
} |
@@ -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)); | |||
} | |||
} |
@@ -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); | |||
} | |||
} |
@@ -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)); | |||
} | |||
} |
@@ -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)); | |||
} | |||
} |
@@ -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); | |||
} | |||
} |
@@ -14,7 +14,7 @@ | |||
* the License. | |||
*/ | |||
package com.vaadin.tests.data.converter; | |||
package com.vaadin.v7.tests.data.converter; | |||
import java.util.Locale; | |||
@@ -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; | |||
@@ -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)); | |||
} | |||
} |
@@ -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)); | |||
} | |||
} |
@@ -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; |
@@ -14,7 +14,7 @@ | |||
* the License. | |||
*/ | |||
package com.vaadin.tests.data.converter; | |||
package com.vaadin.v7.tests.data.converter; | |||
import java.util.Locale; | |||
@@ -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); | |||
} | |||
} |
@@ -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); | |||
} | |||
} |
@@ -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)); | |||
} | |||
} |
@@ -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; |
@@ -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; |
@@ -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)); | |||
} | |||
} |
@@ -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); | |||
} | |||
} |
@@ -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; |
@@ -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)); | |||
} | |||
} |
@@ -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)); | |||
} | |||
} |
@@ -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); | |||
} | |||
} |
@@ -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; |