*/
@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();
}
* @param newStatus
* the new read-only status of the Property.
*/
+ @Override
public void setReadOnly(boolean newStatus) {
if (dataSource != null) {
dataSource.setReadOnly(newStatus);
}
} 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);
}
}
}
--- /dev/null
+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
+ }
+
+
+ }
+}