Polish contribution cbdb251441

* Fix assertion messages in tests
* Use transactional tests instead of a separate
  service with a transactional method
This commit is contained in:
Mahmoud Ben Hassine
2020-11-20 14:38:16 +01:00
parent 305674f8ae
commit dc6618c475
3 changed files with 38 additions and 88 deletions

View File

@@ -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<Author> reader;
@@ -52,51 +51,59 @@ public class RepositoryItemReaderIntegrationTests {
@Test
public void testReadFromFirstPos() throws Exception {
service.openReader(new ExecutionContext());
reader.open(new ExecutionContext());
final List<Book> 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<Book> 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<Book> 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<Book> 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<Book> 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<Book> 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<Book> 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<Book> 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<Book> 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<Book> 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());
}
}

View File

@@ -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<Author> itemReader;
public SimpleService(RepositoryItemReader<Author> 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<Book> nextAuthorBooks() throws Exception {
List<Book> result = new ArrayList<>();
final Author nextAuthor = itemReader.read();
if (nextAuthor != null) {
result.addAll(nextAuthor.getBooks());
}
return result;
}
}

View File

@@ -49,8 +49,4 @@
</property>
</bean>
<bean id="myService" class="org.springframework.batch.item.sample.books.data.SimpleService">
<constructor-arg ref="authorRepositoryItemReader"/>
</bean>
</beans>