@@ -170,9 +170,9 @@ public void shouldSupportFindByParameterizedCriteriaAndFields() throws Exception
170
170
171
171
ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , new Object [] {
172
172
new BasicDBObject ("firstname" , "first" ).append ("lastname" , "last" ), Collections .singletonMap ("lastname" , 1 ) });
173
-
174
173
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByParameterizedCriteriaAndFields" , DBObject .class ,
175
174
Map .class );
175
+
176
176
org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
177
177
178
178
assertThat (query .getQueryObject (),
@@ -187,13 +187,74 @@ public void shouldSupportFindByParameterizedCriteriaAndFields() throws Exception
187
187
public void shouldSupportRespectExistingQuotingInFindByTitleBeginsWithExplicitQuoting () throws Exception {
188
188
189
189
ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , new Object [] { "fun" });
190
-
191
190
StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByTitleBeginsWithExplicitQuoting" , String .class );
191
+
192
192
org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
193
193
194
194
assertThat (query .getQueryObject (), is (new BasicQuery ("{title: {$regex: '^fun', $options: 'i'}}" ).getQueryObject ()));
195
195
}
196
196
197
+ /**
198
+ * @see DATAMONGO-995, DATAMONGO-420
199
+ */
200
+ @ Test
201
+ public void shouldParseQueryWithParametersInExpression () throws Exception {
202
+
203
+ ConvertingParameterAccessor accessor = StubParameterAccessor .getAccessor (converter , new Object [] { 1 , 2 , 3 , 4 });
204
+ StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByQueryWithParametersInExpression" , int .class ,
205
+ int .class , int .class , int .class );
206
+
207
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accessor );
208
+
209
+ assertThat (query .getQueryObject (), is (new BasicQuery (
210
+ "{$where: 'return this.date.getUTCMonth() == 3 && this.date.getUTCDay() == 4;'}" ).getQueryObject ()));
211
+ }
212
+
213
+ /**
214
+ * @see DATAMONGO-995, DATAMONGO-420
215
+ */
216
+ @ Test
217
+ public void bindsSimplePropertyAlreadyQuotedCorrectly () throws Exception {
218
+
219
+ ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , "Matthews" );
220
+ StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByLastnameQuoted" , String .class );
221
+
222
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
223
+ org .springframework .data .mongodb .core .query .Query reference = new BasicQuery ("{'lastname' : 'Matthews'}" );
224
+
225
+ assertThat (query .getQueryObject (), is (reference .getQueryObject ()));
226
+ }
227
+
228
+ /**
229
+ * @see DATAMONGO-995, DATAMONGO-420
230
+ */
231
+ @ Test
232
+ public void bindsSimplePropertyAlreadyQuotedWithRegexCorrectly () throws Exception {
233
+
234
+ ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , "^Mat.*" );
235
+ StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByLastnameQuoted" , String .class );
236
+
237
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
238
+ org .springframework .data .mongodb .core .query .Query reference = new BasicQuery ("{'lastname' : '^Mat.*'}" );
239
+
240
+ assertThat (query .getQueryObject (), is (reference .getQueryObject ()));
241
+ }
242
+
243
+ /**
244
+ * @see DATAMONGO-995, DATAMONGO-420
245
+ */
246
+ @ Test
247
+ public void bindsSimplePropertyWithRegexCorrectly () throws Exception {
248
+
249
+ StringBasedMongoQuery mongoQuery = createQueryForMethod ("findByLastname" , String .class );
250
+ ConvertingParameterAccessor accesor = StubParameterAccessor .getAccessor (converter , "^Mat.*" );
251
+
252
+ org .springframework .data .mongodb .core .query .Query query = mongoQuery .createQuery (accesor );
253
+ org .springframework .data .mongodb .core .query .Query reference = new BasicQuery ("{'lastname' : '^Mat.*'}" );
254
+
255
+ assertThat (query .getQueryObject (), is (reference .getQueryObject ()));
256
+ }
257
+
197
258
private StringBasedMongoQuery createQueryForMethod (String name , Class <?>... parameters ) throws Exception {
198
259
199
260
Method method = SampleRepository .class .getMethod (name , parameters );
@@ -206,6 +267,9 @@ private interface SampleRepository {
206
267
@ Query ("{ 'lastname' : ?0 }" )
207
268
Person findByLastname (String lastname );
208
269
270
+ @ Query ("{ 'lastname' : '?0' }" )
271
+ Person findByLastnameQuoted (String lastname );
272
+
209
273
@ Query ("{ 'address' : ?0 }" )
210
274
Person findByAddress (Address address );
211
275
@@ -226,5 +290,8 @@ private interface SampleRepository {
226
290
227
291
@ Query ("{'title': { $regex : '^?0', $options : 'i'}}" )
228
292
List <DBObject > findByTitleBeginsWithExplicitQuoting (String title );
293
+
294
+ @ Query (value = "{$where: 'return this.date.getUTCMonth() == ?2 && this.date.getUTCDay() == ?3;'}" )
295
+ List <DBObject > findByQueryWithParametersInExpression (int param1 , int param2 , int param3 , int param4 );
229
296
}
230
297
}
0 commit comments