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.
20
20
import static org .mockito .Mockito .*;
21
21
22
22
import java .lang .reflect .Method ;
23
+ import java .util .ArrayList ;
23
24
import java .util .Collections ;
24
25
import java .util .List ;
25
26
import java .util .Map ;
@@ -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
Document document = new Document ();
106
107
converter .write (address , document );
107
108
document .remove (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
Document queryObject = new Document ("address" , document );
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
Document addressDocument = new Document ();
125
126
converter .write (address , addressDocument );
@@ -128,7 +129,7 @@ public void bindsMultipleParametersCorrectly() throws Exception {
128
129
Document reference = new Document ("lastname" , "Matthews" );
129
130
reference .append ("address" , addressDocument );
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 ().toJson (), is (reference .toJson ()));
133
134
}
134
135
@@ -232,10 +233,10 @@ public void shouldParseQueryWithParametersInExpression() throws Exception {
232
233
@ Test
233
234
public void bindsSimplePropertyAlreadyQuotedCorrectly () throws Exception {
234
235
235
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , "Matthews" );
236
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , "Matthews" );
236
237
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByLastnameQuoted" , String .class );
237
238
238
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
239
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
239
240
org .springframework .data .mongodb .core .query .Query reference = new BasicQuery ("{'lastname' : 'Matthews'}" );
240
241
241
242
assertThat (query .getQueryObject (), is (reference .getQueryObject ()));
@@ -247,10 +248,10 @@ public void bindsSimplePropertyAlreadyQuotedCorrectly() throws Exception {
247
248
@ Test
248
249
public void bindsSimplePropertyAlreadyQuotedWithRegexCorrectly () throws Exception {
249
250
250
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , "^Mat.*" );
251
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , "^Mat.*" );
251
252
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByLastnameQuoted" , String .class );
252
253
253
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
254
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
254
255
org .springframework .data .mongodb .core .query .Query reference = new BasicQuery ("{'lastname' : '^Mat.*'}" );
255
256
256
257
assertThat (query .getQueryObject (), is (reference .getQueryObject ()));
@@ -263,9 +264,9 @@ public void bindsSimplePropertyAlreadyQuotedWithRegexCorrectly() throws Exceptio
263
264
public void bindsSimplePropertyWithRegexCorrectly () throws Exception {
264
265
265
266
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByLastname" , String .class );
266
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , "^Mat.*" );
267
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , "^Mat.*" );
267
268
268
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
269
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
269
270
org .springframework .data .mongodb .core .query .Query reference = new BasicQuery ("{'lastname' : '^Mat.*'}" );
270
271
271
272
assertThat (query .getQueryObject (), is (reference .getQueryObject ()));
@@ -307,10 +308,10 @@ public void shouldParseJsonKeyReplacementCorrectly() throws Exception {
307
308
@ Test
308
309
public void shouldSupportExpressionsInCustomQueries () throws Exception {
309
310
310
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , "Matthews" );
311
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , "Matthews" );
311
312
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByQueryWithExpression" , String .class );
312
313
313
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
314
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
314
315
org .springframework .data .mongodb .core .query .Query reference = new BasicQuery ("{'lastname' : 'Matthews'}" );
315
316
316
317
assertThat (query .getQueryObject (), is (reference .getQueryObject ()));
@@ -322,11 +323,11 @@ public void shouldSupportExpressionsInCustomQueries() throws Exception {
322
323
@ Test
323
324
public void shouldSupportExpressionsInCustomQueriesWithNestedObject () throws Exception {
324
325
325
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , true , "param1" , "param2" );
326
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , true , "param1" , "param2" );
326
327
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByQueryWithExpressionAndNestedObject" , boolean .class ,
327
328
String .class );
328
329
329
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
330
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
330
331
org .springframework .data .mongodb .core .query .Query reference = new BasicQuery ("{ \" id\" : { \" $exists\" : true}}" );
331
332
332
333
assertThat (query .getQueryObject (), is (reference .getQueryObject ()));
@@ -338,11 +339,11 @@ public void shouldSupportExpressionsInCustomQueriesWithNestedObject() throws Exc
338
339
@ Test
339
340
public void shouldSupportExpressionsInCustomQueriesWithMultipleNestedObjects () throws Exception {
340
341
341
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , true , "param1" , "param2" );
342
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , true , "param1" , "param2" );
342
343
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByQueryWithExpressionAndMultipleNestedObjects" ,
343
344
boolean .class , String .class , String .class );
344
345
345
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
346
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
346
347
org .springframework .data .mongodb .core .query .Query reference = new BasicQuery (
347
348
"{ \" id\" : { \" $exists\" : true} , \" foo\" : 42 , \" bar\" : { \" $exists\" : false}}" );
348
349
@@ -356,16 +357,36 @@ public void shouldSupportExpressionsInCustomQueriesWithMultipleNestedObjects() t
356
357
public void shouldSupportNonQuotedBinaryDataReplacement () throws Exception {
357
358
358
359
byte [] binaryData = "Matthews" .getBytes ("UTF-8" );
359
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , binaryData );
360
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , binaryData );
360
361
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByLastnameAsBinary" , byte [].class );
361
362
362
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
363
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
363
364
org .springframework .data .mongodb .core .query .Query reference = new BasicQuery ("{'lastname' : { '$binary' : '"
364
365
+ DatatypeConverter .printBase64Binary (binaryData ) + "', '$type' : '" + BSON .B_GENERAL + "'}}" );
365
366
366
367
assertThat (query .getQueryObject ().toJson (), is (reference .getQueryObject ().toJson ()));
367
368
}
368
369
370
+ /**
371
+ * @see DATAMONGO-1565
372
+ */
373
+ @ Test
374
+ public void bindsPropertyReferenceMultipleTimesCorrectly () throws Exception {
375
+
376
+ StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByAgeQuotedAndUnquoted" , Integer .TYPE );
377
+
378
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , 3 );
379
+
380
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
381
+ List <Object > or = new ArrayList <>();
382
+ or .add (new Document ("age" , 3 ));
383
+ or .add (new Document ("displayAge" , "3" ));
384
+ Document queryObject = new Document ("$or" , or );
385
+ org .springframework .data .mongodb .core .query .Query reference = new BasicQuery (queryObject );
386
+
387
+ assertThat (query .getQueryObject (), is (reference .getQueryObject ()));
388
+ }
389
+
369
390
/**
370
391
* @see DATAMONGO-1454
371
392
*/
@@ -383,12 +404,12 @@ public void shouldSupportExistsProjection() throws Exception {
383
404
@ Test
384
405
public void shouldIgnorePlaceholderPatternInReplacementValue () throws Exception {
385
406
386
- ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , "argWith?1andText" ,
407
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , "argWith?1andText" ,
387
408
"nothing-special" );
388
409
389
410
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByStringWithWildcardChar" , String .class , String .class );
390
411
391
- org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
412
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
392
413
assertThat (query .getQueryObject (),
393
414
is (Document .parse ("{ \" arg0\" : \" argWith?1andText\" , \" arg1\" : \" nothing-special\" }" )));
394
415
}
@@ -527,6 +548,9 @@ private interface SampleRepository extends Repository<Person, Long> {
527
548
@ Query ("{'id':?#{ [0] ? { $exists :true} : [1] }, 'foo':42, 'bar': ?#{ [0] ? { $exists :false} : [1] }}" )
528
549
List <Person > findByQueryWithExpressionAndMultipleNestedObjects (boolean param0 , String param1 , String param2 );
529
550
551
+ @ Query (value = "{ $or : [{'age' : ?0 }, {'displayAge' : '?0'}] }" )
552
+ boolean findByAgeQuotedAndUnquoted (int age );
553
+
530
554
@ Query (value = "{ 'lastname' : ?0 }" , exists = true )
531
555
boolean existsByLastname (String lastname );
532
556
0 commit comments