RESOLVED - issue BATCH-987: Create JobRepositoryTestUtils

This commit is contained in:
dsyer
2009-01-05 11:49:14 +00:00
parent 3f85b58dbb
commit f8d239e18d
4 changed files with 637 additions and 0 deletions

View File

@@ -0,0 +1,114 @@
/*
* 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.test;
import static org.junit.Assert.assertEquals;
import java.util.List;
import javax.sql.DataSource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersIncrementer;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.jdbc.SimpleJdbcTestUtils;
/**
* @author Dave Syer
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/simple-job-launcher-context.xml")
public class JobRepositoryTestUtilsTests {
private JobRepositoryTestUtils utils;
@Autowired
private JobRepository jobRepository;
@Autowired
private DataSource dataSource;
private SimpleJdbcTemplate jdbcTemplate;
private int before;
@Before
public void init() {
jdbcTemplate = new SimpleJdbcTemplate(dataSource);
before = SimpleJdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_JOB_EXECUTION");
}
@Test(expected=IllegalArgumentException.class)
public void testMandatoryProperties() throws Exception {
utils = new JobRepositoryTestUtils();
utils.afterPropertiesSet();
}
@Test(expected=IllegalArgumentException.class)
public void testMandatoryDataSource() throws Exception {
utils = new JobRepositoryTestUtils();
utils.setJobRepository(jobRepository);
utils.afterPropertiesSet();
}
@Test
public void testCreateJobExecutions() throws Exception {
utils = new JobRepositoryTestUtils(jobRepository, dataSource);
List<JobExecution> list = utils.createJobExecutions(3);
assertEquals(3, list.size());
assertEquals(before + 3, SimpleJdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_JOB_EXECUTION"));
utils.removeJobExecutions(list);
assertEquals(before, SimpleJdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_JOB_EXECUTION"));
}
@Test
public void testRemoveJobExecutionsIncrementally() throws Exception {
utils = new JobRepositoryTestUtils(jobRepository, dataSource);
List<JobExecution> list1 = utils.createJobExecutions(3);
List<JobExecution> list2 = utils.createJobExecutions(2);
assertEquals(before + 5, SimpleJdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_JOB_EXECUTION"));
utils.removeJobExecutions(list2);
assertEquals(before + 3, SimpleJdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_JOB_EXECUTION"));
utils.removeJobExecutions(list1);
assertEquals(before, SimpleJdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_JOB_EXECUTION"));
}
@Test
public void testCreateJobExecutionsWithIncrementer() throws Exception {
utils = new JobRepositoryTestUtils(jobRepository, dataSource);
utils.setJobParametersIncrementer(new JobParametersIncrementer() {
public JobParameters getNext(JobParameters parameters) {
return new JobParametersBuilder().addString("foo","bar").toJobParameters();
}
});
List<JobExecution> list = utils.createJobExecutions(1);
assertEquals(1, list.size());
assertEquals("bar", list.get(0).getJobInstance().getJobParameters().getString("foo"));
utils.removeJobExecutions(list);
assertEquals(before, SimpleJdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_JOB_EXECUTION"));
}
}

View File

@@ -0,0 +1,168 @@
/*
* 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.test;
import static org.junit.Assert.assertNotNull;
import java.util.Arrays;
import org.junit.Test;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.converter.DefaultJobParametersConverter;
import org.springframework.batch.support.PropertiesConverter;
/**
* @author Dave Syer
*
*/
public class MetaDataInstanceFactoryTests {
private String jobName = "JOB";
private Long instanceId = 321L;
private String jobParametersString = "foo=bar";
private JobParameters jobParameters = new DefaultJobParametersConverter().getJobParameters(PropertiesConverter
.stringToProperties(jobParametersString));
private Long executionId = 4321L;
private String stepName = "step";
private Long stepExecutionId = 11L;
/**
* Test method for
* {@link MetaDataInstanceFactory#createJobInstance(String, Long, String)} .
*/
@Test
public void testCreateJobInstanceStringLongString() {
assertNotNull(MetaDataInstanceFactory.createJobInstance(jobName, instanceId, jobParametersString));
}
/**
* Test method for
* {@link MetaDataInstanceFactory#createJobInstance(String, Long, JobParameters)}
* .
*/
@Test
public void testCreateJobInstanceStringLongJobParameters() {
assertNotNull(MetaDataInstanceFactory.createJobInstance(jobName, instanceId, jobParameters));
}
/**
* Test method for
* {@link MetaDataInstanceFactory#createJobInstance(String, Long)} .
*/
@Test
public void testCreateJobInstanceStringLong() {
assertNotNull(MetaDataInstanceFactory.createJobInstance(jobName, instanceId));
}
/**
* Test method for {@link MetaDataInstanceFactory#createJobInstance()} .
*/
@Test
public void testCreateJobInstance() {
assertNotNull(MetaDataInstanceFactory.createJobInstance());
}
/**
* Test method for {@link MetaDataInstanceFactory#createJobExecution()} .
*/
@Test
public void testCreateJobExecution() {
assertNotNull(MetaDataInstanceFactory.createJobExecution());
}
/**
* Test method for {@link MetaDataInstanceFactory#createJobExecution(Long)}
* .
*/
@Test
public void testCreateJobExecutionLong() {
assertNotNull(MetaDataInstanceFactory.createJobExecution(instanceId));
}
/**
* Test method for
* {@link MetaDataInstanceFactory#createJobExecution(String, Long, Long)} .
*/
@Test
public void testCreateJobExecutionStringLongLong() {
assertNotNull(MetaDataInstanceFactory.createJobExecution(jobName, instanceId, executionId));
}
/**
* Test method for
* {@link MetaDataInstanceFactory#createJobExecution(String, Long, Long, String)}
* .
*/
@Test
public void testCreateJobExecutionStringLongLongString() {
assertNotNull(MetaDataInstanceFactory.createJobExecution(jobName, instanceId, executionId, jobParametersString));
}
/**
* Test method for
* {@link MetaDataInstanceFactory#createJobExecution(String, Long, Long, JobParameters)}
* .
*/
@Test
public void testCreateJobExecutionStringLongLongJobParameters() {
assertNotNull(MetaDataInstanceFactory.createJobExecution(jobName, instanceId, executionId, jobParameters));
}
/**
* Test method for {@link MetaDataInstanceFactory#createStepExecution()} .
*/
@Test
public void testCreateStepExecution() {
assertNotNull(MetaDataInstanceFactory.createStepExecution());
}
/**
* Test method for
* {@link MetaDataInstanceFactory#createStepExecution(String, Long)} .
*/
@Test
public void testCreateStepExecutionStringLong() {
assertNotNull(MetaDataInstanceFactory.createStepExecution(stepName, stepExecutionId));
}
/**
* Test method for
* {@link MetaDataInstanceFactory#createStepExecution(JobExecution, String, Long)}
* .
*/
@Test
public void testCreateStepExecutionJobExecutionStringLong() {
assertNotNull(MetaDataInstanceFactory.createStepExecution(stepName, stepExecutionId));
}
/**
* Test method for
* {@link MetaDataInstanceFactory#createJobExecutionWithStepExecutions(Long, java.util.Collection)}
* .
*/
@Test
public void testCreateJobExecutionWithStepExecutions() {
assertNotNull(MetaDataInstanceFactory.createJobExecutionWithStepExecutions(executionId, Arrays.asList(stepName)));
}
}