Skip to content

Commit 2e68451

Browse files
nstdiofmbenhassine
authored andcommitted
Check arguments of MongoItemReader#setSort
Issue #4014
1 parent 212b3ce commit 2e68451

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/MongoItemReader.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -164,6 +164,7 @@ public void setFields(String fields) {
164164
* @param sorts map of properties and direction to sort each.
165165
*/
166166
public void setSort(Map<String, Sort.Direction> sorts) {
167+
Assert.notNull(sorts, "Sorts must not be null");
167168
this.sort = convertToSort(sorts);
168169
}
169170

@@ -248,7 +249,7 @@ private String replacePlaceholders(String input, List<Object> values) {
248249
}
249250

250251
private Sort convertToSort(Map<String, Sort.Direction> sorts) {
251-
List<Sort.Order> sortValues = new ArrayList<>();
252+
List<Sort.Order> sortValues = new ArrayList<>(sorts.size());
252253

253254
for (Map.Entry<String, Sort.Direction> curSort : sorts.entrySet()) {
254255
sortValues.add(new Sort.Order(curSort.getValue(), curSort.getKey()));

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/MongoItemReaderTests.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2021 the original author or authors.
2+
* Copyright 2013-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@
3333
import org.springframework.data.mongodb.core.MongoOperations;
3434
import org.springframework.data.mongodb.core.query.Query;
3535

36+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
3637
import static org.junit.Assert.assertEquals;
3738
import static org.junit.Assert.assertFalse;
3839
import static org.junit.Assert.assertTrue;
@@ -377,4 +378,15 @@ public void testQueryObjectWithCollection() throws Exception {
377378
assertEquals(0, actualQuery.getSkip());
378379
assertEquals("collection", stringContainer.getValue());
379380
}
381+
382+
@Test
383+
public void testSortThrowsExceptionWhenInvokedWithNull() {
384+
// given
385+
reader = new MongoItemReader<>();
386+
387+
// when + then
388+
assertThatIllegalArgumentException()
389+
.isThrownBy(() -> reader.setSort(null))
390+
.withMessage("Sorts must not be null");
391+
}
380392
}

0 commit comments

Comments
 (0)