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.

AbsFieldValueConversionsTest.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package com.vaadin.tests.server.component.abstractfield;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.assertTrue;
  4. import java.util.Locale;
  5. import org.junit.Assert;
  6. import org.junit.Test;
  7. import com.vaadin.data.util.MethodProperty;
  8. import com.vaadin.data.util.ObjectProperty;
  9. import com.vaadin.legacy.data.util.converter.LegacyConverter;
  10. import com.vaadin.legacy.data.util.converter.LegacyConverter.ConversionException;
  11. import com.vaadin.legacy.data.util.converter.LegacyStringToIntegerConverter;
  12. import com.vaadin.legacy.ui.LegacyTextField;
  13. import com.vaadin.server.VaadinSession;
  14. import com.vaadin.tests.data.bean.Address;
  15. import com.vaadin.tests.data.bean.Country;
  16. import com.vaadin.tests.data.bean.Person;
  17. import com.vaadin.tests.data.bean.Sex;
  18. import com.vaadin.tests.util.AlwaysLockedVaadinSession;
  19. public class AbsFieldValueConversionsTest {
  20. Person paulaBean = new Person("Paula", "Brilliant", "paula@brilliant.com",
  21. 34, Sex.FEMALE,
  22. new Address("Paula street 1", 12345, "P-town", Country.FINLAND));
  23. /**
  24. * Java uses a non-breaking space (ascii 160) instead of space when
  25. * formatting
  26. */
  27. private static final char FORMATTED_SPACE = 160;
  28. @Test
  29. public void testWithoutConversion() {
  30. LegacyTextField tf = new LegacyTextField();
  31. tf.setPropertyDataSource(
  32. new MethodProperty<String>(paulaBean, "firstName"));
  33. assertEquals("Paula", tf.getValue());
  34. assertEquals("Paula", tf.getPropertyDataSource().getValue());
  35. tf.setValue("abc");
  36. assertEquals("abc", tf.getValue());
  37. assertEquals("abc", tf.getPropertyDataSource().getValue());
  38. assertEquals("abc", paulaBean.getFirstName());
  39. }
  40. @Test
  41. public void testNonmodifiedBufferedFieldConversion() {
  42. VaadinSession.setCurrent(new AlwaysLockedVaadinSession(null));
  43. LegacyTextField tf = new LegacyTextField("salary");
  44. tf.setBuffered(true);
  45. tf.setLocale(new Locale("en", "US"));
  46. ObjectProperty<Integer> ds = new ObjectProperty<Integer>(123456789);
  47. tf.setPropertyDataSource(ds);
  48. assertEquals((Integer) 123456789, ds.getValue());
  49. assertEquals("123,456,789", tf.getValue());
  50. tf.setLocale(new Locale("fi", "FI"));
  51. assertEquals((Integer) 123456789, ds.getValue());
  52. assertEquals("123" + FORMATTED_SPACE + "456" + FORMATTED_SPACE + "789",
  53. tf.getValue());
  54. }
  55. @Test
  56. public void testModifiedBufferedFieldConversion() {
  57. VaadinSession.setCurrent(new AlwaysLockedVaadinSession(null));
  58. LegacyTextField tf = new LegacyTextField("salary");
  59. tf.setBuffered(true);
  60. tf.setLocale(new Locale("en", "US"));
  61. ObjectProperty<Integer> ds = new ObjectProperty<Integer>(123456789);
  62. tf.setPropertyDataSource(ds);
  63. assertEquals((Integer) 123456789, ds.getValue());
  64. assertEquals("123,456,789", tf.getValue());
  65. tf.setValue("123,123");
  66. assertEquals((Integer) 123456789, ds.getValue());
  67. assertEquals("123,123", tf.getValue());
  68. tf.setLocale(new Locale("fi", "FI"));
  69. assertEquals((Integer) 123456789, ds.getValue());
  70. // Value should not be updated when field is buffered
  71. assertEquals("123,123", tf.getValue());
  72. }
  73. @Test
  74. public void testStringIdentityConversion() {
  75. LegacyTextField tf = new LegacyTextField();
  76. tf.setConverter(new LegacyConverter<String, String>() {
  77. @Override
  78. public String convertToModel(String value,
  79. Class<? extends String> targetType, Locale locale) {
  80. return value;
  81. }
  82. @Override
  83. public String convertToPresentation(String value,
  84. Class<? extends String> targetType, Locale locale) {
  85. return value;
  86. }
  87. @Override
  88. public Class<String> getModelType() {
  89. return String.class;
  90. }
  91. @Override
  92. public Class<String> getPresentationType() {
  93. return String.class;
  94. }
  95. });
  96. tf.setPropertyDataSource(
  97. new MethodProperty<String>(paulaBean, "firstName"));
  98. assertEquals("Paula", tf.getValue());
  99. assertEquals("Paula", tf.getPropertyDataSource().getValue());
  100. tf.setValue("abc");
  101. assertEquals("abc", tf.getValue());
  102. assertEquals("abc", tf.getPropertyDataSource().getValue());
  103. assertEquals("abc", paulaBean.getFirstName());
  104. }
  105. @Test
  106. public void testIntegerStringConversion() {
  107. LegacyTextField tf = new LegacyTextField();
  108. tf.setConverter(new LegacyStringToIntegerConverter());
  109. tf.setPropertyDataSource(new MethodProperty<Integer>(paulaBean, "age"));
  110. assertEquals(34, tf.getPropertyDataSource().getValue());
  111. assertEquals("34", tf.getValue());
  112. tf.setValue("12");
  113. assertEquals(12, tf.getPropertyDataSource().getValue());
  114. assertEquals("12", tf.getValue());
  115. tf.getPropertyDataSource().setValue(42);
  116. assertEquals(42, tf.getPropertyDataSource().getValue());
  117. assertEquals("42", tf.getValue());
  118. }
  119. @Test
  120. public void testChangeReadOnlyFieldLocale() {
  121. VaadinSession.setCurrent(new AlwaysLockedVaadinSession(null));
  122. LegacyTextField tf = new LegacyTextField("salary");
  123. tf.setLocale(new Locale("en", "US"));
  124. ObjectProperty<Integer> ds = new ObjectProperty<Integer>(123456789);
  125. ds.setReadOnly(true);
  126. tf.setPropertyDataSource(ds);
  127. assertEquals((Integer) 123456789, ds.getValue());
  128. assertEquals("123,456,789", tf.getValue());
  129. tf.setLocale(new Locale("fi", "FI"));
  130. assertEquals((Integer) 123456789, ds.getValue());
  131. assertEquals("123" + FORMATTED_SPACE + "456" + FORMATTED_SPACE + "789",
  132. tf.getValue());
  133. }
  134. // Now specific to Integer because StringToNumberConverter has been removed
  135. public static class NumberBean {
  136. private Integer number;
  137. public Integer getNumber() {
  138. return number;
  139. }
  140. public void setNumber(Integer number) {
  141. this.number = number;
  142. }
  143. }
  144. @Test
  145. public void testNumberDoubleConverterChange() {
  146. final VaadinSession a = new AlwaysLockedVaadinSession(null);
  147. VaadinSession.setCurrent(a);
  148. LegacyTextField tf = new LegacyTextField() {
  149. @Override
  150. public VaadinSession getSession() {
  151. return a;
  152. }
  153. };
  154. NumberBean nb = new NumberBean();
  155. nb.setNumber(490);
  156. tf.setPropertyDataSource(new MethodProperty<Number>(nb, "number"));
  157. assertEquals(490, tf.getPropertyDataSource().getValue());
  158. assertEquals("490", tf.getValue());
  159. LegacyConverter c1 = tf.getConverter();
  160. tf.setPropertyDataSource(new MethodProperty<Number>(nb, "number"));
  161. LegacyConverter c2 = tf.getConverter();
  162. assertTrue(
  163. "StringToInteger converter is ok for integer types and should stay even though property is changed",
  164. c1 == c2);
  165. assertEquals(490, tf.getPropertyDataSource().getValue());
  166. assertEquals("490", tf.getValue());
  167. }
  168. @Test
  169. public void testNullConverter() {
  170. LegacyTextField tf = new LegacyTextField("foo");
  171. tf.setConverter(new LegacyStringToIntegerConverter());
  172. tf.setPropertyDataSource(new ObjectProperty<Integer>(12));
  173. tf.setConverter((LegacyConverter) null);
  174. try {
  175. Object v = tf.getConvertedValue();
  176. System.out.println(v);
  177. Assert.fail(
  178. "Trying to convert String -> Integer should fail when there is no converter");
  179. } catch (ConversionException e) {
  180. // ok, should happen when there is no converter but conversion is
  181. // needed
  182. }
  183. }
  184. }