@ -28,6 +28,9 @@ import java.util.regex.Pattern;
@@ -28,6 +28,9 @@ import java.util.regex.Pattern;
import javax.xml.bind.DatatypeConverter ;
import com.mongodb.BasicDBObject ;
import com.mongodb.DBObject ;
import com.mongodb.util.JSON ;
import org.bson.BSON ;
import org.bson.BsonRegularExpression ;
import org.bson.Document ;
@ -56,7 +59,7 @@ import org.springframework.expression.spel.standard.SpelExpressionParser;
@@ -56,7 +59,7 @@ import org.springframework.expression.spel.standard.SpelExpressionParser;
/ * *
* Unit tests for { @link StringBasedMongoQuery } .
*
*
* @author Oliver Gierke
* @author Christoph Strobl
* @author Thomas Darimont
@ -435,6 +438,78 @@ public class StringBasedMongoQueryUnitTests {
@@ -435,6 +438,78 @@ public class StringBasedMongoQueryUnitTests {
assertThat ( query . getQueryObject ( ) , is ( new Document ( "lastname" , new BsonRegularExpression ( "^(calamity)" ) ) ) ) ;
}
@Test // DATAMONGO-1603
public void shouldAllowReuseOfPlaceholderWithinQuery ( ) throws Exception {
StringBasedMongoQuery mongoQuery = createQueryForMethod ( "findByReusingPlaceholdersMultipleTimes" , String . class ,
String . class ) ;
ConvertingParameterAccessor accessor = StubParameterAccessor . getAccessor ( converter , "calamity" , "regalia" ) ;
org . springframework . data . mongodb . core . query . Query query = mongoQuery . createQuery ( accessor ) ;
assertThat ( query . getQueryObject ( ) , is ( new Document ( ) . append ( "arg0" , "calamity" )
. append ( "arg1" , "regalia" ) . append ( "arg2" , "calamity" ) ) ) ;
}
@Test // DATAMONGO-1603
public void shouldAllowReuseOfQuotedPlaceholderWithinQuery ( ) throws Exception {
StringBasedMongoQuery mongoQuery = createQueryForMethod ( "findByReusingPlaceholdersMultipleTimesWhenQuoted" ,
String . class , String . class ) ;
ConvertingParameterAccessor accessor = StubParameterAccessor . getAccessor ( converter , "calamity" , "regalia" ) ;
org . springframework . data . mongodb . core . query . Query query = mongoQuery . createQuery ( accessor ) ;
assertThat ( query . getQueryObject ( ) , is ( new Document ( ) . append ( "arg0" , "calamity" )
. append ( "arg1" , "regalia" ) . append ( "arg2" , "calamity" ) ) ) ;
}
@Test // DATAMONGO-1603
public void shouldAllowReuseOfQuotedPlaceholderWithinQueryAndIncludeSuffixCorrectly ( ) throws Exception {
StringBasedMongoQuery mongoQuery = createQueryForMethod (
"findByReusingPlaceholdersMultipleTimesWhenQuotedAndSomeStuffAppended" , String . class , String . class ) ;
ConvertingParameterAccessor accessor = StubParameterAccessor . getAccessor ( converter , "calamity" , "regalia" ) ;
org . springframework . data . mongodb . core . query . Query query = mongoQuery . createQuery ( accessor ) ;
assertThat ( query . getQueryObject ( ) , is ( new Document ( ) . append ( "arg0" , "calamity" )
. append ( "arg1" , "regalia" ) . append ( "arg2" , "calamitys" ) ) ) ;
}
@Test // DATAMONGO-1603
public void shouldAllowQuotedParameterWithSuffixAppended ( ) throws Exception {
StringBasedMongoQuery mongoQuery = createQueryForMethod ( "findByWhenQuotedAndSomeStuffAppended" , String . class ,
String . class ) ;
ConvertingParameterAccessor accessor = StubParameterAccessor . getAccessor ( converter , "calamity" , "regalia" ) ;
org . springframework . data . mongodb . core . query . Query query = mongoQuery . createQuery ( accessor ) ;
assertThat ( query . getQueryObject ( ) ,
is ( new Document ( ) . append ( "arg0" , "calamity" ) . append ( "arg1" , "regalias" ) ) ) ;
}
@Test // DATAMONGO-1603
public void shouldCaptureReplacementWithComplexSuffixCorrectly ( ) throws Exception {
StringBasedMongoQuery mongoQuery = createQueryForMethod ( "findByMultiRegex" , String . class ) ;
ConvertingParameterAccessor accessor = StubParameterAccessor . getAccessor ( converter , "calamity" ) ;
org . springframework . data . mongodb . core . query . Query query = mongoQuery . createQuery ( accessor ) ;
assertThat ( query . getQueryObject ( ) , is ( Document . parse (
"{ \"$or\" : [ { \"firstname\" : { \"$regex\" : \".*calamity.*\" , \"$options\" : \"i\"}} , { \"lastname\" : { \"$regex\" : \".*calamityxyz.*\" , \"$options\" : \"i\"}}]}" ) ) ) ;
}
@Test // DATAMONGO-1603
public void shouldAllowPlaceholderReuseInQuotedValue ( ) throws Exception {
StringBasedMongoQuery mongoQuery = createQueryForMethod ( "findByLastnameRegex" , String . class , String . class ) ;
ConvertingParameterAccessor accessor = StubParameterAccessor . getAccessor ( converter , "calamity" , "regalia" ) ;
org . springframework . data . mongodb . core . query . Query query = mongoQuery . createQuery ( accessor ) ;
assertThat ( query . getQueryObject ( ) ,
is ( Document . parse ( "{ 'lastname' : { '$regex' : '^(calamity|John regalia|regalia)'} }" ) ) ) ;
}
private StringBasedMongoQuery createQueryForMethod ( String name , Class < ? > . . . parameters ) throws Exception {
Method method = SampleRepository . class . getMethod ( name , parameters ) ;
@ -458,6 +533,9 @@ public class StringBasedMongoQueryUnitTests {
@@ -458,6 +533,9 @@ public class StringBasedMongoQueryUnitTests {
@Query ( "{ 'lastname' : { '$regex' : '^(?0)'} }" )
Person findByLastnameRegex ( String lastname ) ;
@Query ( "{'$or' : [{'firstname': {'$regex': '.*?0.*', '$options': 'i'}}, {'lastname' : {'$regex': '.*?0xyz.*', '$options': 'i'}} ]}" )
Person findByMultiRegex ( String arg0 ) ;
@Query ( "{ 'address' : ?0 }" )
Person findByAddress ( Address address ) ;
@ -508,5 +586,20 @@ public class StringBasedMongoQueryUnitTests {
@@ -508,5 +586,20 @@ public class StringBasedMongoQueryUnitTests {
@Query ( "{ 'arg0' : ?0 }" )
List < Person > findByWithBsonArgument ( Document arg0 ) ;
@Query ( "{ 'arg0' : ?0, 'arg1' : ?1, 'arg2' : ?0 }" )
List < Person > findByReusingPlaceholdersMultipleTimes ( String arg0 , String arg1 ) ;
@Query ( "{ 'arg0' : ?0, 'arg1' : ?1, 'arg2' : '?0' }" )
List < Person > findByReusingPlaceholdersMultipleTimesWhenQuoted ( String arg0 , String arg1 ) ;
@Query ( "{ 'arg0' : '?0', 'arg1' : ?1, 'arg2' : '?0s' }" )
List < Person > findByReusingPlaceholdersMultipleTimesWhenQuotedAndSomeStuffAppended ( String arg0 , String arg1 ) ;
@Query ( "{ 'arg0' : '?0', 'arg1' : '?1s' }" )
List < Person > findByWhenQuotedAndSomeStuffAppended ( String arg0 , String arg1 ) ;
@Query ( "{ 'lastname' : { '$regex' : '^(?0|John ?1|?1)'} }" ) // use spel or some regex string this is fucking bad
Person findByLastnameRegex ( String lastname , String alternative ) ;
}
}