IN PROGRESS - BATCH-710: updated tests to use SpringJUnit4ClassRunner and SimpleJdbcTemplate
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
package org.springframework.batch.core.repository.dao;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.springframework.batch.core.Job;
|
||||
@@ -7,9 +11,10 @@ import org.springframework.batch.core.JobInstance;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.job.JobSupport;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
public abstract class AbstractJobInstanceDaoTests extends AbstractTransactionalDataSourceSpringContextTests {
|
||||
public abstract class AbstractJobInstanceDaoTests extends AbstractTransactionalJUnit4SpringContextTests {
|
||||
|
||||
private static final long DATE = 777;
|
||||
|
||||
@@ -18,18 +23,20 @@ public abstract class AbstractJobInstanceDaoTests extends AbstractTransactionalD
|
||||
private Job fooJob = new JobSupport("foo");
|
||||
|
||||
private JobParameters fooParams = new JobParametersBuilder().addString("stringKey", "stringValue").addLong(
|
||||
"longKey", new Long(Long.MAX_VALUE)).addDouble("doubleKey", new Double(Double.MAX_VALUE)).addDate(
|
||||
"longKey", Long.MAX_VALUE).addDouble("doubleKey", Double.MAX_VALUE).addDate(
|
||||
"dateKey", new Date(DATE)).toJobParameters();
|
||||
|
||||
protected abstract JobInstanceDao getJobInstanceDao();
|
||||
|
||||
protected void onSetUp() throws Exception {
|
||||
@Before
|
||||
public void onSetUp() throws Exception {
|
||||
dao = getJobInstanceDao();
|
||||
}
|
||||
|
||||
/**
|
||||
/*
|
||||
* Create and retrieve a job instance.
|
||||
*/
|
||||
@Transactional @Test
|
||||
public void testCreateAndRetrieve() throws Exception {
|
||||
|
||||
JobInstance fooInstance = dao.createJobInstance(fooJob, fooParams);
|
||||
@@ -52,6 +59,7 @@ public abstract class AbstractJobInstanceDaoTests extends AbstractTransactionalD
|
||||
/**
|
||||
* Trying to create instance twice for the same job+parameters causes error
|
||||
*/
|
||||
@Transactional @Test
|
||||
public void testCreateDuplicateInstance() {
|
||||
|
||||
dao.createJobInstance(fooJob, fooParams);
|
||||
@@ -65,9 +73,10 @@ public abstract class AbstractJobInstanceDaoTests extends AbstractTransactionalD
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional @Test
|
||||
public void testCreationAddsVersion() {
|
||||
|
||||
JobInstance jobInstance = new JobInstance(new Long(1), new JobParameters(), "testVersionAndId");
|
||||
JobInstance jobInstance = new JobInstance((long) 1, new JobParameters(), "testVersionAndId");
|
||||
|
||||
assertNull(jobInstance.getVersion());
|
||||
|
||||
|
||||
@@ -16,6 +16,10 @@
|
||||
|
||||
package org.springframework.batch.core.repository.dao;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.batch.core.BatchStatus;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobInstance;
|
||||
@@ -26,14 +30,15 @@ import org.springframework.batch.core.job.JobSupport;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.step.StepSupport;
|
||||
import org.springframework.dao.OptimisticLockingFailureException;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* Tests for {@link StepExecutionDao} implementations.
|
||||
*
|
||||
* @see #getStepExecutionDao()
|
||||
*/
|
||||
public abstract class AbstractStepExecutionDaoTests extends AbstractTransactionalDataSourceSpringContextTests {
|
||||
public abstract class AbstractStepExecutionDaoTests extends AbstractTransactionalJUnit4SpringContextTests {
|
||||
|
||||
protected StepExecutionDao dao;
|
||||
|
||||
@@ -57,7 +62,8 @@ public abstract class AbstractStepExecutionDaoTests extends AbstractTransactiona
|
||||
*/
|
||||
protected abstract JobRepository getJobRepository();
|
||||
|
||||
protected void onSetUp() throws Exception {
|
||||
@Before
|
||||
public void onSetUp() throws Exception {
|
||||
repository = getJobRepository();
|
||||
|
||||
|
||||
@@ -68,6 +74,7 @@ public abstract class AbstractStepExecutionDaoTests extends AbstractTransactiona
|
||||
dao = getStepExecutionDao();
|
||||
}
|
||||
|
||||
@Transactional @Test
|
||||
public void testSaveExecutionAssignsIdAndVersion() throws Exception {
|
||||
|
||||
assertNull(stepExecution.getId());
|
||||
@@ -77,6 +84,7 @@ public abstract class AbstractStepExecutionDaoTests extends AbstractTransactiona
|
||||
assertNotNull(stepExecution.getVersion());
|
||||
}
|
||||
|
||||
@Transactional @Test
|
||||
public void testSaveAndFindExecution() {
|
||||
|
||||
stepExecution.setStatus(BatchStatus.STARTED);
|
||||
@@ -95,15 +103,17 @@ public abstract class AbstractStepExecutionDaoTests extends AbstractTransactiona
|
||||
assertNull(dao.getStepExecution(jobExecution, new StepSupport("not-existing step")));
|
||||
}
|
||||
|
||||
@Transactional @Test
|
||||
public void testGetForNotExistingJobExecution() {
|
||||
assertNull(dao.getStepExecution(new JobExecution(jobInstance, new Long(777)), step));
|
||||
assertNull(dao.getStepExecution(new JobExecution(jobInstance, (long) 777), step));
|
||||
}
|
||||
|
||||
/**
|
||||
* To-be-saved execution must not already have an id.
|
||||
*/
|
||||
@Transactional @Test
|
||||
public void testSaveExecutionWithIdAlreadySet() {
|
||||
stepExecution.setId(new Long(7));
|
||||
stepExecution.setId((long) 7);
|
||||
try {
|
||||
dao.saveStepExecution(stepExecution);
|
||||
fail();
|
||||
@@ -116,6 +126,7 @@ public abstract class AbstractStepExecutionDaoTests extends AbstractTransactiona
|
||||
/**
|
||||
* To-be-saved execution must not already have a version.
|
||||
*/
|
||||
@Transactional @Test
|
||||
public void testSaveExecutionWithVersionAlreadySet() {
|
||||
stepExecution.incrementVersion();
|
||||
try {
|
||||
@@ -131,6 +142,7 @@ public abstract class AbstractStepExecutionDaoTests extends AbstractTransactiona
|
||||
* Update and retrieve updated StepExecution - make sure the update is
|
||||
* reflected as expected and version number has been incremented
|
||||
*/
|
||||
@Transactional @Test
|
||||
public void testUpdateExecution() {
|
||||
stepExecution.setStatus(BatchStatus.STARTED);
|
||||
dao.saveStepExecution(stepExecution);
|
||||
@@ -138,7 +150,7 @@ public abstract class AbstractStepExecutionDaoTests extends AbstractTransactiona
|
||||
|
||||
stepExecution.setStatus(BatchStatus.STOPPED);
|
||||
dao.updateStepExecution(stepExecution);
|
||||
assertEquals(versionAfterSave.intValue() + 1, stepExecution.getVersion().intValue());
|
||||
assertEquals(versionAfterSave + 1, stepExecution.getVersion().intValue());
|
||||
|
||||
StepExecution retrieved = dao.getStepExecution(jobExecution, step);
|
||||
assertEquals(stepExecution, retrieved);
|
||||
@@ -149,6 +161,7 @@ public abstract class AbstractStepExecutionDaoTests extends AbstractTransactiona
|
||||
* Exception should be raised when the version of update argument doesn't
|
||||
* match the version of persisted entity.
|
||||
*/
|
||||
@Transactional @Test
|
||||
public void testConcurrentModificationException() {
|
||||
step = new StepSupport("foo");
|
||||
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
package org.springframework.batch.core.repository.dao;
|
||||
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = "sql-dao-test.xml")
|
||||
public class JdbcJobInstanceDaoTests extends AbstractJobInstanceDaoTests {
|
||||
|
||||
protected JobInstanceDao getJobInstanceDao() {
|
||||
deleteFromTables(new String[] { "BATCH_EXECUTION_CONTEXT", "BATCH_STEP_EXECUTION", "BATCH_JOB_EXECUTION",
|
||||
"BATCH_JOB_PARAMS", "BATCH_JOB_INSTANCE" });
|
||||
return (JobInstanceDao) getApplicationContext().getBean("jobInstanceDao");
|
||||
deleteFromTables("BATCH_EXECUTION_CONTEXT", "BATCH_STEP_EXECUTION", "BATCH_JOB_EXECUTION",
|
||||
"BATCH_JOB_PARAMS", "BATCH_JOB_INSTANCE");
|
||||
return (JobInstanceDao) applicationContext.getBean("jobInstanceDao");
|
||||
}
|
||||
|
||||
protected String[] getConfigLocations() {
|
||||
return new String[] { ClassUtils.addResourcePathToPackagePath(getClass(), "sql-dao-test.xml") };
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,29 +1,34 @@
|
||||
package org.springframework.batch.core.repository.dao;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.repeat.ExitStatus;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = "sql-dao-test.xml")
|
||||
public class JdbcStepExecutionDaoTests extends AbstractStepExecutionDaoTests {
|
||||
|
||||
protected StepExecutionDao getStepExecutionDao() {
|
||||
return (StepExecutionDao) getApplicationContext().getBean("stepExecutionDao");
|
||||
return (StepExecutionDao) applicationContext.getBean("stepExecutionDao");
|
||||
}
|
||||
|
||||
protected JobRepository getJobRepository() {
|
||||
deleteFromTables(new String[] { "BATCH_EXECUTION_CONTEXT", "BATCH_STEP_EXECUTION", "BATCH_JOB_EXECUTION",
|
||||
"BATCH_JOB_PARAMS", "BATCH_JOB_INSTANCE" });
|
||||
return (JobRepository) getApplicationContext().getBean("jobRepository");
|
||||
}
|
||||
|
||||
protected String[] getConfigLocations() {
|
||||
return new String[] { ClassUtils.addResourcePathToPackagePath(getClass(), "sql-dao-test.xml") };
|
||||
deleteFromTables("BATCH_EXECUTION_CONTEXT", "BATCH_STEP_EXECUTION", "BATCH_JOB_EXECUTION",
|
||||
"BATCH_JOB_PARAMS", "BATCH_JOB_INSTANCE");
|
||||
return (JobRepository) applicationContext.getBean("jobRepository");
|
||||
}
|
||||
|
||||
/**
|
||||
* Long exit descriptions are truncated on both save and update.
|
||||
*/
|
||||
@Transactional @Test
|
||||
public void testTruncateExitDescription() {
|
||||
|
||||
StringBuffer sb = new StringBuffer();
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
package org.springframework.batch.core.repository.dao;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.internal.runners.JUnit4ClassRunner;
|
||||
|
||||
@RunWith(JUnit4ClassRunner.class)
|
||||
public class MapJobInstanceDaoTests extends AbstractJobInstanceDaoTests {
|
||||
|
||||
protected JobInstanceDao getJobInstanceDao() {
|
||||
|
||||
@@ -2,7 +2,10 @@ package org.springframework.batch.core.repository.dao;
|
||||
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.repository.support.SimpleJobRepository;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.internal.runners.JUnit4ClassRunner;
|
||||
|
||||
@RunWith(JUnit4ClassRunner.class)
|
||||
public class MapStepExecutionDaoTests extends AbstractStepExecutionDaoTests {
|
||||
|
||||
protected StepExecutionDao getStepExecutionDao() {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package org.springframework.batch.core.repository.support;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.springframework.batch.core.BatchStatus;
|
||||
@@ -11,22 +13,23 @@ import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.job.JobSupport;
|
||||
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
|
||||
import org.springframework.batch.core.repository.JobRestartException;
|
||||
import org.springframework.batch.core.repository.dao.AbstractJobDaoTests;
|
||||
import org.springframework.batch.core.step.StepSupport;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Repository tests using JDBC DAOs (rather than mocks).
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class SimpleJobRepositoryIntegrationTests extends AbstractTransactionalDataSourceSpringContextTests {
|
||||
|
||||
protected String[] getConfigLocations() {
|
||||
return new String[] { ClassUtils.addResourcePathToPackagePath(AbstractJobDaoTests.class, "sql-dao-test.xml") };
|
||||
}
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = "/org/springframework/batch/core/repository/dao/sql-dao-test.xml")
|
||||
public class SimpleJobRepositoryIntegrationTests {
|
||||
|
||||
private SimpleJobRepository jobRepository;
|
||||
|
||||
@@ -34,14 +37,16 @@ public class SimpleJobRepositoryIntegrationTests extends AbstractTransactionalDa
|
||||
|
||||
private JobParameters jobParameters = new JobParameters();
|
||||
|
||||
@Autowired
|
||||
public void setJobRepository(SimpleJobRepository jobRepository) {
|
||||
this.jobRepository = jobRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
/*
|
||||
* Create two job executions for same job+parameters tuple. Check both
|
||||
* executions belong to the same job instance and job.
|
||||
*/
|
||||
@Transactional @Test
|
||||
public void testCreateAndFind() throws Exception {
|
||||
|
||||
job.setRestartable(true);
|
||||
@@ -64,10 +69,11 @@ public class SimpleJobRepositoryIntegrationTests extends AbstractTransactionalDa
|
||||
assertEquals(job.getName(), secondExecution.getJobInstance().getJobName());
|
||||
}
|
||||
|
||||
/**
|
||||
/*
|
||||
* Create two job executions for same job+parameters tuple. Check both
|
||||
* executions belong to the same job instance and job.
|
||||
*/
|
||||
@Transactional @Test
|
||||
public void testCreateAndFindWithNoStartDate() throws Exception {
|
||||
job.setRestartable(true);
|
||||
|
||||
@@ -81,10 +87,11 @@ public class SimpleJobRepositoryIntegrationTests extends AbstractTransactionalDa
|
||||
assertEquals(job.getName(), secondExecution.getJobInstance().getJobName());
|
||||
}
|
||||
|
||||
/**
|
||||
/*
|
||||
* Non-restartable JobInstance can be run only once - attempt to run
|
||||
* existing non-restartable JobInstance causes error.
|
||||
*/
|
||||
@Transactional @Test
|
||||
public void testRunNonRestartableJobInstanceTwice() throws Exception {
|
||||
job.setRestartable(false);
|
||||
|
||||
@@ -100,10 +107,11 @@ public class SimpleJobRepositoryIntegrationTests extends AbstractTransactionalDa
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
/*
|
||||
* Save multiple StepExecutions for the same step and check the returned
|
||||
* count and last execution are correct.
|
||||
*/
|
||||
@Transactional @Test
|
||||
public void testGetStepExecutionCountAndLastStepExecution() throws Exception {
|
||||
job.setRestartable(true);
|
||||
StepSupport step = new StepSupport("restartedStep");
|
||||
@@ -137,9 +145,10 @@ public class SimpleJobRepositoryIntegrationTests extends AbstractTransactionalDa
|
||||
assertEquals(secondStepExec, jobRepository.getLastStepExecution(secondJobExec.getJobInstance(), step));
|
||||
}
|
||||
|
||||
/**
|
||||
/*
|
||||
* Save execution context and retrieve it.
|
||||
*/
|
||||
@Transactional @Test
|
||||
public void testSaveExecutionContext() throws Exception {
|
||||
ExecutionContext ctx = new ExecutionContext() {
|
||||
{
|
||||
@@ -165,10 +174,11 @@ public class SimpleJobRepositoryIntegrationTests extends AbstractTransactionalDa
|
||||
// assertEquals(ctx, retrievedJobExec.getExecutionContext());
|
||||
}
|
||||
|
||||
/**
|
||||
/*
|
||||
* If JobExecution is already running, exception will be thrown in attempt
|
||||
* to create new execution.
|
||||
*/
|
||||
@Transactional @Test
|
||||
public void testOnlyOneJobExecutionAllowedRunning() throws Exception {
|
||||
job.setRestartable(true);
|
||||
jobRepository.createJobExecution(job, jobParameters);
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package org.springframework.batch.core.resource;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -8,22 +10,36 @@ import org.springframework.batch.core.JobInstance;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.repository.dao.AbstractJobDaoTests;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.database.JdbcCursorItemReader;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
public class JdbcCursorItemReaderPreparedStatementIntegrationTests extends
|
||||
AbstractTransactionalDataSourceSpringContextTests {
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = "/org/springframework/batch/core/repository/dao/data-source-context.xml")
|
||||
public class JdbcCursorItemReaderPreparedStatementIntegrationTests {
|
||||
|
||||
JdbcCursorItemReader<Foo> itemReader;
|
||||
|
||||
private DataSource dataSource;
|
||||
|
||||
@Autowired
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
protected void onSetUpInTransaction() throws Exception {
|
||||
super.onSetUpInTransaction();
|
||||
@Before
|
||||
public void onSetUpInTransaction() throws Exception {
|
||||
|
||||
itemReader = new JdbcCursorItemReader<Foo>();
|
||||
itemReader.setDataSource(super.getJdbcTemplate().getDataSource());
|
||||
itemReader.setDataSource(dataSource);
|
||||
itemReader.setSql("select ID, NAME, VALUE from T_FOOS where ID > ? and ID < ?");
|
||||
itemReader.setIgnoreWarnings(true);
|
||||
itemReader.setVerifyCursorPosition(true);
|
||||
@@ -34,10 +50,10 @@ public class JdbcCursorItemReaderPreparedStatementIntegrationTests extends
|
||||
itemReader.setQueryTimeout(1000);
|
||||
itemReader.setSaveState(true);
|
||||
StepExecutionPreparedStatementSetter pss = new StepExecutionPreparedStatementSetter();
|
||||
JobParameters jobParameters = new JobParametersBuilder().addLong("begin.id", new Long(1)).addLong("end.id", new Long(4)).toJobParameters();
|
||||
JobInstance jobInstance = new JobInstance(new Long(1), jobParameters, "simpleJob");
|
||||
JobExecution jobExecution = new JobExecution(jobInstance, new Long(2));
|
||||
StepExecution stepExecution = new StepExecution("taskletStep", jobExecution, new Long(3) );
|
||||
JobParameters jobParameters = new JobParametersBuilder().addLong("begin.id", 1L).addLong("end.id", 4L).toJobParameters();
|
||||
JobInstance jobInstance = new JobInstance(1L, jobParameters, "simpleJob");
|
||||
JobExecution jobExecution = new JobExecution(jobInstance, (long) 2);
|
||||
StepExecution stepExecution = new StepExecution("taskletStep", jobExecution, 3L);
|
||||
pss.beforeStep(stepExecution);
|
||||
|
||||
List<String> parameterNames = new ArrayList<String>();
|
||||
@@ -48,6 +64,7 @@ public class JdbcCursorItemReaderPreparedStatementIntegrationTests extends
|
||||
itemReader.setPreparedStatementSetter(pss);
|
||||
}
|
||||
|
||||
@Transactional @Test
|
||||
public void testRead() throws Exception{
|
||||
itemReader.open(new ExecutionContext());
|
||||
Foo foo = itemReader.read();
|
||||
@@ -57,7 +74,4 @@ public class JdbcCursorItemReaderPreparedStatementIntegrationTests extends
|
||||
assertNull(itemReader.read());
|
||||
}
|
||||
|
||||
protected String[] getConfigLocations() {
|
||||
return new String[] { ClassUtils.addResourcePathToPackagePath(AbstractJobDaoTests.class, "data-source-context.xml") };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,41 +20,56 @@ import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobInstance;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.repository.dao.AbstractJobDaoTests;
|
||||
import org.springframework.jdbc.core.RowCallbackHandler;
|
||||
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* @author Lucas Ward
|
||||
*
|
||||
*/
|
||||
public class StepExecutionPreparedStatementSetterTests extends AbstractTransactionalDataSourceSpringContextTests {
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = "/org/springframework/batch/core/repository/dao/data-source-context.xml")
|
||||
public class StepExecutionPreparedStatementSetterTests {
|
||||
|
||||
StepExecutionPreparedStatementSetter pss;
|
||||
|
||||
StepExecution stepExecution;
|
||||
|
||||
protected String[] getConfigLocations() {
|
||||
return new String[] { ClassUtils.addResourcePathToPackagePath(AbstractJobDaoTests.class, "data-source-context.xml") };
|
||||
private SimpleJdbcTemplate simpleJdbcTemplate;
|
||||
|
||||
@Autowired
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
protected void onSetUpInTransaction() throws Exception {
|
||||
super.onSetUpInTransaction();
|
||||
|
||||
@Before
|
||||
public void onSetUpInTransaction() throws Exception {
|
||||
|
||||
pss = new StepExecutionPreparedStatementSetter();
|
||||
JobParameters jobParameters = new JobParametersBuilder().addLong("begin.id", new Long(1)).addLong("end.id", new Long(4)).toJobParameters();
|
||||
JobInstance jobInstance = new JobInstance(new Long(1), jobParameters, "simpleJob");
|
||||
JobExecution jobExecution = new JobExecution(jobInstance, new Long(2));
|
||||
stepExecution = new StepExecution("taskletStep", jobExecution, new Long(3) );
|
||||
JobParameters jobParameters = new JobParametersBuilder().addLong("begin.id", 1L).addLong("end.id", 4L).toJobParameters();
|
||||
JobInstance jobInstance = new JobInstance(1L, jobParameters, "simpleJob");
|
||||
JobExecution jobExecution = new JobExecution(jobInstance, 2L);
|
||||
stepExecution = new StepExecution("taskletStep", jobExecution, 3L);
|
||||
pss.beforeStep(stepExecution);
|
||||
jdbcTemplate = getJdbcTemplate();
|
||||
}
|
||||
|
||||
@Transactional @Test
|
||||
public void testSetValues(){
|
||||
|
||||
List<String> parameterNames = new ArrayList<String>();
|
||||
@@ -62,17 +77,20 @@ public class StepExecutionPreparedStatementSetterTests extends AbstractTransacti
|
||||
parameterNames.add("end.id");
|
||||
pss.setParameterKeys(parameterNames);
|
||||
final List<String> results = new ArrayList<String>();
|
||||
jdbcTemplate.query("SELECT NAME from T_FOOS where ID > ? and ID < ?", pss, new RowCallbackHandler(){
|
||||
|
||||
public void processRow(ResultSet rs) throws SQLException {
|
||||
results.add(rs.getString(1));
|
||||
}});
|
||||
simpleJdbcTemplate.getJdbcOperations().query(
|
||||
"SELECT NAME from T_FOOS where ID > ? and ID < ?",
|
||||
pss,
|
||||
new RowCallbackHandler(){
|
||||
public void processRow(ResultSet rs) throws SQLException {
|
||||
results.add(rs.getString(1));
|
||||
}});
|
||||
|
||||
assertEquals(2, results.size());
|
||||
assertEquals("bar2", results.get(0));
|
||||
assertEquals("bar3", results.get(1));
|
||||
}
|
||||
|
||||
@Transactional @Test
|
||||
public void testAfterPropertiesSet() throws Exception{
|
||||
try{
|
||||
pss.afterPropertiesSet();
|
||||
@@ -83,6 +101,7 @@ public class StepExecutionPreparedStatementSetterTests extends AbstractTransacti
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional @Test
|
||||
public void testNonExistentProperties(){
|
||||
|
||||
List<String> parameterNames = new ArrayList<String>();
|
||||
@@ -91,11 +110,13 @@ public class StepExecutionPreparedStatementSetterTests extends AbstractTransacti
|
||||
pss.setParameterKeys(parameterNames);
|
||||
|
||||
try{
|
||||
jdbcTemplate.query("SELECT NAME from T_FOOS where ID > ? and ID < ?", pss, new RowCallbackHandler(){
|
||||
|
||||
public void processRow(ResultSet rs) throws SQLException {
|
||||
fail();
|
||||
}});
|
||||
simpleJdbcTemplate.getJdbcOperations().query(
|
||||
"SELECT NAME from T_FOOS where ID > ? and ID < ?",
|
||||
pss,
|
||||
new RowCallbackHandler(){
|
||||
public void processRow(ResultSet rs) throws SQLException {
|
||||
fail();
|
||||
}});
|
||||
|
||||
fail();
|
||||
}catch(IllegalStateException ex){
|
||||
|
||||
@@ -15,9 +15,9 @@
|
||||
*/
|
||||
package org.springframework.batch.core.step.item;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@@ -28,7 +28,6 @@ import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.job.JobSupport;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.repository.dao.AbstractJobDaoTests;
|
||||
import org.springframework.batch.core.repository.dao.MapJobExecutionDao;
|
||||
import org.springframework.batch.core.repository.dao.MapJobInstanceDao;
|
||||
import org.springframework.batch.core.repository.dao.MapStepExecutionDao;
|
||||
@@ -39,51 +38,42 @@ import org.springframework.batch.item.support.AbstractItemWriter;
|
||||
import org.springframework.batch.item.support.ListItemReader;
|
||||
import org.springframework.batch.repeat.policy.SimpleCompletionPolicy;
|
||||
import org.springframework.batch.repeat.support.RepeatTemplate;
|
||||
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationAdapter;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class ItemOrientedStepIntegrationTests extends AbstractDependencyInjectionSpringContextTests {
|
||||
|
||||
private List<String> processed = new ArrayList<String>();
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = "/org/springframework/batch/core/repository/dao/sql-dao-test.xml")
|
||||
public class ItemOrientedStepIntegrationTests {
|
||||
|
||||
private ItemOrientedStep step;
|
||||
|
||||
private Job job;
|
||||
|
||||
@Autowired
|
||||
private PlatformTransactionManager transactionManager;
|
||||
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
|
||||
private JobRepository jobRepository;
|
||||
|
||||
/**
|
||||
* Public setter for the PlatformTransactionManager.
|
||||
* @param transactionManager the transactionManager to set
|
||||
*/
|
||||
public void setTransactionManager(PlatformTransactionManager transactionManager) {
|
||||
this.transactionManager = transactionManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public setter for the DataSource.
|
||||
* @param dataSource the dataSource to set
|
||||
*/
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
private ItemReader<String> getReader(String[] args) {
|
||||
return new ListItemReader<String>(Arrays.asList(args));
|
||||
}
|
||||
|
||||
protected void onSetUp() throws Exception {
|
||||
@Before
|
||||
public void onSetUp() throws Exception {
|
||||
MapJobInstanceDao.clear();
|
||||
MapStepExecutionDao.clear();
|
||||
MapJobExecutionDao.clear();
|
||||
@@ -114,19 +104,11 @@ public class ItemOrientedStepIntegrationTests extends AbstractDependencyInjectio
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.test.AbstractSingleSpringContextTests#getConfigLocations()
|
||||
*/
|
||||
protected String[] getConfigLocations() {
|
||||
return new String[] { ClassUtils.addResourcePathToPackagePath(AbstractJobDaoTests.class, "sql-dao-test.xml") };
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStatusForCommitFailedException() throws Exception {
|
||||
|
||||
step.setItemHandler(new SimpleItemHandler<String>(getReader(new String[] { "a", "b", "c" }), new AbstractItemWriter<String>() {
|
||||
public void write(String data) throws Exception {
|
||||
processed.add((String) data);
|
||||
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
|
||||
public void beforeCommit(boolean readOnly) {
|
||||
throw new RuntimeException("Simulate commit failure");
|
||||
|
||||
Reference in New Issue
Block a user