From dc6618c475ea344a1bad6cfd34a6ad09cfb6b433 Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Fri, 20 Nov 2020 14:38:16 +0100 Subject: [PATCH] Polish contribution cbdb251441968197aca805cdda4e634ab8542c39 * Fix assertion messages in tests * Use transactional tests instead of a separate service with a transactional method --- .../RepositoryItemReaderIntegrationTests.java | 67 +++++++++++-------- .../item/sample/books/data/SimpleService.java | 55 --------------- ...epositoryItemReaderCommonTests-context.xml | 4 -- 3 files changed, 38 insertions(+), 88 deletions(-) delete mode 100644 spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/sample/books/data/SimpleService.java diff --git a/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/database/RepositoryItemReaderIntegrationTests.java b/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/database/RepositoryItemReaderIntegrationTests.java index 71db4c2d4..e1dcfe12b 100644 --- a/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/database/RepositoryItemReaderIntegrationTests.java +++ b/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/database/RepositoryItemReaderIntegrationTests.java @@ -16,6 +16,7 @@ package org.springframework.batch.item.database; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import java.util.List; @@ -27,21 +28,19 @@ import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.item.data.RepositoryItemReader; import org.springframework.batch.item.sample.books.Author; import org.springframework.batch.item.sample.books.Book; -import org.springframework.batch.item.sample.books.data.SimpleService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.transaction.annotation.Transactional; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "RepositoryItemReaderCommonTests-context.xml") +@Transactional public class RepositoryItemReaderIntegrationTests { private static final String CONTEXT_KEY = "RepositoryItemReader.read.count"; - @Autowired - private SimpleService service; - @Autowired private RepositoryItemReader reader; @@ -52,51 +51,59 @@ public class RepositoryItemReaderIntegrationTests { @Test public void testReadFromFirstPos() throws Exception { - service.openReader(new ExecutionContext()); + reader.open(new ExecutionContext()); - final List books = service.nextAuthorBooks(); + Author author = reader.read(); - assertEquals("Books list size", 2, books.size()); - assertEquals("First book", "author 1 - book 1", books.get(0).getName()); - assertEquals("Second book", "author 1 - book 2", books.get(1).getName()); + assertNotNull(author); + final List books = author.getBooks(); + assertEquals("Books list size must be = 2", 2, books.size()); + assertEquals("First book must be author 1 - book 1", "author 1 - book 1", books.get(0).getName()); + assertEquals("Second book must be author 1 - book 2", "author 1 - book 2", books.get(1).getName()); } @Test public void testReadFromWithinPage() throws Exception { reader.setCurrentItemCount(1); - service.openReader(new ExecutionContext()); + reader.open(new ExecutionContext()); - final List books = service.nextAuthorBooks(); + Author author = reader.read(); - assertEquals("Books list size", 2, books.size()); - assertEquals("First book", "author 2 - book 1", books.get(0).getName()); - assertEquals("Second book", "author 2 - book 2", books.get(1).getName()); + assertNotNull(author); + final List books = author.getBooks(); + assertEquals("Books list size must be = 2", 2, books.size()); + assertEquals("First book must be author 2 - book 1", "author 2 - book 1", books.get(0).getName()); + assertEquals("Second book must be author 2 - book 2", "author 2 - book 2", books.get(1).getName()); } @Test public void testReadFromNewPage() throws Exception { reader.setPageSize(2); reader.setCurrentItemCount(2); // 3rd item = 1rst of page 2 - service.openReader(new ExecutionContext()); + reader.open(new ExecutionContext()); - final List books = service.nextAuthorBooks(); + Author author = reader.read(); - assertEquals("Books list size", 2, books.size()); - assertEquals("First book", "author 3 - book 1", books.get(0).getName()); - assertEquals("Second book", "author 3 - book 2", books.get(1).getName()); + assertNotNull(author); + final List books = author.getBooks(); + assertEquals("Books list size must be = 2", 2, books.size()); + assertEquals("First book must be author 3 - book 1", "author 3 - book 1", books.get(0).getName()); + assertEquals("Second book must be author 3 - book 2", "author 3 - book 2", books.get(1).getName()); } @Test public void testReadFromWithinPage_Restart() throws Exception { final ExecutionContext executionContext = new ExecutionContext(); executionContext.putInt(CONTEXT_KEY, 1); - service.openReader(executionContext); + reader.open(executionContext); - final List books = service.nextAuthorBooks(); + Author author = reader.read(); - assertEquals("Books list size", 2, books.size()); - assertEquals("First book", "author 2 - book 1", books.get(0).getName()); - assertEquals("Second book", "author 2 - book 2", books.get(1).getName()); + assertNotNull(author); + final List books = author.getBooks(); + assertEquals("Books list size must be = 2", 2, books.size()); + assertEquals("First book must be author 2 - book 1", "author 2 - book 1", books.get(0).getName()); + assertEquals("Second book must be author 2 - book 2", "author 2 - book 2", books.get(1).getName()); } @Test @@ -104,13 +111,15 @@ public class RepositoryItemReaderIntegrationTests { reader.setPageSize(2); final ExecutionContext executionContext = new ExecutionContext(); executionContext.putInt(CONTEXT_KEY, 2); - service.openReader(executionContext); + reader.open(executionContext); - final List books = service.nextAuthorBooks(); + Author author = reader.read(); - assertEquals("Books list size", 2, books.size()); - assertEquals("First book", "author 3 - book 1", books.get(0).getName()); - assertEquals("Second book", "author 3 - book 2", books.get(1).getName()); + assertNotNull(author); + final List books = author.getBooks(); + assertEquals("Books list size must be = 2", 2, books.size()); + assertEquals("First book must be author 3 - book 1", "author 3 - book 1", books.get(0).getName()); + assertEquals("Second book must be author 3 - book 2", "author 3 - book 2", books.get(1).getName()); } } diff --git a/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/sample/books/data/SimpleService.java b/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/sample/books/data/SimpleService.java deleted file mode 100644 index fdd5848a5..000000000 --- a/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/sample/books/data/SimpleService.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2020 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.batch.item.sample.books.data; - -import javax.transaction.Transactional; -import java.util.ArrayList; -import java.util.List; - -import org.springframework.batch.item.ExecutionContext; -import org.springframework.batch.item.data.RepositoryItemReader; -import org.springframework.batch.item.sample.books.Author; -import org.springframework.batch.item.sample.books.Book; - -/** - * A simple service based upon a {@link RepositoryItemReader} - */ -public class SimpleService { - - private final RepositoryItemReader itemReader; - - public SimpleService(RepositoryItemReader itemReader) { - this.itemReader = itemReader; - } - - // Prepare the reader - public void openReader(ExecutionContext executionContext) throws Exception { - itemReader.open(executionContext); - } - - // Reads next Author and returns his (lazy-loaded) books, inside a transaction (simulates the chunk's transaction) - @Transactional - public List nextAuthorBooks() throws Exception { - List result = new ArrayList<>(); - - final Author nextAuthor = itemReader.read(); - if (nextAuthor != null) { - result.addAll(nextAuthor.getBooks()); - } - - return result; - } -} diff --git a/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/item/database/RepositoryItemReaderCommonTests-context.xml b/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/item/database/RepositoryItemReaderCommonTests-context.xml index 3c4afdb79..1ae30705d 100644 --- a/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/item/database/RepositoryItemReaderCommonTests-context.xml +++ b/spring-batch-infrastructure-tests/src/test/resources/org/springframework/batch/item/database/RepositoryItemReaderCommonTests-context.xml @@ -49,8 +49,4 @@ - - - - \ No newline at end of file