Skip to content

Commit 56fbc27

Browse files
parikshitduttafmbenhassine
authored andcommitted
Removed redundant logical checks from MongoItemReaderBuilder and MongoItemReader
to prevent false alarm for not providing the limit for query or pagesize for the reader Issue #3673
1 parent bd016a9 commit 56fbc27

File tree

4 files changed

+84
-10
lines changed

4 files changed

+84
-10
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
* @author Michael Minella
8080
* @author Takaaki Iida
8181
* @author Mahmoud Ben Hassine
82+
* @author Parikshit Dutta
8283
*/
8384
public class MongoItemReader<T> extends AbstractPaginatedDataItemReader<T> implements InitializingBean {
8485

@@ -241,10 +242,6 @@ public void afterPropertiesSet() throws Exception {
241242
if (queryString != null) {
242243
Assert.state(sort != null, "A sort is required.");
243244
}
244-
245-
if (query != null && query.getLimit() != 0) {
246-
log.warn("PageSize in Query object was ignored. Please set it by MongoItemReader.setPageSize().");
247-
}
248245
}
249246

250247
private String replacePlaceholders(String input, List<Object> values) {

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
* @author Glenn Renfro
3535
* @author Mahmoud Ben Hassine
3636
* @author Drummond Dawson
37+
* @author Parikshit Dutta
3738
* @since 4.0
3839
* @see MongoItemReader
3940
*/
@@ -286,9 +287,6 @@ public MongoItemReader<T> build() {
286287
if(StringUtils.hasText(this.jsonQuery)) {
287288
Assert.notNull(this.sorts, "sorts map is required.");
288289
}
289-
else {
290-
Assert.state(this.query.getLimit() != 0, "PageSize in Query object was ignored.");
291-
}
292290

293291
MongoItemReader<T> reader = new MongoItemReader<>();
294292
reader.setTemplate(this.template);

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

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2018 the original author or authors.
2+
* Copyright 2013-2020 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.
@@ -38,6 +38,10 @@
3838
import static org.mockito.ArgumentMatchers.eq;
3939
import static org.mockito.Mockito.when;
4040

41+
/**
42+
* @author Michael Minella
43+
* @author Parikshit Dutta
44+
*/
4145
public class MongoItemReaderTests {
4246

4347
private MongoItemReader<String> reader;
@@ -310,7 +314,46 @@ public void testQueryObjectWithPageSize() throws Exception {
310314
assertEquals(100, actualQuery.getLimit());
311315
assertEquals(0, actualQuery.getSkip());
312316
}
313-
317+
318+
@Test
319+
public void testQueryObjectWithoutLimit() throws Exception {
320+
reader = new MongoItemReader<>();
321+
reader.setTemplate(template);
322+
323+
reader.setQuery(new Query());
324+
reader.setTargetType(String.class);
325+
reader.setPageSize(100);
326+
327+
reader.afterPropertiesSet();
328+
329+
ArgumentCaptor<Query> queryContainer = ArgumentCaptor.forClass(Query.class);
330+
when(template.find(queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<>());
331+
332+
assertFalse(reader.doPageRead().hasNext());
333+
334+
Query actualQuery = queryContainer.getValue();
335+
assertEquals(100, actualQuery.getLimit());
336+
}
337+
338+
@Test
339+
public void testQueryObjectWithoutLimitAndPageSize() throws Exception {
340+
reader = new MongoItemReader<>();
341+
reader.setTemplate(template);
342+
343+
reader.setQuery(new Query());
344+
reader.setTargetType(String.class);
345+
346+
reader.afterPropertiesSet();
347+
348+
ArgumentCaptor<Query> queryContainer = ArgumentCaptor.forClass(Query.class);
349+
when(template.find(queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<>());
350+
351+
assertFalse(reader.doPageRead().hasNext());
352+
353+
Query actualQuery = queryContainer.getValue();
354+
assertEquals(10, actualQuery.getLimit());
355+
}
356+
314357
@Test
315358
public void testQueryObjectWithCollection() throws Exception {
316359
reader = new MongoItemReader<>();

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2018 the original author or authors.
2+
* Copyright 2017-2020 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.
@@ -41,6 +41,7 @@
4141
/**
4242
* @author Glenn Renfro
4343
* @author Drummond Dawson
44+
* @author Parikshit Dutta
4445
*/
4546
public class MongoItemReaderBuilderTests {
4647
@Mock
@@ -145,6 +146,41 @@ public void testVarargs() throws Exception {
145146
assertEquals("collection", collectionContainer.getValue());
146147
}
147148

149+
@Test
150+
public void testWithoutQueryLimit() throws Exception {
151+
MongoItemReader<String> reader = new MongoItemReaderBuilder<String>().template(this.template)
152+
.targetType(String.class)
153+
.query(new Query())
154+
.sorts(this.sortOptions)
155+
.name("mongoReaderTest")
156+
.pageSize(50)
157+
.build();
158+
159+
when(template.find(this.queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<>());
160+
161+
assertNull("reader should not return result", reader.read());
162+
163+
Query query = this.queryContainer.getValue();
164+
assertEquals(50, query.getLimit());
165+
}
166+
167+
@Test
168+
public void testWithoutQueryLimitAndPageSize() throws Exception {
169+
MongoItemReader<String> reader = new MongoItemReaderBuilder<String>().template(this.template)
170+
.targetType(String.class)
171+
.query(new Query())
172+
.sorts(this.sortOptions)
173+
.name("mongoReaderTest")
174+
.build();
175+
176+
when(template.find(this.queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<>());
177+
178+
assertNull("reader should not return result", reader.read());
179+
180+
Query query = this.queryContainer.getValue();
181+
assertEquals(10, query.getLimit());
182+
}
183+
148184
@Test
149185
public void testNullTemplate() {
150186
validateExceptionMessage(new MongoItemReaderBuilder<String>().targetType(String.class)

0 commit comments

Comments
 (0)