Browse Source

Convert Vaadin 7 converters to Vaadin 8 converters

Change-Id: I347e6cd195b2d77ab2399667a69e4c23b0b54eb7
tags/8.0.0.alpha1
Artur Signell 7 years ago
parent
commit
38b825cf89
44 changed files with 1287 additions and 188 deletions
  1. 1
    1
      compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/textfield/TextFieldWithConverterAndValidatorTest.java
  2. 15
    0
      server/src/main/java/com/vaadin/data/Result.java
  3. 12
    0
      server/src/main/java/com/vaadin/data/SimpleResult.java
  4. 1
    0
      server/src/main/java/com/vaadin/data/util/converter/Converter.java
  5. 50
    0
      server/src/main/java/com/vaadin/data/util/converter/DateToLongConverter.java
  6. 57
    0
      server/src/main/java/com/vaadin/data/util/converter/DateToSqlDateConverter.java
  7. 56
    0
      server/src/main/java/com/vaadin/data/util/converter/StringToBigDecimalConverter.java
  8. 63
    0
      server/src/main/java/com/vaadin/data/util/converter/StringToBigIntegerConverter.java
  9. 120
    0
      server/src/main/java/com/vaadin/data/util/converter/StringToBooleanConverter.java
  10. 87
    0
      server/src/main/java/com/vaadin/data/util/converter/StringToDateConverter.java
  11. 47
    0
      server/src/main/java/com/vaadin/data/util/converter/StringToDoubleConverter.java
  12. 47
    0
      server/src/main/java/com/vaadin/data/util/converter/StringToFloatConverter.java
  13. 1
    1
      server/src/main/java/com/vaadin/data/util/converter/StringToIntegerConverter.java
  14. 61
    0
      server/src/main/java/com/vaadin/data/util/converter/StringToLongConverter.java
  15. 30
    0
      server/src/test/java/com/vaadin/tests/data/converter/AbstractConverterTest.java
  16. 35
    0
      server/src/test/java/com/vaadin/tests/data/converter/ConverterTest.java
  17. 10
    8
      server/src/test/java/com/vaadin/tests/data/converter/DateToLongConverterTest.java
  18. 7
    11
      server/src/test/java/com/vaadin/tests/data/converter/DateToSqlDateConverterTest.java
  19. 10
    13
      server/src/test/java/com/vaadin/tests/data/converter/StringToBigDecimalConverterTest.java
  20. 13
    15
      server/src/test/java/com/vaadin/tests/data/converter/StringToBigIntegerConverterTest.java
  21. 23
    30
      server/src/test/java/com/vaadin/tests/data/converter/StringToBooleanConverterTest.java
  22. 8
    13
      server/src/test/java/com/vaadin/tests/data/converter/StringToDateConverterTest.java
  23. 9
    13
      server/src/test/java/com/vaadin/tests/data/converter/StringToDoubleConverterTest.java
  24. 11
    10
      server/src/test/java/com/vaadin/tests/data/converter/StringToFloatConverterTest.java
  25. 11
    16
      server/src/test/java/com/vaadin/tests/data/converter/StringToIntegerConverterTest.java
  26. 17
    49
      server/src/test/java/com/vaadin/tests/data/converter/StringToLongConverterTest.java
  27. 1
    1
      server/src/test/java/com/vaadin/v7/tests/data/converter/AnyEnumToStringConverterTest.java
  28. 1
    1
      server/src/test/java/com/vaadin/v7/tests/data/converter/ConverterFactoryTest.java
  29. 28
    0
      server/src/test/java/com/vaadin/v7/tests/data/converter/DateToLongConverterTest.java
  30. 28
    0
      server/src/test/java/com/vaadin/v7/tests/data/converter/DateToSqlDateConverterTest.java
  31. 1
    1
      server/src/test/java/com/vaadin/v7/tests/data/converter/DefaultConverterFactoryTest.java
  32. 1
    1
      server/src/test/java/com/vaadin/v7/tests/data/converter/SpecificEnumToStringConverterTest.java
  33. 59
    0
      server/src/test/java/com/vaadin/v7/tests/data/converter/StringToBigDecimalConverterTest.java
  34. 64
    0
      server/src/test/java/com/vaadin/v7/tests/data/converter/StringToBigIntegerConverterTest.java
  35. 82
    0
      server/src/test/java/com/vaadin/v7/tests/data/converter/StringToBooleanConverterTest.java
  36. 1
    1
      server/src/test/java/com/vaadin/v7/tests/data/converter/StringToByteConverterTest.java
  37. 1
    1
      server/src/test/java/com/vaadin/v7/tests/data/converter/StringToCollectionConverterTest.java
  38. 32
    0
      server/src/test/java/com/vaadin/v7/tests/data/converter/StringToDateConverterTest.java
  39. 29
    0
      server/src/test/java/com/vaadin/v7/tests/data/converter/StringToDoubleConverterTest.java
  40. 1
    1
      server/src/test/java/com/vaadin/v7/tests/data/converter/StringToEnumConverterTest.java
  41. 29
    0
      server/src/test/java/com/vaadin/v7/tests/data/converter/StringToFloatConverterTest.java
  42. 48
    0
      server/src/test/java/com/vaadin/v7/tests/data/converter/StringToIntegerConverterTest.java
  43. 78
    0
      server/src/test/java/com/vaadin/v7/tests/data/converter/StringToLongConverterTest.java
  44. 1
    1
      server/src/test/java/com/vaadin/v7/tests/data/converter/StringToShortConverterTest.java

