]> source.dussan.org Git - vaadin-framework.git/commitdiff
Test for converter factory and related fixes (#8992)
authorArtur Signell <artur@vaadin.com>
Mon, 25 Jun 2012 12:48:27 +0000 (15:48 +0300)
committerArtur Signell <artur@vaadin.com>
Mon, 25 Jun 2012 12:51:37 +0000 (15:51 +0300)
src/com/vaadin/data/util/converter/ConverterUtil.java
src/com/vaadin/ui/AbstractField.java
tests/server-side/com/vaadin/tests/data/converter/ConverterFactory.java [new file with mode: 0644]

index 239baf6b6d12b56aae18d6b9b7b0070c5f310358..206295b293abf62ccbc54c786cf84da4cd8e0c1f 100644 (file)
@@ -120,8 +120,11 @@ public class ConverterUtil implements Serializable {
             return null;
         }
 
-        // check that the value class is compatible with the model type
-        if (modelType.isAssignableFrom(presentationValue.getClass())) {
+        if (modelType == null) {
+            // No model type, return original value
+            return (MODELTYPE) presentationValue;
+        } else if (modelType.isAssignableFrom(presentationValue.getClass())) {
+            // presentation type directly compatible with model type
             return modelType.cast(presentationValue);
         } else {
             throw new Converter.ConversionException(
index 21ca01f5929f21db488a9bfc49286c6ee960afd8..ce62fb43edde6c9d65a0b03bbf4ee5dc88a55c8b 100644 (file)
@@ -591,7 +591,7 @@ public abstract class AbstractField<T> extends AbstractComponent implements
                     // Commits the value to datasource
                     committingValueToDataSource = true;
                     getPropertyDataSource().setValue(
-                            convertToDataSource(newFieldValue));
+                            convertToModel(newFieldValue));
 
                     // The buffer is now unmodified
                     setModified(false);
@@ -792,13 +792,15 @@ public abstract class AbstractField<T> extends AbstractComponent implements
      *             if there is no converter and the type is not compatible with
      *             the data source type.
      */
-    private Object convertToDataSource(T fieldValue)
+    private Object convertToModel(T fieldValue)
             throws Converter.ConversionException {
         try {
             Class<?> modelType = null;
             Property pd = getPropertyDataSource();
             if (pd != null) {
                 modelType = pd.getType();
+            } else if (getConverter() != null) {
+                modelType = getConverter().getModelType();
             }
             return ConverterUtil.convertToModel(fieldValue,
                     (Class<Object>) modelType, getConverter(), getLocale());
@@ -836,7 +838,7 @@ public abstract class AbstractField<T> extends AbstractComponent implements
      * @return The converted value that is compatible with the data source type
      */
     public Object getConvertedValue() {
-        return convertToDataSource(getFieldValue());
+        return convertToModel(getFieldValue());
     }
 
     /**
diff --git a/tests/server-side/com/vaadin/tests/data/converter/ConverterFactory.java b/tests/server-side/com/vaadin/tests/data/converter/ConverterFactory.java
new file mode 100644 (file)
index 0000000..b97f987
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+@VaadinApache2LicenseForJavaFiles@
+ */
+package com.vaadin.tests.data.converter;
+
+import java.util.Locale;
+
+import junit.framework.TestCase;
+
+import com.vaadin.Application;
+import com.vaadin.data.util.converter.Converter;
+import com.vaadin.data.util.converter.DefaultConverterFactory;
+import com.vaadin.ui.TextField;
+
+public class ConverterFactory extends TestCase {
+
+    public static class ConvertTo42 implements Converter<String, Integer> {
+
+        public Integer convertToModel(String value, Locale locale)
+                throws com.vaadin.data.util.converter.Converter.ConversionException {
+            return 42;
+        }
+
+        public String convertToPresentation(Integer value, Locale locale)
+                throws com.vaadin.data.util.converter.Converter.ConversionException {
+            return "42";
+        }
+
+        public Class<Integer> getModelType() {
+            return Integer.class;
+        }
+
+        public Class<String> getPresentationType() {
+            return String.class;
+        }
+
+    }
+
+    public static class ConverterFactory42 extends DefaultConverterFactory {
+        @Override
+        public <PRESENTATION, MODEL> Converter<PRESENTATION, MODEL> createConverter(
+                Class<PRESENTATION> presentationType, Class<MODEL> modelType) {
+            if (modelType == Integer.class) {
+                return (Converter<PRESENTATION, MODEL>) new ConvertTo42();
+            }
+
+            return super.createConverter(presentationType, modelType);
+        }
+    }
+
+    public void testApplicationConverterFactoryInBackgroundThread() {
+        Application.setCurrentApplication(null);
+        final Application appWithCustomIntegerConverter = new Application();
+        appWithCustomIntegerConverter
+                .setConverterFactory(new ConverterFactory42());
+
+        TextField tf = new TextField("", "123") {
+            @Override
+            public Application getApplication() {
+                return appWithCustomIntegerConverter;
+            };
+        };
+        tf.setConverter(Integer.class);
+        // The application converter always returns 42. Current application is
+        // null
+        assertEquals(42, tf.getConvertedValue());
+    }
+
+    public void testApplicationConverterFactoryForDetachedComponent() {
+        final Application appWithCustomIntegerConverter = new Application();
+        appWithCustomIntegerConverter
+                .setConverterFactory(new ConverterFactory42());
+        Application.setCurrentApplication(appWithCustomIntegerConverter);
+
+        TextField tf = new TextField("", "123");
+        tf.setConverter(Integer.class);
+        // The application converter always returns 42. Current application is
+        // null
+        assertEquals(42, tf.getConvertedValue());
+    }
+
+    public void testApplicationConverterFactoryForDifferentThanCurrentApplication() {
+        final Application fieldAppWithCustomIntegerConverter = new Application();
+        fieldAppWithCustomIntegerConverter
+                .setConverterFactory(new ConverterFactory42());
+        Application.setCurrentApplication(new Application());
+
+        TextField tf = new TextField("", "123") {
+            @Override
+            public Application getApplication() {
+                return fieldAppWithCustomIntegerConverter;
+            }
+        };
+        tf.setConverter(Integer.class);
+
+        // The application converter always returns 42. Application.getCurrent()
+        // should not be used
+        assertEquals(42, tf.getConvertedValue());
+    }
+}