|
20 | 20 | import lombok.Data;
|
21 | 21 | import lombok.Value;
|
22 | 22 |
|
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +import org.junit.Before; |
23 | 26 | import org.junit.Test;
|
| 27 | + |
| 28 | +import org.springframework.core.convert.converter.GenericConverter; |
| 29 | +import org.springframework.data.convert.ConverterBuilder; |
| 30 | +import org.springframework.data.convert.CustomConversions; |
24 | 31 | import org.springframework.data.mapping.PersistentPropertyAccessor;
|
25 | 32 | import org.springframework.data.relational.core.mapping.RelationalMappingContext;
|
26 | 33 | import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
|
|
35 | 42 | public class BasicRelationalConverterUnitTests {
|
36 | 43 |
|
37 | 44 | RelationalMappingContext context = new RelationalMappingContext();
|
38 |
| - RelationalConverter converter = new BasicRelationalConverter(context); |
| 45 | + RelationalConverter converter; |
| 46 | + |
| 47 | + @Before |
| 48 | + public void before() throws Exception { |
| 49 | + |
| 50 | + Set<GenericConverter> converters = ConverterBuilder.writing(MyValue.class, String.class, MyValue::getFoo) |
| 51 | + .andReading(MyValue::new).getConverters(); |
| 52 | + |
| 53 | + CustomConversions conversions = new CustomConversions(CustomConversions.StoreConversions.NONE, converters); |
| 54 | + context.setSimpleTypeHolder(conversions.getSimpleTypeHolder()); |
| 55 | + |
| 56 | + converter = new BasicRelationalConverter(context, conversions); |
| 57 | + } |
39 | 58 |
|
40 | 59 | @Test // DATAJDBC-235
|
41 | 60 | @SuppressWarnings("unchecked")
|
@@ -73,22 +92,49 @@ public void shouldConvertStringToEnum() {
|
73 | 92 | @SuppressWarnings("unchecked")
|
74 | 93 | public void shouldCreateInstance() {
|
75 | 94 |
|
76 |
| - RelationalPersistentEntity<MyValue> entity = (RelationalPersistentEntity) context |
77 |
| - .getRequiredPersistentEntity(MyValue.class); |
| 95 | + RelationalPersistentEntity<WithConstructorCreation> entity = (RelationalPersistentEntity) context |
| 96 | + .getRequiredPersistentEntity(WithConstructorCreation.class); |
78 | 97 |
|
79 |
| - MyValue result = converter.createInstance(entity, it -> "bar"); |
| 98 | + WithConstructorCreation result = converter.createInstance(entity, it -> "bar"); |
80 | 99 |
|
81 | 100 | assertThat(result.getFoo()).isEqualTo("bar");
|
82 | 101 | }
|
83 | 102 |
|
| 103 | + @Test // DATAJDBC-516 |
| 104 | + public void shouldConsiderWriteConverter() { |
| 105 | + |
| 106 | + Object result = converter.writeValue(new MyValue("hello-world"), ClassTypeInformation.from(MyValue.class)); |
| 107 | + |
| 108 | + assertThat(result).isEqualTo("hello-world"); |
| 109 | + } |
| 110 | + |
| 111 | + @Test // DATAJDBC-516 |
| 112 | + public void shouldConsiderReadConverter() { |
| 113 | + |
| 114 | + Object result = converter.readValue("hello-world", ClassTypeInformation.from(MyValue.class)); |
| 115 | + |
| 116 | + assertThat(result).isEqualTo(new MyValue("hello-world")); |
| 117 | + } |
| 118 | + |
84 | 119 | @Data
|
85 | 120 | static class MyEntity {
|
86 | 121 | boolean flag;
|
87 | 122 | }
|
88 | 123 |
|
| 124 | + @Value |
| 125 | + static class WithConstructorCreation { |
| 126 | + String foo; |
| 127 | + } |
| 128 | + |
89 | 129 | @Value
|
90 | 130 | static class MyValue {
|
91 |
| - final String foo; |
| 131 | + String foo; |
| 132 | + } |
| 133 | + |
| 134 | + @Value |
| 135 | + static class MyEntityWithConvertibleProperty { |
| 136 | + |
| 137 | + MyValue myValue; |
92 | 138 | }
|
93 | 139 |
|
94 | 140 | enum MyEnum {
|
|
0 commit comments