]> source.dussan.org Git - vaadin-framework.git/commitdiff
Pass ConversionException to getConversionError (#11960)
authorArtur Signell <artur@vaadin.com>
Fri, 31 May 2013 06:56:58 +0000 (09:56 +0300)
committerVaadin Code Review <review@vaadin.com>
Fri, 7 Jun 2013 06:32:15 +0000 (06:32 +0000)
Change-Id: I6a25c4e4bfd9fe73f973670583c708503f77f60a

server/src/com/vaadin/ui/AbstractField.java
server/tests/src/com/vaadin/tests/server/component/abstractfield/AbsFieldValueConversionError.java [new file with mode: 0644]

index 606bf5fb213d05f115c62718cdbc907f82074eff..7ac3a57c468d585be31c3232e88fe5bb7bc2bf00 100644 (file)
@@ -751,24 +751,36 @@ public abstract class AbstractField<T> extends AbstractComponent implements
             return ConverterUtil.convertToModel(fieldValue,
                     (Class<Object>) modelType, getConverter(), locale);
         } catch (ConversionException e) {
-            throw new ConversionException(getConversionError(modelType), e);
+            throw new ConversionException(getConversionError(modelType, e), e);
         }
     }
 
     /**
-     * Returns the conversion error with {0} replaced by the data source type.
+     * Returns the conversion error with {0} replaced by the data source type
+     * and {1} replaced by the exception (localized) message.
      * 
+     * @since 7.1
      * @param dataSourceType
-     *            The type of the data source
+     *            the type of the data source
+     * @param e
+     *            a conversion exception which can provide additional
+     *            information
      * @return The value conversion error string with parameters replaced.
      */
-    protected String getConversionError(Class<?> dataSourceType) {
-        if (dataSourceType == null) {
-            return getConversionError();
-        } else {
-            return getConversionError().replace("{0}",
+    protected String getConversionError(Class<?> dataSourceType,
+            ConversionException e) {
+        String conversionError = getConversionError();
+
+        if (dataSourceType != null) {
+            conversionError = conversionError.replace("{0}",
                     dataSourceType.getSimpleName());
         }
+        if (e != null) {
+            conversionError = conversionError.replace("{1}",
+                    e.getLocalizedMessage());
+        }
+
+        return conversionError;
     }
 
     /**
@@ -924,9 +936,9 @@ public abstract class AbstractField<T> extends AbstractComponent implements
             try {
                 valueToValidate = getConverter().convertToModel(fieldValue,
                         getLocale());
-            } catch (Exception e) {
-                throw new InvalidValueException(
-                        getConversionError(getConverter().getModelType()));
+            } catch (ConversionException e) {
+                throw new InvalidValueException(getConversionError(
+                        getConverter().getModelType(), e));
             }
         }
 
@@ -1461,7 +1473,8 @@ public abstract class AbstractField<T> extends AbstractComponent implements
     /**
      * Sets the error that is shown if the field value cannot be converted to
      * the data source type. If {0} is present in the message, it will be
-     * replaced by the simple name of the data source type.
+     * replaced by the simple name of the data source type. If {1} is present in
+     * the message, it will be replaced by the ConversionException message.
      * 
      * @param valueConversionError
      *            Message to be shown when conversion of the value fails
diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractfield/AbsFieldValueConversionError.java b/server/tests/src/com/vaadin/tests/server/component/abstractfield/AbsFieldValueConversionError.java
new file mode 100644 (file)
index 0000000..ad762f8
--- /dev/null
@@ -0,0 +1,72 @@
+package com.vaadin.tests.server.component.abstractfield;
+
+import junit.framework.TestCase;
+
+import org.junit.Assert;
+
+import com.vaadin.data.Validator.InvalidValueException;
+import com.vaadin.data.util.MethodProperty;
+import com.vaadin.data.util.converter.Converter.ConversionException;
+import com.vaadin.data.util.converter.StringToIntegerConverter;
+import com.vaadin.tests.data.bean.Address;
+import com.vaadin.tests.data.bean.Country;
+import com.vaadin.tests.data.bean.Person;
+import com.vaadin.tests.data.bean.Sex;
+import com.vaadin.ui.TextField;
+
+public class AbsFieldValueConversionError extends TestCase {
+
+    Person paulaBean = new Person("Paula", "Brilliant", "paula@brilliant.com",
+            34, Sex.FEMALE, new Address("Paula street 1", 12345, "P-town",
+                    Country.FINLAND));
+
+    public void testValidateConversionErrorParameters() {
+        TextField tf = new TextField();
+        tf.setConverter(new StringToIntegerConverter());
+        tf.setPropertyDataSource(new MethodProperty<String>(paulaBean, "age"));
+        tf.setConversionError("(Type: {0}) Converter exception message: {1}");
+        tf.setValue("abc");
+        try {
+            tf.validate();
+            fail();
+        } catch (InvalidValueException e) {
+            Assert.assertEquals(
+                    "(Type: Integer) Converter exception message: Could not convert 'abc' to java.lang.Integer",
+                    e.getMessage());
+        }
+
+    }
+
+    public void testConvertToModelConversionErrorParameters() {
+        TextField tf = new TextField();
+        tf.setConverter(new StringToIntegerConverter());
+        tf.setPropertyDataSource(new MethodProperty<String>(paulaBean, "age"));
+        tf.setConversionError("(Type: {0}) Converter exception message: {1}");
+        tf.setValue("abc");
+        try {
+            tf.getConvertedValue();
+            fail();
+        } catch (ConversionException e) {
+            Assert.assertEquals(
+                    "(Type: Integer) Converter exception message: Could not convert 'abc' to java.lang.Integer",
+                    e.getMessage());
+        }
+
+    }
+
+    public void testDefaultConversionErrorMessage() {
+        TextField tf = new TextField();
+        tf.setConverter(new StringToIntegerConverter());
+        tf.setPropertyDataSource(new MethodProperty<String>(paulaBean, "age"));
+        tf.setValue("abc");
+
+        try {
+            tf.validate();
+            fail();
+        } catch (InvalidValueException e) {
+            Assert.assertEquals("Could not convert value to Integer",
+                    e.getMessage());
+        }
+
+    }
+}