|
56 | 56 | import org.mockito.Mockito;
|
57 | 57 | import org.mockito.runners.MockitoJUnitRunner;
|
58 | 58 | import org.springframework.aop.framework.ProxyFactory;
|
| 59 | +import org.springframework.beans.ConversionNotSupportedException; |
59 | 60 | import org.springframework.beans.factory.annotation.Value;
|
60 | 61 | import org.springframework.context.ApplicationContext;
|
61 | 62 | import org.springframework.core.convert.converter.Converter;
|
62 | 63 | import org.springframework.data.annotation.Id;
|
63 | 64 | import org.springframework.data.annotation.PersistenceConstructor;
|
64 | 65 | import org.springframework.data.annotation.TypeAlias;
|
| 66 | +import org.springframework.data.convert.ReadingConverter; |
| 67 | +import org.springframework.data.convert.WritingConverter; |
65 | 68 | import org.springframework.data.geo.Box;
|
66 | 69 | import org.springframework.data.geo.Circle;
|
67 | 70 | import org.springframework.data.geo.Distance;
|
@@ -1964,6 +1967,7 @@ public void convertsJava8DateTimeTypesToDateAndBack() {
|
1964 | 1967 | assertThat(converter.read(TypeWithLocalDateTime.class, result).date, is(reference));
|
1965 | 1968 | }
|
1966 | 1969 |
|
| 1970 | + |
1967 | 1971 | /**
|
1968 | 1972 | * @see DATAMONGO-1128
|
1969 | 1973 | */
|
@@ -2001,6 +2005,68 @@ public void readsOptionalsCorrectly() {
|
2001 | 2005 | assertThat(read.localDateTime, is(Optional.of(now)));
|
2002 | 2006 | }
|
2003 | 2007 |
|
| 2008 | + /** |
| 2009 | + * @see DATAMONGO-1118 |
| 2010 | + */ |
| 2011 | + @Test |
| 2012 | + public void convertsMapKeyUsingCustomConverterForAndBackwards() { |
| 2013 | + |
| 2014 | + MappingMongoConverter converter = new MappingMongoConverter(resolver, mappingContext); |
| 2015 | + converter.setCustomConversions(new CustomConversions(Arrays.asList(new FooBarEnumToStringConverter(), |
| 2016 | + new StringToFooNumConverter()))); |
| 2017 | + converter.afterPropertiesSet(); |
| 2018 | + |
| 2019 | + ClassWithMapUsingEnumAsKey source = new ClassWithMapUsingEnumAsKey(); |
| 2020 | + source.map = new HashMap<MappingMongoConverterUnitTests.FooBarEnum, String>(); |
| 2021 | + source.map.put(FooBarEnum.FOO, "wohoo"); |
| 2022 | + |
| 2023 | + DBObject target = new BasicDBObject(); |
| 2024 | + converter.write(source, target); |
| 2025 | + |
| 2026 | + assertThat(converter.read(ClassWithMapUsingEnumAsKey.class, target).map, equalTo(source.map)); |
| 2027 | + } |
| 2028 | + |
| 2029 | + /** |
| 2030 | + * @see DATAMONGO-1118 |
| 2031 | + */ |
| 2032 | + @Test |
| 2033 | + public void writesMapKeyUsingCustomConverter() { |
| 2034 | + |
| 2035 | + MappingMongoConverter converter = new MappingMongoConverter(resolver, mappingContext); |
| 2036 | + converter.setCustomConversions(new CustomConversions(Arrays.asList(new FooBarEnumToStringConverter()))); |
| 2037 | + converter.afterPropertiesSet(); |
| 2038 | + |
| 2039 | + ClassWithMapUsingEnumAsKey source = new ClassWithMapUsingEnumAsKey(); |
| 2040 | + source.map = new HashMap<MappingMongoConverterUnitTests.FooBarEnum, String>(); |
| 2041 | + source.map.put(FooBarEnum.FOO, "spring"); |
| 2042 | + source.map.put(FooBarEnum.BAR, "data"); |
| 2043 | + |
| 2044 | + DBObject target = new BasicDBObject(); |
| 2045 | + converter.write(source, target); |
| 2046 | + |
| 2047 | + DBObject map = DBObjectTestUtils.getAsDBObject(target, "map"); |
| 2048 | + |
| 2049 | + assertThat(map.containsField("foo-enum-value"), is(true)); |
| 2050 | + assertThat(map.containsField("bar-enum-value"), is(true)); |
| 2051 | + } |
| 2052 | + |
| 2053 | + /** |
| 2054 | + * @see DATAMONGO-1118 |
| 2055 | + */ |
| 2056 | + @Test |
| 2057 | + public void readsMapKeyUsingCustomConverter() { |
| 2058 | + |
| 2059 | + MappingMongoConverter converter = new MappingMongoConverter(resolver, mappingContext); |
| 2060 | + converter.setCustomConversions(new CustomConversions(Arrays.asList(new StringToFooNumConverter()))); |
| 2061 | + converter.afterPropertiesSet(); |
| 2062 | + |
| 2063 | + DBObject source = new BasicDBObject("map", new BasicDBObject("foo-enum-value", "spring")); |
| 2064 | + |
| 2065 | + ClassWithMapUsingEnumAsKey target = converter.read(ClassWithMapUsingEnumAsKey.class, source); |
| 2066 | + |
| 2067 | + assertThat(target.map.get(FooBarEnum.FOO), is("spring")); |
| 2068 | + } |
| 2069 | + |
2004 | 2070 | static class GenericType<T> {
|
2005 | 2071 | T content;
|
2006 | 2072 | }
|
@@ -2303,4 +2369,50 @@ static class TypeWithOptional {
|
2303 | 2369 | Optional<String> string = Optional.empty();
|
2304 | 2370 | Optional<LocalDateTime> localDateTime = Optional.empty();
|
2305 | 2371 | }
|
| 2372 | + |
| 2373 | + static enum FooBarEnum { |
| 2374 | + FOO, BAR; |
| 2375 | + } |
| 2376 | + |
| 2377 | + static class ClassWithMapUsingEnumAsKey { |
| 2378 | + Map<FooBarEnum, String> map; |
| 2379 | + } |
| 2380 | + |
| 2381 | + @WritingConverter |
| 2382 | + static class FooBarEnumToStringConverter implements Converter<FooBarEnum, String> { |
| 2383 | + |
| 2384 | + @Override |
| 2385 | + public String convert(FooBarEnum source) { |
| 2386 | + if (source == null) { |
| 2387 | + return null; |
| 2388 | + } |
| 2389 | + |
| 2390 | + return FooBarEnum.FOO.equals(source) ? "foo-enum-value" : "bar-enum-value"; |
| 2391 | + } |
| 2392 | + |
| 2393 | + } |
| 2394 | + |
| 2395 | + @ReadingConverter |
| 2396 | + static class StringToFooNumConverter implements Converter<String, FooBarEnum> { |
| 2397 | + |
| 2398 | + @Override |
| 2399 | + public FooBarEnum convert(String source) { |
| 2400 | + |
| 2401 | + if (source == null) { |
| 2402 | + return null; |
| 2403 | + } |
| 2404 | + |
| 2405 | + if (source.equals("foo-enum-value")) { |
| 2406 | + return FooBarEnum.FOO; |
| 2407 | + } |
| 2408 | + if (source.equals("bar-enum-value")) { |
| 2409 | + return FooBarEnum.BAR; |
| 2410 | + } |
| 2411 | + |
| 2412 | + throw new ConversionNotSupportedException(source, String.class, null); |
| 2413 | + } |
| 2414 | + |
| 2415 | + } |
| 2416 | + |
| 2417 | + |
2306 | 2418 | }
|
0 commit comments