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 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.v7.tests.data.converter;
  17. import java.util.Locale;
  18. import org.junit.Assert;
  19. import org.junit.Test;
  20. import com.vaadin.server.VaadinSession;
  21. import com.vaadin.tests.util.AlwaysLockedVaadinSession;
  22. import com.vaadin.v7.data.util.converter.Converter;
  23. import com.vaadin.v7.data.util.converter.DefaultConverterFactory;
  24. import com.vaadin.v7.ui.TextField;
  25. public class ConverterFactoryTest {
  26. public static class ConvertTo42 implements Converter<String, Integer> {
  27. @Override
  28. public Integer convertToModel(String value,
  29. Class<? extends Integer> targetType, Locale locale)
  30. throws com.vaadin.v7.data.util.converter.Converter.ConversionException {
  31. return 42;
  32. }
  33. @Override
  34. public String convertToPresentation(Integer value,
  35. Class<? extends String> targetType, Locale locale)
  36. throws com.vaadin.v7.data.util.converter.Converter.ConversionException {
  37. return "42";
  38. }
  39. @Override
  40. public Class<Integer> getModelType() {
  41. return Integer.class;
  42. }
  43. @Override
  44. public Class<String> getPresentationType() {
  45. return String.class;
  46. }
  47. }
  48. public static class ConverterFactory42 extends DefaultConverterFactory {
  49. @Override
  50. public <PRESENTATION, MODEL> Converter<PRESENTATION, MODEL> createConverter(
  51. Class<PRESENTATION> presentationType, Class<MODEL> modelType) {
  52. if (modelType == Integer.class) {
  53. return (Converter<PRESENTATION, MODEL>) new ConvertTo42();
  54. }
  55. return super.createConverter(presentationType, modelType);
  56. }
  57. }
  58. @Test
  59. public void testApplicationConverterFactoryInBackgroundThread() {
  60. VaadinSession.setCurrent(null);
  61. final VaadinSession appWithCustomIntegerConverter = new AlwaysLockedVaadinSession(
  62. null);
  63. appWithCustomIntegerConverter
  64. .setConverterFactory(new ConverterFactory42());
  65. TextField tf = new TextField("", "123") {
  66. @Override
  67. public VaadinSession getSession() {
  68. return appWithCustomIntegerConverter;
  69. }
  70. };
  71. tf.setConverter(Integer.class);
  72. // The application converter always returns 42. Current application is
  73. // null
  74. Assert.assertEquals(42, tf.getConvertedValue());
  75. }
  76. @Test
  77. public void testApplicationConverterFactoryForDetachedComponent() {
  78. final VaadinSession appWithCustomIntegerConverter = new AlwaysLockedVaadinSession(
  79. null);
  80. appWithCustomIntegerConverter
  81. .setConverterFactory(new ConverterFactory42());
  82. VaadinSession.setCurrent(appWithCustomIntegerConverter);
  83. TextField tf = new TextField("", "123");
  84. tf.setConverter(Integer.class);
  85. // The application converter always returns 42. Current application is
  86. // null
  87. Assert.assertEquals(42, tf.getConvertedValue());
  88. }
  89. @Test
  90. public void testApplicationConverterFactoryForDifferentThanCurrentApplication() {
  91. final VaadinSession fieldAppWithCustomIntegerConverter = new AlwaysLockedVaadinSession(
  92. null);
  93. fieldAppWithCustomIntegerConverter
  94. .setConverterFactory(new ConverterFactory42());
  95. VaadinSession.setCurrent(new AlwaysLockedVaadinSession(null));
  96. TextField tf = new TextField("", "123") {
  97. @Override
  98. public VaadinSession getSession() {
  99. return fieldAppWithCustomIntegerConverter;
  100. }
  101. };
  102. tf.setConverter(Integer.class);
  103. // The application converter always returns 42. Application.getCurrent()
  104. // should not be used
  105. Assert.assertEquals(42, tf.getConvertedValue());
  106. }
  107. }