BATCH-760: added parameter support to all PagingItemReaders

This commit is contained in:
trisberg
2008-08-31 02:59:36 +00:00
parent dd46e7c2f6
commit 77065c3ea6
34 changed files with 725 additions and 47 deletions

View File

@@ -0,0 +1,55 @@
package org.springframework.batch.item.database;
import org.junit.Before;
import org.junit.After;
import org.junit.Test;
import org.junit.Assert;
import org.junit.runner.RunWith;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.sample.Foo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.ContextConfiguration;
import javax.sql.DataSource;
/**
* @author trisberg
*/
public abstract class AbstractPagingItemReaderParameterTests {
protected ItemReader<Foo> tested;
protected ExecutionContext executionContext = new ExecutionContext();
@Autowired
protected DataSource dataSource;
@Before
public void setUp() throws Exception {
tested = getItemReader();
((ItemStream)tested).open(executionContext);
}
@After
public void tearDown() {
((ItemStream)tested).close(executionContext);
}
@Test
public void testRead() throws Exception {
Foo foo3 = tested.read();
Assert.assertEquals(3, foo3.getValue());
Foo foo4 = tested.read();
Assert.assertEquals(4, foo4.getValue());
Foo foo5 = tested.read();
Assert.assertEquals(5, foo5.getValue());
Object o = tested.read();
Assert.assertNull(o);
}
protected abstract ItemReader<Foo> getItemReader() throws Exception;
}

View File

@@ -0,0 +1,45 @@
package org.springframework.batch.item.database;
import org.junit.runner.RunWith;
import org.springframework.batch.item.sample.Foo;
import org.springframework.batch.item.ItemReader;
import org.springframework.orm.ibatis.SqlMapClientFactoryBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.ContextConfiguration;
import com.ibatis.sqlmap.client.SqlMapClient;
import java.util.Collections;
@SuppressWarnings("unchecked")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/org/springframework/batch/item/database/data-source-context.xml")
public class IbatisPagingItemReaderParameterTests extends AbstractPagingItemReaderParameterTests {
protected ItemReader<Foo> getItemReader() throws Exception {
SqlMapClientFactoryBean factory = new SqlMapClientFactoryBean();
factory.setConfigLocation(new ClassPathResource("ibatis-config.xml", getClass()));
factory.setDataSource(dataSource);
factory.afterPropertiesSet();
SqlMapClient sqlMapClient = createSqlMapClient();
IbatisPagingItemReader reader = new IbatisPagingItemReader();
reader.setQueryId("getFoos3AndUp");
reader.setParameterValues(Collections.<String, Object>singletonMap("limit", 3));
reader.setSqlMapClient(sqlMapClient);
reader.setSaveState(true);
reader.afterPropertiesSet();
return reader;
}
private SqlMapClient createSqlMapClient() throws Exception {
SqlMapClientFactoryBean factory = new SqlMapClientFactoryBean();
factory.setConfigLocation(new ClassPathResource("ibatis-config.xml", getClass()));
factory.setDataSource(dataSource);
factory.afterPropertiesSet();
return (SqlMapClient) factory.getObject();
}
}

View File

@@ -0,0 +1,52 @@
package org.springframework.batch.item.database;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.batch.item.sample.Foo;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.database.support.HsqlPagingQueryProvider;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import javax.persistence.EntityManagerFactory;
import java.util.Collections;
import java.sql.ResultSet;
import java.sql.SQLException;
@SuppressWarnings("unchecked")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/org/springframework/batch/item/database/JdbcPagingItemReaderParameterTests-context.xml")
public class JdbcPagingItemReaderClassicParameterTests extends AbstractPagingItemReaderParameterTests {
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.setWhereClause("where VALUE >= ?");
queryProvider.setSortKey("ID");
reader.setParameterValues(Collections.<String, Object>singletonMap("limit", 3));
reader.setQueryProvider(queryProvider);
reader.setParameterizedRowMapper(
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(3);
reader.afterPropertiesSet();
reader.setSaveState(true);
return reader;
}
}

View File

@@ -0,0 +1,50 @@
package org.springframework.batch.item.database;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.batch.item.sample.Foo;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.database.support.HsqlPagingQueryProvider;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import java.util.Collections;
import java.sql.ResultSet;
import java.sql.SQLException;
@SuppressWarnings("unchecked")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/org/springframework/batch/item/database/JdbcPagingItemReaderParameterTests-context.xml")
public class JdbcPagingItemReaderNamedParameterTests extends AbstractPagingItemReaderParameterTests {
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.setWhereClause("where VALUE >= :limit");
queryProvider.setSortKey("ID");
reader.setParameterValues(Collections.<String, Object>singletonMap("limit", 3));
reader.setQueryProvider(queryProvider);
reader.setParameterizedRowMapper(
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(2);
reader.afterPropertiesSet();
reader.setSaveState(true);
return reader;
}
}

View File

