diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/MongoItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/MongoItemReader.java index 43c5ea9fd..bb37a9a81 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/MongoItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/MongoItemReader.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2022 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. @@ -82,9 +82,9 @@ import org.springframework.util.StringUtils; * @author Parikshit Dutta */ public class MongoItemReader extends AbstractPaginatedDataItemReader implements InitializingBean { - + private static final Logger log = LoggerFactory.getLogger(MongoItemReader.class); - + private MongoOperations template; private Query query; private String queryString; @@ -168,6 +168,7 @@ public class MongoItemReader extends AbstractPaginatedDataItemReader imple * @param sorts map of properties and direction to sort each. */ public void setSort(Map sorts) { + Assert.notNull(sorts, "Sorts must not be null"); this.sort = convertToSort(sorts); } @@ -252,7 +253,7 @@ public class MongoItemReader extends AbstractPaginatedDataItemReader imple } private Sort convertToSort(Map sorts) { - List sortValues = new ArrayList<>(); + List sortValues = new ArrayList<>(sorts.size()); for (Map.Entry curSort : sorts.entrySet()) { sortValues.add(new Sort.Order(curSort.getValue(), curSort.getKey())); diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/MongoItemReaderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/MongoItemReaderTests.java index c01f9018d..e3d1c0d79 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/MongoItemReaderTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/MongoItemReaderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2020 the original author or authors. + * Copyright 2013-2022 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. @@ -31,6 +31,7 @@ import org.springframework.data.domain.Sort.Order; import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.query.Query; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -379,4 +380,15 @@ public class MongoItemReaderTests { assertEquals(0, actualQuery.getSkip()); assertEquals("collection", stringContainer.getValue()); } + + @Test + public void testSortThrowsExceptionWhenInvokedWithNull() { + // given + reader = new MongoItemReader<>(); + + // when + then + assertThatIllegalArgumentException() + .isThrownBy(() -> reader.setSort(null)) + .withMessage("Sorts must not be null"); + } }