You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConverterFactoryTest.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package com.vaadin.v7.tests.data.converter;
  2. import static org.junit.Assert.assertEquals;
  3. import java.util.Locale;
  4. import org.junit.Test;
  5. import com.vaadin.server.VaadinSession;
  6. import com.vaadin.tests.util.AlwaysLockedVaadinSession;
  7. import com.vaadin.v7.data.util.converter.Converter;
  8. import com.vaadin.v7.data.util.converter.DefaultConverterFactory;
  9. import com.vaadin.v7.ui.TextField;
  10. public class ConverterFactoryTest {
  11. public static class ConvertTo42 implements Converter<String, Integer> {
  12. @Override
  13. public Integer convertToModel(String value,
  14. Class<? extends Integer> targetType, Locale locale)
  15. throws ConversionException {
  16. return 42;
  17. }
  18. @Override
  19. public String convertToPresentation(Integer value,
  20. Class<? extends String> targetType, Locale locale)
  21. throws ConversionException {
  22. return "42";
  23. }
  24. @Override
  25. public Class<Integer> getModelType() {
  26. return Integer.class;
  27. }
  28. @Override
  29. public Class<String> getPresentationType() {
  30. return String.class;
  31. }
  32. }
  33. public static class ConverterFactory42 extends DefaultConverterFactory {
  34. @Override
  35. public <PRESENTATION, MODEL> Converter<PRESENTATION, MODEL> createConverter(
  36. Class<PRESENTATION> presentationType, Class<MODEL> modelType) {
  37. if (modelType == Integer.class) {
  38. return (Converter<PRESENTATION, MODEL>) new ConvertTo42();
  39. }
  40. return super.createConverter(presentationType, modelType);
  41. }
  42. }
  43. @Test
  44. public void testApplicationConverterFactoryInBackgroundThread() {
  45. VaadinSession.setCurrent(null);
  46. final VaadinSession appWithCustomIntegerConverter = new AlwaysLockedVaadinSession(
  47. null);
  48. appWithCustomIntegerConverter
  49. .setConverterFactory(new ConverterFactory42());
  50. TextField tf = new TextField("", "123") {
  51. @Override
  52. public VaadinSession getSession() {
  53. return appWithCustomIntegerConverter;
  54. }
  55. };
  56. tf.setConverter(Integer.class);
  57. // The application converter always returns 42. Current application is
  58. // null
  59. assertEquals(42, tf.getConvertedValue());
  60. }
  61. @Test
  62. public void testApplicationConverterFactoryForDetachedComponent() {
  63. final VaadinSession appWithCustomIntegerConverter = new AlwaysLockedVaadinSession(
  64. null);
  65. appWithCustomIntegerConverter
  66. .setConverterFactory(new ConverterFactory42());
  67. VaadinSession.setCurrent(appWithCustomIntegerConverter);
  68. TextField tf = new TextField("", "123");
  69. tf.setConverter(Integer.class);
  70. // The application converter always returns 42. Current application is
  71. // null
  72. assertEquals(42, tf.getConvertedValue());
  73. }
  74. @Test
  75. public void testApplicationConverterFactoryForDifferentThanCurrentApplication() {
  76. final VaadinSession fieldAppWithCustomIntegerConverter = new AlwaysLockedVaadinSession(
  77. null);
  78. fieldAppWithCustomIntegerConverter
  79. .setConverterFactory(new ConverterFactory42());
  80. VaadinSession.setCurrent(new AlwaysLockedVaadinSession(null));
  81. TextField tf = new TextField("", "123") {
  82. @Override
  83. public VaadinSession getSession() {
  84. return fieldAppWithCustomIntegerConverter;
  85. }
  86. };
  87. tf.setConverter(Integer.class);
  88. // The application converter always returns 42. Application.getCurrent()
  89. // should not be used
  90. assertEquals(42, tf.getConvertedValue());
  91. }
  92. }