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.

StringToByteConverterTest.java 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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;
  5. import com.vaadin.v7.data.util.converter.LegacyConverter.ConversionException;
  6. import com.vaadin.v7.data.util.converter.LegacyReverseConverter;
  7. import com.vaadin.v7.data.util.converter.LegacyStringToByteConverter;
  8. public class StringToByteConverterTest {
  9. LegacyStringToByteConverter converter = new LegacyStringToByteConverter();
  10. LegacyConverter<Byte, String> reverseConverter = new LegacyReverseConverter<Byte, String>(
  11. converter);
  12. @Test
  13. public void testNullConversion() {
  14. Assert.assertEquals("Null value was converted incorrectly", null,
  15. converter.convertToModel(null, Byte.class, null));
  16. }
  17. @Test
  18. public void testReverseNullConversion() {
  19. Assert.assertEquals("Null value reversely was converted incorrectly",
  20. null,
  21. reverseConverter.convertToModel(null, String.class, null));
  22. }
  23. @Test
  24. public void testEmptyStringConversion() {
  25. Assert.assertEquals("Empty value was converted incorrectly", null,
  26. converter.convertToModel("", Byte.class, null));
  27. }
  28. @Test
  29. public void testValueConversion() {
  30. Assert.assertEquals("Byte value was converted incorrectly",
  31. Byte.valueOf((byte) 10),
  32. converter.convertToModel("10", Byte.class, null));
  33. }
  34. @Test
  35. public void testReverseValueConversion() {
  36. Assert.assertEquals("Byte value reversely was converted incorrectly",
  37. reverseConverter.convertToModel((byte) 10, String.class, null),
  38. "10");
  39. }
  40. @Test
  41. public void testExtremeByteValueConversion() {
  42. byte b = converter.convertToModel("127", Byte.class, null);
  43. Assert.assertEquals(Byte.MAX_VALUE, b);
  44. b = converter.convertToModel("-128", Byte.class, null);
  45. Assert.assertEquals("Min byte value was converted incorrectly",
  46. Byte.MIN_VALUE, b);
  47. }
  48. @Test
  49. public void testValueOutOfRange() {
  50. Double[] values = new Double[] { Byte.MAX_VALUE * 2.0,
  51. Byte.MIN_VALUE * 2.0, Long.MAX_VALUE * 2.0,
  52. Long.MIN_VALUE * 2.0 };
  53. boolean accepted = false;
  54. for (Number value : values) {
  55. try {
  56. converter.convertToModel(String.format("%.0f", value),
  57. Byte.class, null);
  58. accepted = true;
  59. } catch (ConversionException expected) {
  60. }
  61. }
  62. Assert.assertFalse("Accepted value outside range of int", accepted);
  63. }
  64. }