1
1
/*
2
- * Copyright 2011-2015 the original author or authors.
2
+ * Copyright 2011-2016 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 .*;
21
- import static org .mockito .Mockito .*;
22
20
import static org .springframework .data .mongodb .core .query .Criteria .*;
23
21
import static org .springframework .data .mongodb .core .query .Query .*;
24
22
import static org .springframework .data .mongodb .repository .query .StubParameterAccessor .*;
25
23
26
24
import java .lang .reflect .Method ;
25
+ import java .math .BigInteger ;
27
26
import java .util .List ;
28
27
28
+ import org .bson .types .ObjectId ;
29
29
import org .junit .Before ;
30
30
import org .junit .Rule ;
31
31
import org .junit .Test ;
32
32
import org .junit .rules .ExpectedException ;
33
33
import org .junit .runner .RunWith ;
34
34
import org .mockito .Mock ;
35
- import org .mockito .Mockito ;
36
- import org .mockito .invocation .InvocationOnMock ;
37
35
import org .mockito .runners .MockitoJUnitRunner ;
38
- import org .mockito .stubbing .Answer ;
39
36
import org .springframework .data .domain .Range ;
40
37
import org .springframework .data .geo .Distance ;
41
38
import org .springframework .data .geo .Metrics ;
42
39
import org .springframework .data .geo .Point ;
43
40
import org .springframework .data .geo .Polygon ;
44
41
import org .springframework .data .geo .Shape ;
45
- import org .springframework .data .mapping .context .MappingContext ;
46
42
import org .springframework .data .mongodb .core .Person ;
47
43
import org .springframework .data .mongodb .core .Venue ;
48
- import org .springframework .data .mongodb .core .convert .MongoConverter ;
44
+ import org .springframework .data .mongodb .core .convert .DbRefResolver ;
45
+ import org .springframework .data .mongodb .core .convert .MappingMongoConverter ;
49
46
import org .springframework .data .mongodb .core .index .GeoSpatialIndexType ;
50
47
import org .springframework .data .mongodb .core .index .GeoSpatialIndexed ;
51
48
import org .springframework .data .mongodb .core .mapping .DBRef ;
52
49
import org .springframework .data .mongodb .core .mapping .Field ;
53
50
import org .springframework .data .mongodb .core .mapping .MongoMappingContext ;
54
- import org .springframework .data .mongodb .core .mapping .MongoPersistentProperty ;
55
51
import org .springframework .data .mongodb .core .query .Criteria ;
56
52
import org .springframework .data .mongodb .core .query .Query ;
57
53
import org .springframework .data .repository .Repository ;
58
54
import org .springframework .data .repository .core .support .DefaultRepositoryMetadata ;
59
55
import org .springframework .data .repository .query .parser .PartTree ;
60
- import org .springframework .data .util .TypeInformation ;
61
56
62
57
/**
63
58
* Unit test for {@link MongoQueryCreator}.
@@ -71,22 +66,20 @@ public class MongoQueryCreatorUnitTests {
71
66
72
67
Method findByFirstname , findByFirstnameAndFriend , findByFirstnameNotNull ;
73
68
74
- @ Mock MongoConverter converter ;
69
+ @ Mock DbRefResolver resolver ;
75
70
76
- MappingContext <?, MongoPersistentProperty > context ;
71
+ MongoMappingContext context ;
72
+ MappingMongoConverter converter ;
77
73
78
74
@ Rule public ExpectedException expection = ExpectedException .none ();
79
75
80
76
@ Before
81
77
public void setUp () throws SecurityException , NoSuchMethodException {
82
78
83
- context = new MongoMappingContext ();
79
+ this . context = new MongoMappingContext ();
84
80
85
- doAnswer (new Answer <Object >() {
86
- public Object answer (InvocationOnMock invocation ) throws Throwable {
87
- return invocation .getArguments ()[0 ];
88
- }
89
- }).when (converter ).convertToMongoType (any (), Mockito .any (TypeInformation .class ));
81
+ this .converter = new MappingMongoConverter (resolver , context );
82
+ this .converter .afterPropertiesSet ();
90
83
}
91
84
92
85
@ Test
@@ -137,8 +130,8 @@ public void bindsMetricDistanceParameterToNearSphereCorrectly() throws Exception
137
130
Point point = new Point (10 , 20 );
138
131
Distance distance = new Distance (2.5 , Metrics .KILOMETERS );
139
132
140
- Query query = query (where ( "location" ). nearSphere ( point ). maxDistance ( distance . getNormalizedValue ()). and ( "firstname" )
141
- .is ("Dave" ));
133
+ Query query = query (
134
+ where ( "location" ). nearSphere ( point ). maxDistance ( distance . getNormalizedValue ()). and ( "firstname" ) .is ("Dave" ));
142
135
assertBindsDistanceToQuery (point , distance , query );
143
136
}
144
137
@@ -148,8 +141,8 @@ public void bindsDistanceParameterToNearCorrectly() throws Exception {
148
141
Point point = new Point (10 , 20 );
149
142
Distance distance = new Distance (2.5 );
150
143
151
- Query query = query (where ( "location" ). near ( point ). maxDistance ( distance . getNormalizedValue ()). and ( "firstname" )
152
- .is ("Dave" ));
144
+ Query query = query (
145
+ where ( "location" ). near ( point ). maxDistance ( distance . getNormalizedValue ()). and ( "firstname" ) .is ("Dave" ));
153
146
assertBindsDistanceToQuery (point , distance , query );
154
147
}
155
148
@@ -234,23 +227,6 @@ public void createsOrQueryCorrectly() {
234
227
assertThat (query , is (query (new Criteria ().orOperator (where ("firstName" ).is ("Dave" ), where ("age" ).is (42 )))));
235
228
}
236
229
237
- /**
238
- * @see DATAMONGO-347
239
- */
240
- @ Test
241
- public void createsQueryReferencingADBRefCorrectly () {
242
-
243
- User user = new User ();
244
- com .mongodb .DBRef dbref = new com .mongodb .DBRef ("user" , "id" );
245
- when (converter .toDBRef (eq (user ), Mockito .any (MongoPersistentProperty .class ))).thenReturn (dbref );
246
-
247
- PartTree tree = new PartTree ("findByCreator" , User .class );
248
- MongoQueryCreator creator = new MongoQueryCreator (tree , getAccessor (converter , user ), context );
249
- Query query = creator .createQuery ();
250
-
251
- assertThat (query , is (query (where ("creator" ).is (dbref ))));
252
- }
253
-
254
230
/**
255
231
* @see DATAMONGO-418
256
232
*/
@@ -292,16 +268,14 @@ public void createsQueryWithContainingPredicateCorrectly() {
292
268
293
269
private void assertBindsDistanceToQuery (Point point , Distance distance , Query reference ) throws Exception {
294
270
295
- when (converter .convertToMongoType ("Dave" )).thenReturn ("Dave" );
296
-
297
271
PartTree tree = new PartTree ("findByLocationNearAndFirstname" ,
298
272
org .springframework .data .mongodb .repository .Person .class );
299
273
Method method = PersonRepository .class .getMethod ("findByLocationNearAndFirstname" , Point .class , Distance .class ,
300
274
String .class );
301
275
MongoQueryMethod queryMethod = new MongoQueryMethod (method , new DefaultRepositoryMetadata (PersonRepository .class ),
302
276
new MongoMappingContext ());
303
- MongoParameterAccessor accessor = new MongoParametersParameterAccessor (queryMethod , new Object [] { point , distance ,
304
- "Dave" });
277
+ MongoParameterAccessor accessor = new MongoParametersParameterAccessor (queryMethod ,
278
+ new Object [] { point , distance , "Dave" });
305
279
306
280
Query query = new MongoQueryCreator (tree , new ConvertingParameterAccessor (converter , accessor ), context )
307
281
.createQuery ();
@@ -676,6 +650,21 @@ public void bindsNullValueToContainsClause() {
676
650
assertThat (query , is (query (where ("emailAddresses" ).in ((Object ) null ))));
677
651
}
678
652
653
+ /**
654
+ * @see DATAMONGO-1445
655
+ */
656
+ @ Test
657
+ public void doesNotPreConvertValues () {
658
+
659
+ PartTree tree = new PartTree ("id" , WithBigIntegerId .class );
660
+ BigInteger id = new BigInteger (new ObjectId ().toString (), 16 );
661
+ ConvertingParameterAccessor accessor = getAccessor (converter , new Object [] { id });
662
+
663
+ Query query = new MongoQueryCreator (tree , accessor , context ).createQuery ();
664
+
665
+ assertThat (query , is (query (where ("id" ).is (id ))));
666
+ }
667
+
679
668
interface PersonRepository extends Repository <Person , Long > {
680
669
681
670
List <Person > findByLocationNearAndFirstname (Point location , Distance maxDistance , String firstname );
@@ -704,4 +693,8 @@ static class Address2dSphere {
704
693
String street ;
705
694
@ GeoSpatialIndexed (type = GeoSpatialIndexType .GEO_2DSPHERE ) Point geo ;
706
695
}
696
+
697
+ static class WithBigIntegerId {
698
+ BigInteger id ;
699
+ }
707
700
}
0 commit comments