BATCH-1546: add and run integration tests for oracle

This commit is contained in:
dsyer
2010-04-06 07:51:25 +00:00
parent 135845f9ec
commit d04a904eaf
11 changed files with 190 additions and 22 deletions

View File

@@ -0,0 +1,140 @@
/*
* 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.assertNotSame;
import static org.junit.Assert.assertTrue;
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.database.support.SqlPagingQueryProviderFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
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 JdbcPagingQueryIntegrationTests {
private static Log logger = LogFactory.getLog(JdbcPagingQueryIntegrationTests.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 testQueryFromStart() throws Exception {
PagingQueryProvider queryProvider = getPagingQueryProvider();
int total = SimpleJdbcTestUtils.countRowsInTable(jdbcTemplate, "T_FOOS");
assertTrue(total > pageSize);
int pages = total / pageSize;
int count = 0;
List<Map<String, Object>> list = jdbcTemplate.queryForList(queryProvider.generateFirstPageQuery(pageSize));
logger.debug("First page result: " + list);
assertEquals(pageSize, list.size());
count += pageSize;
Object oldValue = -1L;
while (count < pages * pageSize) {
Object startAfterValue = list.get(pageSize - 1).get(queryProvider.getSortKey());
assertNotSame(oldValue, startAfterValue);
list = jdbcTemplate.queryForList(queryProvider.generateRemainingPagesQuery(pageSize), startAfterValue);
assertEquals(pageSize, list.size());
count += pageSize;
oldValue = startAfterValue;
}
if (count < total) {
Object startAfterValue = list.get(pageSize - 1).get(queryProvider.getSortKey());
list = jdbcTemplate.queryForList(queryProvider.generateRemainingPagesQuery(pageSize), startAfterValue);
assertEquals(total - pages * pageSize, list.size());
count += list.size();
}
assertEquals(total, count);
}
@Test
public void testJumpToItem() throws Exception {
PagingQueryProvider queryProvider = getPagingQueryProvider();
List<Map<String, Object>> list = jdbcTemplate.queryForList(queryProvider.generateJumpToItemQuery(pageSize,
pageSize));
logger.debug("Jump to page result: " + list);
assertEquals(1, list.size());
Object startAfterValue = list.get(0).entrySet().iterator().next().getValue();
list = jdbcTemplate.queryForList(queryProvider.generateRemainingPagesQuery(pageSize), startAfterValue);
assertEquals(pageSize, list.size());
}
protected PagingQueryProvider getPagingQueryProvider() throws Exception {
SqlPagingQueryProviderFactoryBean factory = new SqlPagingQueryProviderFactoryBean();
factory.setDataSource(dataSource);
factory.setSelectClause("select ID, NAME, VALUE");
factory.setFromClause("from T_FOOS");
factory.setSortKey("ID");
return (PagingQueryProvider) factory.getObject();
}
}

View File

@@ -53,7 +53,7 @@ import org.springframework.test.jdbc.SimpleJdbcTestUtils;
@ContextConfiguration(locations = "JdbcPagingItemReaderCommonTests-context.xml")
public class JdbcPagingRestartIntegrationTests {
private static Log logger = LogFactory.getLog(JdbcPagingItemReaderAsyncTests.class);
private static Log logger = LogFactory.getLog(JdbcPagingRestartIntegrationTests.class);
@Autowired
private DataSource dataSource;
@@ -160,7 +160,7 @@ public class JdbcPagingRestartIntegrationTests {
});
reader.setPageSize(pageSize);
reader.afterPropertiesSet();
reader.setSaveState(false);
reader.setSaveState(true);
return reader;
}

View File

@@ -2,12 +2,12 @@
# for Derby:
batch.jdbc.driver=org.apache.derby.jdbc.EmbeddedDriver
batch.jdbc.url=jdbc:derby:derby-home/test;create=true
batch.jdbc.user=sa
batch.jdbc.user=app
batch.jdbc.password=
batch.jdbc.testWhileIdle=false
batch.jdbc.validationQuery=
batch.drop.script=classpath:/org/springframework/batch/core/schema-drop-derby.sql
batch.schema.script=classpath:/org/springframework/batch/core/schema-derby.sql
batch.schema.script=classpath:org/springframework/batch/item/database/init-foo-schema-derby.sql
batch.business.schema.script=classpath:/org/springframework/batch/jms/init.sql
batch.data.source.init=true
batch.database.incrementer.class=org.springframework.jdbc.support.incrementer.DerbyMaxValueIncrementer

View File

@@ -6,7 +6,7 @@ log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{1}:%L - %m
log4j.category.org.apache.activemq=ERROR
# log4j.category.org.springframework=DEBUG
# log4j.category.org.springframework.jdbc=DEBUG
log4j.category.org.springframework.jdbc=DEBUG
# log4j.category.org.springframework.jms=DEBUG
# log4j.category.org.springframework.batch=DEBUG
log4j.category.org.springframework.batch.support=INFO

View File

@@ -0,0 +1,24 @@
DROP TABLE T_FOOS;
DROP TABLE T_WRITE_FOOS;
CREATE TABLE T_FOOS (
ID BIGINT NOT NULL,
NAME VARCHAR(45),
VALUE INTEGER
);
ALTER TABLE T_FOOS ADD PRIMARY KEY (ID);
INSERT INTO t_foos (id, name, value) VALUES (1, 'bar1', 1);
INSERT INTO t_foos (id, name, value) VALUES (2, 'bar2', 2);
INSERT INTO t_foos (id, name, value) VALUES (3, 'bar3', 3);
INSERT INTO t_foos (id, name, value) VALUES (4, 'bar4', 4);
INSERT INTO t_foos (id, name, value) VALUES (5, 'bar5', 5);
CREATE TABLE T_WRITE_FOOS (
ID BIGINT NOT NULL,
NAME VARCHAR(45),
VALUE INTEGER
);
ALTER TABLE T_WRITE_FOOS ADD PRIMARY KEY (ID);