]> source.dussan.org Git - vaadin-framework.git/commitdiff
PropertyFormatter's format now don't get called with weird Boolean value when ds...
authorMatti Tahvonen <matti.tahvonen@itmill.com>
Wed, 16 Nov 2011 09:32:28 +0000 (09:32 +0000)
committerMatti Tahvonen <matti.tahvonen@itmill.com>
Wed, 16 Nov 2011 09:32:28 +0000 (09:32 +0000)
svn changeset:22015/svn branch:6.7

src/com/vaadin/data/util/PropertyFormatter.java
tests/server-side/com/vaadin/tests/server/TestPropertyFormatter.java [new file with mode: 0644]

index c43a4771dccd51dcca839aeae72eb35d53d93027..3afb1cede8b0efb1ae03717b498dfe5d23ad4b8c 100644 (file)
@@ -146,14 +146,14 @@ public abstract class PropertyFormatter extends AbstractProperty implements
      */
     @Override
     public String toString() {
-        Object value = dataSource == null ? false : dataSource.getValue();
-        if (value == null) {
+        if (dataSource == null || dataSource.getValue() == null) {
             return null;
         }
-        return format(value);
+        return format(dataSource.getValue());
     }
 
     /** Reflects the read-only status of the datasource. */
+    @Override
     public boolean isReadOnly() {
         return dataSource == null ? false : dataSource.isReadOnly();
     }
@@ -190,6 +190,7 @@ public abstract class PropertyFormatter extends AbstractProperty implements
      * @param newStatus
      *            the new read-only status of the Property.
      */
+    @Override
     public void setReadOnly(boolean newStatus) {
         if (dataSource != null) {
             dataSource.setReadOnly(newStatus);
@@ -208,16 +209,14 @@ public abstract class PropertyFormatter extends AbstractProperty implements
             }
         } else {
             try {
-                dataSource.setValue(parse((String) newValue));
+                dataSource.setValue(parse(newValue.toString()));
                 if (!newValue.equals(toString())) {
                     fireValueChange();
                 }
+            } catch (ConversionException e) {
+                throw e;
             } catch (Exception e) {
-                if (e instanceof ConversionException) {
-                    throw (ConversionException) e;
-                } else {
-                    throw new ConversionException(e);
-                }
+                throw new ConversionException(e);
             }
         }
     }
diff --git a/tests/server-side/com/vaadin/tests/server/TestPropertyFormatter.java b/tests/server-side/com/vaadin/tests/server/TestPropertyFormatter.java
new file mode 100644 (file)
index 0000000..91e36b5
--- /dev/null
@@ -0,0 +1,69 @@
+package com.vaadin.tests.server;
+
+import java.util.Date;
+
+import junit.framework.TestCase;
+
+import org.junit.Test;
+
+import com.vaadin.data.util.ObjectProperty;
+import com.vaadin.data.util.PropertyFormatter;
+
+@SuppressWarnings("unchecked")
+public class TestPropertyFormatter extends TestCase {
+
+    class TestFormatter extends PropertyFormatter {
+
+        @Override
+        public String format(Object value) {
+            boolean isCorrectType = getExpectedClass().isAssignableFrom(
+                    value.getClass());
+            assertTrue(isCorrectType);
+            return "FOO";
+        }
+
+        @Override
+        public Object parse(String formattedValue) throws Exception {
+            return getExpectedClass().newInstance();
+        }
+    };
+    @SuppressWarnings("rawtypes")
+    private Class expectedClass;
+
+    @SuppressWarnings("rawtypes")
+    private Class getExpectedClass() {
+        return expectedClass;
+    }
+    
+    /**
+     * The object passed to format should be same as property's type.
+     * @throws IllegalAccessException 
+     * @throws InstantiationException 
+     */
+    @Test
+    @SuppressWarnings({ "rawtypes" })
+    public void testCorrectTypeForFormat() throws InstantiationException, IllegalAccessException {
+        Class[] testedTypes = new Class[] {Integer.class, Boolean.class, Double.class, String.class, Date.class};
+        Object[] testValues = new Object[] {new Integer(3), Boolean.FALSE, new Double(3.3), "bar", new Date()};
+        
+        int i = 0;
+        for (Class class1 : testedTypes) {
+            expectedClass = class1;
+            
+            TestFormatter formatter = new TestFormatter();
+            
+            // Should just return null, without formatting
+            Object value = formatter.getValue();
+            
+            // test with property which value is null
+            formatter.setPropertyDataSource(new ObjectProperty(null, expectedClass));
+            formatter.getValue(); // calls format
+            
+            // test with a value
+            formatter.setPropertyDataSource(new ObjectProperty(testValues[i++], expectedClass));
+            formatter.getValue(); // calls format
+        }
+        
+
+    }
+}