@@ -0,0 +1,40 @@
package org.springframework.batch.item.database;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.batch.item.sample.Foo;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.orm.ibatis.SqlMapClientFactoryBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.beans.factory.annotation.Autowired;
import com.ibatis.sqlmap.client.SqlMapClient;
import javax.persistence.EntityManagerFactory;
import java.util.Collections;
@SuppressWarnings("unchecked")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class JpaPagingItemReaderParameterTests extends AbstractPagingItemReaderParameterTests {
@Autowired
private EntityManagerFactory entityManagerFactory;
protected ItemReader<Foo> getItemReader() throws Exception {
String jpqlQuery = "select f from Foo f where f.value >= :limit";
JpaPagingItemReader<Foo> reader = new JpaPagingItemReader<Foo>();
reader.setQueryString(jpqlQuery);
reader.setParameterValues(Collections.<String, Object>singletonMap("limit", 3));
reader.setEntityManagerFactory(entityManagerFactory);
reader.setPageSize(3);
reader.afterPropertiesSet();
reader.setSaveState(true);
return reader;
}
}

View File

@@ -22,7 +22,7 @@ public class Db2PagingQueryProviderTests extends AbstractSqlPagingQueryProviderT
@Test @Override
public void testGenerateRemainingPagesQuery() {
String sql = "SELECT id, name, age FROM foo WHERE id > ? AND bar = 1 ORDER BY id ASC FETCH FIRST 100 ROWS ONLY";
String sql = "SELECT id, name, age FROM foo WHERE bar = 1 AND id > ? ORDER BY id ASC FETCH FIRST 100 ROWS ONLY";
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
Assert.assertEquals("", sql, s);
}

View File

@@ -87,7 +87,7 @@ public class DerbyPagingQueryProviderTests extends AbstractSqlPagingQueryProvide
@Test @Override
public void testGenerateRemainingPagesQuery() {
String sql = "SELECT * FROM ( SELECT id, name, age, ROW_NUMBER() OVER (ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE id > ? AND bar = 1) WHERE ROW_NUMBER <= 100";
String sql = "SELECT * FROM ( SELECT id, name, age, ROW_NUMBER() OVER (ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1 AND id > ?) WHERE ROW_NUMBER <= 100";
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
Assert.assertEquals("", sql, s);
}

View File

