Move over Jsbc integration test from samples

This commit is contained in:
dsyer
2010-03-29 10:55:13 +00:00
parent 762c11c80b
commit 75a35de711
2 changed files with 156 additions and 1 deletions

View File

@@ -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<Foo> 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<Map<String, Object>> 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<Foo> getItemReader() throws Exception {
JdbcPagingItemReader<Foo> reader = new JdbcPagingItemReader<Foo>();
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<Foo>() {
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;
}
}

View File

@@ -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<String> 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();
}