diff options
author | Leif Åstrand <leif@vaadin.com> | 2015-08-29 15:53:20 +0300 |
---|---|---|
committer | Leif Åstrand <leif@vaadin.com> | 2015-09-09 18:42:26 +0300 |
commit | 22dfc6231cce451d171a1e6204dd75e815423cad (patch) | |
tree | f50f509731a955fa48d2b75f53e945d7247f1609 /server/tests/src/com | |
parent | 8b9cb7b88e7bb442c058aa44bad454ad45460fec (diff) | |
download | vaadin-framework-22dfc6231cce451d171a1e6204dd75e815423cad.tar.gz vaadin-framework-22dfc6231cce451d171a1e6204dd75e815423cad.zip |
Support custom Enum.toString in converter (#17301)
Change-Id: Icd3c164fb252bd048ffcd953f967a9c7acdc4514
Diffstat (limited to 'server/tests/src/com')
-rw-r--r-- | server/tests/src/com/vaadin/tests/data/converter/StringToEnumConverterTest.java | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/server/tests/src/com/vaadin/tests/data/converter/StringToEnumConverterTest.java b/server/tests/src/com/vaadin/tests/data/converter/StringToEnumConverterTest.java index 59c1ed133a..6660ecc9d5 100644 --- a/server/tests/src/com/vaadin/tests/data/converter/StringToEnumConverterTest.java +++ b/server/tests/src/com/vaadin/tests/data/converter/StringToEnumConverterTest.java @@ -14,10 +14,36 @@ public class StringToEnumConverterTest { VALUE1, SOME_VALUE, FOO_BAR_BAZ, Bar, nonStandardCase, _HUGH; } + public static enum EnumWithCustomToString { + ONE, TWO, THREE; + + @Override + public String toString() { + return "case " + (ordinal() + 1); + } + } + + public static enum EnumWithAmbigousToString { + FOO, FOOBAR, FOO_BAR; + + @Override + public String toString() { + return name().replaceAll("_", ""); + } + } + StringToEnumConverter converter = new StringToEnumConverter(); Converter<Enum, String> reverseConverter = new ReverseConverter<Enum, String>( converter); + private String convertToString(Enum value) { + return converter.convertToPresentation(value, String.class, null); + } + + public Enum convertToEnum(String string, Class<? extends Enum> type) { + return converter.convertToModel(string, type, null); + } + @Test public void testEmptyStringConversion() { Assert.assertEquals(null, @@ -79,4 +105,31 @@ public class StringToEnumConverterTest { } + @Test + public void preserveFormattingWithCustomToString() { + for (EnumWithCustomToString e : EnumWithCustomToString.values()) { + Assert.assertEquals(e.toString(), convertToString(e)); + } + } + + @Test + public void findEnumWithCustomToString() { + for (EnumWithCustomToString e : EnumWithCustomToString.values()) { + Assert.assertSame(e, + convertToEnum(e.toString(), EnumWithCustomToString.class)); + Assert.assertSame(e, + convertToEnum(e.name(), EnumWithCustomToString.class)); + } + } + + @Test + public void unambigousValueInEnumWithAmbigous_succeed() { + Assert.assertSame(EnumWithAmbigousToString.FOO, + convertToEnum("foo", EnumWithAmbigousToString.class)); + } + + @Test(expected = ConversionException.class) + public void ambigousValue_throws() { + convertToEnum("foobar", EnumWithAmbigousToString.class); + } } |