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.

SpecificEnumToStringConverterTest.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 2000-2014 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.tests.data.converter;
  17. import java.util.Locale;
  18. import org.junit.Assert;
  19. import org.junit.Before;
  20. import org.junit.Test;
  21. import com.vaadin.data.util.ObjectProperty;
  22. import com.vaadin.legacy.data.util.converter.LegacyConverter;
  23. import com.vaadin.legacy.data.util.converter.LegacyReverseConverter;
  24. import com.vaadin.legacy.ui.LegacyTextField;
  25. import com.vaadin.tests.data.bean.AnotherTestEnum;
  26. import com.vaadin.tests.data.bean.TestEnum;
  27. public class SpecificEnumToStringConverterTest {
  28. public class SpecificEnumToStringConverter
  29. implements LegacyConverter<Enum, String> {
  30. private Class<? extends Enum> enumClass;
  31. public SpecificEnumToStringConverter(Class<? extends Enum> enumClass) {
  32. this.enumClass = enumClass;
  33. }
  34. @Override
  35. public String convertToModel(Enum value,
  36. Class<? extends String> targetType, Locale locale)
  37. throws com.vaadin.legacy.data.util.converter.LegacyConverter.ConversionException {
  38. if (value == null) {
  39. return null;
  40. }
  41. return value.toString();
  42. }
  43. @Override
  44. public Enum convertToPresentation(String value,
  45. Class<? extends Enum> targetType, Locale locale)
  46. throws com.vaadin.legacy.data.util.converter.LegacyConverter.ConversionException {
  47. if (value == null) {
  48. return null;
  49. }
  50. for (Enum e : enumClass.getEnumConstants()) {
  51. if (e.toString().equals(value)) {
  52. return e;
  53. }
  54. }
  55. return null;
  56. }
  57. @Override
  58. public Class<String> getModelType() {
  59. return String.class;
  60. }
  61. @Override
  62. public Class<Enum> getPresentationType() {
  63. return (Class<Enum>) enumClass;
  64. }
  65. }
  66. SpecificEnumToStringConverter testEnumConverter;
  67. SpecificEnumToStringConverter anotherTestEnumConverter;
  68. @Before
  69. public void setup() {
  70. testEnumConverter = new SpecificEnumToStringConverter(TestEnum.class);
  71. anotherTestEnumConverter = new SpecificEnumToStringConverter(
  72. AnotherTestEnum.class);
  73. }
  74. @Test
  75. public void nullConversion() {
  76. Assert.assertEquals(null,
  77. testEnumConverter.convertToModel(null, null, null));
  78. }
  79. @Test
  80. public void enumToStringConversion() {
  81. Assert.assertEquals(TestEnum.TWO.toString(), testEnumConverter
  82. .convertToModel(TestEnum.TWO, String.class, null));
  83. }
  84. @Test
  85. public void stringToEnumConversion() {
  86. Assert.assertEquals(TestEnum.TWO,
  87. testEnumConverter.convertToPresentation(TestEnum.TWO.toString(),
  88. TestEnum.class, null));
  89. }
  90. @Test
  91. public void stringToEnumWithField() {
  92. LegacyTextField tf = new LegacyTextField();
  93. tf.setConverter(new LegacyReverseConverter(anotherTestEnumConverter));
  94. tf.setPropertyDataSource(new ObjectProperty(AnotherTestEnum.TWO));
  95. Assert.assertEquals(AnotherTestEnum.TWO.toString(), tf.getValue());
  96. tf.setValue(AnotherTestEnum.ONE.toString());
  97. Assert.assertEquals(AnotherTestEnum.ONE.toString(), tf.getValue());
  98. Assert.assertEquals(AnotherTestEnum.ONE, tf.getConvertedValue());
  99. Assert.assertEquals(AnotherTestEnum.ONE,
  100. tf.getPropertyDataSource().getValue());
  101. }
  102. }