Skip to content

Commit 6417065

Browse files
nstdiofmbenhassine
authored andcommitted
Check arguments of MongoItemReader#setSort
Issue #4014
1 parent d8b8bab commit 6417065

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 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.
@@ -82,9 +82,9 @@
8282
* @author Parikshit Dutta
8383
*/
8484
public class MongoItemReader<T> extends AbstractPaginatedDataItemReader<T> implements InitializingBean {
85-
85+
8686
private static final Logger log = LoggerFactory.getLogger(MongoItemReader.class);
87-
87+
8888
private MongoOperations template;
8989
private Query query;
9090
private String queryString;
@@ -168,6 +168,7 @@ public void setFields(String fields) {
168168
* @param sorts map of properties and direction to sort each.
169169
*/
170170
public void setSort(Map<String, Sort.Direction> sorts) {
171+
Assert.notNull(sorts, "Sorts must not be null");
171172
this.sort = convertToSort(sorts);
172173
}
173174

@@ -252,7 +253,7 @@ private String replacePlaceholders(String input, List<Object> values) {
252253
}
253254

254255
private Sort convertToSort(Map<String, Sort.Direction> sorts) {
255-
List<Sort.Order> sortValues = new ArrayList<>();
256+
List<Sort.Order> sortValues = new ArrayList<>(sorts.size());
256257

257258
for (Map.Entry<String, Sort.Direction> curSort : sorts.entrySet()) {
258259
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-2020 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.
@@ -31,6 +31,7 @@
3131
import org.springframework.data.mongodb.core.MongoOperations;
3232
import org.springframework.data.mongodb.core.query.Query;
3333

34+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
3435
import static org.junit.Assert.assertEquals;
3536
import static org.junit.Assert.assertFalse;
3637
import static org.junit.Assert.assertTrue;
@@ -379,4 +380,15 @@ public void testQueryObjectWithCollection() throws Exception {
379380
assertEquals(0, actualQuery.getSkip());
380381
assertEquals("collection", stringContainer.getValue());
381382
}
383+
384+
@Test
385+
public void testSortThrowsExceptionWhenInvokedWithNull() {
386+
// given
387+
reader = new MongoItemReader<>();
388+
389+
// when + then
390+
assertThatIllegalArgumentException()
391+
.isThrownBy(() -> reader.setSort(null))
392+
.withMessage("Sorts must not be null");
393+
}
382394
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy