|
| 1 | +/* |
| 2 | + * Copyright 2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.batch.item.database; |
| 17 | + |
| 18 | +import static org.junit.Assert.assertNull; |
| 19 | + |
| 20 | +import java.util.Collections; |
| 21 | + |
| 22 | +import javax.sql.DataSource; |
| 23 | + |
| 24 | +import org.junit.Test; |
| 25 | +import org.junit.runner.RunWith; |
| 26 | + |
| 27 | +import org.springframework.batch.item.ItemReader; |
| 28 | +import org.springframework.batch.item.database.support.HsqlPagingQueryProvider; |
| 29 | +import org.springframework.beans.factory.annotation.Autowired; |
| 30 | +import org.springframework.jdbc.core.SingleColumnRowMapper; |
| 31 | +import org.springframework.test.context.ContextConfiguration; |
| 32 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 33 | + |
| 34 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 35 | +@ContextConfiguration(locations = "JdbcPagingItemReaderCommonTests-context.xml") |
| 36 | +public class JdbcPagingItemReaderEmptyResultSetTests { |
| 37 | + |
| 38 | + private static final int PAGE_SIZE = 2; |
| 39 | + private static final int EMPTY_READS = PAGE_SIZE + 1; |
| 40 | + |
| 41 | + @Autowired |
| 42 | + private DataSource dataSource; |
| 43 | + |
| 44 | + @Test |
| 45 | + public void testMultiplePageReadsOnEmptyResultSet() throws Exception { |
| 46 | + final ItemReader<Long> reader = getItemReader(); |
| 47 | + for (int i = 0; i < EMPTY_READS; i++) { |
| 48 | + assertNull(reader.read()); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private ItemReader<Long> getItemReader() throws Exception { |
| 53 | + JdbcPagingItemReader<Long> reader = new JdbcPagingItemReader<>(); |
| 54 | + reader.setDataSource(dataSource); |
| 55 | + HsqlPagingQueryProvider queryProvider = new HsqlPagingQueryProvider(); |
| 56 | + queryProvider.setSelectClause("select ID"); |
| 57 | + queryProvider.setFromClause("from T_FOOS"); |
| 58 | + queryProvider.setWhereClause("1 = 0"); |
| 59 | + queryProvider.setSortKeys(Collections.singletonMap("ID", Order.ASCENDING)); |
| 60 | + reader.setQueryProvider(queryProvider); |
| 61 | + reader.setRowMapper(new SingleColumnRowMapper<>()); |
| 62 | + reader.setPageSize(PAGE_SIZE); |
| 63 | + reader.afterPropertiesSet(); |
| 64 | + reader.setSaveState(false); |
| 65 | + |
| 66 | + return reader; |
| 67 | + } |
| 68 | +} |
0 commit comments