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.
50
50
import org .springframework .data .repository .query .DefaultEvaluationContextProvider ;
51
51
import org .springframework .expression .spel .standard .SpelExpressionParser ;
52
52
53
+ import com .mongodb .BasicDBList ;
53
54
import com .mongodb .BasicDBObject ;
54
55
import com .mongodb .BasicDBObjectBuilder ;
55
56
import com .mongodb .DBObject ;
@@ -86,9 +87,9 @@ public void setUp() {
86
87
public void bindsSimplePropertyCorrectly () throws Exception {
87
88
88
89
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByLastname" , String .class );
89
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , "Matthews" );
90
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , "Matthews" );
90
91
91
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
92
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
92
93
org .springframework .data .mongodb .core .query .Query reference = new BasicQuery ("{'lastname' : 'Matthews'}" );
93
94
94
95
assertThat (query .getQueryObject (), is (reference .getQueryObject ()));
@@ -100,13 +101,13 @@ public void bindsComplexPropertyCorrectly() throws Exception {
100
101
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByAddress" , Address .class );
101
102
102
103
Address address = new Address ("Foo" , "0123" , "Bar" );
103
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , address );
104
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , address );
104
105
105
106
DBObject dbObject = new BasicDBObject ();
106
107
converter .write (address , dbObject );
107
108
dbObject .removeField (DefaultMongoTypeMapper .DEFAULT_TYPE_KEY );
108
109
109
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
110
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
110
111
BasicDBObject queryObject = new BasicDBObject ("address" , dbObject );
111
112
org .springframework .data .mongodb .core .query .Query reference = new BasicQuery (queryObject );
112
113
@@ -119,7 +120,7 @@ public void bindsMultipleParametersCorrectly() throws Exception {
119
120
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByLastnameAndAddress" , String .class , Address .class );
120
121
121
122
Address address = new Address ("Foo" , "0123" , "Bar" );
122
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , "Matthews" , address );
123
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , "Matthews" , address );
123
124
124
125
DBObject addressDbObject = new BasicDBObject ();
125
126
converter .write (address , addressDbObject );
@@ -128,7 +129,7 @@ public void bindsMultipleParametersCorrectly() throws Exception {
128
129
DBObject reference = new BasicDBObject ("address" , addressDbObject );
129
130
reference .put ("lastname" , "Matthews" );
130
131
131
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
132
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
132
133
assertThat (query .getQueryObject (), is (reference ));
133
134
}
134
135
@@ -229,10 +230,10 @@ public void shouldParseQueryWithParametersInExpression() throws Exception {
229
230
@ Test
230
231
public void bindsSimplePropertyAlreadyQuotedCorrectly () throws Exception {
231
232
232
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , "Matthews" );
233
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , "Matthews" );
233
234
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByLastnameQuoted" , String .class );
234
235
235
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
236
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
236
237
org .springframework .data .mongodb .core .query .Query reference = new BasicQuery ("{'lastname' : 'Matthews'}" );
237
238
238
239
assertThat (query .getQueryObject (), is (reference .getQueryObject ()));
@@ -244,10 +245,10 @@ public void bindsSimplePropertyAlreadyQuotedCorrectly() throws Exception {
244
245
@ Test
245
246
public void bindsSimplePropertyAlreadyQuotedWithRegexCorrectly () throws Exception {
246
247
247
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , "^Mat.*" );
248
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , "^Mat.*" );
248
249
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByLastnameQuoted" , String .class );
249
250
250
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
251
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
251
252
org .springframework .data .mongodb .core .query .Query reference = new BasicQuery ("{'lastname' : '^Mat.*'}" );
252
253
253
254
assertThat (query .getQueryObject (), is (reference .getQueryObject ()));
@@ -260,9 +261,9 @@ public void bindsSimplePropertyAlreadyQuotedWithRegexCorrectly() throws Exceptio
260
261
public void bindsSimplePropertyWithRegexCorrectly () throws Exception {
261
262
262
263
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByLastname" , String .class );
263
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , "^Mat.*" );
264
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , "^Mat.*" );
264
265
265
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
266
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
266
267
org .springframework .data .mongodb .core .query .Query reference = new BasicQuery ("{'lastname' : '^Mat.*'}" );
267
268
268
269
assertThat (query .getQueryObject (), is (reference .getQueryObject ()));
@@ -305,10 +306,10 @@ public void shouldParseJsonKeyReplacementCorrectly() throws Exception {
305
306
@ Test
306
307
public void shouldSupportExpressionsInCustomQueries () throws Exception {
307
308
308
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , "Matthews" );
309
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , "Matthews" );
309
310
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByQueryWithExpression" , String .class );
310
311
311
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
312
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
312
313
org .springframework .data .mongodb .core .query .Query reference = new BasicQuery ("{'lastname' : 'Matthews'}" );
313
314
314
315
assertThat (query .getQueryObject (), is (reference .getQueryObject ()));
@@ -320,11 +321,11 @@ public void shouldSupportExpressionsInCustomQueries() throws Exception {
320
321
@ Test
321
322
public void shouldSupportExpressionsInCustomQueriesWithNestedObject () throws Exception {
322
323
323
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , true , "param1" , "param2" );
324
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , true , "param1" , "param2" );
324
325
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByQueryWithExpressionAndNestedObject" , boolean .class ,
325
326
String .class );
326
327
327
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
328
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
328
329
org .springframework .data .mongodb .core .query .Query reference = new BasicQuery ("{ \" id\" : { \" $exists\" : true}}" );
329
330
330
331
assertThat (query .getQueryObject (), is (reference .getQueryObject ()));
@@ -336,11 +337,11 @@ public void shouldSupportExpressionsInCustomQueriesWithNestedObject() throws Exc
336
337
@ Test
337
338
public void shouldSupportExpressionsInCustomQueriesWithMultipleNestedObjects () throws Exception {
338
339
339
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , true , "param1" , "param2" );
340
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , true , "param1" , "param2" );
340
341
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByQueryWithExpressionAndMultipleNestedObjects" ,
341
342
boolean .class , String .class , String .class );
342
343
343
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
344
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
344
345
org .springframework .data .mongodb .core .query .Query reference = new BasicQuery (
345
346
"{ \" id\" : { \" $exists\" : true} , \" foo\" : 42 , \" bar\" : { \" $exists\" : false}}" );
346
347
@@ -354,16 +355,36 @@ public void shouldSupportExpressionsInCustomQueriesWithMultipleNestedObjects() t
354
355
public void shouldSupportNonQuotedBinaryDataReplacement () throws Exception {
355
356
356
357
byte [] binaryData = "Matthews" .getBytes ("UTF-8" );
357
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , binaryData );
358
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , binaryData );
358
359
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByLastnameAsBinary" , byte [].class );
359
360
360
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
361
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
361
362
org .springframework .data .mongodb .core .query .Query reference = new BasicQuery ("{'lastname' : { '$binary' : '"
362
363
+ DatatypeConverter .printBase64Binary (binaryData ) + "', '$type' : " + BSON .B_GENERAL + "}}" );
363
364
364
365
assertThat (query .getQueryObject (), is (reference .getQueryObject ()));
365
366
}
366
367
368
+ /**
369
+ * @see DATAMONGO-1565
370
+ */
371
+ @ Test
372
+ public void bindsPropertyReferenceMultipleTimesCorrectly () throws Exception {
373
+
374
+ StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByAgeQuotedAndUnquoted" , Integer .TYPE );
375
+
376
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , 3 );
377
+
378
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
379
+ BasicDBList or = new BasicDBList ();
380
+ or .add (new BasicDBObject ("age" , 3 ));
381
+ or .add (new BasicDBObject ("displayAge" , "3" ));
382
+ BasicDBObject queryObject = new BasicDBObject ("$or" , or );
383
+ org .springframework .data .mongodb .core .query .Query reference = new BasicQuery (queryObject );
384
+
385
+ assertThat (query .getQueryObject (), is (reference .getQueryObject ()));
386
+ }
387
+
367
388
/**
368
389
* @see DATAMONGO-1454
369
390
*/
@@ -381,12 +402,12 @@ public void shouldSupportExistsProjection() throws Exception {
381
402
@ Test
382
403
public void shouldIgnorePlaceholderPatternInReplacementValue () throws Exception {
383
404
384
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , "argWith?1andText" ,
405
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , "argWith?1andText" ,
385
406
"nothing-special" );
386
407
387
408
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByStringWithWildcardChar" , String .class , String .class );
388
409
389
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
410
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
390
411
assertThat (query .getQueryObject (),
391
412
is (JSON .parse ("{ \" arg0\" : \" argWith?1andText\" , \" arg1\" : \" nothing-special\" }" )));
392
413
}
@@ -524,6 +545,9 @@ private interface SampleRepository extends Repository<Person, Long> {
524
545
@ Query ("{'id':?#{ [0] ? { $exists :true} : [1] }, 'foo':42, 'bar': ?#{ [0] ? { $exists :false} : [1] }}" )
525
546
List <Person > findByQueryWithExpressionAndMultipleNestedObjects (boolean param0 , String param1 , String param2 );
526
547
548
+ @ Query (value = "{ $or : [{'age' : ?0 }, {'displayAge' : '?0'}] }" )
549
+ boolean findByAgeQuotedAndUnquoted (int age );
550
+
527
551
@ Query (value = "{ 'lastname' : ?0 }" , exists = true )
528
552
boolean existsByLastname (String lastname );
529
553
0 commit comments