Skip to content

Commit 6e0df99

Browse files
committed
Polish
1 parent 6d88d1f commit 6e0df99

File tree

2 files changed

+21
-36
lines changed

2 files changed

+21
-36
lines changed

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,11 @@ public void setHint(String hint) {
188188
@SuppressWarnings("unchecked")
189189
protected Iterator<T> doPageRead() {
190190
if (queryString != null) {
191-
Pageable pageRequest = new PageRequest(page, pageSize, sort);
191+
Pageable pageRequest = PageRequest.of(page, pageSize, sort);
192192

193193
String populatedQuery = replacePlaceholders(queryString, parameterValues);
194194

195-
Query mongoQuery = null;
195+
Query mongoQuery;
196196

197197
if(StringUtils.hasText(fields)) {
198198
mongoQuery = new BasicQuery(populatedQuery, fields);
@@ -214,7 +214,7 @@ protected Iterator<T> doPageRead() {
214214
}
215215

216216
} else {
217-
Pageable pageRequest = new PageRequest(page, pageSize);
217+
Pageable pageRequest = PageRequest.of(page, pageSize);
218218
query.with(pageRequest);
219219

220220
if(StringUtils.hasText(collection)) {
@@ -239,10 +239,7 @@ public void afterPropertiesSet() throws Exception {
239239
if (queryString != null) {
240240
Assert.state(sort != null, "A sort is required.");
241241
}
242-
if (query != null) {
243-
Assert.state(query.getSortObject() != null, "A Sort in Query object is required.");
244-
}
245-
242+
246243
if (query != null && query.getLimit() != 0) {
247244
log.warn("PageSize in Query object was ignored. Please set it by MongoItemReader.setPageSize().");
248245
}

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

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void setUp() throws Exception {
6363

6464
@Test
6565
public void testAfterPropertiesSetForQueryString() throws Exception{
66-
reader = new MongoItemReader<String>();
66+
reader = new MongoItemReader<>();
6767

6868
try {
6969
reader.afterPropertiesSet();
@@ -114,26 +114,14 @@ public void testAfterPropertiesSetForQueryString() throws Exception{
114114

115115
@Test
116116
public void testAfterPropertiesSetForQueryObject() throws Exception{
117-
reader = new MongoItemReader<String>();
117+
reader = new MongoItemReader<>();
118118

119119
reader.setTemplate(template);
120120
reader.setTargetType(String.class);
121121

122-
Query query1 = new Query();
122+
Query query1 = new Query().with(Sort.by(new Order(Sort.Direction.ASC, "_id")));
123123
reader.setQuery(query1);
124124

125-
try {
126-
reader.afterPropertiesSet();
127-
fail("Sort was not set but exception was not thrown.");
128-
} catch (IllegalStateException iae) {
129-
assertEquals("A Sort in Query object is required.", iae.getMessage());
130-
} catch (Throwable t) {
131-
fail("Wrong exception was thrown.");
132-
}
133-
134-
Query query2 = new Query().with(new Sort(new Order(Sort.Direction.ASC, "_id")));
135-
reader.setQuery(query2);
136-
137125
reader.afterPropertiesSet();
138126
}
139127

@@ -253,18 +241,18 @@ public void testQueryWithCollection() {
253241

254242
@Test
255243
public void testQueryObject() throws Exception {
256-
reader = new MongoItemReader<String>();
244+
reader = new MongoItemReader<>();
257245
reader.setTemplate(template);
258246

259247
Query query = new Query()
260-
.with(new Sort(new Order(Sort.Direction.ASC, "_id")));
248+
.with(Sort.by(new Order(Sort.Direction.ASC, "_id")));
261249
reader.setQuery(query);
262250
reader.setTargetType(String.class);
263251

264252
reader.afterPropertiesSet();
265253

266254
ArgumentCaptor<Query> queryContainer = ArgumentCaptor.forClass(Query.class);
267-
when(template.find(queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<String>());
255+
when(template.find(queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<>());
268256

269257
assertFalse(reader.doPageRead().hasNext());
270258

@@ -276,19 +264,19 @@ public void testQueryObject() throws Exception {
276264

277265
@Test
278266
public void testQueryObjectWithIgnoredPageSize() throws Exception {
279-
reader = new MongoItemReader<String>();
267+
reader = new MongoItemReader<>();
280268
reader.setTemplate(template);
281269

282270
Query query = new Query()
283-
.with(new Sort(new Order(Sort.Direction.ASC, "_id")))
284-
.with(new PageRequest(0, 50));
271+
.with(Sort.by(new Order(Sort.Direction.ASC, "_id")))
272+
.with(PageRequest.of(0, 50));
285273
reader.setQuery(query);
286274
reader.setTargetType(String.class);
287275

288276
reader.afterPropertiesSet();
289277

290278
ArgumentCaptor<Query> queryContainer = ArgumentCaptor.forClass(Query.class);
291-
when(template.find(queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<String>());
279+
when(template.find(queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<>());
292280

293281
assertFalse(reader.doPageRead().hasNext());
294282

@@ -300,20 +288,20 @@ public void testQueryObjectWithIgnoredPageSize() throws Exception {
300288

301289
@Test
302290
public void testQueryObjectWithPageSize() throws Exception {
303-
reader = new MongoItemReader<String>();
291+
reader = new MongoItemReader<>();
304292
reader.setTemplate(template);
305293

306294
Query query = new Query()
307-
.with(new Sort(new Order(Sort.Direction.ASC, "_id")))
308-
.with(new PageRequest(30, 50));
295+
.with(Sort.by(new Order(Sort.Direction.ASC, "_id")))
296+
.with(PageRequest.of(30, 50));
309297
reader.setQuery(query);
310298
reader.setTargetType(String.class);
311299
reader.setPageSize(100);
312300

313301
reader.afterPropertiesSet();
314302

315303
ArgumentCaptor<Query> queryContainer = ArgumentCaptor.forClass(Query.class);
316-
when(template.find(queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<String>());
304+
when(template.find(queryContainer.capture(), eq(String.class))).thenReturn(new ArrayList<>());
317305

318306
assertFalse(reader.doPageRead().hasNext());
319307

@@ -325,11 +313,11 @@ public void testQueryObjectWithPageSize() throws Exception {
325313

326314
@Test
327315
public void testQueryObjectWithCollection() throws Exception {
328-
reader = new MongoItemReader<String>();
316+
reader = new MongoItemReader<>();
329317
reader.setTemplate(template);
330318

331319
Query query = new Query()
332-
.with(new Sort(new Order(Sort.Direction.ASC, "_id")));
320+
.with(Sort.by(new Order(Sort.Direction.ASC, "_id")));
333321
reader.setQuery(query);
334322
reader.setTargetType(String.class);
335323
reader.setCollection("collection");
@@ -338,7 +326,7 @@ public void testQueryObjectWithCollection() throws Exception {
338326

339327
ArgumentCaptor<Query> queryContainer = ArgumentCaptor.forClass(Query.class);
340328
ArgumentCaptor<String> stringContainer = ArgumentCaptor.forClass(String.class);
341-
when(template.find(queryContainer.capture(), eq(String.class), stringContainer.capture())).thenReturn(new ArrayList<String>());
329+
when(template.find(queryContainer.capture(), eq(String.class), stringContainer.capture())).thenReturn(new ArrayList<>());
342330

343331
assertFalse(reader.doPageRead().hasNext());
344332

0 commit comments

Comments
 (0)