3
3
import static org .hamcrest .Matchers .*;
4
4
import static org .junit .Assert .*;
5
5
6
+ import java .text .DateFormat ;
7
+ import java .text .Format ;
6
8
import java .util .Arrays ;
7
9
import java .util .Locale ;
8
10
import java .util .UUID ;
9
11
12
+ import org .bson .types .Binary ;
10
13
import org .bson .types .ObjectId ;
11
14
import org .junit .Test ;
12
15
import org .springframework .core .convert .converter .Converter ;
13
- import org .springframework .core .convert .support .ConversionServiceFactory ;
16
+ import org .springframework .core .convert .support .DefaultConversionService ;
14
17
import org .springframework .core .convert .support .GenericConversionService ;
15
18
import org .springframework .data .mongodb .core .convert .MongoConverters .StringToBigIntegerConverter ;
16
19
@@ -27,13 +30,13 @@ public class CustomConversionsUnitTests {
27
30
@ SuppressWarnings ("unchecked" )
28
31
public void findsBasicReadAndWriteConversions () {
29
32
30
- CustomConversions conversions = new CustomConversions (Arrays .asList (UuidToStringConverter .INSTANCE ,
31
- StringToUUIDConverter .INSTANCE ));
33
+ CustomConversions conversions = new CustomConversions (Arrays .asList (FormatToStringConverter .INSTANCE ,
34
+ StringToFormatConverter .INSTANCE ));
32
35
33
- assertThat (conversions .getCustomWriteTarget (UUID .class , null ), is (typeCompatibleWith (String .class )));
36
+ assertThat (conversions .getCustomWriteTarget (Format .class , null ), is (typeCompatibleWith (String .class )));
34
37
assertThat (conversions .getCustomWriteTarget (String .class , null ), is (nullValue ()));
35
38
36
- assertThat (conversions .hasCustomReadTarget (String .class , UUID .class ), is (true ));
39
+ assertThat (conversions .hasCustomReadTarget (String .class , Format .class ), is (true ));
37
40
assertThat (conversions .hasCustomReadTarget (String .class , Locale .class ), is (false ));
38
41
}
39
42
@@ -51,7 +54,7 @@ public void considersSubtypesCorrectly() {
51
54
@ Test
52
55
public void considersTypesWeRegisteredConvertersForAsSimple () {
53
56
54
- CustomConversions conversions = new CustomConversions (Arrays .asList (UuidToStringConverter .INSTANCE ));
57
+ CustomConversions conversions = new CustomConversions (Arrays .asList (FormatToStringConverter .INSTANCE ));
55
58
assertThat (conversions .isSimpleType (UUID .class ), is (true ));
56
59
}
57
60
@@ -95,23 +98,22 @@ public void considersDBRefsToBeSimpleTypes() {
95
98
@ Test
96
99
public void populatesConversionServiceCorrectly () {
97
100
98
- @ SuppressWarnings ("deprecation" )
99
- GenericConversionService conversionService = ConversionServiceFactory .createDefaultConversionService ();
101
+ GenericConversionService conversionService = new DefaultConversionService ();
100
102
assertThat (conversionService .canConvert (String .class , UUID .class ), is (false ));
101
103
102
- CustomConversions conversions = new CustomConversions (Arrays .asList (StringToUUIDConverter .INSTANCE ));
104
+ CustomConversions conversions = new CustomConversions (Arrays .asList (StringToFormatConverter .INSTANCE ));
103
105
conversions .registerConvertersIn (conversionService );
104
106
105
- assertThat (conversionService .canConvert (String .class , UUID .class ), is (true ));
107
+ assertThat (conversionService .canConvert (String .class , Format .class ), is (true ));
106
108
}
107
109
108
110
/**
109
111
* @see DATAMONGO-259
110
112
*/
111
113
@ Test
112
114
public void doesNotConsiderTypeSimpleIfOnlyReadConverterIsRegistered () {
113
- CustomConversions conversions = new CustomConversions (Arrays .asList (StringToUUIDConverter .INSTANCE ));
114
- assertThat (conversions .isSimpleType (UUID .class ), is (false ));
115
+ CustomConversions conversions = new CustomConversions (Arrays .asList (StringToFormatConverter .INSTANCE ));
116
+ assertThat (conversions .isSimpleType (Format .class ), is (false ));
115
117
}
116
118
117
119
/**
@@ -140,18 +142,38 @@ public void doesNotHaveConverterForStringToBigIntegerByDefault() {
140
142
assertThat (conversions .getCustomWriteTarget (String .class ), is (nullValue ()));
141
143
}
142
144
143
- enum UuidToStringConverter implements Converter <UUID , String > {
145
+ /**
146
+ * @see DATAMONGO-390
147
+ */
148
+ @ Test
149
+ public void considersBinaryASimpleType () {
150
+
151
+ CustomConversions conversions = new CustomConversions ();
152
+ assertThat (conversions .isSimpleType (Binary .class ), is (true ));
153
+ }
154
+
155
+ /**
156
+ * @see DATAMONGO-390
157
+ */
158
+ @ Test
159
+ public void convertsUUIDsToBinaryByDefault () {
160
+
161
+ CustomConversions conversions = new CustomConversions ();
162
+ assertThat (conversions .hasCustomWriteTarget (UUID .class ), is (true ));
163
+ }
164
+
165
+ enum FormatToStringConverter implements Converter <Format , String > {
144
166
INSTANCE ;
145
167
146
- public String convert (UUID source ) {
168
+ public String convert (Format source ) {
147
169
return source .toString ();
148
170
}
149
171
}
150
172
151
- enum StringToUUIDConverter implements Converter <String , UUID > {
173
+ enum StringToFormatConverter implements Converter <String , Format > {
152
174
INSTANCE ;
153
- public UUID convert (String source ) {
154
- return UUID . fromString ( source );
175
+ public Format convert (String source ) {
176
+ return DateFormat . getInstance ( );
155
177
}
156
178
}
157
179
0 commit comments