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.4KB

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