1 package com.vaadin.tests.server.component.abstractfield;
3 import java.math.BigDecimal;
4 import java.util.Locale;
6 import junit.framework.TestCase;
8 import com.vaadin.data.util.MethodProperty;
9 import com.vaadin.server.VaadinSession;
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.tests.util.AlwaysLockedVaadinSession;
15 import com.vaadin.ui.TextField;
17 public class DefaultConverterFactoryTest extends TestCase {
19 public static class FloatBean {
23 public FloatBean(float f1, Float f2) {
28 public float getF1() {
32 public void setF1(float f1) {
36 public Float getF2() {
40 public void setF2(Float f2) {
46 public static class LongBean {
50 public LongBean(long l1, Long l2) {
59 public void setL1(long l1) {
67 public void setL2(Long l2) {
73 Person paulaBean = new Person("Paula", "Brilliant", "paula@brilliant.com",
74 34, Sex.FEMALE, new Address("Paula street 1", 12345, "P-town",
77 paulaBean.setSalary(49000);
78 BigDecimal rent = new BigDecimal(57223);
79 rent = rent.scaleByPowerOfTen(-2);
80 paulaBean.setRent(rent);
83 public void testFloatConversion() {
84 VaadinSession sess = new AlwaysLockedVaadinSession(null);
85 VaadinSession.setCurrent(sess);
87 TextField tf = new TextField();
88 tf.setLocale(new Locale("en", "US"));
89 tf.setPropertyDataSource(new MethodProperty<Integer>(new FloatBean(12f,
91 assertEquals("23", tf.getValue());
93 assertEquals("24", tf.getValue());
94 assertEquals(24f, tf.getConvertedValue());
95 assertEquals(24f, tf.getPropertyDataSource().getValue());
98 public void testLongConversion() {
99 VaadinSession sess = new AlwaysLockedVaadinSession(null);
100 VaadinSession.setCurrent(sess);
102 TextField tf = new TextField();
103 tf.setLocale(new Locale("en", "US"));
104 tf.setPropertyDataSource(new MethodProperty<Integer>(new LongBean(12,
105 1982739187238L), "l2"));
106 assertEquals("1,982,739,187,238", tf.getValue());
107 tf.setValue("1982739187239");
108 assertEquals("1,982,739,187,239", tf.getValue());
109 assertEquals(1982739187239L, tf.getConvertedValue());
110 assertEquals(1982739187239L, tf.getPropertyDataSource().getValue());
113 public void testDefaultNumberConversion() {
114 VaadinSession app = new AlwaysLockedVaadinSession(null);
115 VaadinSession.setCurrent(app);
116 TextField tf = new TextField();
117 tf.setLocale(new Locale("en", "US"));
118 tf.setPropertyDataSource(new MethodProperty<Integer>(paulaBean,
120 assertEquals("49,000", tf.getValue());
122 tf.setLocale(new Locale("fi", "FI"));
123 // FIXME: The following line should not be necessary and should be
125 tf.setPropertyDataSource(new MethodProperty<Integer>(paulaBean,
127 String value = tf.getValue();
128 // Java uses a non-breaking space (ascii 160) instead of space when
130 String expected = "49" + (char) 160 + "000";
131 assertEquals(expected, value);