BATCH-2178: allow injection of custom JdbcOperations implementation in
JobRepositoryFactoryBean and JobExplorerFactoryBean
This commit is contained in:
@@ -52,7 +52,7 @@ implements InitializingBean {
|
||||
|
||||
private DataSource dataSource;
|
||||
|
||||
private JdbcOperations jdbcTemplate;
|
||||
private JdbcOperations jdbcOperations;
|
||||
|
||||
private String tablePrefix = AbstractJdbcBatchMetadataDao.DEFAULT_TABLE_PREFIX;
|
||||
|
||||
@@ -87,6 +87,15 @@ implements InitializingBean {
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public setter for the {@link JdbcOperations}. If this property is not set explicitly,
|
||||
* a new {@link JdbcTemplate} will be created for the configured DataSource by default.
|
||||
* @param jdbcOperations a {@link JdbcOperations}
|
||||
*/
|
||||
public void setJdbcOperations(JdbcOperations jdbcOperations) {
|
||||
this.jdbcOperations = jdbcOperations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the table prefix for all the batch meta-data tables.
|
||||
@@ -112,7 +121,9 @@ implements InitializingBean {
|
||||
|
||||
Assert.notNull(dataSource, "DataSource must not be null.");
|
||||
|
||||
jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
if (jdbcOperations == null) {
|
||||
jdbcOperations = new JdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
if(serializer == null) {
|
||||
XStreamExecutionContextStringSerializer defaultSerializer = new XStreamExecutionContextStringSerializer();
|
||||
@@ -131,7 +142,7 @@ implements InitializingBean {
|
||||
@Override
|
||||
protected ExecutionContextDao createExecutionContextDao() throws Exception {
|
||||
JdbcExecutionContextDao dao = new JdbcExecutionContextDao();
|
||||
dao.setJdbcTemplate(jdbcTemplate);
|
||||
dao.setJdbcTemplate(jdbcOperations);
|
||||
dao.setLobHandler(lobHandler);
|
||||
dao.setTablePrefix(tablePrefix);
|
||||
dao.setSerializer(serializer);
|
||||
@@ -142,7 +153,7 @@ implements InitializingBean {
|
||||
@Override
|
||||
protected JobInstanceDao createJobInstanceDao() throws Exception {
|
||||
JdbcJobInstanceDao dao = new JdbcJobInstanceDao();
|
||||
dao.setJdbcTemplate(jdbcTemplate);
|
||||
dao.setJdbcTemplate(jdbcOperations);
|
||||
dao.setJobIncrementer(incrementer);
|
||||
dao.setTablePrefix(tablePrefix);
|
||||
dao.afterPropertiesSet();
|
||||
@@ -152,7 +163,7 @@ implements InitializingBean {
|
||||
@Override
|
||||
protected JobExecutionDao createJobExecutionDao() throws Exception {
|
||||
JdbcJobExecutionDao dao = new JdbcJobExecutionDao();
|
||||
dao.setJdbcTemplate(jdbcTemplate);
|
||||
dao.setJdbcTemplate(jdbcOperations);
|
||||
dao.setJobExecutionIncrementer(incrementer);
|
||||
dao.setTablePrefix(tablePrefix);
|
||||
dao.afterPropertiesSet();
|
||||
@@ -162,7 +173,7 @@ implements InitializingBean {
|
||||
@Override
|
||||
protected StepExecutionDao createStepExecutionDao() throws Exception {
|
||||
JdbcStepExecutionDao dao = new JdbcStepExecutionDao();
|
||||
dao.setJdbcTemplate(jdbcTemplate);
|
||||
dao.setJdbcTemplate(jdbcOperations);
|
||||
dao.setStepExecutionIncrementer(incrementer);
|
||||
dao.setTablePrefix(tablePrefix);
|
||||
dao.afterPropertiesSet();
|
||||
|
||||
@@ -65,7 +65,7 @@ public class JobRepositoryFactoryBean extends AbstractJobRepositoryFactoryBean i
|
||||
|
||||
private DataSource dataSource;
|
||||
|
||||
private JdbcOperations jdbcTemplate;
|
||||
private JdbcOperations jdbcOperations;
|
||||
|
||||
private String databaseType;
|
||||
|
||||
@@ -135,6 +135,15 @@ public class JobRepositoryFactoryBean extends AbstractJobRepositoryFactoryBean i
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public setter for the {@link JdbcOperations}. If this property is not set explicitly,
|
||||
* a new {@link JdbcTemplate} will be created for the configured DataSource by default.
|
||||
* @param jdbcOperations a {@link JdbcOperations}
|
||||
*/
|
||||
public void setJdbcOperations(JdbcOperations jdbcOperations) {
|
||||
this.jdbcOperations = jdbcOperations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the database type.
|
||||
@@ -162,7 +171,9 @@ public class JobRepositoryFactoryBean extends AbstractJobRepositoryFactoryBean i
|
||||
|
||||
Assert.notNull(dataSource, "DataSource must not be null.");
|
||||
|
||||
jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
if (jdbcOperations == null) {
|
||||
jdbcOperations = new JdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
if (incrementerFactory == null) {
|
||||
incrementerFactory = new DefaultDataFieldMaxValueIncrementerFactory(dataSource);
|
||||
@@ -198,7 +209,7 @@ public class JobRepositoryFactoryBean extends AbstractJobRepositoryFactoryBean i
|
||||
@Override
|
||||
protected JobInstanceDao createJobInstanceDao() throws Exception {
|
||||
JdbcJobInstanceDao dao = new JdbcJobInstanceDao();
|
||||
dao.setJdbcTemplate(jdbcTemplate);
|
||||
dao.setJdbcTemplate(jdbcOperations);
|
||||
dao.setJobIncrementer(incrementerFactory.getIncrementer(databaseType, tablePrefix + "JOB_SEQ"));
|
||||
dao.setTablePrefix(tablePrefix);
|
||||
dao.afterPropertiesSet();
|
||||
@@ -208,7 +219,7 @@ public class JobRepositoryFactoryBean extends AbstractJobRepositoryFactoryBean i
|
||||
@Override
|
||||
protected JobExecutionDao createJobExecutionDao() throws Exception {
|
||||
JdbcJobExecutionDao dao = new JdbcJobExecutionDao();
|
||||
dao.setJdbcTemplate(jdbcTemplate);
|
||||
dao.setJdbcTemplate(jdbcOperations);
|
||||
dao.setJobExecutionIncrementer(incrementerFactory.getIncrementer(databaseType, tablePrefix
|
||||
+ "JOB_EXECUTION_SEQ"));
|
||||
dao.setTablePrefix(tablePrefix);
|
||||
@@ -221,7 +232,7 @@ public class JobRepositoryFactoryBean extends AbstractJobRepositoryFactoryBean i
|
||||
@Override
|
||||
protected StepExecutionDao createStepExecutionDao() throws Exception {
|
||||
JdbcStepExecutionDao dao = new JdbcStepExecutionDao();
|
||||
dao.setJdbcTemplate(jdbcTemplate);
|
||||
dao.setJdbcTemplate(jdbcOperations);
|
||||
dao.setStepExecutionIncrementer(incrementerFactory.getIncrementer(databaseType, tablePrefix
|
||||
+ "STEP_EXECUTION_SEQ"));
|
||||
dao.setTablePrefix(tablePrefix);
|
||||
@@ -234,7 +245,7 @@ public class JobRepositoryFactoryBean extends AbstractJobRepositoryFactoryBean i
|
||||
@Override
|
||||
protected ExecutionContextDao createExecutionContextDao() throws Exception {
|
||||
JdbcExecutionContextDao dao = new JdbcExecutionContextDao();
|
||||
dao.setJdbcTemplate(jdbcTemplate);
|
||||
dao.setJdbcTemplate(jdbcOperations);
|
||||
dao.setTablePrefix(tablePrefix);
|
||||
dao.setClobTypeToUse(determineClobTypeToUse(this.databaseType));
|
||||
dao.setSerializer(serializer);
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.batch.core.explore.support;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
import static junit.framework.Assert.fail;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -26,6 +27,9 @@ 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.jdbc.core.JdbcOperations;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
@@ -49,6 +53,24 @@ public class JobExplorerFactoryBeanTests {
|
||||
factory.setTablePrefix(tablePrefix);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDefaultJdbcOperations() throws Exception {
|
||||
|
||||
factory.afterPropertiesSet();
|
||||
JdbcOperations jdbcOperations = (JdbcOperations) ReflectionTestUtils.getField(factory, "jdbcOperations");
|
||||
assertTrue(jdbcOperations instanceof JdbcTemplate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomJdbcOperations() throws Exception {
|
||||
|
||||
JdbcOperations customJdbcOperations = mock(JdbcOperations.class);
|
||||
factory.setJdbcOperations(customJdbcOperations);
|
||||
factory.afterPropertiesSet();
|
||||
assertEquals(customJdbcOperations, ReflectionTestUtils.getField(factory, "jdbcOperations"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMissingDataSource() throws Exception {
|
||||
|
||||
@@ -40,6 +40,8 @@ import org.springframework.batch.core.repository.dao.XStreamExecutionContextStri
|
||||
import org.springframework.batch.item.database.support.DataFieldMaxValueIncrementerFactory;
|
||||
import org.springframework.core.serializer.Serializer;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.jdbc.core.JdbcOperations;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
|
||||
import org.springframework.jdbc.support.lob.DefaultLobHandler;
|
||||
import org.springframework.jdbc.support.lob.LobHandler;
|
||||
@@ -173,7 +175,45 @@ public class JobRepositoryFactoryBeanTests {
|
||||
factory.afterPropertiesSet();
|
||||
assertEquals(customSerializer, ReflectionTestUtils.getField(factory, "serializer"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultJdbcOperations() throws Exception {
|
||||
|
||||
factory.setDatabaseType("ORACLE");
|
||||
|
||||
incrementerFactory = mock(DataFieldMaxValueIncrementerFactory.class);
|
||||
when(incrementerFactory.isSupportedIncrementerType("ORACLE")).thenReturn(true);
|
||||
when(incrementerFactory.getIncrementer("ORACLE", tablePrefix + "JOB_SEQ")).thenReturn(new StubIncrementer());
|
||||
when(incrementerFactory.getIncrementer("ORACLE", tablePrefix + "JOB_EXECUTION_SEQ")).thenReturn(new StubIncrementer());
|
||||
when(incrementerFactory.getIncrementer("ORACLE", tablePrefix + "STEP_EXECUTION_SEQ")).thenReturn(new StubIncrementer());
|
||||
factory.setIncrementerFactory(incrementerFactory);
|
||||
|
||||
factory.afterPropertiesSet();
|
||||
|
||||
JdbcOperations jdbcOperations = (JdbcOperations) ReflectionTestUtils.getField(factory, "jdbcOperations");
|
||||
assertTrue(jdbcOperations instanceof JdbcTemplate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomJdbcOperations() throws Exception {
|
||||
|
||||
factory.setDatabaseType("ORACLE");
|
||||
|
||||
incrementerFactory = mock(DataFieldMaxValueIncrementerFactory.class);
|
||||
when(incrementerFactory.isSupportedIncrementerType("ORACLE")).thenReturn(true);
|
||||
when(incrementerFactory.getIncrementer("ORACLE", tablePrefix + "JOB_SEQ")).thenReturn(new StubIncrementer());
|
||||
when(incrementerFactory.getIncrementer("ORACLE", tablePrefix + "JOB_EXECUTION_SEQ")).thenReturn(new StubIncrementer());
|
||||
when(incrementerFactory.getIncrementer("ORACLE", tablePrefix + "STEP_EXECUTION_SEQ")).thenReturn(new StubIncrementer());
|
||||
factory.setIncrementerFactory(incrementerFactory);
|
||||
|
||||
JdbcOperations customJdbcOperations = mock(JdbcOperations.class);
|
||||
factory.setJdbcOperations(customJdbcOperations);
|
||||
|
||||
factory.afterPropertiesSet();
|
||||
|
||||
assertEquals(customJdbcOperations, ReflectionTestUtils.getField(factory, "jdbcOperations"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMissingDataSource() throws Exception {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user