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.

AbsFieldValueConversionErrorTest.java 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package com.vaadin.v7.tests.server.component.abstractfield;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.fail;
  4. import org.junit.Test;
  5. import com.vaadin.tests.data.bean.Address;
  6. import com.vaadin.tests.data.bean.Country;
  7. import com.vaadin.tests.data.bean.Person;
  8. import com.vaadin.tests.data.bean.Sex;
  9. import com.vaadin.v7.data.Validator.InvalidValueException;
  10. import com.vaadin.v7.data.util.MethodProperty;
  11. import com.vaadin.v7.data.util.converter.Converter.ConversionException;
  12. import com.vaadin.v7.data.util.converter.StringToIntegerConverter;
  13. import com.vaadin.v7.ui.TextField;
  14. public class AbsFieldValueConversionErrorTest {
  15. Person paulaBean = new Person("Paula", "Brilliant", "paula@brilliant.com",
  16. 34, Sex.FEMALE,
  17. new Address("Paula street 1", 12345, "P-town", Country.FINLAND));
  18. @Test
  19. public void testValidateConversionErrorParameters() {
  20. TextField tf = new TextField();
  21. tf.setConverter(new StringToIntegerConverter());
  22. tf.setPropertyDataSource(new MethodProperty<String>(paulaBean, "age"));
  23. tf.setConversionError("(Type: {0}) Converter exception message: {1}");
  24. tf.setValue("abc");
  25. try {
  26. tf.validate();
  27. fail();
  28. } catch (InvalidValueException e) {
  29. assertEquals(
  30. "(Type: Integer) Converter exception message: Could not convert 'abc' to java.lang.Integer",
  31. e.getMessage());
  32. }
  33. }
  34. @Test
  35. public void testConvertToModelConversionErrorParameters() {
  36. TextField tf = new TextField();
  37. tf.setConverter(new StringToIntegerConverter());
  38. tf.setPropertyDataSource(new MethodProperty<String>(paulaBean, "age"));
  39. tf.setConversionError("(Type: {0}) Converter exception message: {1}");
  40. tf.setValue("abc");
  41. try {
  42. tf.getConvertedValue();
  43. fail();
  44. } catch (ConversionException e) {
  45. assertEquals(
  46. "(Type: Integer) Converter exception message: Could not convert 'abc' to java.lang.Integer",
  47. e.getMessage());
  48. }
  49. }
  50. @Test
  51. public void testNullConversionMessages() {
  52. TextField tf = new TextField();
  53. tf.setConverter(new StringToIntegerConverter());
  54. tf.setPropertyDataSource(new MethodProperty<String>(paulaBean, "age"));
  55. tf.setConversionError(null);
  56. tf.setValue("abc");
  57. try {
  58. tf.validate();
  59. fail();
  60. } catch (InvalidValueException e) {
  61. assertEquals(null, e.getMessage());
  62. }
  63. }
  64. @Test
  65. public void testDefaultConversionErrorMessage() {
  66. TextField tf = new TextField();
  67. tf.setConverter(new StringToIntegerConverter());
  68. tf.setPropertyDataSource(new MethodProperty<String>(paulaBean, "age"));
  69. tf.setValue("abc");
  70. try {
  71. tf.validate();
  72. fail();
  73. } catch (InvalidValueException e) {
  74. assertEquals("Could not convert value to Integer", e.getMessage());
  75. }
  76. }
  77. }