@@ -37,7 +37,7 @@ public class HsqlPagingQueryProviderTests extends AbstractSqlPagingQueryProvider
@Test @Override
public void testGenerateRemainingPagesQuery() {
String sql = "SELECT TOP 100 id, name, age FROM foo WHERE id > ? AND bar = 1 ORDER BY id ASC";
String sql = "SELECT TOP 100 id, name, age FROM foo WHERE bar = 1 AND id > ? ORDER BY id ASC";
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
Assert.assertEquals("", sql, s);
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright 2002-2008 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.support;
import static org.junit.Assert.*;
import org.junit.Test;
import java.util.List;
import java.util.ArrayList;
/**
* @author Thomas Risberg
*/
public class JdbcParameterUtilsTests {
@Test
public void testCountParameterPlaceholders() {
assertEquals(0, JdbcParameterUtils.countParameterPlaceholders(null, null));
assertEquals(0, JdbcParameterUtils.countParameterPlaceholders("", null));
assertEquals(1, JdbcParameterUtils.countParameterPlaceholders("?", null));
assertEquals(1, JdbcParameterUtils.countParameterPlaceholders("The \"big\" ? 'bad wolf'", null));
assertEquals(2, JdbcParameterUtils.countParameterPlaceholders("The big ?? bad wolf", null));
assertEquals(3, JdbcParameterUtils.countParameterPlaceholders("The big ? ? bad ? wolf", null));
assertEquals(1, JdbcParameterUtils.countParameterPlaceholders("The \"big?\" 'ba''ad?' ? wolf", null));
assertEquals(1, JdbcParameterUtils.countParameterPlaceholders(":parameter", null));
assertEquals(1, JdbcParameterUtils.countParameterPlaceholders("The \"big\" :parameter 'bad wolf'", null));
assertEquals(1, JdbcParameterUtils.countParameterPlaceholders("The big :parameter :parameter bad wolf", null));
assertEquals(2, JdbcParameterUtils.countParameterPlaceholders("The big :parameter :newpar :parameter bad wolf", null));
assertEquals(2, JdbcParameterUtils.countParameterPlaceholders("The big :parameter, :newpar, :parameter bad wolf", null));
assertEquals(1, JdbcParameterUtils.countParameterPlaceholders("The \"big:\" 'ba''ad:p' :parameter wolf", null));
assertEquals(1, JdbcParameterUtils.countParameterPlaceholders("&parameter", null));
assertEquals(1, JdbcParameterUtils.countParameterPlaceholders("The \"big\" &parameter 'bad wolf'", null));
assertEquals(1, JdbcParameterUtils.countParameterPlaceholders("The big &parameter &parameter bad wolf", null));
assertEquals(2, JdbcParameterUtils.countParameterPlaceholders("The big &parameter &newparameter &parameter bad wolf", null));
assertEquals(2, JdbcParameterUtils.countParameterPlaceholders("The big &parameter, &newparameter, &parameter bad wolf", null));
assertEquals(1, JdbcParameterUtils.countParameterPlaceholders("The \"big &x \" 'ba''ad&p' &parameter wolf", null));
assertEquals(2, JdbcParameterUtils.countParameterPlaceholders("The big :parameter, &newparameter, &parameter bad wolf", null));
assertEquals(2, JdbcParameterUtils.countParameterPlaceholders("The big :parameter, &sameparameter, &sameparameter bad wolf", null));
assertEquals(2, JdbcParameterUtils.countParameterPlaceholders("The big :parameter, :sameparameter, :sameparameter bad wolf", null));
assertEquals(0, JdbcParameterUtils.countParameterPlaceholders("xxx & yyy", null));
List<String> l = new ArrayList<String>();
assertEquals(3, JdbcParameterUtils.countParameterPlaceholders("select :par1, :par2 :par3", l));
assertEquals(3, l.size());
}
}

View File

@@ -37,7 +37,7 @@ public class MySqlPagingQueryProviderTests extends AbstractSqlPagingQueryProvide
@Test @Override
public void testGenerateRemainingPagesQuery() {
String sql = "SELECT id, name, age FROM foo WHERE id > ? AND bar = 1 ORDER BY id ASC LIMIT 100";
String sql = "SELECT id, name, age FROM foo WHERE bar = 1 AND id > ? ORDER BY id ASC LIMIT 100";
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
Assert.assertEquals("", sql, s);
}

View File

@@ -15,14 +15,14 @@ public class OraclePagingQueryProviderTests extends AbstractSqlPagingQueryProvid
@Test
@Override
public void testGenerateFirstPageQuery() {
String sql = "SELECT id, name, age FROM foo WHERE ROWNUM <= 100 AND bar = 1 ORDER BY id ASC";
String sql = "SELECT id, name, age FROM foo WHERE bar = 1 AND ROWNUM <= 100 ORDER BY id ASC";
String s = pagingQueryProvider.generateFirstPageQuery(pageSize);
Assert.assertEquals("", sql, s);
}
@Test @Override
public void testGenerateRemainingPagesQuery() {
String sql = "SELECT id, name, age FROM foo WHERE id > ? AND ROWNUM <= 100 AND bar = 1 ORDER BY id ASC";
String sql = "SELECT id, name, age FROM foo WHERE bar = 1 AND id > ? AND ROWNUM <= 100 ORDER BY id ASC";
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
Assert.assertEquals("", sql, s);
}

View File

@@ -22,7 +22,7 @@ public class PostgresPagingQueryProviderTests extends AbstractSqlPagingQueryProv
@Test @Override
public void testGenerateRemainingPagesQuery() {
String sql = "SELECT id, name, age FROM foo WHERE id > ? AND bar = 1 ORDER BY id ASC LIMIT 100";
String sql = "SELECT id, name, age FROM foo WHERE bar = 1 AND id > ? ORDER BY id ASC LIMIT 100";
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
Assert.assertEquals("", sql, s);
}

View File

@@ -22,7 +22,7 @@ public class SqlServerPagingQueryProviderTests extends AbstractSqlPagingQueryPro
@Test @Override
public void testGenerateRemainingPagesQuery() {
String sql = "SELECT TOP 100 id, name, age FROM foo WHERE id > ? AND bar = 1 ORDER BY id ASC";
String sql = "SELECT TOP 100 id, name, age FROM foo WHERE bar = 1 AND id > ? ORDER BY id ASC";
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
Assert.assertEquals("", sql, s);
}

View File

@@ -37,7 +37,7 @@ public class SqlWindowingPagingQueryProviderTests extends AbstractSqlPagingQuery
@Test @Override
public void testGenerateRemainingPagesQuery() {
String sql = "SELECT * FROM ( SELECT id, name, age, ROW_NUMBER() OVER (ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE id > ? AND bar = 1) WHERE ROW_NUMBER <= 100";
String sql = "SELECT * FROM ( SELECT id, name, age, ROW_NUMBER() OVER (ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1 AND id > ?) WHERE ROW_NUMBER <= 100";
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
assertEquals("", sql, s);
}

View File

@@ -22,7 +22,7 @@ public class SybasePagingQueryProviderTests extends AbstractSqlPagingQueryProvid
@Test @Override
public void testGenerateRemainingPagesQuery() {
String sql = "SELECT TOP 100 id, name, age FROM foo WHERE id > ? AND bar = 1 ORDER BY id ASC";
String sql = "SELECT TOP 100 id, name, age FROM foo WHERE bar = 1 AND id > ? ORDER BY id ASC";
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
Assert.assertEquals("", sql, s);
}