BATCH-760: refactoring, moving SQL snippets to PagingQueryProvider implementations, adding some database specific implementations (more to come)
This commit is contained in:
@@ -6,6 +6,7 @@ import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.batch.item.CommonItemStreamItemReaderTests;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
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;
|
||||
@@ -24,10 +25,12 @@ public class JdbcPagingItemReaderCommonTests extends CommonItemStreamItemReaderT
|
||||
protected ItemReader<Foo> getItemReader() throws Exception {
|
||||
|
||||
JdbcPagingItemReader<Foo> reader = new JdbcPagingItemReader<Foo>();
|
||||
reader.setSelectClause("select ID, NAME, VALUE");
|
||||
reader.setFromClause("from T_FOOS");
|
||||
reader.setSortKey("ID");
|
||||
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.setParameterizedRowMapper(
|
||||
new ParameterizedRowMapper<Foo>() {
|
||||
public Foo mapRow(ResultSet rs, int i) throws SQLException {
|
||||
@@ -49,10 +52,13 @@ public class JdbcPagingItemReaderCommonTests extends CommonItemStreamItemReaderT
|
||||
protected void pointToEmptyInput(ItemReader<Foo> tested) throws Exception {
|
||||
JdbcPagingItemReader<Foo> reader = (JdbcPagingItemReader<Foo>) tested;
|
||||
reader.close(new ExecutionContext());
|
||||
reader.setSelectClause("select ID, NAME, VALUE");
|
||||
reader.setFromClause("from T_FOOS");
|
||||
reader.setWhereClause("where id = -1");
|
||||
reader.setDataSource(dataSource);
|
||||
HsqlPagingQueryProvider queryProvider = new HsqlPagingQueryProvider();
|
||||
queryProvider.setSelectClause("select ID, NAME, VALUE");
|
||||
queryProvider.setFromClause("from T_FOOS");
|
||||
queryProvider.setWhereClause("where ID = -1");
|
||||
queryProvider.setSortKey("ID");
|
||||
reader.setQueryProvider(queryProvider);
|
||||
reader.setPageSize(3);
|
||||
reader.afterPropertiesSet();
|
||||
reader.open(new ExecutionContext());
|
||||
|
||||
@@ -5,6 +5,7 @@ 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.sql.ResultSet;
|
||||
@@ -22,10 +23,12 @@ public class JdbcPagingItemReaderIntegrationTests extends AbstractDataSourceItem
|
||||
protected ItemReader<Foo> createItemReader() throws Exception {
|
||||
|
||||
JdbcPagingItemReader<Foo> inputSource = new JdbcPagingItemReader<Foo>();
|
||||
inputSource.setSelectClause("select ID, NAME, VALUE");
|
||||
inputSource.setFromClause("from T_FOOS");
|
||||
inputSource.setSortKey("ID");
|
||||
inputSource.setDataSource(dataSource);
|
||||
HsqlPagingQueryProvider queryProvider = new HsqlPagingQueryProvider();
|
||||
queryProvider.setSelectClause("select ID, NAME, VALUE");
|
||||
queryProvider.setFromClause("from T_FOOS");
|
||||
queryProvider.setSortKey("ID");
|
||||
inputSource.setQueryProvider(queryProvider);
|
||||
inputSource.setParameterizedRowMapper(
|
||||
new ParameterizedRowMapper<Foo>() {
|
||||
public Foo mapRow(ResultSet rs, int i) throws SQLException {
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2006-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 org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Thomas Risberg
|
||||
*/
|
||||
public abstract class AbstractSqlPagingQueryProviderTests {
|
||||
|
||||
protected AbstractSqlPagingQueryProvider pagingQueryProvider;
|
||||
protected int pageSize;
|
||||
|
||||
|
||||
@Before
|
||||
public void onSetUp() {
|
||||
if (pagingQueryProvider == null) {
|
||||
throw new IllegalArgumentException("pagingQuery{rovider can't be null");
|
||||
}
|
||||
pagingQueryProvider.setSelectClause("id, name, age");
|
||||
pagingQueryProvider.setFromClause("foo");
|
||||
pagingQueryProvider.setWhereClause("bar = 1");
|
||||
pagingQueryProvider.setSortKey("id");
|
||||
pageSize = 100;
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public abstract void testGenerateFirstPageQuery();
|
||||
|
||||
@Test
|
||||
public abstract void testGenerateRemainingPagesQuery();
|
||||
|
||||
@Test
|
||||
public abstract void testGenerateJumpToItemQuery();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Copyright 2006-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.fail;
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.Assert;
|
||||
import org.springframework.dao.InvalidDataAccessResourceUsageException;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Connection;
|
||||
|
||||
/**
|
||||
* @author Thomas Risberg
|
||||
*/
|
||||
public class DerbyPagingQueryProviderTests extends AbstractSqlPagingQueryProviderTests {
|
||||
|
||||
public DerbyPagingQueryProviderTests() {
|
||||
pagingQueryProvider = new DerbyPagingQueryProvider();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInit() throws Exception {
|
||||
DataSource ds = createMock(DataSource.class);
|
||||
Connection con = createMock(Connection.class);
|
||||
DatabaseMetaData dmd = createMock(DatabaseMetaData.class);
|
||||
expect(dmd.getDatabaseProductVersion()).andReturn("10.4.1.3");
|
||||
expect(con.getMetaData()).andReturn(dmd);
|
||||
expect(ds.getConnection()).andReturn(con);
|
||||
replay(dmd);
|
||||
replay(con);
|
||||
replay(ds);
|
||||
pagingQueryProvider.init(ds);
|
||||
verify(ds);
|
||||
verify(con);
|
||||
verify(dmd);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitWithUnsupportedVErsion() throws Exception {
|
||||
DataSource ds = createMock(DataSource.class);
|
||||
Connection con = createMock(Connection.class);
|
||||
DatabaseMetaData dmd = createMock(DatabaseMetaData.class);
|
||||
expect(dmd.getDatabaseProductVersion()).andReturn("10.2.9.9");
|
||||
expect(con.getMetaData()).andReturn(dmd);
|
||||
expect(ds.getConnection()).andReturn(con);
|
||||
replay(dmd);
|
||||
replay(con);
|
||||
replay(ds);
|
||||
try {
|
||||
pagingQueryProvider.init(ds);
|
||||
fail();
|
||||
} catch (InvalidDataAccessResourceUsageException e) {
|
||||
// expected
|
||||
}
|
||||
verify(ds);
|
||||
verify(con);
|
||||
verify(dmd);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void testGenerateFirstPageQuery() {
|
||||
String sql = "SELECT * FROM ( SELECT id, name, age, ROW_NUMBER() OVER (ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1) WHERE ROW_NUMBER <= 100";
|
||||
String s = pagingQueryProvider.generateFirstPageQuery(pageSize);
|
||||
Assert.assertEquals("", sql, s);
|
||||
}
|
||||
|
||||
@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 s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
|
||||
Assert.assertEquals("", sql, s);
|
||||
}
|
||||
|
||||
@Test @Override
|
||||
public void testGenerateJumpToItemQuery() {
|
||||
String sql = "SELECT SORT_KEY FROM ( SELECT id AS SORT_KEY, ROW_NUMBER() OVER (ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1) WHERE ROW_NUMBER = 100";
|
||||
String s = pagingQueryProvider.generateJumpToItemQuery(145, pageSize);
|
||||
Assert.assertEquals("", sql, s);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2006-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 org.junit.Test;
|
||||
import org.junit.Assert;
|
||||
|
||||
/**
|
||||
* @author Thomas Risberg
|
||||
*/
|
||||
public class HsqlPagingQueryProviderTests extends AbstractSqlPagingQueryProviderTests {
|
||||
|
||||
public HsqlPagingQueryProviderTests() {
|
||||
pagingQueryProvider = new HsqlPagingQueryProvider();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void testGenerateFirstPageQuery() {
|
||||
String sql = "SELECT TOP 100 id, name, age FROM foo WHERE bar = 1";
|
||||
String s = pagingQueryProvider.generateFirstPageQuery(pageSize);
|
||||
Assert.assertEquals("", sql, s);
|
||||
}
|
||||
|
||||
@Test @Override
|
||||
public void testGenerateRemainingPagesQuery() {
|
||||
String sql = "SELECT TOP 100 id, name, age FROM foo WHERE id > ? AND bar = 1";
|
||||
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
|
||||
Assert.assertEquals("", sql, s);
|
||||
}
|
||||
|
||||
@Test @Override
|
||||
public void testGenerateJumpToItemQuery() {
|
||||
String sql = "SELECT LIMIT 99 1 id AS SORT_KEY FROM foo WHERE bar = 1";
|
||||
String s = pagingQueryProvider.generateJumpToItemQuery(145, pageSize);
|
||||
Assert.assertEquals("", sql, s);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2006-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 org.junit.Test;
|
||||
import org.junit.Assert;
|
||||
|
||||
/**
|
||||
* @author Thomas Risberg
|
||||
*/
|
||||
public class MySqlPagingQueryProviderTests extends AbstractSqlPagingQueryProviderTests {
|
||||
|
||||
public MySqlPagingQueryProviderTests() {
|
||||
pagingQueryProvider = new MySqlPagingQueryProvider();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void testGenerateFirstPageQuery() {
|
||||
String sql = "SELECT id, name, age FROM foo WHERE bar = 1 LIMIT 100";
|
||||
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 bar = 1 LIMIT 100";
|
||||
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
|
||||
Assert.assertEquals("", sql, s);
|
||||
}
|
||||
|
||||
@Test @Override
|
||||
public void testGenerateJumpToItemQuery() {
|
||||
String sql = "SELECT id AS SORT_KEY FROM foo WHERE bar = 1 LIMIT 99 1";
|
||||
String s = pagingQueryProvider.generateJumpToItemQuery(145, pageSize);
|
||||
Assert.assertEquals("", sql, s);
|
||||
}
|
||||
}
|
||||
@@ -18,37 +18,35 @@ package org.springframework.batch.item.database.support;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.Before;
|
||||
|
||||
/**
|
||||
* @author Thomas Risberg
|
||||
*/
|
||||
public class SqlWindowingPagingQueryProviderTests {
|
||||
public class SqlWindowingPagingQueryProviderTests extends AbstractSqlPagingQueryProviderTests {
|
||||
|
||||
JdbcPagingQueryProvider pagingQueryProvider = new SqlWindowingPagingQueryProvider();
|
||||
private int pageSize = 100;
|
||||
private String selectClause = "id, name, age";
|
||||
private String fromClause = "foo";
|
||||
private String whereClaues = "bar = 1";
|
||||
private String sortKey = "id";
|
||||
public SqlWindowingPagingQueryProviderTests() {
|
||||
pagingQueryProvider = new SqlWindowingPagingQueryProvider();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test @Override
|
||||
public void testGenerateFirstPageQuery() {
|
||||
String sql = "SELECT * FROM ( SELECT id, name, age, ROW_NUMBER() OVER (ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1) WHERE ROW_NUMBER <= 100";
|
||||
String s = pagingQueryProvider.generateFirstPageQuery(pageSize, selectClause, fromClause, whereClaues, sortKey);
|
||||
String s = pagingQueryProvider.generateFirstPageQuery(pageSize);
|
||||
assertEquals("", sql, s);
|
||||
}
|
||||
|
||||
@Test
|
||||
@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 s = pagingQueryProvider.generateRemainingPagesQuery(pageSize, selectClause, fromClause, whereClaues, sortKey);
|
||||
String s = pagingQueryProvider.generateRemainingPagesQuery(pageSize);
|
||||
assertEquals("", sql, s);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test @Override
|
||||
public void testGenerateJumpToItemQuery() {
|
||||
String sql = "SELECT SORT_KEY FROM ( SELECT id AS SORT_KEY, ROW_NUMBER() OVER (ORDER BY id ASC) AS ROW_NUMBER FROM foo WHERE bar = 1) WHERE ROW_NUMBER = 100";
|
||||
String s = pagingQueryProvider.generateJumpToItemQuery(145, pageSize, selectClause, fromClause, whereClaues, sortKey);
|
||||
String s = pagingQueryProvider.generateJumpToItemQuery(145, pageSize);
|
||||
assertEquals("", sql, s);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user