RESOLVED - BATCH-396: jdbc and map daos should share tests

This commit is contained in:
robokaso
2008-04-10 14:07:52 +00:00
parent 3e787fb680
commit c7d2a7e8bf
14 changed files with 472 additions and 494 deletions

View File

@@ -286,7 +286,7 @@ public abstract class AbstractJobDaoTests extends AbstractTransactionalDataSourc
public void testCreationAddsVersion() {
jobInstance = jobInstanceDao.createJobInstance(new JobSupport("testVersion"), new JobParameters());
jobInstance = jobInstanceDao.createJobInstance(new JobSupport("testCreationAddsVersion"), new JobParameters());
assertNotNull(jobInstance.getVersion());
}

View File

@@ -0,0 +1,100 @@
package org.springframework.batch.core.repository.dao;
import java.util.Date;
import java.util.List;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.job.JobSupport;
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
public abstract class AbstractJobExecutionDaoTests extends AbstractTransactionalDataSourceSpringContextTests {
JobExecutionDao dao;
JobInstance jobInstance = new JobInstance(new Long(1), new JobParameters(), new JobSupport("execTestJob"));
JobExecution execution = new JobExecution(jobInstance);
/**
* @return tested object ready for use
*/
protected abstract JobExecutionDao getJobExecutionDao();
protected void onSetUp() throws Exception {
dao = getJobExecutionDao();
}
/**
* Save and find a job execution.
*/
public void testSaveAndFind() {
dao.saveJobExecution(execution);
List executions = dao.findJobExecutions(jobInstance);
assertTrue(executions.size() == 1);
assertEquals(execution, executions.get(0));
}
/**
* Saving sets id to the entity.
*/
public void testSaveAddsIdAndVersion() {
assertNull(execution.getId());
assertNull(execution.getVersion());
dao.saveJobExecution(execution);
assertNotNull(execution.getId());
assertNotNull(execution.getVersion());
}
/**
* Execution count increases by one with every save for the same job
* instance.
*/
public void testGetExecutionCount() {
JobExecution exec1 = new JobExecution(jobInstance);
JobExecution exec2 = new JobExecution(jobInstance);
dao.saveJobExecution(exec1);
assertEquals(1, dao.getJobExecutionCount(jobInstance));
dao.saveJobExecution(exec2);
assertEquals(2, dao.getJobExecutionCount(jobInstance));
}
/**
* Update and retrieve job execution - check attributes have changed as
* expected.
*/
public void testUpdateExecution() {
execution.setStatus(BatchStatus.STARTED);
dao.saveJobExecution(execution);
execution.setStatus(BatchStatus.COMPLETED);
dao.updateJobExecution(execution);
JobExecution updated = (JobExecution) dao.findJobExecutions(jobInstance).get(0);
assertEquals(execution, updated);
assertEquals(BatchStatus.COMPLETED, updated.getStatus());
}
/**
* Check the execution with most recent start time is returned
*/
public void testGetLastExecution() {
JobExecution exec1 = new JobExecution(jobInstance);
exec1.setStartTime(new Date(0));
JobExecution exec2 = new JobExecution(jobInstance);
exec2.setStartTime(new Date(1));
dao.saveJobExecution(exec1);
dao.saveJobExecution(exec2);
assertEquals(exec2, dao.getLastJobExecution(jobInstance));
}
}

View File

