From 2e68451cf39c91e531312f163653e591ff445414 Mon Sep 17 00:00:00 2001 From: Edgar Asatryan Date: Sun, 17 Oct 2021 01:10:26 +0400 Subject: [PATCH] Check arguments of MongoItemReader#setSort Issue #4014 --- .../batch/item/data/MongoItemReader.java | 5 +++-- .../batch/item/data/MongoItemReaderTests.java | 14 +++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) 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 6e5083c066..734fce0ad9 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-2021 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. @@ -164,6 +164,7 @@ public void setFields(String fields) { * @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); } @@ -248,7 +249,7 @@ private String replacePlaceholders(String input, List values) { } 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 7bff682799..35471b0a66 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-2021 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. @@ -33,6 +33,7 @@ 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; @@ -377,4 +378,15 @@ public void testQueryObjectWithCollection() throws Exception { 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"); + } }