Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

StringToEnumConverterTest.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package com.vaadin.tests.data.converter;
  2. import org.junit.Assert;
  3. import org.junit.Test;
  4. import com.vaadin.v7.data.util.converter.LegacyConverter;
  5. import com.vaadin.v7.data.util.converter.LegacyConverter.ConversionException;
  6. import com.vaadin.v7.data.util.converter.LegacyReverseConverter;
  7. import com.vaadin.v7.data.util.converter.LegacyStringToEnumConverter;
  8. public class StringToEnumConverterTest {
  9. public static enum FooEnum {
  10. VALUE1, SOME_VALUE, FOO_BAR_BAZ, Bar, nonStandardCase, _HUGH;
  11. }
  12. public static enum EnumWithCustomToString {
  13. ONE, TWO, THREE;
  14. @Override
  15. public String toString() {
  16. return "case " + (ordinal() + 1);
  17. }
  18. }
  19. public static enum EnumWithAmbigousToString {
  20. FOO, FOOBAR, FOO_BAR;
  21. @Override
  22. public String toString() {
  23. return name().replaceAll("_", "");
  24. }
  25. }
  26. LegacyStringToEnumConverter converter = new LegacyStringToEnumConverter();
  27. LegacyConverter<Enum, String> reverseConverter = new LegacyReverseConverter<Enum, String>(
  28. converter);
  29. private String convertToString(Enum value) {
  30. return converter.convertToPresentation(value, String.class, null);
  31. }
  32. public Enum convertToEnum(String string, Class<? extends Enum> type) {
  33. return converter.convertToModel(string, type, null);
  34. }
  35. @Test
  36. public void testEmptyStringConversion() {
  37. Assert.assertEquals(null,
  38. converter.convertToModel("", Enum.class, null));
  39. }
  40. @Test
  41. public void testInvalidEnumClassConversion() {
  42. try {
  43. converter.convertToModel("Foo", Enum.class, null);
  44. Assert.fail("No exception thrown");
  45. } catch (ConversionException e) {
  46. // OK
  47. }
  48. }
  49. @Test
  50. public void testNullConversion() {
  51. Assert.assertEquals(null,
  52. converter.convertToModel(null, Enum.class, null));
  53. }
  54. @Test
  55. public void testReverseNullConversion() {
  56. Assert.assertEquals(null,
  57. reverseConverter.convertToModel(null, String.class, null));
  58. }
  59. @Test
  60. public void testValueConversion() {
  61. Assert.assertEquals(FooEnum.VALUE1,
  62. converter.convertToModel("Value1", FooEnum.class, null));
  63. Assert.assertEquals(FooEnum.SOME_VALUE,
  64. converter.convertToModel("Some value", FooEnum.class, null));
  65. Assert.assertEquals(FooEnum.FOO_BAR_BAZ,
  66. converter.convertToModel("Foo bar baz", FooEnum.class, null));
  67. Assert.assertEquals(FooEnum.Bar,
  68. converter.convertToModel("Bar", FooEnum.class, null));
  69. Assert.assertEquals(FooEnum.nonStandardCase, converter
  70. .convertToModel("Nonstandardcase", FooEnum.class, null));
  71. Assert.assertEquals(FooEnum._HUGH,
  72. converter.convertToModel("_hugh", FooEnum.class, null));
  73. }
  74. @Test
  75. public void testReverseValueConversion() {
  76. Assert.assertEquals("Value1", reverseConverter
  77. .convertToModel(FooEnum.VALUE1, String.class, null));
  78. Assert.assertEquals("Some value", reverseConverter
  79. .convertToModel(FooEnum.SOME_VALUE, String.class, null));
  80. Assert.assertEquals("Foo bar baz", reverseConverter
  81. .convertToModel(FooEnum.FOO_BAR_BAZ, String.class, null));
  82. Assert.assertEquals("Bar", reverseConverter.convertToModel(FooEnum.Bar,
  83. String.class, null));
  84. Assert.assertEquals("Nonstandardcase", reverseConverter
  85. .convertToModel(FooEnum.nonStandardCase, String.class, null));
  86. Assert.assertEquals("_hugh", reverseConverter
  87. .convertToModel(FooEnum._HUGH, String.class, null));
  88. }
  89. @Test
  90. public void preserveFormattingWithCustomToString() {
  91. for (EnumWithCustomToString e : EnumWithCustomToString.values()) {
  92. Assert.assertEquals(e.toString(), convertToString(e));
  93. }
  94. }
  95. @Test
  96. public void findEnumWithCustomToString() {
  97. for (EnumWithCustomToString e : EnumWithCustomToString.values()) {
  98. Assert.assertSame(e,
  99. convertToEnum(e.toString(), EnumWithCustomToString.class));
  100. Assert.assertSame(e,
  101. convertToEnum(e.name(), EnumWithCustomToString.class));
  102. }
  103. }
  104. @Test
  105. public void unambigousValueInEnumWithAmbigous_succeed() {
  106. Assert.assertSame(EnumWithAmbigousToString.FOO,
  107. convertToEnum("foo", EnumWithAmbigousToString.class));
  108. }
  109. @Test(expected = ConversionException.class)
  110. public void ambigousValue_throws() {
  111. convertToEnum("foobar", EnumWithAmbigousToString.class);
  112. }
  113. }