1 package com.vaadin.tests.server.component.abstractfield;
3 import java.util.Locale;
5 import junit.framework.TestCase;
7 import com.vaadin.Application;
8 import com.vaadin.data.util.MethodProperty;
9 import com.vaadin.data.util.converter.Converter;
10 import com.vaadin.data.util.converter.StringToIntegerConverter;
11 import com.vaadin.tests.data.bean.Address;
12 import com.vaadin.tests.data.bean.Country;
13 import com.vaadin.tests.data.bean.Person;
14 import com.vaadin.tests.data.bean.Sex;
15 import com.vaadin.ui.CheckBox;
16 import com.vaadin.ui.TextField;
18 public class AbstractFieldValueConversions extends TestCase {
20 Person paulaBean = new Person("Paula", "Brilliant", "paula@brilliant.com",
21 34, Sex.FEMALE, new Address("Paula street 1", 12345, "P-town",
24 public void testWithoutConversion() {
25 TextField tf = new TextField();
26 tf.setPropertyDataSource(new MethodProperty<String>(paulaBean,
28 assertEquals("Paula", tf.getValue());
29 assertEquals("Paula", tf.getPropertyDataSource().getValue());
31 assertEquals("abc", tf.getValue());
32 assertEquals("abc", tf.getPropertyDataSource().getValue());
33 assertEquals("abc", paulaBean.getFirstName());
36 public void testStringIdentityConversion() {
37 TextField tf = new TextField();
38 tf.setConverter(new Converter<String, String>() {
40 public String convertToModel(String value, Locale locale) {
44 public String convertToPresentation(String value, Locale locale) {
48 public Class<String> getModelType() {
52 public Class<String> getPresentationType() {
56 tf.setPropertyDataSource(new MethodProperty<String>(paulaBean,
58 assertEquals("Paula", tf.getValue());
59 assertEquals("Paula", tf.getPropertyDataSource().getValue());
61 assertEquals("abc", tf.getValue());
62 assertEquals("abc", tf.getPropertyDataSource().getValue());
63 assertEquals("abc", paulaBean.getFirstName());
66 public void testFailingConversion() {
67 TextField tf = new TextField();
68 tf.setConverter(new Converter<String, Integer>() {
70 public Integer convertToModel(String value, Locale locale) {
71 throw new ConversionException("Failed");
74 public String convertToPresentation(Integer value, Locale locale) {
75 throw new ConversionException("Failed");
78 public Class<Integer> getModelType() {
79 // TODO Auto-generated method stub
83 public Class<String> getPresentationType() {
84 // TODO Auto-generated method stub
90 fail("setValue(Integer) should throw an exception");
91 } catch (Converter.ConversionException e) {
96 public void testIntegerStringConversion() {
97 TextField tf = new TextField();
99 tf.setConverter(new StringToIntegerConverter());
100 tf.setPropertyDataSource(new MethodProperty<Integer>(paulaBean, "age"));
101 assertEquals(34, tf.getPropertyDataSource().getValue());
102 assertEquals("34", tf.getValue());
104 assertEquals(12, tf.getPropertyDataSource().getValue());
105 assertEquals("12", tf.getValue());
106 tf.getPropertyDataSource().setValue(42);
107 assertEquals(42, tf.getPropertyDataSource().getValue());
108 assertEquals("42", tf.getValue());
111 public void testBooleanNullConversion() {
112 CheckBox cb = new CheckBox();
113 cb.setConverter(new Converter<Boolean, Boolean>() {
115 public Boolean convertToModel(Boolean value, Locale locale) {
116 // value from a CheckBox should never be null as long as it is
117 // not set to null (handled by conversion below).
118 assertNotNull(value);
122 public Boolean convertToPresentation(Boolean value, Locale locale) {
123 // Datamodel -> field
131 public Class<Boolean> getModelType() {
132 return Boolean.class;
135 public Class<Boolean> getPresentationType() {
136 return Boolean.class;
140 MethodProperty<Boolean> property = new MethodProperty<Boolean>(
141 paulaBean, "deceased");
142 cb.setPropertyDataSource(property);
143 assertEquals(Boolean.FALSE, property.getValue());
144 assertEquals(Boolean.FALSE, cb.getValue());
145 Boolean newDmValue = cb.getConverter().convertToPresentation(
146 cb.getValue(), new Locale("fi", "FI"));
147 assertEquals(Boolean.FALSE, newDmValue);
149 // FIXME: Should be able to set to false here to cause datamodel to be
150 // set to false but the change will not be propagated to the Property
151 // (field value is already false)
154 assertEquals(Boolean.TRUE, cb.getValue());
155 assertEquals(Boolean.TRUE, property.getValue());
158 assertEquals(Boolean.FALSE, cb.getValue());
159 assertEquals(Boolean.FALSE, property.getValue());
163 public static class NumberBean {
164 private Number number;
166 public Number getNumber() {
170 public void setNumber(Number number) {
171 this.number = number;
176 public void testNumberDoubleConverterChange() {
177 final Application a = new Application();
178 Application.setCurrentApplication(a);
179 TextField tf = new TextField() {
181 public Application getApplication() {
185 NumberBean nb = new NumberBean();
188 tf.setPropertyDataSource(new MethodProperty<Number>(nb, "number"));
189 assertEquals(490, tf.getPropertyDataSource().getValue());
190 assertEquals("490", tf.getValue());
192 Converter c1 = tf.getConverter();
194 tf.setPropertyDataSource(new MethodProperty<Number>(nb, "number"));
195 Converter c2 = tf.getConverter();
197 "StringToNumber converter is ok for integer types and should stay even though property is changed",
199 assertEquals(490, tf.getPropertyDataSource().getValue());
200 assertEquals("490", tf.getValue());