@@ -0,0 +1,64 @@
package org.springframework.batch.core.repository.dao;
import org.springframework.batch.core.Job;
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;
public abstract class AbstractJobInstanceDaoTests extends AbstractTransactionalDataSourceSpringContextTests {
private JobInstanceDao dao = new MapJobInstanceDao();
private Job fooJob = new JobSupport("foo");
private JobParameters fooParams = new JobParametersBuilder().addString("fooKey", "fooValue").toJobParameters();
protected abstract JobInstanceDao getJobInstanceDao();
protected void onSetUp() throws Exception {
dao = getJobInstanceDao();
}
/**
* Create and retrieve a job instance.
*/
public void testCreateAndRetrieve() throws Exception {
JobInstance fooInstance = dao.createJobInstance(fooJob, fooParams);
assertNotNull(fooInstance.getId());
assertEquals(fooJob, fooInstance.getJob());
assertEquals(fooParams, fooInstance.getJobParameters());
JobInstance retrievedInstance = dao.getJobInstance(fooJob, fooParams);
assertEquals(fooInstance, retrievedInstance);
}
/**
* Trying to create instance twice for the same job+parameters causes error
*/
public void testCreateDuplicateInstance() {
dao.createJobInstance(fooJob, fooParams);
try {
dao.createJobInstance(fooJob, fooParams);
fail();
}
catch (IllegalStateException e) {
// expected
}
}
public void testCreationAddsVersion() {
JobInstance jobInstance = new JobInstance(new Long(1), new JobParameters(), new JobSupport("testVersionAndId"));
assertNull(jobInstance.getVersion());
jobInstance = dao.createJobInstance(new JobSupport("testVersion"), new JobParameters());
assertNotNull(jobInstance.getVersion());
}
}

View File

@@ -0,0 +1,205 @@
/*
* Copyright 2006-2007 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.core.repository.dao;
import java.util.HashMap;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.Step;
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.step.StepSupport;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
/**
* Tests for {@link StepExecutionDao} implementations.
*
* @see #getStepExecutionDao()
*/
public abstract class AbstractStepExecutionDaoTests extends AbstractTransactionalDataSourceSpringContextTests {
private StepExecutionDao dao;
private JobInstance jobInstance;
private JobExecution jobExecution;
private Step step;
private StepExecution stepExecution;
private JobRepository repository;
/**
* @return {@link StepExecutionDao} implementation ready for use.
*/
protected abstract StepExecutionDao getStepExecutionDao();
/**
* @return {@link JobRepository} that uses the stepExecution dao.
*/
protected abstract JobRepository getJobRepository();
protected void onSetUp() throws Exception {
repository = getJobRepository();
jobExecution = repository.createJobExecution(new JobSupport("testJob"), new JobParameters());
jobInstance = jobExecution.getJobInstance();
step = new StepSupport("foo");
stepExecution = new StepExecution(step, jobExecution);
dao = getStepExecutionDao();
}
public void testSaveExecutionAssignsIdAndVersion() throws Exception {
assertNull(stepExecution.getId());
assertNull(stepExecution.getVersion());
dao.saveStepExecution(stepExecution);
assertNotNull(stepExecution.getId());
assertNotNull(stepExecution.getVersion());
}
public void testSaveAndFindExecution() {
stepExecution.setStatus(BatchStatus.STARTED);
dao.saveStepExecution(stepExecution);
StepExecution retrieved = dao.getStepExecution(jobExecution, step);
assertEquals(stepExecution, retrieved);
assertEquals(BatchStatus.STARTED, retrieved.getStatus());
assertNull(dao.getStepExecution(jobExecution, new StepSupport("not-existing step")));
}
public void testGetForNotExistingJobExecution() {
assertNull(dao.getStepExecution(new JobExecution(jobInstance, new Long(777)), step));
}
/**
* To-be-saved execution must not already have an id.
*/
public void testSaveExecutionWithIdAlreadySet() {
stepExecution.setId(new Long(7));
try {
dao.saveStepExecution(stepExecution);
fail();
}
catch (IllegalArgumentException e) {
// expected
}
}
/**
* To-be-saved execution must not already have a version.
*/
public void testSaveExecutionWithVersionAlreadySet() {
stepExecution.incrementVersion();
try {
dao.saveStepExecution(stepExecution);
fail();
}
catch (IllegalArgumentException e) {
// expected
}
}
/**
* Update and retrieve updated StepExecution - make sure the update is
* reflected as expected and version number has been incremented
*/
public void testUpdateExecution() {
stepExecution.setStatus(BatchStatus.STARTED);
dao.saveStepExecution(stepExecution);
Integer versionAfterSave = stepExecution.getVersion();
stepExecution.setStatus(BatchStatus.STOPPED);
dao.updateStepExecution(stepExecution);
assertEquals(versionAfterSave.intValue() + 1, stepExecution.getVersion().intValue());
StepExecution retrieved = dao.getStepExecution(jobExecution, step);
assertEquals(stepExecution, retrieved);
assertEquals(BatchStatus.STOPPED, retrieved.getStatus());
}
public void testSaveAndFindContext() {
dao.saveStepExecution(stepExecution);
ExecutionContext ctx = new ExecutionContext(new HashMap() {
{
put("key", "value");
}
});
stepExecution.setExecutionContext(ctx);
dao.saveOrUpdateExecutionContext(stepExecution);
ExecutionContext retrieved = dao.findExecutionContext(stepExecution);
assertEquals(ctx, retrieved);
}
public void testUpdateContext() {
dao.saveStepExecution(stepExecution);
ExecutionContext ctx = new ExecutionContext(new HashMap() {
{
put("key", "value");
}
});
stepExecution.setExecutionContext(ctx);
dao.saveOrUpdateExecutionContext(stepExecution);
ctx.putLong("longKey", 7);
dao.saveOrUpdateExecutionContext(stepExecution);
ExecutionContext retrieved = dao.findExecutionContext(stepExecution);
assertEquals(ctx, retrieved);
assertEquals(7, retrieved.getLong("longKey"));
}
/**
* Exception should be raised when the version of update argument doesn't
* match the version of persisted entity.
*/
public void testConcurrentModificationException() {
step = new StepSupport("foo");
StepExecution exec1 = new StepExecution(step, jobExecution);
dao.saveStepExecution(exec1);
StepExecution exec2 = new StepExecution(step, jobExecution);
exec2.setId(exec1.getId());
exec2.incrementVersion();
assertEquals(new Integer(0), exec1.getVersion());
assertEquals(exec1.getVersion(), exec2.getVersion());
dao.updateStepExecution(exec1);
assertEquals(new Integer(1), exec1.getVersion());
try {
dao.updateStepExecution(exec2);
fail();
}
catch (OptimisticLockingFailureException e) {
// expected
}
}
}

