From 4a997b3d5334d07185011f62e5508a88abc370fc Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Mon, 25 Jun 2012 15:48:27 +0300 Subject: Test for converter factory and related fixes (#8992) --- .../tests/data/converter/ConverterFactory.java | 100 +++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 tests/server-side/com/vaadin/tests/data/converter/ConverterFactory.java (limited to 'tests') 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 index 0000000000..b97f987ae3 --- /dev/null +++ b/tests/server-side/com/vaadin/tests/data/converter/ConverterFactory.java @@ -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 { + + 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 getModelType() { + return Integer.class; + } + + public Class getPresentationType() { + return String.class; + } + + } + + public static class ConverterFactory42 extends DefaultConverterFactory { + @Override + public Converter createConverter( + Class presentationType, Class modelType) { + if (modelType == Integer.class) { + return (Converter) 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()); + } +} -- cgit v1.2.3