diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java index e4edae82f..fb40c731f 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java @@ -24,6 +24,7 @@ import java.util.List; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; +import org.springframework.data.domain.Sort.Order; import org.springframework.data.mongodb.InvalidMongoDbApiUsageException; import org.springframework.util.Assert; @@ -150,6 +151,13 @@ public class Query { return this; } + for (Order order : sort) { + if (order.isIgnoreCase()) { + throw new IllegalArgumentException(String.format("Gven sort contained an Order for %s with ignore case! " + + "MongoDB does not support sorting ignoreing case currently!", order.getProperty())); + } + } + if (this.coreSort == null) { this.coreSort = sort; } else { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/QueryTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/QueryTests.java index 76bdda8d2..41f02f0a9 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/QueryTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/QueryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2012 the original author or authors. + * Copyright 2010-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,12 +20,24 @@ import static org.junit.Assert.*; import static org.springframework.data.mongodb.core.query.Criteria.*; import org.junit.Assert; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Direction; import org.springframework.data.mongodb.InvalidMongoDbApiUsageException; +/** + * Unit tests for {@link Query}. + * + * @author Thomas Risberg + * @author Oliver Gierke + */ public class QueryTests { + @Rule + public ExpectedException exception = ExpectedException.none(); + @Test public void testSimpleQuery() { Query q = new Query(where("name").is("Thomas").and("age").lt(80)); @@ -170,4 +182,13 @@ public class QueryTests { Query query = new Query().with(new org.springframework.data.domain.Sort(Direction.DESC, "foo")); assertThat(query.getSortObject().toString(), is("{ \"foo\" : -1}")); } + + @Test + public void rejectsOrderWithIgnoreCase() { + + exception.expect(IllegalArgumentException.class); + exception.expectMessage("foo"); + + new Query().with(new Sort(new Sort.Order("foo").ignoreCase())); + } }