View File

@@ -0,0 +1,18 @@
package org.springframework.batch.core.repository.dao;
import junit.framework.Test;
import junit.framework.TestSuite;
public class AllTests {
public static Test suite() {
TestSuite suite = new TestSuite("Test for org.springframework.batch.core.repository.dao");
//$JUnit-BEGIN$
suite.addTestSuite(JdbcJobInstanceDaoTests.class);
suite.addTestSuite(JdbcStepExecutionDaoTests.class);
suite.addTestSuite(JdbcJobExecutionDaoTests.class);
//$JUnit-END$
return suite;
}
}

View File

@@ -0,0 +1,20 @@
package org.springframework.batch.core.repository.dao;
public class JdbcJobExecutionDaoTests extends AbstractJobExecutionDaoTests {
protected JobExecutionDao getJobExecutionDao() {
deleteFromTables(new String[] { "BATCH_STEP_EXECUTION_CONTEXT", "BATCH_STEP_EXECUTION", "BATCH_JOB_EXECUTION",
"BATCH_JOB_PARAMS", "BATCH_JOB_INSTANCE" });
// job instance needs to exist before job execution can be created
getJdbcTemplate()
.execute(
"insert into BATCH_JOB_INSTANCE (JOB_INSTANCE_ID, JOB_NAME, JOB_KEY, VERSION) values (1,'execTestJob', '', 0)");
return (JobExecutionDao) getApplicationContext().getBean("jobExecutionDao");
}
protected String[] getConfigLocations() {
return new String[] { "sql-dao-test.xml" };
}
}

View File

@@ -0,0 +1,16 @@
package org.springframework.batch.core.repository.dao;
public class JdbcJobInstanceDaoTests extends AbstractJobInstanceDaoTests {
protected JobInstanceDao getJobInstanceDao() {
deleteFromTables(new String[] { "BATCH_STEP_EXECUTION_CONTEXT", "BATCH_STEP_EXECUTION", "BATCH_JOB_EXECUTION",
"BATCH_JOB_PARAMS", "BATCH_JOB_INSTANCE" });
return (JobInstanceDao) getApplicationContext().getBean("jobInstanceDao");
}
protected String[] getConfigLocations() {
return new String[] { "sql-dao-test.xml" };
}
}

