From 669768365fc929cffc4217166faa7c46f552e27f Mon Sep 17 00:00:00 2001 From: Henning Poettker Date: Thu, 9 Feb 2023 22:25:38 +0100 Subject: [PATCH] Prefer `Slice` over `Page` in `RepositoryItemReader` Resolves #4115 --- .../batch/item/data/RepositoryItemReader.java | 8 ++++---- .../item/data/RepositoryItemReaderTests.java | 9 +++++---- .../RepositoryItemReaderBuilderTests.java | 18 +++++++++--------- .../sample/data/CustomerCreditRepository.java | 6 +++--- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/RepositoryItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/RepositoryItemReader.java index d2590a71f..a6f9dee1b 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/RepositoryItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/data/RepositoryItemReader.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2023 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. @@ -28,9 +28,9 @@ import org.springframework.batch.item.adapter.AbstractMethodInvokingDelegator.In import org.springframework.batch.item.adapter.DynamicMethodInvocationException; import org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader; import org.springframework.beans.factory.InitializingBean; -import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Slice; import org.springframework.data.domain.Sort; import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.lang.Nullable; @@ -168,7 +168,7 @@ public class RepositoryItemReader extends AbstractItemCountingItemStreamItemR results = doPageRead(); page ++; - if(results.size() <= 0) { + if(results.isEmpty()) { return null; } @@ -220,7 +220,7 @@ public class RepositoryItemReader extends AbstractItemCountingItemStreamItemR invoker.setArguments(parameters.toArray()); - Page curPage = (Page) doInvoke(invoker); + Slice curPage = (Slice) doInvoke(invoker); return curPage.getContent(); } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/RepositoryItemReaderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/RepositoryItemReaderTests.java index 22e42f1b2..eca4a83e3 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/RepositoryItemReaderTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/RepositoryItemReaderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2020 the original author or authors. + * Copyright 2013-2023 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. @@ -29,10 +29,11 @@ import org.mockito.MockitoAnnotations; import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.item.adapter.DynamicMethodInvocationException; -import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Slice; +import org.springframework.data.domain.SliceImpl; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Direction; import org.springframework.data.repository.PagingAndSortingRepository; @@ -263,7 +264,7 @@ public class RepositoryItemReaderTests { reader.setMethodName("findFirstNames"); ArgumentCaptor pageRequestContainer = ArgumentCaptor.forClass(PageRequest.class); - when(differentRepository.findFirstNames(pageRequestContainer.capture())).thenReturn(new PageImpl<>(singletonList( + when(differentRepository.findFirstNames(pageRequestContainer.capture())).thenReturn(new SliceImpl<>(singletonList( "result" ))); @@ -365,7 +366,7 @@ public class RepositoryItemReaderTests { } public interface TestRepository extends PagingAndSortingRepository { - Page findFirstNames(Pageable pageable); + Slice findFirstNames(Pageable pageable); } // Simple object for readability diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/builder/RepositoryItemReaderBuilderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/builder/RepositoryItemReaderBuilderTests.java index a7daf8cf3..b2806bcc0 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/builder/RepositoryItemReaderBuilderTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/data/builder/RepositoryItemReaderBuilderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2018 the original author or authors. + * Copyright 2017-2023 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. @@ -28,8 +28,8 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.springframework.batch.item.data.RepositoryItemReader; -import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Slice; import org.springframework.data.domain.Sort; import org.springframework.data.repository.PagingAndSortingRepository; @@ -54,7 +54,7 @@ public class RepositoryItemReaderBuilderTests { private TestRepository repository; @Mock - private Page page; + private Slice slice; private Map sorts; @@ -69,9 +69,9 @@ public class RepositoryItemReaderBuilderTests { List testResult = new ArrayList<>(); testResult.add(TEST_CONTENT); - when(page.getContent()).thenReturn(testResult); - when(page.getSize()).thenReturn(5); - when(this.repository.foo(this.pageRequestContainer.capture())).thenReturn(this.page); + when(slice.getContent()).thenReturn(testResult); + when(slice.getSize()).thenReturn(5); + when(this.repository.foo(this.pageRequestContainer.capture())).thenReturn(this.slice); } @Test @@ -116,7 +116,7 @@ public class RepositoryItemReaderBuilderTests { ArgumentCaptor arg2Captor = ArgumentCaptor.forClass(String.class); ArgumentCaptor arg3Captor = ArgumentCaptor.forClass(String.class); when(this.repository.foo(arg1Captor.capture(), arg2Captor.capture(), arg3Captor.capture(), - this.pageRequestContainer.capture())).thenReturn(this.page); + this.pageRequestContainer.capture())).thenReturn(this.slice); String result = (String) reader.read(); assertEquals("Result returned from reader was not expected value.", TEST_CONTENT, result); @@ -244,7 +244,7 @@ public class RepositoryItemReaderBuilderTests { ArgumentCaptor arg2Captor = ArgumentCaptor.forClass(String.class); ArgumentCaptor arg3Captor = ArgumentCaptor.forClass(String.class); when(this.repository.foo(arg1Captor.capture(), arg2Captor.capture(), arg3Captor.capture(), - this.pageRequestContainer.capture())).thenReturn(this.page); + this.pageRequestContainer.capture())).thenReturn(this.slice); RepositoryItemReader reader = new RepositoryItemReaderBuilder<>().repository(this.repository) .sorts(this.sorts) @@ -264,7 +264,7 @@ public class RepositoryItemReaderBuilderTests { ArgumentCaptor arg2Captor = ArgumentCaptor.forClass(String.class); ArgumentCaptor arg3Captor = ArgumentCaptor.forClass(String.class); when(this.repository.foo(arg1Captor.capture(), arg2Captor.capture(), arg3Captor.capture(), - this.pageRequestContainer.capture())).thenReturn(this.page); + this.pageRequestContainer.capture())).thenReturn(this.slice); RepositoryItemReader reader = new RepositoryItemReaderBuilder<>().repository(this.repository) .sorts(this.sorts) diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/data/CustomerCreditRepository.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/data/CustomerCreditRepository.java index bccf791c9..8a1f9550f 100644 --- a/spring-batch-samples/src/main/java/org/springframework/batch/sample/data/CustomerCreditRepository.java +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/data/CustomerCreditRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 the original author or authors. + * Copyright 2013-2023 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. @@ -18,10 +18,10 @@ package org.springframework.batch.sample.data; import java.math.BigDecimal; import org.springframework.batch.sample.domain.trade.CustomerCredit; -import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Slice; import org.springframework.data.repository.PagingAndSortingRepository; public interface CustomerCreditRepository extends PagingAndSortingRepository{ - Page findByCreditGreaterThan(BigDecimal credit, Pageable request); + Slice findByCreditGreaterThan(BigDecimal credit, Pageable request); }