1 package com.vaadin.tests.server.component.abstractfield;
3 import java.util.Locale;
5 import junit.framework.TestCase;
7 import org.junit.Assert;
10 import com.vaadin.data.util.MethodProperty;
11 import com.vaadin.data.util.ObjectProperty;
12 import com.vaadin.data.util.converter.Converter;
13 import com.vaadin.data.util.converter.Converter.ConversionException;
14 import com.vaadin.data.util.converter.StringToIntegerConverter;
15 import com.vaadin.server.VaadinSession;
16 import com.vaadin.tests.data.bean.Address;
17 import com.vaadin.tests.data.bean.Country;
18 import com.vaadin.tests.data.bean.Person;
19 import com.vaadin.tests.data.bean.Sex;
20 import com.vaadin.tests.util.AlwaysLockedVaadinSession;
21 import com.vaadin.ui.CheckBox;
22 import com.vaadin.ui.TextField;
24 public class AbsFieldValueConversionsTest extends TestCase {
26 Person paulaBean = new Person("Paula", "Brilliant", "paula@brilliant.com",
27 34, Sex.FEMALE, new Address("Paula street 1", 12345, "P-town",
31 * Java uses a non-breaking space (ascii 160) instead of space when
34 private static final char FORMATTED_SPACE = 160;
36 public void testWithoutConversion() {
37 TextField tf = new TextField();
38 tf.setPropertyDataSource(new MethodProperty<String>(paulaBean,
40 assertEquals("Paula", tf.getValue());
41 assertEquals("Paula", tf.getPropertyDataSource().getValue());
43 assertEquals("abc", tf.getValue());
44 assertEquals("abc", tf.getPropertyDataSource().getValue());
45 assertEquals("abc", paulaBean.getFirstName());
48 public void testNonmodifiedBufferedFieldConversion() {
49 VaadinSession.setCurrent(new AlwaysLockedVaadinSession(null));
50 TextField tf = new TextField("salary");
52 tf.setLocale(new Locale("en", "US"));
53 ObjectProperty<Integer> ds = new ObjectProperty<Integer>(123456789);
54 tf.setPropertyDataSource(ds);
55 assertEquals((Integer) 123456789, ds.getValue());
56 assertEquals("123,456,789", tf.getValue());
57 tf.setLocale(new Locale("fi", "FI"));
58 assertEquals((Integer) 123456789, ds.getValue());
59 assertEquals("123" + FORMATTED_SPACE + "456" + FORMATTED_SPACE + "789",
64 public void testModifiedBufferedFieldConversion() {
65 VaadinSession.setCurrent(new AlwaysLockedVaadinSession(null));
66 TextField tf = new TextField("salary");
68 tf.setLocale(new Locale("en", "US"));
69 ObjectProperty<Integer> ds = new ObjectProperty<Integer>(123456789);
70 tf.setPropertyDataSource(ds);
71 assertEquals((Integer) 123456789, ds.getValue());
72 assertEquals("123,456,789", tf.getValue());
73 tf.setValue("123,123");
74 assertEquals((Integer) 123456789, ds.getValue());
75 assertEquals("123,123", tf.getValue());
77 tf.setLocale(new Locale("fi", "FI"));
78 assertEquals((Integer) 123456789, ds.getValue());
79 // Value should not be updated when field is buffered
80 assertEquals("123,123", tf.getValue());
83 public void testStringIdentityConversion() {
84 TextField tf = new TextField();
85 tf.setConverter(new Converter<String, String>() {
88 public String convertToModel(String value,
89 Class<? extends String> targetType, Locale locale) {
94 public String convertToPresentation(String value,
95 Class<? extends String> targetType, Locale locale) {
100 public Class<String> getModelType() {
105 public Class<String> getPresentationType() {
109 tf.setPropertyDataSource(new MethodProperty<String>(paulaBean,
111 assertEquals("Paula", tf.getValue());
112 assertEquals("Paula", tf.getPropertyDataSource().getValue());
114 assertEquals("abc", tf.getValue());
115 assertEquals("abc", tf.getPropertyDataSource().getValue());
116 assertEquals("abc", paulaBean.getFirstName());
119 public void testIntegerStringConversion() {
120 TextField tf = new TextField();
122 tf.setConverter(new StringToIntegerConverter());
123 tf.setPropertyDataSource(new MethodProperty<Integer>(paulaBean, "age"));
124 assertEquals(34, tf.getPropertyDataSource().getValue());
125 assertEquals("34", tf.getValue());
127 assertEquals(12, tf.getPropertyDataSource().getValue());
128 assertEquals("12", tf.getValue());
129 tf.getPropertyDataSource().setValue(42);
130 assertEquals(42, tf.getPropertyDataSource().getValue());
131 assertEquals("42", tf.getValue());
134 public void testChangeReadOnlyFieldLocale() {
135 VaadinSession.setCurrent(new AlwaysLockedVaadinSession(null));
137 TextField tf = new TextField("salary");
138 tf.setLocale(new Locale("en", "US"));
139 ObjectProperty<Integer> ds = new ObjectProperty<Integer>(123456789);
140 ds.setReadOnly(true);
141 tf.setPropertyDataSource(ds);
142 assertEquals((Integer) 123456789, ds.getValue());
143 assertEquals("123,456,789", tf.getValue());
144 tf.setLocale(new Locale("fi", "FI"));
145 assertEquals((Integer) 123456789, ds.getValue());
146 assertEquals("123" + FORMATTED_SPACE + "456" + FORMATTED_SPACE + "789",
150 public void testBooleanNullConversion() {
151 CheckBox cb = new CheckBox();
152 cb.setConverter(new Converter<Boolean, Boolean>() {
155 public Boolean convertToModel(Boolean value,
156 Class<? extends Boolean> targetType, Locale locale) {
157 // value from a CheckBox should never be null as long as it is
158 // not set to null (handled by conversion below).
159 assertNotNull(value);
164 public Boolean convertToPresentation(Boolean value,
165 Class<? extends Boolean> targetType, Locale locale) {
166 // Datamodel -> field
175 public Class<Boolean> getModelType() {
176 return Boolean.class;
180 public Class<Boolean> getPresentationType() {
181 return Boolean.class;
185 MethodProperty<Boolean> property = new MethodProperty<Boolean>(
186 paulaBean, "deceased");
187 cb.setPropertyDataSource(property);
188 assertEquals(Boolean.FALSE, property.getValue());
189 assertEquals(Boolean.FALSE, cb.getValue());
190 Boolean newDmValue = cb.getConverter().convertToPresentation(
191 cb.getValue(), Boolean.class, new Locale("fi", "FI"));
192 assertEquals(Boolean.FALSE, newDmValue);
194 // FIXME: Should be able to set to false here to cause datamodel to be
195 // set to false but the change will not be propagated to the Property
196 // (field value is already false)
199 assertEquals(Boolean.TRUE, cb.getValue());
200 assertEquals(Boolean.TRUE, property.getValue());
203 assertEquals(Boolean.FALSE, cb.getValue());
204 assertEquals(Boolean.FALSE, property.getValue());
208 // Now specific to Integer because StringToNumberConverter has been removed
209 public static class NumberBean {
210 private Integer number;
212 public Integer getNumber() {
216 public void setNumber(Integer number) {
217 this.number = number;
222 public void testNumberDoubleConverterChange() {
223 final VaadinSession a = new AlwaysLockedVaadinSession(null);
224 VaadinSession.setCurrent(a);
225 TextField tf = new TextField() {
227 public VaadinSession getSession() {
231 NumberBean nb = new NumberBean();
234 tf.setPropertyDataSource(new MethodProperty<Number>(nb, "number"));
235 assertEquals(490, tf.getPropertyDataSource().getValue());
236 assertEquals("490", tf.getValue());
238 Converter c1 = tf.getConverter();
240 tf.setPropertyDataSource(new MethodProperty<Number>(nb, "number"));
241 Converter c2 = tf.getConverter();
243 "StringToInteger converter is ok for integer types and should stay even though property is changed",
245 assertEquals(490, tf.getPropertyDataSource().getValue());
246 assertEquals("490", tf.getValue());
251 public void testNullConverter() {
252 TextField tf = new TextField("foo");
253 tf.setConverter(new StringToIntegerConverter());
254 tf.setPropertyDataSource(new ObjectProperty<Integer>(12));
255 tf.setConverter((Converter) null);
257 Object v = tf.getConvertedValue();
258 System.out.println(v);
259 Assert.fail("Trying to convert String -> Integer should fail when there is no converter");
260 } catch (ConversionException e) {
261 // ok, should happen when there is no converter but conversion is