View File

@@ -1,112 +0,0 @@
package org.springframework.batch.core.repository.dao;
import java.util.List;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.job.JobSupport;
import org.springframework.batch.core.repository.dao.JdbcStepExecutionDao;
import org.springframework.batch.core.step.StepSupport;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
/**
* Unit Test of SqlStepDao that only tests prefix matching. A
* separate test is needed because all other tests hit hsql,
* while this test needs to mock JdbcTemplate to analyze the
* Sql passed in.
*
* @author Lucas Ward
*
*/
public class JdbcStepDaoPrefixTests extends TestCase {
private JdbcStepExecutionDao stepExecutionDao;
MockJdbcTemplate jdbcTemplate = new MockJdbcTemplate();
JobInstance job = new JobInstance(new Long(1), new JobParameters(), new JobSupport("testJob"));
Step step = new StepSupport("foo");
StepExecution stepExecution = new StepExecution(step, new JobExecution(job), null);
MockControl stepExecutionIncrementerControl = MockControl.createControl(DataFieldMaxValueIncrementer.class);
DataFieldMaxValueIncrementer stepExecutionIncrementer;
MockControl stepIncrementerControl = MockControl.createControl(DataFieldMaxValueIncrementer.class);
DataFieldMaxValueIncrementer stepIncrementer;
protected void setUp() throws Exception {
super.setUp();
stepExecutionDao = new JdbcStepExecutionDao();
stepExecutionIncrementer = (DataFieldMaxValueIncrementer)stepExecutionIncrementerControl.getMock();
stepIncrementer = (DataFieldMaxValueIncrementer)stepIncrementerControl.getMock();
stepExecutionDao.setJdbcTemplate(jdbcTemplate);
stepExecutionDao.setStepExecutionIncrementer(stepExecutionIncrementer);
stepExecution.setId(new Long(1));
stepExecution.incrementVersion();
}
public void testModifiedUpdateStepExecution(){
stepExecutionDao.setTablePrefix("FOO_");
stepExecutionDao.updateStepExecution(stepExecution);
assertTrue(jdbcTemplate.getSqlStatement().indexOf("FOO_STEP_EXECUTION") != -1);
}
public void testModifiedSaveStepExecution(){
stepExecutionDao.setTablePrefix("FOO_");
stepExecutionIncrementer.nextLongValue();
stepExecutionIncrementerControl.setReturnValue(1);
stepExecutionIncrementerControl.replay();
stepExecutionDao.saveStepExecution(stepExecution);
assertTrue(jdbcTemplate.getSqlStatement().indexOf("FOO_STEP_EXECUTION") != -1);
}
public void testDefaultSaveStepExecution(){
stepExecutionIncrementer.nextLongValue();
stepExecutionIncrementerControl.setReturnValue(1);
stepExecutionIncrementerControl.replay();
stepExecutionDao.saveStepExecution(stepExecution);
assertTrue(jdbcTemplate.getSqlStatement().indexOf("BATCH_STEP_EXECUTION") != -1);
}
public void testDefaultUpdateStepExecution(){
stepExecutionDao.updateStepExecution(stepExecution);
assertTrue(jdbcTemplate.getSqlStatement().indexOf("BATCH_STEP_EXECUTION") != -1);
}
private class MockJdbcTemplate extends JdbcTemplate {
private String sql;
public int update(String sql, Object[] args) throws DataAccessException {
this.sql = sql;
return 1;
}
public int update(String sql, Object[] args, int[] argTypes) throws DataAccessException {
this.sql = sql;
return 1;
}
public List query(String sql, Object[] args, RowMapper rowMapper) throws DataAccessException {
this.sql = sql;
return null;
}
public String getSqlStatement() {
return sql;
}
}
}

View File