+ 1
- 1
compatibility-server/src/test/java/com/vaadin/v7/tests/server/component/textfield/TextFieldWithConverterAndValidatorTest.java View File

@@ -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 {

+ 15
- 0
server/src/main/java/com/vaadin/data/Result.java View File

@@ -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;
}

+ 12
- 0
server/src/main/java/com/vaadin/data/SimpleResult.java View File

@@ -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;
}
}

}

+ 1
- 0
server/src/main/java/com/vaadin/data/util/converter/Converter.java View File

@@ -170,4 +170,5 @@ public interface Converter<PRESENTATION, MODEL> extends Serializable {
}
};
}

}

+ 50
- 0
server/src/main/java/com/vaadin/data/util/converter/DateToLongConverter.java View File

@@ -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);
}

}

+ 57
- 0
server/src/main/java/com/vaadin/data/util/converter/DateToSqlDateConverter.java View File

@@ -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());
}

}

+ 56
- 0
server/src/main/java/com/vaadin/data/util/converter/StringToBigDecimalConverter.java View File

@@ -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));
}

}

+ 63
- 0
server/src/main/java/com/vaadin/data/util/converter/StringToBigIntegerConverter.java View File

@@ -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);
}

}

+ 120
- 0
server/src/main/java/com/vaadin/data/util/converter/StringToBooleanConverter.java View File

@@ -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;
}

}

+ 87
- 0
server/src/main/java/com/vaadin/data/util/converter/StringToDateConverter.java View File

@@ -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);
}

}

+ 47
- 0
server/src/main/java/com/vaadin/data/util/converter/StringToDoubleConverter.java View File

@@ -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());
}

}

+ 47
- 0
server/src/main/java/com/vaadin/data/util/converter/StringToFloatConverter.java View File

@@ -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());
}

}

+ 1
- 1
server/src/main/java/com/vaadin/data/util/converter/StringToIntegerConverter.java View File

@@ -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();

+ 61
- 0
server/src/main/java/com/vaadin/data/util/converter/StringToLongConverter.java View File

@@ -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());

}

}

+ 30
- 0
server/src/test/java/com/vaadin/tests/data/converter/AbstractConverterTest.java View File

@@ -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)));
}

}

+ 35
- 0
server/src/test/java/com/vaadin/tests/data/converter/ConverterTest.java View File

@@ -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)));
}

}

+ 10
- 8
server/src/test/java/com/vaadin/tests/data/converter/DateToLongConverterTest.java View File

@@ -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));
}
}

+ 7
- 11
server/src/test/java/com/vaadin/tests/data/converter/DateToSqlDateConverterTest.java View File

@@ -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));
}
}

+ 10
- 13
server/src/test/java/com/vaadin/tests/data/converter/StringToBigDecimalConverterTest.java View File

@@ -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);
}

+ 13
- 15
server/src/test/java/com/vaadin/tests/data/converter/StringToBigIntegerConverterTest.java View File

@@ -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",

+ 23
- 30
server/src/test/java/com/vaadin/tests/data/converter/StringToBooleanConverterTest.java View File

@@ -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));
}
}

+ 8
- 13
server/src/test/java/com/vaadin/tests/data/converter/StringToDateConverterTest.java View File

