RESOLVED - issue BATCH-986: Provide factory bean for SqlPagingQueryProvider
This commit is contained in:
@@ -25,15 +25,13 @@ import org.springframework.batch.core.repository.dao.JdbcStepExecutionDao;
|
||||
import org.springframework.batch.core.repository.dao.JobExecutionDao;
|
||||
import org.springframework.batch.core.repository.dao.JobInstanceDao;
|
||||
import org.springframework.batch.core.repository.dao.StepExecutionDao;
|
||||
import org.springframework.batch.item.database.support.DataFieldMaxValueIncrementerFactory;
|
||||
import org.springframework.batch.item.database.support.DefaultDataFieldMaxValueIncrementerFactory;
|
||||
import org.springframework.batch.support.DatabaseType;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcOperations;
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
import org.springframework.jdbc.support.incrementer.AbstractDataFieldMaxValueIncrementer;
|
||||
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* A {@link FactoryBean} that automates the creation of a
|
||||
@@ -48,11 +46,14 @@ public class JobExplorerFactoryBean extends AbstractJobExplorerFactoryBean imple
|
||||
|
||||
private SimpleJdbcOperations jdbcTemplate;
|
||||
|
||||
private String databaseType;
|
||||
|
||||
private String tablePrefix = AbstractJdbcBatchMetadataDao.DEFAULT_TABLE_PREFIX;
|
||||
|
||||
private DataFieldMaxValueIncrementerFactory incrementerFactory;
|
||||
private DataFieldMaxValueIncrementer incrementer = new AbstractDataFieldMaxValueIncrementer() {
|
||||
@Override
|
||||
protected long getNextKey() {
|
||||
throw new IllegalStateException("JobExplorer is read only.");
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Public setter for the {@link DataSource}.
|
||||
@@ -62,15 +63,6 @@ public class JobExplorerFactoryBean extends AbstractJobExplorerFactoryBean imple
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the database type.
|
||||
* @param dbType as specified by
|
||||
* {@link DefaultDataFieldMaxValueIncrementerFactory}
|
||||
*/
|
||||
public void setDatabaseType(String dbType) {
|
||||
this.databaseType = dbType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the table prefix for all the batch meta-data tables.
|
||||
* @param tablePrefix
|
||||
@@ -79,28 +71,12 @@ public class JobExplorerFactoryBean extends AbstractJobExplorerFactoryBean imple
|
||||
this.tablePrefix = tablePrefix;
|
||||
}
|
||||
|
||||
public void setIncrementerFactory(DataFieldMaxValueIncrementerFactory incrementerFactory) {
|
||||
this.incrementerFactory = incrementerFactory;
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
|
||||
Assert.notNull(dataSource, "DataSource must not be null.");
|
||||
|
||||
jdbcTemplate = new SimpleJdbcTemplate(dataSource);
|
||||
|
||||
if (incrementerFactory == null) {
|
||||
incrementerFactory = new DefaultDataFieldMaxValueIncrementerFactory(dataSource);
|
||||
}
|
||||
|
||||
if (databaseType == null) {
|
||||
databaseType = DatabaseType.fromMetaData(dataSource).name();
|
||||
}
|
||||
|
||||
Assert.isTrue(incrementerFactory.isSupportedIncrementerType(databaseType), "'" + databaseType
|
||||
+ "' is an unsupported database type. The supported database types are "
|
||||
+ StringUtils.arrayToCommaDelimitedString(incrementerFactory.getSupportedIncrementerTypes()));
|
||||
|
||||
}
|
||||
|
||||
private Object getTarget() throws Exception {
|
||||
@@ -111,7 +87,7 @@ public class JobExplorerFactoryBean extends AbstractJobExplorerFactoryBean imple
|
||||
protected JobInstanceDao createJobInstanceDao() throws Exception {
|
||||
JdbcJobInstanceDao dao = new JdbcJobInstanceDao();
|
||||
dao.setJdbcTemplate(jdbcTemplate);
|
||||
dao.setJobIncrementer(incrementerFactory.getIncrementer(databaseType, tablePrefix + "JOB_SEQ"));
|
||||
dao.setJobIncrementer(incrementer);
|
||||
dao.setTablePrefix(tablePrefix);
|
||||
dao.afterPropertiesSet();
|
||||
return dao;
|
||||
@@ -121,8 +97,7 @@ public class JobExplorerFactoryBean extends AbstractJobExplorerFactoryBean imple
|
||||
protected JobExecutionDao createJobExecutionDao() throws Exception {
|
||||
JdbcJobExecutionDao dao = new JdbcJobExecutionDao();
|
||||
dao.setJdbcTemplate(jdbcTemplate);
|
||||
dao.setJobExecutionIncrementer(incrementerFactory.getIncrementer(databaseType, tablePrefix
|
||||
+ "JOB_EXECUTION_SEQ"));
|
||||
dao.setJobExecutionIncrementer(incrementer);
|
||||
dao.setTablePrefix(tablePrefix);
|
||||
dao.afterPropertiesSet();
|
||||
return dao;
|
||||
@@ -131,8 +106,7 @@ public class JobExplorerFactoryBean extends AbstractJobExplorerFactoryBean imple
|
||||
protected StepExecutionDao createStepExecutionDao() throws Exception {
|
||||
JdbcStepExecutionDao dao = new JdbcStepExecutionDao();
|
||||
dao.setJdbcTemplate(jdbcTemplate);
|
||||
dao.setStepExecutionIncrementer(incrementerFactory.getIncrementer(databaseType, tablePrefix
|
||||
+ "STEP_EXECUTION_SEQ"));
|
||||
dao.setStepExecutionIncrementer(incrementer);
|
||||
dao.setTablePrefix(tablePrefix);
|
||||
dao.afterPropertiesSet();
|
||||
return dao;
|
||||
|
||||
@@ -87,7 +87,7 @@ public class JdbcJobExecutionDao extends AbstractJdbcBatchMetadataDao implements
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
super.afterPropertiesSet();
|
||||
Assert.notNull(jobExecutionIncrementer);
|
||||
Assert.notNull(jobExecutionIncrementer, "The jobExecutionIncrementer must not be null.");
|
||||
}
|
||||
|
||||
public List<JobExecution> findJobExecutions(final JobInstance job) {
|
||||
@@ -271,7 +271,7 @@ public class JdbcJobExecutionDao extends AbstractJdbcBatchMetadataDao implements
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
private static class JobExecutionRowMapper implements ParameterizedRowMapper<JobExecution> {
|
||||
protected static class JobExecutionRowMapper implements ParameterizedRowMapper<JobExecution> {
|
||||
|
||||
private JobInstance jobInstance;
|
||||
|
||||
|
||||
@@ -18,32 +18,23 @@ package org.springframework.batch.core.exlore.support;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
import static junit.framework.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 java.sql.Connection;
|
||||
import java.sql.DatabaseMetaData;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.core.explore.JobExplorer;
|
||||
import org.springframework.batch.core.explore.support.JobExplorerFactoryBean;
|
||||
import org.springframework.batch.item.database.support.DataFieldMaxValueIncrementerFactory;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
|
||||
|
||||
/**
|
||||
* @author Lucas Ward
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class JobExplorerFactoryBeanTests {
|
||||
|
||||
private JobExplorerFactoryBean factory;
|
||||
|
||||
private DataFieldMaxValueIncrementerFactory incrementerFactory;
|
||||
|
||||
private DataSource dataSource;
|
||||
|
||||
private String tablePrefix = "TEST_BATCH_PREFIX_";
|
||||
@@ -54,55 +45,10 @@ public class JobExplorerFactoryBeanTests {
|
||||
factory = new JobExplorerFactoryBean();
|
||||
dataSource = createMock(DataSource.class);
|
||||
factory.setDataSource(dataSource);
|
||||
incrementerFactory = createMock(DataFieldMaxValueIncrementerFactory.class);
|
||||
factory.setIncrementerFactory(incrementerFactory);
|
||||
factory.setTablePrefix(tablePrefix);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetectDatabaseType() throws Exception {
|
||||
|
||||
DatabaseMetaData dmd = createMock(DatabaseMetaData.class);
|
||||
Connection con = createMock(Connection.class);
|
||||
expect(dataSource.getConnection()).andReturn(con);
|
||||
expect(con.getMetaData()).andReturn(dmd);
|
||||
expect(dmd.getDatabaseProductName()).andReturn("Oracle");
|
||||
expect(incrementerFactory.isSupportedIncrementerType("ORACLE")).andReturn(true);
|
||||
expect(incrementerFactory.getSupportedIncrementerTypes()).andReturn(new String[0]);
|
||||
expect(incrementerFactory.getIncrementer("ORACLE", tablePrefix + "JOB_SEQ")).andReturn(new StubIncrementer());
|
||||
expect(incrementerFactory.getIncrementer("ORACLE", tablePrefix + "JOB_EXECUTION_SEQ")).andReturn(
|
||||
new StubIncrementer());
|
||||
expect(incrementerFactory.getIncrementer("ORACLE", tablePrefix + "STEP_EXECUTION_SEQ")).andReturn(
|
||||
new StubIncrementer());
|
||||
replay(dataSource, con, dmd, incrementerFactory);
|
||||
factory.afterPropertiesSet();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoDatabaseType() throws Exception {
|
||||
|
||||
DatabaseMetaData dmd = createMock(DatabaseMetaData.class);
|
||||
Connection con = createMock(Connection.class);
|
||||
expect(dataSource.getConnection()).andReturn(con);
|
||||
expect(con.getMetaData()).andReturn(dmd);
|
||||
expect(dmd.getDatabaseProductName()).andReturn("foo");
|
||||
try {
|
||||
expect(incrementerFactory.isSupportedIncrementerType(null)).andReturn(false);
|
||||
expect(incrementerFactory.getSupportedIncrementerTypes()).andReturn(new String[0]);
|
||||
replay(dataSource, con, dmd, incrementerFactory);
|
||||
factory.afterPropertiesSet();
|
||||
fail();
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
String message = ex.getMessage();
|
||||
assertTrue("Wrong message: " + message, message.indexOf("DatabaseType") >= 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMissingDataSource() throws Exception {
|
||||
|
||||
@@ -119,60 +65,12 @@ public class JobExplorerFactoryBeanTests {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidDatabaseType() throws Exception {
|
||||
|
||||
factory.setDatabaseType("foo");
|
||||
try {
|
||||
expect(incrementerFactory.isSupportedIncrementerType("foo")).andReturn(false);
|
||||
expect(incrementerFactory.getSupportedIncrementerTypes()).andReturn(new String[0]);
|
||||
replay(incrementerFactory);
|
||||
factory.afterPropertiesSet();
|
||||
fail();
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
String message = ex.getMessage();
|
||||
assertTrue("Wrong message: " + message, message.indexOf("foo") >= 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateExplorer() throws Exception {
|
||||
String databaseType = "foo";
|
||||
factory.setDatabaseType(databaseType);
|
||||
|
||||
expect(incrementerFactory.isSupportedIncrementerType("foo")).andReturn(true);
|
||||
expect(incrementerFactory.getSupportedIncrementerTypes()).andReturn(new String[0]);
|
||||
expect(incrementerFactory.getIncrementer(databaseType, tablePrefix + "JOB_SEQ")).andReturn(
|
||||
new StubIncrementer());
|
||||
expect(incrementerFactory.getIncrementer(databaseType, tablePrefix + "JOB_EXECUTION_SEQ")).andReturn(
|
||||
new StubIncrementer());
|
||||
expect(incrementerFactory.getIncrementer(databaseType, tablePrefix + "STEP_EXECUTION_SEQ")).andReturn(
|
||||
new StubIncrementer());
|
||||
replay(incrementerFactory);
|
||||
|
||||
factory.afterPropertiesSet();
|
||||
factory.getObject();
|
||||
|
||||
verify(incrementerFactory);
|
||||
|
||||
}
|
||||
|
||||
private static class StubIncrementer implements DataFieldMaxValueIncrementer {
|
||||
|
||||
public int nextIntValue() throws DataAccessException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public long nextLongValue() throws DataAccessException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public String nextStringValue() throws DataAccessException {
|
||||
return null;
|
||||
}
|
||||
JobExplorer explorer = (JobExplorer) factory.getObject();
|
||||
assertNotNull(explorer);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user