@@ -1,43 +0,0 @@
package org.springframework.batch.core.repository.dao;
import java.util.List;
import java.util.Map;
import org.springframework.batch.core.repository.dao.AbstractJdbcBatchMetadataDao;
import org.springframework.batch.core.repository.dao.JdbcStepExecutionDao;
import org.springframework.batch.repeat.ExitStatus;
public class JdbcStepDaoTests extends AbstractStepDaoTests {
private static final String LONG_STRING = JdbcJobDaoTests.LONG_STRING;
protected void onSetUpBeforeTransaction() throws Exception {
((JdbcStepExecutionDao) stepExecutionDao).setTablePrefix(AbstractJdbcBatchMetadataDao.DEFAULT_TABLE_PREFIX);
}
public void testTablePrefix() throws Exception {
// ((JdbcStepInstanceDao) stepInstanceDao).setTablePrefix("FOO_");
// ((JdbcStepExecutionDao) stepExecutionDao).setTablePrefix("FOO_");
// try {
// testCreateStep();
// fail("Expected DataAccessException");
// } catch (DataAccessException e) {
// // expected
// }
}
public void testUpdateStepExecutionWithLongExitCode() {
assertTrue(LONG_STRING.length()>250);
stepExecution.setExitStatus(ExitStatus.FINISHED.addExitDescription(LONG_STRING));
stepExecutionDao.updateStepExecution(stepExecution);
List executions = jdbcTemplate.queryForList(
"SELECT * FROM BATCH_STEP_EXECUTION where STEP_NAME=?",
new Object[] { step1.getName() });
assertEquals(1, executions.size());
assertEquals(LONG_STRING.substring(0, 250), ((Map) executions.get(0))
.get("EXIT_MESSAGE"));
}
}

View File

