Skip to content

Commit 0a947da

Browse files
committed
DATAMONGO-1668 - Polishing.
Replace new Sort(…) with Sort.by(…) and new PageRequest(…) with PageRequest.of(…).
1 parent eb85fb3 commit 0a947da

25 files changed

+108
-95
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Aggregation.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ public static SortOperation sort(Sort sort) {
348348
* @return
349349
*/
350350
public static SortOperation sort(Direction direction, String... fields) {
351-
return new SortOperation(new Sort(direction, fields));
351+
return new SortOperation(Sort.by(direction, fields));
352352
}
353353

354354
/**
@@ -585,7 +585,7 @@ public Document toDocument(String inputCollectionName, AggregationOperationConte
585585
return command;
586586
}
587587

588-
/*
588+
/*
589589
* (non-Javadoc)
590590
* @see java.lang.Object#toString()
591591
*/

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/SortOperation.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* @author Thomas Darimont
3232
* @author Oliver Gierke
3333
* @author Christoph Strobl
34+
* @author Mark Paluch
3435
* @since 1.3
3536
* @see <a href="https://docs.mongodb.com/manual/reference/operator/aggregation/sort/">MongoDB Aggregation Framework: $sort</a>
3637
*/
@@ -40,7 +41,7 @@ public class SortOperation implements AggregationOperation {
4041

4142
/**
4243
* Creates a new {@link SortOperation} for the given {@link Sort} instance.
43-
*
44+
*
4445
* @param sort must not be {@literal null}.
4546
*/
4647
public SortOperation(Sort sort) {
@@ -50,14 +51,14 @@ public SortOperation(Sort sort) {
5051
}
5152

5253
public SortOperation and(Direction direction, String... fields) {
53-
return and(new Sort(direction, fields));
54+
return and(Sort.by(direction, fields));
5455
}
5556

5657
public SortOperation and(Sort sort) {
5758
return new SortOperation(this.sort.and(sort));
5859
}
5960

60-
/*
61+
/*
6162
* (non-Javadoc)
6263
* @see org.springframework.data.mongodb.core.aggregation.AggregationOperation#toDocument(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext)
6364
*/

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
import org.springframework.dao.DataAccessException;
5858
import org.springframework.dao.DataIntegrityViolationException;
5959
import org.springframework.dao.DuplicateKeyException;
60-
import org.springframework.dao.InvalidDataAccessApiUsageException;
6160
import org.springframework.dao.OptimisticLockingFailureException;
6261
import org.springframework.data.annotation.Id;
6362
import org.springframework.data.annotation.PersistenceConstructor;
@@ -1122,7 +1121,7 @@ public void testFindOneWithSort() {
11221121

11231122
// test query with a sort
11241123
Query q2 = new Query(Criteria.where("age").gt(10));
1125-
q2.with(new Sort(Direction.DESC, "age"));
1124+
q2.with(Sort.by(Direction.DESC, "age"));
11261125
PersonWithAList p5 = template.findOne(q2, PersonWithAList.class);
11271126
assertThat(p5.getFirstName(), is("Mark"));
11281127
}
@@ -2064,8 +2063,8 @@ public void itShouldBePossibleToReuseAnExistingQuery() {
20642063

20652064
assertThat(template.count(query, Sample.class), is(1L));
20662065

2067-
query.with(new PageRequest(0, 10));
2068-
query.with(new Sort("field"));
2066+
query.with(PageRequest.of(0, 10));
2067+
query.with(Sort.by("field"));
20692068

20702069
assertThat(template.find(query, Sample.class), is(not(empty())));
20712070
}
@@ -2696,7 +2695,7 @@ public void sortOnIdFieldPropertyShouldBeMappedCorrectly() {
26962695
template.save(one);
26972696
template.save(two);
26982697

2699-
Query query = query(where("_id").in("1", "2")).with(new Sort(Direction.DESC, "someIdKey"));
2698+
Query query = query(where("_id").in("1", "2")).with(Sort.by(Direction.DESC, "someIdKey"));
27002699
assertThat(template.find(query, DoucmentWithNamedIdField.class), contains(two, one));
27012700
}
27022701

@@ -2714,7 +2713,7 @@ public void sortOnAnnotatedFieldPropertyShouldBeMappedCorrectly() {
27142713
template.save(one);
27152714
template.save(two);
27162715

2717-
Query query = query(where("_id").in("1", "2")).with(new Sort(Direction.DESC, "value"));
2716+
Query query = query(where("_id").in("1", "2")).with(Sort.by(Direction.DESC, "value"));
27182717
assertThat(template.find(query, DoucmentWithNamedIdField.class), contains(two, one));
27192718
}
27202719

@@ -2819,7 +2818,7 @@ public void takesSortIntoAccountWhenStreaming() {
28192818
template.insertAll(Arrays.asList(oldestPerson, youngestPerson));
28202819

28212820
Query q = new Query();
2822-
q.with(new Sort(Direction.ASC, "age"));
2821+
q.with(Sort.by(Direction.ASC, "age"));
28232822
CloseableIterator<Person> stream = template.stream(q, Person.class);
28242823

28252824
assertThat(stream.next().getAge(), is(youngestPerson.getAge()));
@@ -2835,7 +2834,7 @@ public void takesLimitIntoAccountWhenStreaming() {
28352834
template.insertAll(Arrays.asList(oldestPerson, youngestPerson));
28362835

28372836
Query q = new Query();
2838-
q.with(new PageRequest(0, 1, new Sort(Direction.ASC, "age")));
2837+
q.with(PageRequest.of(0, 1, Sort.by(Direction.ASC, "age")));
28392838
CloseableIterator<Person> stream = template.stream(q, Person.class);
28402839

28412840
assertThat(stream.next().getAge(), is(youngestPerson.getAge()));

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUnitTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
*
8888
* @author Oliver Gierke
8989
* @author Christoph Strobl
90+
* @author Mark Paluch
9091
*/
9192
@RunWith(MockitoJUnitRunner.class)
9293
public class MongoTemplateUnitTests extends MongoOperationsUnitTests {
@@ -321,7 +322,7 @@ public void findAllAndRemoveShouldNotTriggerRemoveIfFindResultIsEmpty() {
321322
@Test // DATAMONGO-948
322323
public void sortShouldBeTakenAsIsWhenExecutingQueryWithoutSpecificTypeInformation() {
323324

324-
Query query = Query.query(Criteria.where("foo").is("bar")).with(new Sort("foo"));
325+
Query query = Query.query(Criteria.where("foo").is("bar")).with(Sort.by("foo"));
325326
template.executeQuery(query, "collection1", new DocumentCallbackHandler() {
326327

327328
@Override

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/SortOperationUnitTests.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,16 @@
2626

2727
/**
2828
* Unit tests for {@link SortOperation}.
29-
*
29+
*
3030
* @author Oliver Gierke
31+
* @author Mark Paluch
3132
*/
3233
public class SortOperationUnitTests {
3334

3435
@Test
3536
public void createsDocumentForAscendingSortCorrectly() {
3637

37-
SortOperation operation = new SortOperation(new Sort(Direction.ASC, "foobar"));
38+
SortOperation operation = new SortOperation(Sort.by(Direction.ASC, "foobar"));
3839
Document result = operation.toDocument(Aggregation.DEFAULT_CONTEXT);
3940

4041
Document sortValue = getAsDocument(result, "$sort");
@@ -45,7 +46,7 @@ public void createsDocumentForAscendingSortCorrectly() {
4546
@Test
4647
public void createsDocumentForDescendingSortCorrectly() {
4748

48-
SortOperation operation = new SortOperation(new Sort(Direction.DESC, "foobar"));
49+
SortOperation operation = new SortOperation(Sort.by(Direction.DESC, "foobar"));
4950
Document result = operation.toDocument(Aggregation.DEFAULT_CONTEXT);
5051

5152
Document sortValue = getAsDocument(result, "$sort");

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464

6565
/**
6666
* Unit tests for {@link QueryMapper}.
67-
*
67+
*
6868
* @author Oliver Gierke
6969
* @author Patryk Wasik
7070
* @author Thomas Darimont
@@ -565,7 +565,7 @@ public void shouldMapQueryForNestedCustomizedPropertiesUsingConfiguredFieldNames
565565
@Test // DATAMONGO-647
566566
public void customizedFieldNameShouldBeMappedCorrectlyWhenApplyingSort() {
567567

568-
Query query = query(where("field").is("bar")).with(new Sort(Direction.DESC, "field"));
568+
Query query = query(where("field").is("bar")).with(Sort.by(Direction.DESC, "field"));
569569
org.bson.Document document = mapper.getMappedObject(query.getSortObject(),
570570
context.getPersistentEntity(CustomizedField.class));
571571
assertThat(document, equalTo(new org.bson.Document().append("foo", -1)));
@@ -597,7 +597,7 @@ public void getMappedFieldsReplacesTextScoreFieldProperlyCorrectlyWhenPresent()
597597
@Test // DATAMONGO-973
598598
public void getMappedSortAppendsTextScoreProperlyWhenSortedByScore() {
599599

600-
Query query = new Query().with(new Sort("textScore"));
600+
Query query = new Query().with(Sort.by("textScore"));
601601

602602
org.bson.Document document = mapper.getMappedSort(query.getSortObject(),
603603
context.getPersistentEntity(WithTextScoreProperty.class));
@@ -608,7 +608,7 @@ public void getMappedSortAppendsTextScoreProperlyWhenSortedByScore() {
608608
@Test // DATAMONGO-973
609609
public void getMappedSortIgnoresTextScoreWhenNotSortedByScore() {
610610

611-
Query query = new Query().with(new Sort("id"));
611+
Query query = new Query().with(Sort.by("id"));
612612

613613
org.bson.Document document = mapper.getMappedSort(query.getSortObject(),
614614
context.getPersistentEntity(WithTextScoreProperty.class));
@@ -643,7 +643,7 @@ public void shouldUseExplicitlySetFieldnameForIdPropertyCandidates() {
643643
@Test // DATAMONGO-1050
644644
public void shouldUseExplicitlySetFieldnameForIdPropertyCandidatesUsedInSortClause() {
645645

646-
Query query = new Query().with(new Sort("nested.id"));
646+
Query query = new Query().with(Sort.by("nested.id"));
647647

648648
org.bson.Document document = mapper.getMappedSort(query.getSortObject(),
649649
context.getPersistentEntity(RootForClassWithExplicitlyRenamedIdField.class));

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/UpdateMapperUnitTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ public void updatePushEachWithValueSortShouldRenderCorrectly() {
379379
public void updatePushEachWithDocumentSortShouldRenderCorrectly() {
380380

381381
Update update = new Update().push("list")
382-
.sort(new Sort(new Order(Direction.ASC, "value"), new Order(Direction.ASC, "field")))
382+
.sort(Sort.by(new Order(Direction.ASC, "value"), new Order(Direction.ASC, "field")))
383383
.each(Collections.emptyList());
384384

385385
Document mappedObject = mapper.getMappedObject(update.getUpdateObject(),
@@ -397,7 +397,7 @@ public void updatePushEachWithDocumentSortShouldRenderCorrectly() {
397397
public void updatePushEachWithSortShouldRenderCorrectlyWhenUsingMultiplePush() {
398398

399399
Update update = new Update().push("authors").sort(Direction.ASC).each("Harry").push("chapters")
400-
.sort(new Sort(Direction.ASC, "order")).each(Collections.emptyList());
400+
.sort(Sort.by(Direction.ASC, "order")).each(Collections.emptyList());
401401

402402
Document mappedObject = mapper.getMappedObject(update.getUpdateObject(), context.getPersistentEntity(Object.class));
403403

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/MappingTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
* @author Jon Brisbin
5252
* @author Oliver Gierke
5353
* @author Thomas Darimont
54+
* @author Mark Paluch
5455
*/
5556
public class MappingTests extends AbstractIntegrationTests {
5657

@@ -427,7 +428,7 @@ public void testNoMappingAnnotationsUsingStringAsId() {
427428
template.insert(p4);
428429

429430
Query q = query(where("id").in("1", "2"));
430-
q.with(new Sort(Direction.ASC, "id"));
431+
q.with(Sort.by(Direction.ASC, "id"));
431432
List<PersonPojoStringId> people = template.find(q, PersonPojoStringId.class);
432433
assertEquals(2, people.size());
433434

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/NearQueryUnitTests.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@
2929

3030
/**
3131
* Unit tests for {@link NearQuery}.
32-
*
32+
*
3333
* @author Oliver Gierke
3434
* @author Thomas Darimont
3535
* @author Christoph Strobl
36+
* @author Mark Paluch
3637
*/
3738
public class NearQueryUnitTests {
3839

@@ -86,7 +87,7 @@ public void configuresResultMetricCorrectly() {
8687
@Test // DATAMONGO-445
8788
public void shouldTakeSkipAndLimitSettingsFromGivenPageable() {
8889

89-
Pageable pageable = new PageRequest(3, 5);
90+
Pageable pageable = PageRequest.of(3, 5);
9091
NearQuery query = NearQuery.near(new Point(1, 1)).with(pageable);
9192

9293
assertThat(query.getSkip(), is((long)pageable.getPageNumber() * pageable.getPageSize()));
@@ -110,7 +111,7 @@ public void shouldTakeSkipAndLimitSettingsFromPageableEvenIfItWasSpecifiedOnQuer
110111

111112
int limit = 10;
112113
int skip = 5;
113-
Pageable pageable = new PageRequest(3, 5);
114+
Pageable pageable = PageRequest.of(3, 5);
114115
NearQuery query = NearQuery.near(new Point(1, 1))
115116
.query(Query.query(Criteria.where("foo").is("bar")).limit(limit).skip(skip)).with(pageable);
116117

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/QueryTests.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* @author Patryk Wasik
4141
* @author Thomas Darimont
4242
* @author Christoph Strobl
43+
* @author Mark Paluch
4344
*/
4445
public class QueryTests {
4546

@@ -190,7 +191,7 @@ public void testQueryWithRegexAndOption() {
190191
@Test // DATAMONGO-538
191192
public void addsSortCorrectly() {
192193

193-
Query query = new Query().with(new Sort(Direction.DESC, "foo"));
194+
Query query = new Query().with(Sort.by(Direction.DESC, "foo"));
194195
assertThat(query.getSortObject(), is(Document.parse("{ \"foo\" : -1}")));
195196
}
196197

@@ -200,7 +201,7 @@ public void rejectsOrderWithIgnoreCase() {
200201
exception.expect(IllegalArgumentException.class);
201202
exception.expectMessage("foo");
202203

203-
new Query().with(new Sort(new Sort.Order("foo").ignoreCase()));
204+
new Query().with(Sort.by(new Sort.Order("foo").ignoreCase()));
204205
}
205206

206207
@Test // DATAMONGO-709

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/SortTests.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,30 @@
2525

2626
/**
2727
* Unit tests for sorting.
28-
*
28+
*
2929
* @author Oliver Gierke
30+
* @author Mark Paluch
3031
*/
3132
public class SortTests {
3233

3334
@Test
3435
public void testWithSortAscending() {
35-
Query s = new Query().with(new Sort(Direction.ASC, "name"));
36+
37+
Query s = new Query().with(Sort.by(Direction.ASC, "name"));
3638
assertEquals(Document.parse("{ \"name\" : 1}"), s.getSortObject());
3739
}
3840

3941
@Test
4042
public void testWithSortDescending() {
41-
Query s = new Query().with(new Sort(Direction.DESC, "name"));
43+
44+
Query s = new Query().with(Sort.by(Direction.DESC, "name"));
4245
assertEquals(Document.parse("{ \"name\" : -1}"), s.getSortObject());
4346
}
4447

4548
@Test // DATADOC-177
4649
public void preservesOrderKeysOnMultipleSorts() {
4750

48-
Query sort = new Query().with(new Sort(Direction.DESC, "foo").and(new Sort(Direction.DESC, "bar")));
51+
Query sort = new Query().with(Sort.by(Direction.DESC, "foo").and(Sort.by(Direction.DESC, "bar")));
4952
assertThat(sort.getSortObject(), is(Document.parse("{ \"foo\" : -1 , \"bar\" : -1}")));
5053
}
5154
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/TextQueryTests.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
/**
4949
* @author Christoph Strobl
50+
* @author Mark Paluch
5051
*/
5152
public class TextQueryTests extends AbstractIntegrationTests {
5253

@@ -207,12 +208,12 @@ public void shouldApplyPaginationCorrectly() {
207208

208209
// page 1
209210
List<FullTextDoc> result = template
210-
.find(new TextQuery("bake coffee cake").sortByScore().with(new PageRequest(0, 2)), FullTextDoc.class);
211+
.find(new TextQuery("bake coffee cake").sortByScore().with(PageRequest.of(0, 2)), FullTextDoc.class);
211212
assertThat(result, hasSize(2));
212213
assertThat(result, contains(BAKE, COFFEE));
213214

214215
// page 2
215-
result = template.find(new TextQuery("bake coffee cake").sortByScore().with(new PageRequest(1, 2)),
216+
result = template.find(new TextQuery("bake coffee cake").sortByScore().with(PageRequest.of(1, 2)),
216217
FullTextDoc.class);
217218
assertThat(result, hasSize(1));
218219
assertThat(result, contains(CAKE));

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/TextQueryUnitTests.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424

2525
/**
2626
* Unit tests for {@link TextQuery}.
27-
*
27+
*
2828
* @author Christoph Strobl
29+
* @author Mark Paluch
2930
*/
3031
public class TextQueryUnitTests {
3132

@@ -65,7 +66,7 @@ public void shouldIncludeSortingByScoreCorrectly() {
6566
public void shouldNotOverrideExistingSort() {
6667

6768
TextQuery query = new TextQuery(QUERY);
68-
query.with(new Sort(Direction.DESC, "foo"));
69+
query.with(Sort.by(Direction.DESC, "foo"));
6970
query.sortByScore();
7071

7172
assertThat(query,

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/gridfs/GridFsTemplateIntegrationTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
/**
4747
* Integration tests for {@link GridFsTemplate}.
48-
*
48+
*
4949
* @author Oliver Gierke
5050
* @author Philipp Schneider
5151
* @author Thomas Darimont
@@ -151,7 +151,7 @@ public void considersSortWhenQueryingFiles() throws IOException {
151151
ObjectId third = operations.store(resource.getInputStream(), "foobar.xml");
152152
ObjectId first = operations.store(resource.getInputStream(), "bar.xml");
153153

154-
Query query = new Query().with(new Sort(Direction.ASC, "filename"));
154+
Query query = new Query().with(Sort.by(Direction.ASC, "filename"));
155155

156156
List<com.mongodb.client.gridfs.model.GridFSFile> files = new ArrayList<com.mongodb.client.gridfs.model.GridFSFile>();
157157
GridFSFindIterable result = operations.find(query);

0 commit comments

Comments
 (0)