@@ -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));
}
}

+ 9
- 13
server/src/test/java/com/vaadin/tests/data/converter/StringToDoubleConverterTest.java View File

@@ -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);
}
}

+ 11
- 10
server/src/test/java/com/vaadin/tests/data/converter/StringToFloatConverterTest.java View File

@@ -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));
}
}

+ 11
- 16
server/src/test/java/com/vaadin/tests/data/converter/StringToIntegerConverterTest.java View File

@@ -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));
}
}

+ 17
- 49
server/src/test/java/com/vaadin/tests/data/converter/StringToLongConverterTest.java View File

@@ -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);

}
}

server/src/test/java/com/vaadin/tests/data/converter/AnyEnumToStringConverterTest.java → server/src/test/java/com/vaadin/v7/tests/data/converter/AnyEnumToStringConverterTest.java View File

@@ -14,7 +14,7 @@
* the License.
*/

package com.vaadin.tests.data.converter;
package com.vaadin.v7.tests.data.converter;

import java.util.Locale;


server/src/test/java/com/vaadin/tests/data/converter/ConverterFactoryTest.java → server/src/test/java/com/vaadin/v7/tests/data/converter/ConverterFactoryTest.java View File

@@ -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;


+ 28
- 0
server/src/test/java/com/vaadin/v7/tests/data/converter/DateToLongConverterTest.java View File

@@ -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));
}
}

+ 28
- 0
server/src/test/java/com/vaadin/v7/tests/data/converter/DateToSqlDateConverterTest.java View File

@@ -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));
}
}

server/src/test/java/com/vaadin/tests/data/converter/DefaultConverterFactoryTest.java → server/src/test/java/com/vaadin/v7/tests/data/converter/DefaultConverterFactoryTest.java View File

@@ -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;

server/src/test/java/com/vaadin/tests/data/converter/SpecificEnumToStringConverterTest.java → server/src/test/java/com/vaadin/v7/tests/data/converter/SpecificEnumToStringConverterTest.java View File

@@ -14,7 +14,7 @@
* the License.
*/

package com.vaadin.tests.data.converter;
package com.vaadin.v7.tests.data.converter;

import java.util.Locale;


+ 59
- 0
server/src/test/java/com/vaadin/v7/tests/data/converter/StringToBigDecimalConverterTest.java View File

@@ -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);
}
}

+ 64
- 0
server/src/test/java/com/vaadin/v7/tests/data/converter/StringToBigIntegerConverterTest.java View File

@@ -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);
}
}

+ 82
- 0
server/src/test/java/com/vaadin/v7/tests/data/converter/StringToBooleanConverterTest.java View File

@@ -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));
}
}

server/src/test/java/com/vaadin/tests/data/converter/StringToByteConverterTest.java → server/src/test/java/com/vaadin/v7/tests/data/converter/StringToByteConverterTest.java View File

@@ -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;

server/src/test/java/com/vaadin/tests/data/converter/StringToCollectionConverterTest.java → server/src/test/java/com/vaadin/v7/tests/data/converter/StringToCollectionConverterTest.java View File

@@ -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;

+ 32
- 0
server/src/test/java/com/vaadin/v7/tests/data/converter/StringToDateConverterTest.java View File

@@ -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));
}
}

+ 29
- 0
server/src/test/java/com/vaadin/v7/tests/data/converter/StringToDoubleConverterTest.java View File

@@ -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);
}
}

server/src/test/java/com/vaadin/tests/data/converter/StringToEnumConverterTest.java → server/src/test/java/com/vaadin/v7/tests/data/converter/StringToEnumConverterTest.java View File

@@ -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;

+ 29
- 0
server/src/test/java/com/vaadin/v7/tests/data/converter/StringToFloatConverterTest.java View File

@@ -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));
}
}

+ 48
- 0
server/src/test/java/com/vaadin/v7/tests/data/converter/StringToIntegerConverterTest.java View File

@@ -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));
}
}

+ 78
- 0
server/src/test/java/com/vaadin/v7/tests/data/converter/StringToLongConverterTest.java View File

@@ -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);

}
}

server/src/test/java/com/vaadin/tests/data/converter/StringToShortConverterTest.java → server/src/test/java/com/vaadin/v7/tests/data/converter/StringToShortConverterTest.java View File

@@ -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;

Loading…
Cancel
Save