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.

AnyEnumToStringConverterTest.java 4.2KB

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