1 package com.vaadin.tests.server.component.abstractfield;
3 import java.util.Locale;
5 import junit.framework.TestCase;
7 import com.vaadin.data.util.MethodProperty;
8 import com.vaadin.data.util.converter.Converter;
9 import com.vaadin.data.util.converter.StringToIntegerConverter;
10 import com.vaadin.tests.data.bean.Address;
11 import com.vaadin.tests.data.bean.Country;
12 import com.vaadin.tests.data.bean.Person;
13 import com.vaadin.tests.data.bean.Sex;
14 import com.vaadin.ui.CheckBox;
15 import com.vaadin.ui.TextField;
17 public class AbstractFieldValueConversions extends TestCase {
19 Person paulaBean = new Person("Paula", "Brilliant", "paula@brilliant.com",
20 34, Sex.FEMALE, new Address("Paula street 1", 12345, "P-town",
23 public void testWithoutConversion() {
24 TextField tf = new TextField();
25 tf.setPropertyDataSource(new MethodProperty<String>(paulaBean,
27 assertEquals("Paula", tf.getValue());
28 assertEquals("Paula", tf.getPropertyDataSource().getValue());
30 assertEquals("abc", tf.getValue());
31 assertEquals("abc", tf.getPropertyDataSource().getValue());
32 assertEquals("abc", paulaBean.getFirstName());
35 public void testStringIdentityConversion() {
36 TextField tf = new TextField();
37 tf.setConverter(new Converter<String, String>() {
39 public String convertToModel(String value, Locale locale) {
43 public String convertToPresentation(String value, Locale locale) {
47 public Class<String> getModelType() {
51 public Class<String> getPresentationType() {
55 tf.setPropertyDataSource(new MethodProperty<String>(paulaBean,
57 assertEquals("Paula", tf.getValue());
58 assertEquals("Paula", tf.getPropertyDataSource().getValue());
60 assertEquals("abc", tf.getValue());
61 assertEquals("abc", tf.getPropertyDataSource().getValue());
62 assertEquals("abc", paulaBean.getFirstName());
65 public void testFailingConversion() {
66 TextField tf = new TextField();
67 tf.setConverter(new Converter<String, Integer>() {
69 public Integer convertToModel(String value, Locale locale) {
70 throw new ConversionException("Failed");
73 public String convertToPresentation(Integer value, Locale locale) {
74 throw new ConversionException("Failed");
77 public Class<Integer> getModelType() {
78 // TODO Auto-generated method stub
82 public Class<String> getPresentationType() {
83 // TODO Auto-generated method stub
89 fail("setValue(Integer) should throw an exception");
90 } catch (Converter.ConversionException e) {
95 public void testIntegerStringConversion() {
96 TextField tf = new TextField();
98 tf.setConverter(new StringToIntegerConverter());
99 tf.setPropertyDataSource(new MethodProperty<Integer>(paulaBean, "age"));
100 assertEquals(34, tf.getPropertyDataSource().getValue());
101 assertEquals("34", tf.getValue());
103 assertEquals(12, tf.getPropertyDataSource().getValue());
104 assertEquals("12", tf.getValue());
105 tf.getPropertyDataSource().setValue(42);
106 assertEquals(42, tf.getPropertyDataSource().getValue());
107 assertEquals("42", tf.getValue());
110 public void testBooleanNullConversion() {
111 CheckBox cb = new CheckBox();
112 cb.setConverter(new Converter<Boolean, Boolean>() {
114 public Boolean convertToModel(Boolean value, Locale locale) {
115 // value from a CheckBox should never be null as long as it is
116 // not set to null (handled by conversion below).
117 assertNotNull(value);
121 public Boolean convertToPresentation(Boolean value, Locale locale) {
122 // Datamodel -> field
130 public Class<Boolean> getModelType() {
131 return Boolean.class;
134 public Class<Boolean> getPresentationType() {
135 return Boolean.class;
139 MethodProperty<Boolean> property = new MethodProperty<Boolean>(
140 paulaBean, "deceased");
141 cb.setPropertyDataSource(property);
142 assertEquals(Boolean.FALSE, property.getValue());
143 assertEquals(Boolean.FALSE, cb.getValue());
144 Boolean newDmValue = cb.getConverter().convertToPresentation(
145 cb.getValue(), new Locale("fi", "FI"));
146 assertEquals(Boolean.FALSE, newDmValue);
148 // FIXME: Should be able to set to false here to cause datamodel to be
149 // set to false but the change will not be propagated to the Property
150 // (field value is already false)
153 assertEquals(Boolean.TRUE, cb.getValue());
154 assertEquals(Boolean.TRUE, property.getValue());
157 assertEquals(Boolean.FALSE, cb.getValue());
158 assertEquals(Boolean.FALSE, property.getValue());