RESOLVED - issue BATCH-88: StatisticsProvider is a leaky abstraction
http://jira.springframework.org/browse/BATCH-88
This commit is contained in:
@@ -11,8 +11,7 @@ import org.springframework.batch.execution.scope.StepSynchronizationManager;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
public class StagingItemProcessorTests extends
|
||||
AbstractTransactionalDataSourceSpringContextTests {
|
||||
public class StagingItemProcessorTests extends AbstractTransactionalDataSourceSpringContextTests {
|
||||
|
||||
private StagingItemProcessor processor;
|
||||
|
||||
@@ -21,25 +20,21 @@ public class StagingItemProcessorTests extends
|
||||
}
|
||||
|
||||
protected String[] getConfigLocations() {
|
||||
return new String[] { ClassUtils.addResourcePathToPackagePath(
|
||||
StagingItemProcessor.class, "staging-test-context.xml") };
|
||||
return new String[] { ClassUtils.addResourcePathToPackagePath(StagingItemProcessor.class,
|
||||
"staging-test-context.xml") };
|
||||
}
|
||||
|
||||
protected void prepareTestInstance() throws Exception {
|
||||
SimpleStepContext stepScopeContext = StepSynchronizationManager
|
||||
.open();
|
||||
stepScopeContext.setStepExecution(new StepExecution(new StepInstance(
|
||||
new Long(11)), new JobExecution(new JobInstance(new Long(12),
|
||||
new JobParameters(), new Job("job")))));
|
||||
SimpleStepContext stepScopeContext = new SimpleStepContext(new StepExecution(new StepInstance(new Long(11)),
|
||||
new JobExecution(new JobInstance(new Long(12), new JobParameters(), new Job("job")))));
|
||||
StepSynchronizationManager.register(stepScopeContext);
|
||||
super.prepareTestInstance();
|
||||
}
|
||||
|
||||
public void testProcessInsertsNewItem() throws Exception {
|
||||
int before = getJdbcTemplate().queryForInt(
|
||||
"SELECT COUNT(*) from BATCH_STAGING");
|
||||
int before = getJdbcTemplate().queryForInt("SELECT COUNT(*) from BATCH_STAGING");
|
||||
processor.process("FOO");
|
||||
int after = getJdbcTemplate().queryForInt(
|
||||
"SELECT COUNT(*) from BATCH_STAGING");
|
||||
int after = getJdbcTemplate().queryForInt("SELECT COUNT(*) from BATCH_STAGING");
|
||||
assertEquals(before + 1, after);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,21 +6,22 @@ import org.springframework.batch.core.domain.JobParameters;
|
||||
import org.springframework.batch.core.domain.StepExecution;
|
||||
import org.springframework.batch.core.domain.StepInstance;
|
||||
import org.springframework.batch.execution.scope.SimpleStepContext;
|
||||
import org.springframework.batch.execution.scope.StepContext;
|
||||
import org.springframework.batch.execution.scope.StepSynchronizationManager;
|
||||
import org.springframework.batch.repeat.context.RepeatContextSupport;
|
||||
import org.springframework.batch.repeat.synch.BatchTransactionSynchronizationManager;
|
||||
import org.springframework.batch.repeat.synch.RepeatSynchronizationManager;
|
||||
import org.springframework.batch.sample.item.processor.StagingItemProcessor;
|
||||
import org.springframework.batch.sample.item.reader.StagingItemReader;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
public class StagingItemReaderTests extends
|
||||
AbstractTransactionalDataSourceSpringContextTests {
|
||||
public class StagingItemReaderTests extends AbstractTransactionalDataSourceSpringContextTests {
|
||||
|
||||
private StagingItemProcessor processor;
|
||||
|
||||
private StagingItemReader provider;
|
||||
private Long jobId;
|
||||
|
||||
private Long jobId = new Long(11);
|
||||
|
||||
public void setProcessor(StagingItemProcessor processor) {
|
||||
this.processor = processor;
|
||||
@@ -31,16 +32,14 @@ public class StagingItemReaderTests extends
|
||||
}
|
||||
|
||||
protected String[] getConfigLocations() {
|
||||
return new String[] { ClassUtils.addResourcePathToPackagePath(
|
||||
StagingItemProcessor.class, "staging-test-context.xml") };
|
||||
return new String[] { ClassUtils.addResourcePathToPackagePath(StagingItemProcessor.class,
|
||||
"staging-test-context.xml") };
|
||||
}
|
||||
|
||||
protected void prepareTestInstance() throws Exception {
|
||||
SimpleStepContext stepScopeContext = StepSynchronizationManager.open();
|
||||
jobId = new Long(11);
|
||||
stepScopeContext.setStepExecution(new StepExecution(new StepInstance(
|
||||
new Long(12)), new JobExecution(new JobInstance(jobId,
|
||||
new JobParameters()))));
|
||||
StepContext stepScopeContext = new SimpleStepContext(new StepExecution(new StepInstance(new Long(12)),
|
||||
new JobExecution(new JobInstance(jobId, new JobParameters()))));
|
||||
StepSynchronizationManager.register(stepScopeContext);
|
||||
RepeatSynchronizationManager.register(new RepeatContextSupport(null));
|
||||
super.prepareTestInstance();
|
||||
}
|
||||
@@ -59,36 +58,31 @@ public class StagingItemReaderTests extends
|
||||
|
||||
public void testReaderUpdatesProcessIndicator() throws Exception {
|
||||
|
||||
long id = getJdbcTemplate().queryForLong(
|
||||
"SELECT MIN(ID) from BATCH_STAGING where JOB_ID=?",
|
||||
long id = getJdbcTemplate().queryForLong("SELECT MIN(ID) from BATCH_STAGING where JOB_ID=?",
|
||||
new Object[] { jobId });
|
||||
String before = (String) getJdbcTemplate().queryForObject(
|
||||
"SELECT PROCESSED from BATCH_STAGING where ID=?",
|
||||
String before = (String) getJdbcTemplate().queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?",
|
||||
new Object[] { new Long(id) }, String.class);
|
||||
assertEquals(StagingItemProcessor.NEW, before);
|
||||
|
||||
Object item = provider.read();
|
||||
assertEquals("FOO", item);
|
||||
|
||||
String after = (String) getJdbcTemplate().queryForObject(
|
||||
"SELECT PROCESSED from BATCH_STAGING where ID=?",
|
||||
String after = (String) getJdbcTemplate().queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?",
|
||||
new Object[] { new Long(id) }, String.class);
|
||||
assertEquals(StagingItemProcessor.DONE, after);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void testUpdateProcessIndicatorAfterCommit() throws Exception {
|
||||
testReaderUpdatesProcessIndicator();
|
||||
setComplete();
|
||||
endTransaction();
|
||||
startNewTransaction();
|
||||
long id = getJdbcTemplate().queryForLong(
|
||||
"SELECT MIN(ID) from BATCH_STAGING where JOB_ID=?",
|
||||
long id = getJdbcTemplate().queryForLong("SELECT MIN(ID) from BATCH_STAGING where JOB_ID=?",
|
||||
new Object[] { jobId });
|
||||
String before = (String) getJdbcTemplate().queryForObject(
|
||||
"SELECT PROCESSED from BATCH_STAGING where ID=?",
|
||||
String before = (String) getJdbcTemplate().queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?",
|
||||
new Object[] { new Long(id) }, String.class);
|
||||
assertEquals(StagingItemProcessor.DONE, before);
|
||||
assertEquals(StagingItemProcessor.DONE, before);
|
||||
}
|
||||
|
||||
public void testProviderRollsBackMultipleTimes() throws Exception {
|
||||
@@ -96,14 +90,14 @@ public class StagingItemReaderTests extends
|
||||
setComplete();
|
||||
endTransaction();
|
||||
startNewTransaction();
|
||||
// After a rollback we have to resynchronize the TX to simulate a real batch
|
||||
// After a rollback we have to resynchronize the TX to simulate a real
|
||||
// batch
|
||||
BatchTransactionSynchronizationManager.resynchronize();
|
||||
|
||||
int count = getJdbcTemplate().queryForInt(
|
||||
"SELECT COUNT(*) from BATCH_STAGING where JOB_ID=? AND PROCESSED=?",
|
||||
int count = getJdbcTemplate().queryForInt("SELECT COUNT(*) from BATCH_STAGING where JOB_ID=? AND PROCESSED=?",
|
||||
new Object[] { jobId, StagingItemProcessor.NEW });
|
||||
assertEquals(4, count);
|
||||
|
||||
|
||||
Object item = provider.read();
|
||||
assertEquals("FOO", item);
|
||||
item = provider.read();
|
||||
@@ -112,7 +106,7 @@ public class StagingItemReaderTests extends
|
||||
endTransaction();
|
||||
startNewTransaction();
|
||||
BatchTransactionSynchronizationManager.resynchronize();
|
||||
|
||||
|
||||
item = provider.read();
|
||||
assertEquals("FOO", item);
|
||||
item = provider.read();
|
||||
@@ -123,25 +117,24 @@ public class StagingItemReaderTests extends
|
||||
endTransaction();
|
||||
startNewTransaction();
|
||||
BatchTransactionSynchronizationManager.resynchronize();
|
||||
|
||||
|
||||
item = provider.read();
|
||||
assertEquals("FOO", item);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void testProviderRollsBackProcessIndicator() throws Exception {
|
||||
|
||||
setComplete();
|
||||
endTransaction();
|
||||
startNewTransaction();
|
||||
// After a rollback we have to resynchronize the TX to simulate a real batch
|
||||
// After a rollback we have to resynchronize the TX to simulate a real
|
||||
// batch
|
||||
BatchTransactionSynchronizationManager.resynchronize();
|
||||
|
||||
long id = getJdbcTemplate().queryForLong(
|
||||
"SELECT MIN(ID) from BATCH_STAGING where JOB_ID=?",
|
||||
long id = getJdbcTemplate().queryForLong("SELECT MIN(ID) from BATCH_STAGING where JOB_ID=?",
|
||||
new Object[] { jobId });
|
||||
String before = (String) getJdbcTemplate().queryForObject(
|
||||
"SELECT PROCESSED from BATCH_STAGING where ID=?",
|
||||
String before = (String) getJdbcTemplate().queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?",
|
||||
new Object[] { new Long(id) }, String.class);
|
||||
assertEquals(StagingItemProcessor.NEW, before);
|
||||
|
||||
@@ -150,11 +143,11 @@ public class StagingItemReaderTests extends
|
||||
|
||||
endTransaction();
|
||||
startNewTransaction();
|
||||
// After a rollback we have to resynchronize the TX to simulate a real batch
|
||||
// After a rollback we have to resynchronize the TX to simulate a real
|
||||
// batch
|
||||
BatchTransactionSynchronizationManager.resynchronize();
|
||||
|
||||
String after = (String) getJdbcTemplate().queryForObject(
|
||||
"SELECT PROCESSED from BATCH_STAGING where ID=?",
|
||||
String after = (String) getJdbcTemplate().queryForObject("SELECT PROCESSED from BATCH_STAGING where ID=?",
|
||||
new Object[] { new Long(id) }, String.class);
|
||||
assertEquals(StagingItemProcessor.NEW, after);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user