You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StringToIntegerConverterTest.java 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package com.vaadin.tests.data.converter;
  2. import org.junit.Assert;
  3. import org.junit.Test;
  4. import com.vaadin.v7.data.util.converter.LegacyConverter.ConversionException;
  5. import com.vaadin.v7.data.util.converter.LegacyStringToIntegerConverter;
  6. public class StringToIntegerConverterTest {
  7. LegacyStringToIntegerConverter converter = new LegacyStringToIntegerConverter();
  8. @Test
  9. public void testNullConversion() {
  10. Assert.assertEquals(null,
  11. converter.convertToModel(null, Integer.class, null));
  12. }
  13. @Test
  14. public void testEmptyStringConversion() {
  15. Assert.assertEquals(null,
  16. converter.convertToModel("", Integer.class, null));
  17. }
  18. @Test
  19. public void testValueOutOfRange() {
  20. Double[] values = new Double[] { Integer.MAX_VALUE * 2.0,
  21. Integer.MIN_VALUE * 2.0, Long.MAX_VALUE * 2.0,
  22. Long.MIN_VALUE * 2.0 };
  23. boolean accepted = false;
  24. for (Number value : values) {
  25. try {
  26. converter.convertToModel(String.format("%.0f", value),
  27. Integer.class, null);
  28. accepted = true;
  29. } catch (ConversionException expected) {
  30. }
  31. }
  32. Assert.assertFalse("Accepted value outside range of int", accepted);
  33. }
  34. @Test
  35. public void testValueConversion() {
  36. Assert.assertEquals(Integer.valueOf(10),
  37. converter.convertToModel("10", Integer.class, null));
  38. }
  39. }