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

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