1
1
/*
2
- * Copyright 2010-2011 the original author or authors.
2
+ * Copyright 2010-2012 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
18
18
import static org .hamcrest .CoreMatchers .*;
19
19
import static org .junit .Assert .*;
20
+ import static org .mockito .Matchers .*;
20
21
import static org .mockito .Mockito .*;
21
22
22
23
import java .math .BigInteger ;
24
+ import java .util .Collections ;
23
25
24
26
import org .bson .types .ObjectId ;
25
27
import org .junit .Before ;
29
31
import org .mockito .Mockito ;
30
32
import org .mockito .runners .MockitoJUnitRunner ;
31
33
import org .springframework .context .support .GenericApplicationContext ;
34
+ import org .springframework .core .convert .converter .Converter ;
32
35
import org .springframework .dao .DataAccessException ;
33
36
import org .springframework .dao .InvalidDataAccessApiUsageException ;
34
37
import org .springframework .data .annotation .Id ;
38
+ import org .springframework .data .mongodb .MongoDbFactory ;
39
+ import org .springframework .data .mongodb .core .convert .CustomConversions ;
35
40
import org .springframework .data .mongodb .core .convert .MappingMongoConverter ;
41
+ import org .springframework .data .mongodb .core .mapping .MongoMappingContext ;
42
+ import org .springframework .data .mongodb .core .query .Query ;
43
+ import org .springframework .data .mongodb .core .query .Update ;
36
44
import org .springframework .test .util .ReflectionTestUtils ;
37
45
38
46
import com .mongodb .DB ;
@@ -51,20 +59,24 @@ public class MongoTemplateUnitTests extends MongoOperationsUnitTests {
51
59
52
60
MongoTemplate template ;
53
61
62
+ @ Mock
63
+ MongoDbFactory factory ;
54
64
@ Mock
55
65
Mongo mongo ;
56
-
57
66
@ Mock
58
67
DB db ;
59
-
60
68
@ Mock
61
69
DBCollection collection ;
62
70
71
+ MappingMongoConverter converter ;
72
+
63
73
@ Before
64
74
public void setUp () {
65
- this .template = new MongoTemplate (mongo , "database" );
66
75
67
- when (mongo .getDB ("database" )).thenReturn (db );
76
+ this .converter = new MappingMongoConverter (factory , new MongoMappingContext ());
77
+ this .template = new MongoTemplate (factory , converter );
78
+
79
+ when (factory .getDb ()).thenReturn (db );
68
80
when (db .getCollection (Mockito .any (String .class ))).thenReturn (collection );
69
81
}
70
82
@@ -126,6 +138,8 @@ public void storesEntityWithSetIdAlthoughNotAutogenerateable() {
126
138
@ Test
127
139
public void autogeneratesIdForEntityWithAutogeneratableId () {
128
140
141
+ this .converter .afterPropertiesSet ();
142
+
129
143
MongoTemplate template = spy (this .template );
130
144
doReturn (new ObjectId ()).when (template ).saveDBObject (Mockito .any (String .class ), Mockito .any (DBObject .class ),
131
145
Mockito .any (Class .class ));
@@ -136,6 +150,27 @@ public void autogeneratesIdForEntityWithAutogeneratableId() {
136
150
assertThat (entity .id , is (notNullValue ()));
137
151
}
138
152
153
+ /**
154
+ * @see DATAMONGO-374
155
+ */
156
+ @ Test
157
+ public void convertsUpdateConstraintsUsingConverters () {
158
+
159
+ CustomConversions conversions = new CustomConversions (Collections .singletonList (MyConverter .INSTANCE ));
160
+ this .converter .setCustomConversions (conversions );
161
+ this .converter .afterPropertiesSet ();
162
+
163
+ Query query = new Query ();
164
+ Update update = new Update ().set ("foo" , new AutogenerateableId ());
165
+
166
+ template .updateFirst (query , update , Wrapper .class );
167
+
168
+ QueryMapper queryMapper = new QueryMapper (converter );
169
+ DBObject reference = queryMapper .getMappedObject (update .getUpdateObject (), null );
170
+
171
+ verify (collection , times (1 )).update (Mockito .any (DBObject .class ), eq (reference ), anyBoolean (), anyBoolean ());
172
+ }
173
+
139
174
class AutogenerateableId {
140
175
141
176
@ Id
@@ -148,6 +183,20 @@ class NotAutogenerateableId {
148
183
Integer id ;
149
184
}
150
185
186
+ enum MyConverter implements Converter <AutogenerateableId , String > {
187
+
188
+ INSTANCE ;
189
+
190
+ public String convert (AutogenerateableId source ) {
191
+ return source .toString ();
192
+ }
193
+ }
194
+
195
+ class Wrapper {
196
+
197
+ AutogenerateableId foo ;
198
+ }
199
+
151
200
/**
152
201
* Mocks out the {@link MongoTemplate#getDb()} method to return the {@link DB} mock instead of executing the actual
153
202
* behaviour.
0 commit comments