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 ;
@@ -85,9 +86,9 @@ public void setUp() {
85
86
public void bindsSimplePropertyCorrectly () throws Exception {
86
87
87
88
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByLastname" , String .class );
88
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , "Matthews" );
89
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , "Matthews" );
89
90
90
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
91
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
91
92
org .springframework .data .mongodb .core .query .Query reference = new BasicQuery ("{'lastname' : 'Matthews'}" );
92
93
93
94
assertThat (query .getQueryObject (), is (reference .getQueryObject ()));
@@ -99,13 +100,13 @@ public void bindsComplexPropertyCorrectly() throws Exception {
99
100
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByAddress" , Address .class );
100
101
101
102
Address address = new Address ("Foo" , "0123" , "Bar" );
102
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , address );
103
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , address );
103
104
104
105
DBObject dbObject = new BasicDBObject ();
105
106
converter .write (address , dbObject );
106
107
dbObject .removeField (DefaultMongoTypeMapper .DEFAULT_TYPE_KEY );
107
108
108
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
109
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
109
110
BasicDBObject queryObject = new BasicDBObject ("address" , dbObject );
110
111
org .springframework .data .mongodb .core .query .Query reference = new BasicQuery (queryObject );
111
112
@@ -118,7 +119,7 @@ public void bindsMultipleParametersCorrectly() throws Exception {
118
119
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByLastnameAndAddress" , String .class , Address .class );
119
120
120
121
Address address = new Address ("Foo" , "0123" , "Bar" );
121
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , "Matthews" , address );
122
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , "Matthews" , address );
122
123
123
124
DBObject addressDbObject = new BasicDBObject ();
124
125
converter .write (address , addressDbObject );
@@ -127,7 +128,7 @@ public void bindsMultipleParametersCorrectly() throws Exception {
127
128
DBObject reference = new BasicDBObject ("address" , addressDbObject );
128
129
reference .put ("lastname" , "Matthews" );
129
130
130
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
131
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
131
132
assertThat (query .getQueryObject (), is (reference ));
132
133
}
133
134
@@ -228,10 +229,10 @@ public void shouldParseQueryWithParametersInExpression() throws Exception {
228
229
@ Test
229
230
public void bindsSimplePropertyAlreadyQuotedCorrectly () throws Exception {
230
231
231
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , "Matthews" );
232
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , "Matthews" );
232
233
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByLastnameQuoted" , String .class );
233
234
234
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
235
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
235
236
org .springframework .data .mongodb .core .query .Query reference = new BasicQuery ("{'lastname' : 'Matthews'}" );
236
237
237
238
assertThat (query .getQueryObject (), is (reference .getQueryObject ()));
@@ -243,10 +244,10 @@ public void bindsSimplePropertyAlreadyQuotedCorrectly() throws Exception {
243
244
@ Test
244
245
public void bindsSimplePropertyAlreadyQuotedWithRegexCorrectly () throws Exception {
245
246
246
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , "^Mat.*" );
247
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , "^Mat.*" );
247
248
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByLastnameQuoted" , String .class );
248
249
249
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
250
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
250
251
org .springframework .data .mongodb .core .query .Query reference = new BasicQuery ("{'lastname' : '^Mat.*'}" );
251
252
252
253
assertThat (query .getQueryObject (), is (reference .getQueryObject ()));
@@ -259,9 +260,9 @@ public void bindsSimplePropertyAlreadyQuotedWithRegexCorrectly() throws Exceptio
259
260
public void bindsSimplePropertyWithRegexCorrectly () throws Exception {
260
261
261
262
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByLastname" , String .class );
262
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , "^Mat.*" );
263
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , "^Mat.*" );
263
264
264
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
265
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
265
266
org .springframework .data .mongodb .core .query .Query reference = new BasicQuery ("{'lastname' : '^Mat.*'}" );
266
267
267
268
assertThat (query .getQueryObject (), is (reference .getQueryObject ()));
@@ -304,10 +305,10 @@ public void shouldParseJsonKeyReplacementCorrectly() throws Exception {
304
305
@ Test
305
306
public void shouldSupportExpressionsInCustomQueries () throws Exception {
306
307
307
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , "Matthews" );
308
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , "Matthews" );
308
309
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByQueryWithExpression" , String .class );
309
310
310
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
311
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
311
312
org .springframework .data .mongodb .core .query .Query reference = new BasicQuery ("{'lastname' : 'Matthews'}" );
312
313
313
314
assertThat (query .getQueryObject (), is (reference .getQueryObject ()));
@@ -319,11 +320,11 @@ public void shouldSupportExpressionsInCustomQueries() throws Exception {
319
320
@ Test
320
321
public void shouldSupportExpressionsInCustomQueriesWithNestedObject () throws Exception {
321
322
322
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , true , "param1" , "param2" );
323
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , true , "param1" , "param2" );
323
324
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByQueryWithExpressionAndNestedObject" , boolean .class ,
324
325
String .class );
325
326
326
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
327
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
327
328
org .springframework .data .mongodb .core .query .Query reference = new BasicQuery ("{ \" id\" : { \" $exists\" : true}}" );
328
329
329
330
assertThat (query .getQueryObject (), is (reference .getQueryObject ()));
@@ -335,11 +336,11 @@ public void shouldSupportExpressionsInCustomQueriesWithNestedObject() throws Exc
335
336
@ Test
336
337
public void shouldSupportExpressionsInCustomQueriesWithMultipleNestedObjects () throws Exception {
337
338
338
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , true , "param1" , "param2" );
339
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , true , "param1" , "param2" );
339
340
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByQueryWithExpressionAndMultipleNestedObjects" ,
340
341
boolean .class , String .class , String .class );
341
342
342
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
343
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
343
344
org .springframework .data .mongodb .core .query .Query reference = new BasicQuery (
344
345
"{ \" id\" : { \" $exists\" : true} , \" foo\" : 42 , \" bar\" : { \" $exists\" : false}}" );
345
346
@@ -353,28 +354,48 @@ public void shouldSupportExpressionsInCustomQueriesWithMultipleNestedObjects() t
353
354
public void shouldSupportNonQuotedBinaryDataReplacement () throws Exception {
354
355
355
356
byte [] binaryData = "Matthews" .getBytes ("UTF-8" );
356
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , binaryData );
357
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , binaryData );
357
358
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByLastnameAsBinary" , byte [].class );
358
359
359
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
360
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
360
361
org .springframework .data .mongodb .core .query .Query reference = new BasicQuery ("{'lastname' : { '$binary' : '"
361
362
+ DatatypeConverter .printBase64Binary (binaryData ) + "', '$type' : " + BSON .B_GENERAL + "}}" );
362
363
363
364
assertThat (query .getQueryObject (), is (reference .getQueryObject ()));
364
365
}
365
366
367
+ /**
368
+ * @see DATAMONGO-1565
369
+ */
370
+ @ Test
371
+ public void bindsPropertyReferenceMultipleTimesCorrectly () throws Exception {
372
+
373
+ StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByAgeQuotedAndUnquoted" , Integer .TYPE );
374
+
375
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , 3 );
376
+
377
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
378
+ BasicDBList or = new BasicDBList ();
379
+ or .add (new BasicDBObject ("age" , 3 ));
380
+ or .add (new BasicDBObject ("displayAge" , "3" ));
381
+ BasicDBObject queryObject = new BasicDBObject ("$or" , or );
382
+ org .springframework .data .mongodb .core .query .Query reference = new BasicQuery (queryObject );
383
+
384
+ assertThat (query .getQueryObject (), is (reference .getQueryObject ()));
385
+ }
386
+
366
387
/**
367
388
* @see DATAMONGO-1565
368
389
*/
369
390
@ Test
370
391
public void shouldIgnorePlaceholderPatternInReplacementValue () throws Exception {
371
392
372
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , "argWith?1andText" ,
393
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , "argWith?1andText" ,
373
394
"nothing-special" );
374
395
375
396
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByStringWithWildcardChar" , String .class , String .class );
376
397
377
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
398
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
378
399
assertThat (query .getQueryObject (),
379
400
is (JSON .parse ("{ \" arg0\" : \" argWith?1andText\" , \" arg1\" : \" nothing-special\" }" )));
380
401
}
@@ -512,6 +533,9 @@ private interface SampleRepository extends Repository<Person, Long> {
512
533
@ Query ("{'id':?#{ [0] ? { $exists :true} : [1] }, 'foo':42, 'bar': ?#{ [0] ? { $exists :false} : [1] }}" )
513
534
List <Person > findByQueryWithExpressionAndMultipleNestedObjects (boolean param0 , String param1 , String param2 );
514
535
536
+ @ Query (value = "{ $or : [{'age' : ?0 }, {'displayAge' : '?0'}] }" )
537
+ boolean findByAgeQuotedAndUnquoted (int age );
538
+
515
539
@ Query ("{ 'arg0' : ?0, 'arg1' : ?1 }" )
516
540
List <Person > findByStringWithWildcardChar (String arg0 , String arg1 );
517
541
}
0 commit comments