From 75a35de7111f254f7d883f7c0670f35470a9c9fc Mon Sep 17 00:00:00 2001 From: dsyer Date: Mon, 29 Mar 2010 10:55:13 +0000 Subject: [PATCH] Move over Jsbc integration test from samples --- .../JdbcPagingRestartIntegrationTests.java | 145 ++++++++++++++++++ ...orRepeatTemplateBulkAsynchronousTests.java | 12 +- 2 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/database/JdbcPagingRestartIntegrationTests.java diff --git a/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/database/JdbcPagingRestartIntegrationTests.java b/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/database/JdbcPagingRestartIntegrationTests.java new file mode 100644 index 000000000..94a3c4a32 --- /dev/null +++ b/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/item/database/JdbcPagingRestartIntegrationTests.java @@ -0,0 +1,145 @@ +/* + * Copyright 2006-2007 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.database; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; +import java.util.Map; + +import javax.sql.DataSource; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.batch.item.ExecutionContext; +import org.springframework.batch.item.ItemReader; +import org.springframework.batch.item.ItemStream; +import org.springframework.batch.item.database.support.HsqlPagingQueryProvider; +import org.springframework.batch.item.sample.Foo; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.simple.ParameterizedRowMapper; +import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.jdbc.SimpleJdbcTestUtils; + +/** + * @author Dave Syer + * @since 2.1 + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = "JdbcPagingItemReaderCommonTests-context.xml") +public class JdbcPagingRestartIntegrationTests { + + private static Log logger = LogFactory.getLog(JdbcPagingItemReaderAsyncTests.class); + + @Autowired + private DataSource dataSource; + + private int maxId; + + private SimpleJdbcTemplate jdbcTemplate; + + private int itemCount = 9; + + private int pageSize = 2; + + @Before + public void init() { + jdbcTemplate = new SimpleJdbcTemplate(dataSource); + maxId = jdbcTemplate.queryForInt("SELECT MAX(ID) from T_FOOS"); + for (int i = maxId + 1; i <= itemCount; i++) { + jdbcTemplate.update("INSERT into T_FOOS (ID,NAME,VALUE) values (?, ?, ?)", i, "foo" + i, i); + } + assertEquals(itemCount, SimpleJdbcTestUtils.countRowsInTable(jdbcTemplate, "T_FOOS")); + } + + @After + public void destroy() { + jdbcTemplate.update("DELETE from T_FOOS where ID>?", maxId); + } + + + @Test + public void testReader() throws Exception { + + ItemReader reader = getItemReader(); + + int total = SimpleJdbcTestUtils.countRowsInTable(jdbcTemplate, "T_FOOS"); + int count = (total / pageSize) * pageSize; + if (count >= pageSize) { + count -= pageSize; + } + + ExecutionContext executionContext = new ExecutionContext(); + executionContext.putInt("JdbcPagingItemReader.read.count", count); + // Assume the primary keys are in order + + List> ids = jdbcTemplate + .queryForList("SELECT ID,NAME FROM T_FOOS ORDER BY ID ASC"); + logger.debug("Ids: "+ids); + int startAfterValue = ((Long) ids.get(count - 1).get("ID")).intValue(); + logger.debug("Start after: " + startAfterValue); + executionContext.putInt("JdbcPagingItemReader.start.after", startAfterValue); + ((ItemStream) reader).open(executionContext); + + for (int i = count; i < total; i++) { + Foo item = reader.read(); + logger.debug("Item: " + item); + assertNotNull(item); + } + + Foo item = reader.read(); + logger.debug("Item: " + item); + assertNull(item); + + } + + protected ItemReader getItemReader() throws Exception { + + JdbcPagingItemReader reader = new JdbcPagingItemReader(); + reader.setDataSource(dataSource); + HsqlPagingQueryProvider queryProvider = new HsqlPagingQueryProvider(); + queryProvider.setSelectClause("select ID, NAME, VALUE"); + queryProvider.setFromClause("from T_FOOS"); + queryProvider.setSortKey("ID"); + reader.setQueryProvider(queryProvider); + reader.setRowMapper(new ParameterizedRowMapper() { + public Foo mapRow(ResultSet rs, int i) throws SQLException { + Foo foo = new Foo(); + foo.setId(rs.getInt(1)); + foo.setName(rs.getString(2)); + foo.setValue(rs.getInt(3)); + return foo; + } + }); + reader.setPageSize(pageSize); + reader.afterPropertiesSet(); + reader.setSaveState(false); + + return reader; + } + +} diff --git a/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/repeat/support/TaskExecutorRepeatTemplateBulkAsynchronousTests.java b/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/repeat/support/TaskExecutorRepeatTemplateBulkAsynchronousTests.java index ec3329bf6..20d7f12a6 100644 --- a/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/repeat/support/TaskExecutorRepeatTemplateBulkAsynchronousTests.java +++ b/spring-batch-infrastructure-tests/src/test/java/org/springframework/batch/repeat/support/TaskExecutorRepeatTemplateBulkAsynchronousTests.java @@ -27,6 +27,7 @@ import java.util.concurrent.atomic.AtomicInteger; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.batch.repeat.RepeatCallback; @@ -61,11 +62,13 @@ public class TaskExecutorRepeatTemplateBulkAsynchronousTests { private List items; + private ThreadPoolTaskExecutor taskExecutor; + @Before public void setUp() { template = new TaskExecutorRepeatTemplate(); - ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); + taskExecutor = new ThreadPoolTaskExecutor(); taskExecutor.setMaxPoolSize(300); taskExecutor.setCorePoolSize(10); taskExecutor.setQueueCapacity(0); @@ -116,6 +119,11 @@ public class TaskExecutorRepeatTemplateBulkAsynchronousTests { } + @After + public void tearDown() { + taskExecutor.destroy(); + } + @Test public void testThrottleLimitEarlyFinish() throws Exception { @@ -154,6 +162,8 @@ public class TaskExecutorRepeatTemplateBulkAsynchronousTests { // Extra tasks will be submitted before the termination is detected assertEquals(total, items.size() - frequency); assertTrue(frequency <= throttleLimit + 1); + + taskExecutor.destroy(); }