@@ -0,0 +1,21 @@
package org.springframework.batch.core.repository.dao;
import org.springframework.batch.core.repository.JobRepository;
public class JdbcStepExecutionDaoTests extends AbstractStepExecutionDaoTests {
protected StepExecutionDao getStepExecutionDao() {
return (StepExecutionDao) getApplicationContext().getBean("stepExecutionDao");
}
protected JobRepository getJobRepository() {
deleteFromTables(new String[] { "BATCH_STEP_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[] { "sql-dao-test.xml" };
}
}

View File

@@ -1,98 +1,12 @@
package org.springframework.batch.core.repository.dao;
import java.util.Date;
import java.util.List;
import junit.framework.TestCase;
public class MapJobExecutionDaoTests extends AbstractJobExecutionDaoTests {
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.job.JobSupport;
import org.springframework.batch.core.repository.dao.JobExecutionDao;
import org.springframework.batch.core.repository.dao.MapJobExecutionDao;
public class MapJobExecutionDaoTests extends TestCase {
JobExecutionDao dao = new MapJobExecutionDao();
JobInstance jobInstance = new JobInstance(new Long(1), new JobParameters(), new JobSupport("execTestJob"));
JobExecution execution = new JobExecution(jobInstance);
protected void setUp() throws Exception {
protected JobExecutionDao getJobExecutionDao() {
MapJobExecutionDao.clear();
MapJobInstanceDao.clear();
return new MapJobExecutionDao();
}
/**
* Save and find a job execution.
*/
public void testSaveAndFind() {
dao.saveJobExecution(execution);
List executions = dao.findJobExecutions(jobInstance);
assertTrue(executions.size() == 1);
assertEquals(execution, executions.get(0));
}
/**
* Saving sets id to the entity.
*/
public void testSaveAddsIdAndVersion() {
assertNull(execution.getId());
assertNull(execution.getVersion());
dao.saveJobExecution(execution);
assertNotNull(execution.getId());
assertNotNull(execution.getVersion());
}
/**
* Execution count increases by one with every save for the same job
* instance.
*/
public void testGetExecutionCount() {
JobExecution exec1 = new JobExecution(jobInstance);
JobExecution exec2 = new JobExecution(jobInstance);
dao.saveJobExecution(exec1);
assertEquals(1, dao.getJobExecutionCount(jobInstance));
dao.saveJobExecution(exec2);
assertEquals(2, dao.getJobExecutionCount(jobInstance));
}
/**
* Update and retrieve job execution - check attributes have changed as
* expected.
*/
public void testUpdateExecution() {
execution.setStatus(BatchStatus.STARTED);
dao.saveJobExecution(execution);
execution.setStatus(BatchStatus.COMPLETED);
dao.updateJobExecution(execution);
JobExecution updated = (JobExecution) dao.findJobExecutions(jobInstance).get(0);
assertEquals(execution, updated);
assertEquals(BatchStatus.COMPLETED, updated.getStatus());
}
/**
* Check the execution with most recent start time is returned
*/
public void testGetLastExecution() {
JobExecution exec1 = new JobExecution(jobInstance);
exec1.setStartTime(new Date(0));
JobExecution exec2 = new JobExecution(jobInstance);
exec2.setStartTime(new Date(1));
dao.saveJobExecution(exec1);
dao.saveJobExecution(exec2);
assertEquals(exec2, dao.getLastJobExecution(jobInstance));
}
}

View File

@@ -1,69 +1,10 @@
package org.springframework.batch.core.repository.dao;
import junit.framework.TestCase;
public class MapJobInstanceDaoTests extends AbstractJobInstanceDaoTests {
import org.springframework.batch.core.Job;
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.batch.core.repository.dao.JobInstanceDao;
import org.springframework.batch.core.repository.dao.MapJobInstanceDao;
public class MapJobInstanceDaoTests extends TestCase {
private JobInstanceDao dao = new MapJobInstanceDao();
private Job fooJob = new JobSupport("foo");
private JobParameters fooParams = new JobParametersBuilder().addString("fooKey", "fooValue").toJobParameters();
protected void setUp() throws Exception {
protected JobInstanceDao getJobInstanceDao() {
MapJobInstanceDao.clear();
return new MapJobInstanceDao();
}
protected void tearDown() throws Exception {
MapJobInstanceDao.clear();
}
/**
* Create and retrieve a job instance.
*/
public void testCreateAndRetrieve() throws Exception {
JobInstance fooInstance = dao.createJobInstance(fooJob, fooParams);
assertNotNull(fooInstance.getId());
assertEquals(fooJob, fooInstance.getJob());
assertEquals(fooParams, fooInstance.getJobParameters());
JobInstance retrievedInstance = dao.getJobInstance(fooJob, fooParams);
assertEquals(fooInstance, retrievedInstance);
}
/**
* Trying to create instance twice for the same job+parameters causes error
*/
public void testCreateDuplicateInstance() {
dao.createJobInstance(fooJob, fooParams);
try {
dao.createJobInstance(fooJob, fooParams);
fail();
}
catch (IllegalStateException e) {
// expected
}
}
public void testCreationAddsVersion() {
JobInstance jobInstance = new JobInstance(new Long(1), new JobParameters(), new JobSupport("testVersionAndId"));
assertNull(jobInstance.getVersion());
jobInstance = dao.createJobInstance(new JobSupport("testVersion"), new JobParameters());
assertNotNull(jobInstance.getVersion());
}
}

View File

@@ -1,190 +1,19 @@
/*
* Copyright 2006-2007 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.core.repository.dao;
import java.util.HashMap;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.SimpleJobRepository;
import junit.framework.TestCase;
public class MapStepExecutionDaoTests extends AbstractStepExecutionDaoTests {
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.job.JobSupport;
import org.springframework.batch.core.repository.dao.MapStepExecutionDao;
import org.springframework.batch.core.repository.dao.StepExecutionDao;
import org.springframework.batch.core.step.StepSupport;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.dao.OptimisticLockingFailureException;
protected StepExecutionDao getStepExecutionDao() {
return new MapStepExecutionDao();
}
/**
* Tests for {@link MapStepExecutionDao}.
*/
public class MapStepExecutionDaoTests extends TestCase {
private StepExecutionDao dao = new MapStepExecutionDao();
private JobInstance jobInstance;
private JobExecution jobExecution;
private Step step;
private StepExecution stepExecution;
protected void setUp() throws Exception {
protected JobRepository getJobRepository() {
MapJobInstanceDao.clear();
MapJobExecutionDao.clear();
MapStepExecutionDao.clear();
jobInstance = new JobInstance(new Long(1), new JobParameters(), new JobSupport("testJob"));
jobExecution = new JobExecution(jobInstance, new Long(1));
step = new StepSupport("foo");
stepExecution = new StepExecution(step, jobExecution);
}
public void testSaveExecutionAssignsIdAndVersion() throws Exception {
assertNull(stepExecution.getId());
assertNull(stepExecution.getVersion());
dao.saveStepExecution(stepExecution);
assertNotNull(stepExecution.getId());
assertNotNull(stepExecution.getVersion());
}
public void testSaveAndFindExecution() {
stepExecution.setStatus(BatchStatus.STARTED);
dao.saveStepExecution(stepExecution);
StepExecution retrieved = dao.getStepExecution(jobExecution, step);
assertEquals(stepExecution, retrieved);
assertEquals(BatchStatus.STARTED, retrieved.getStatus());
assertNull(dao.getStepExecution(jobExecution, new StepSupport("not-existing step")));
}
public void testGetForNotExistingJobExecution() {
assertNull(dao.getStepExecution(new JobExecution(jobInstance, new Long(777)), step));
}
/**
* To-be-saved execution must not already have an id.
*/
public void testSaveExecutionWithIdAlreadySet() {
stepExecution.setId(new Long(7));
try {
dao.saveStepExecution(stepExecution);
fail();
}
catch (IllegalArgumentException e) {
// expected
}
}
/**
* To-be-saved execution must not already have a version.
*/
public void testSaveExecutionWithVersionAlreadySet() {
stepExecution.incrementVersion();
try {
dao.saveStepExecution(stepExecution);
fail();
}
catch (IllegalArgumentException e) {
// expected
}
}
/**
* Update and retrieve updated StepExecution - make sure the update is
* reflected as expected and version number has been incremented
*/
public void testUpdateExecution() {
stepExecution.setStatus(BatchStatus.STARTED);
dao.saveStepExecution(stepExecution);
Integer versionAfterSave = stepExecution.getVersion();
stepExecution.setStatus(BatchStatus.STOPPED);
dao.updateStepExecution(stepExecution);
assertEquals(versionAfterSave.intValue() + 1, stepExecution.getVersion().intValue());
StepExecution retrieved = dao.getStepExecution(jobExecution, step);
assertEquals(stepExecution, retrieved);
assertEquals(BatchStatus.STOPPED, retrieved.getStatus());
}
public void testSaveAndFindContext() {
ExecutionContext ctx = new ExecutionContext(new HashMap() {
{
put("key", "value");
}
});
stepExecution.setExecutionContext(ctx);
dao.saveOrUpdateExecutionContext(stepExecution);
ExecutionContext retrieved = dao.findExecutionContext(stepExecution);
assertEquals(ctx, retrieved);
}
public void testUpdateContext() {
ExecutionContext ctx = new ExecutionContext(new HashMap() {
{
put("key", "value");
}
});
stepExecution.setExecutionContext(ctx);
dao.saveOrUpdateExecutionContext(stepExecution);
ctx.putLong("longKey", 7);
dao.saveOrUpdateExecutionContext(stepExecution);
ExecutionContext retrieved = dao.findExecutionContext(stepExecution);
assertEquals(ctx, retrieved);
assertEquals(7, retrieved.getLong("longKey"));
}
/**
* Exception should be raised when the version of update argument doesn't
* match the version of persisted entity.
*/
public void testConcurrentModificationException() {
jobInstance = new JobInstance(new Long(1), new JobParameters(), new JobSupport("testJob"));
jobExecution = new JobExecution(jobInstance, new Long(1));
step = new StepSupport("foo");
StepExecution exec1 = new StepExecution(step, jobExecution);
dao.saveStepExecution(exec1);
StepExecution exec2 = new StepExecution(step, jobExecution);
exec2.setId(exec1.getId());
exec2.incrementVersion();
assertEquals(new Integer(0), exec1.getVersion());
assertEquals(exec1.getVersion(), exec2.getVersion());
dao.updateStepExecution(exec1);
assertEquals(new Integer(1), exec1.getVersion());
try {
dao.updateStepExecution(exec2);
fail();
}
catch (OptimisticLockingFailureException e) {
// expected
}
return new SimpleJobRepository(new MapJobInstanceDao(), new MapJobExecutionDao(), new MapStepExecutionDao());
}
}