OPEN - issue BATCH-773: Refactor and extend ExportedJobLauncher to JobOperator

Implement and test JobExplorer (formerly BatchMetaDataExplorer)
This commit is contained in:
dsyer
2008-08-17 16:00:19 +00:00
parent d8df436e8e
commit 533b3788d2
12 changed files with 686 additions and 53 deletions

View File

@@ -0,0 +1,148 @@
/*
* Copyright 2006-2008 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.exlore.support;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import javax.sql.DataSource;
import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.core.explore.support.JobExplorerFactoryBean;
import org.springframework.batch.item.database.support.DataFieldMaxValueIncrementerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
/**
* @author Lucas Ward
*
*/
public class JobExplorerFactoryBeanTests {
private JobExplorerFactoryBean factory;
private DataFieldMaxValueIncrementerFactory incrementerFactory;
private DataSource dataSource;
private String tablePrefix = "TEST_BATCH_PREFIX_";
@Before
public void setUp() throws Exception {
factory = new JobExplorerFactoryBean();
dataSource = createMock(DataSource.class);
factory.setDataSource(dataSource);
incrementerFactory = createMock(DataFieldMaxValueIncrementerFactory.class);
factory.setIncrementerFactory(incrementerFactory);
factory.setTablePrefix(tablePrefix);
}
@Test
public void testNoDatabaseType() throws Exception {
try {
expect(incrementerFactory.isSupportedIncrementerType(null)).andReturn(false);
expect(incrementerFactory.getSupportedIncrementerTypes()).andReturn(new String[0]);
replay(incrementerFactory);
factory.afterPropertiesSet();
fail();
}
catch (IllegalArgumentException ex) {
// expected
String message = ex.getMessage();
assertTrue("Wrong message: " + message, message.indexOf("unsupported database type") >= 0);
}
}
@Test
public void testMissingDataSource() throws Exception {
factory.setDataSource(null);
try {
factory.afterPropertiesSet();
fail();
}
catch (IllegalArgumentException ex) {
// expected
String message = ex.getMessage();
assertTrue("Wrong message: " + message, message.indexOf("DataSource") >= 0);
}
}
@Test
public void testInvalidDatabaseType() throws Exception {
factory.setDatabaseType("foo");
try {
expect(incrementerFactory.isSupportedIncrementerType("foo")).andReturn(false);
expect(incrementerFactory.getSupportedIncrementerTypes()).andReturn(new String[0]);
replay(incrementerFactory);
factory.afterPropertiesSet();
fail();
}
catch (IllegalArgumentException ex) {
// expected
String message = ex.getMessage();
assertTrue("Wrong message: " + message, message.indexOf("foo") >= 0);
}
}
@Test
public void testCreateExplorer() throws Exception {
String databaseType = "foo";
factory.setDatabaseType(databaseType);
expect(incrementerFactory.isSupportedIncrementerType("foo")).andReturn(true);
expect(incrementerFactory.getSupportedIncrementerTypes()).andReturn(new String[0]);
expect(incrementerFactory.getIncrementer(databaseType, tablePrefix + "JOB_SEQ")).andReturn(new StubIncrementer()).times(2);
expect(incrementerFactory.getIncrementer(databaseType, tablePrefix + "JOB_EXECUTION_SEQ")).andReturn(new StubIncrementer());
expect(incrementerFactory.getIncrementer(databaseType, tablePrefix + "STEP_EXECUTION_SEQ")).andReturn(new StubIncrementer());
replay(incrementerFactory);
factory.afterPropertiesSet();
factory.getObject();
verify(incrementerFactory);
}
private static class StubIncrementer implements DataFieldMaxValueIncrementer {
public int nextIntValue() throws DataAccessException {
return 0;
}
public long nextLongValue() throws DataAccessException {
return 0;
}
public String nextStringValue() throws DataAccessException {
return null;
}
}
}

View File

@@ -0,0 +1,26 @@
package org.springframework.batch.core.exlore.support;
import org.junit.Test;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.explore.support.MapJobExplorerFactoryBean;
/**
* Tests for {@link MapJobExplorerFactoryBean}.
*/
public class MapJobExplorerFactoryBeanTests {
private MapJobExplorerFactoryBean tested = new MapJobExplorerFactoryBean();
/**
* Use the factory to create repository and check the repository remembers
* created executions.
*/
@Test
public void testCreateRepository() throws Exception {
JobExplorer explorer = (JobExplorer) tested.getObject();
explorer.findRunningJobExecutions("foo");
}
}

View File

@@ -0,0 +1,124 @@
/*
* 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.exlore.support;
import static org.easymock.EasyMock.createMock;
import java.util.Collections;
import junit.framework.TestCase;
import org.easymock.EasyMock;
import org.junit.Test;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.explore.support.SimpleJobExplorer;
import org.springframework.batch.core.repository.dao.JobExecutionDao;
import org.springframework.batch.core.repository.dao.JobInstanceDao;
/**
* Test {@link SimpleJobExplorer}.
*
* @author Dave Syer
*
*/
public class SimpleJobExplorerTests extends TestCase {
SimpleJobExplorer jobExplorer;
JobExecutionDao jobExecutionDao;
JobInstanceDao jobInstanceDao;
JobInstance jobInstance = new JobInstance(111L, new JobParameters(), "job");
JobExecution jobExecution = new JobExecution(jobInstance, 123L);
public void setUp() throws Exception {
jobExecutionDao = createMock(JobExecutionDao.class);
jobInstanceDao = createMock(JobInstanceDao.class);
jobExplorer = new SimpleJobExplorer(jobInstanceDao, jobExecutionDao);
}
@Test
public void testGetJobExecution() throws Exception {
jobExecutionDao.getJobExecution(123L);
EasyMock.expectLastCall().andReturn(jobExecution);
EasyMock.replay(jobExecutionDao, jobInstanceDao);
jobExplorer.getJobExecution(123L);
EasyMock.verify(jobExecutionDao, jobInstanceDao);
}
@Test
public void testFindRunningJobExecutions() throws Exception {
jobExecutionDao.findRunningJobExecutions("job");
EasyMock.expectLastCall().andReturn(Collections.singleton(jobExecution));
EasyMock.replay(jobExecutionDao, jobInstanceDao);
jobExplorer.findRunningJobExecutions("job");
EasyMock.verify(jobExecutionDao, jobInstanceDao);
}
@Test
public void testFindJobExecutions() throws Exception {
jobExecutionDao.findJobExecutions(jobInstance);
EasyMock.expectLastCall().andReturn(Collections.singletonList(jobExecution));
EasyMock.replay(jobExecutionDao, jobInstanceDao);
jobExplorer.findJobExecutions(jobInstance);
EasyMock.verify(jobExecutionDao, jobInstanceDao);
}
@Test
public void testGetJobInstance() throws Exception {
jobInstanceDao.getJobInstance(111L);
EasyMock.expectLastCall().andReturn(jobInstance);
EasyMock.replay(jobExecutionDao, jobInstanceDao);
jobExplorer.getJobInstance(111L);
EasyMock.verify(jobExecutionDao, jobInstanceDao);
}
@Test
public void testGetLastJobInstances() throws Exception {
jobInstanceDao.getLastJobInstances("foo", 1);
EasyMock.expectLastCall().andReturn(Collections.singletonList(jobInstance));
EasyMock.replay(jobExecutionDao, jobInstanceDao);
jobExplorer.getLastJobInstances("foo", 1);
EasyMock.verify(jobExecutionDao, jobInstanceDao);
}
@Test
public void testIsJobInstanceFalse() throws Exception {
jobInstanceDao.getJobInstance("foo", new JobParameters());
EasyMock.expectLastCall().andReturn(null);
EasyMock.replay(jobExecutionDao, jobInstanceDao);
assertFalse(jobExplorer.isJobInstanceExists("foo", new JobParameters()));
EasyMock.verify(jobExecutionDao, jobInstanceDao);
}
@Test
public void testIsJobInstanceTrue() throws Exception {
jobInstanceDao.getJobInstance("foo", new JobParameters());
EasyMock.expectLastCall().andReturn(jobInstance);
EasyMock.replay(jobExecutionDao, jobInstanceDao);
assertTrue(jobExplorer.isJobInstanceExists("foo", new JobParameters()));
EasyMock.verify(jobExecutionDao, jobInstanceDao);
}
}

View File

@@ -37,7 +37,7 @@ import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersIncrementer;
import org.springframework.batch.core.configuration.support.MapJobRegistry;
import org.springframework.batch.core.converter.DefaultJobParametersConverter;
import org.springframework.batch.core.explore.BatchMetaDataExplorer;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.job.JobSupport;
import org.springframework.batch.core.launch.JobInstanceAlreadyExistsException;
import org.springframework.batch.core.launch.JobLauncher;
@@ -58,7 +58,7 @@ public class SimpleJobOperatorTests {
protected Job job;
private BatchMetaDataExplorer batchMetaDataExplorer;
private JobExplorer jobExplorer;
private JobParameters jobParameters;
@@ -102,9 +102,9 @@ public class SimpleJobOperatorTests {
}
});
batchMetaDataExplorer = EasyMock.createNiceMock(BatchMetaDataExplorer.class);
jobExplorer = EasyMock.createNiceMock(JobExplorer.class);
jobOperator.setBatchMetaDataExplorer(batchMetaDataExplorer);
jobOperator.setJobExplorer(jobExplorer);
jobOperator.setJobParametersConverter(new DefaultJobParametersConverter() {
@Override
@@ -155,31 +155,31 @@ public class SimpleJobOperatorTests {
@Test
public void testStartNextInstanceSunnyDay() throws Exception {
final JobParameters jobParameters = new JobParameters();
batchMetaDataExplorer.getLastJobInstances("foo", 1);
jobExplorer.getLastJobInstances("foo", 1);
EasyMock.expectLastCall().andReturn(Collections.singletonList(new JobInstance(321L, jobParameters, "foo")));
EasyMock.replay(batchMetaDataExplorer);
EasyMock.replay(jobExplorer);
Long value = jobOperator.startNextInstance("foo");
assertEquals(999, value.longValue());
EasyMock.verify(batchMetaDataExplorer);
EasyMock.verify(jobExplorer);
}
@Test
public void testStartNewInstanceSunnyDay() throws Exception {
jobParameters = new JobParameters();
batchMetaDataExplorer.isJobInstanceExists("foo", jobParameters);
jobExplorer.isJobInstanceExists("foo", jobParameters);
EasyMock.expectLastCall().andReturn(false);
EasyMock.replay(batchMetaDataExplorer);
EasyMock.replay(jobExplorer);
Long value = jobOperator.start("foo", "a=b");
assertEquals(999, value.longValue());
EasyMock.verify(batchMetaDataExplorer);
EasyMock.verify(jobExplorer);
}
@Test
public void testStartNewInstanceAlreadyExists() throws Exception {
jobParameters = new JobParameters();
batchMetaDataExplorer.isJobInstanceExists("foo", jobParameters);
jobExplorer.isJobInstanceExists("foo", jobParameters);
EasyMock.expectLastCall().andReturn(true);
EasyMock.replay(batchMetaDataExplorer);
EasyMock.replay(jobExplorer);
try {
jobOperator.start("foo", "a=b");
fail("Expected JobInstanceAlreadyExistsException");
@@ -187,82 +187,82 @@ public class SimpleJobOperatorTests {
catch (JobInstanceAlreadyExistsException e) {
// expected
}
EasyMock.verify(batchMetaDataExplorer);
EasyMock.verify(jobExplorer);
}
@Test
public void testResumeSunnyDay() throws Exception {
jobParameters = new JobParameters();
batchMetaDataExplorer.getJobExecution(111L);
jobExplorer.getJobExecution(111L);
EasyMock.expectLastCall()
.andReturn(new JobExecution(new JobInstance(123L, jobParameters, job.getName()), 111L));
EasyMock.replay(batchMetaDataExplorer);
EasyMock.replay(jobExplorer);
Long value = jobOperator.resume(111L);
assertEquals(999, value.longValue());
EasyMock.verify(batchMetaDataExplorer);
EasyMock.verify(jobExplorer);
}
@Test
public void testGetSummarySunnyDay() throws Exception {
jobParameters = new JobParameters();
batchMetaDataExplorer.getJobExecution(111L);
jobExplorer.getJobExecution(111L);
JobExecution jobExecution = new JobExecution(new JobInstance(123L, jobParameters, job.getName()), 111L);
EasyMock.expectLastCall().andReturn(jobExecution);
EasyMock.replay(batchMetaDataExplorer);
EasyMock.replay(jobExplorer);
String value = jobOperator.getSummary(111L);
assertEquals(jobExecution.toString(), value);
EasyMock.verify(batchMetaDataExplorer);
EasyMock.verify(jobExplorer);
}
@Test
public void testGetStepExecutionSummariesSunnyDay() throws Exception {
jobParameters = new JobParameters();
batchMetaDataExplorer.getJobExecution(111L);
jobExplorer.getJobExecution(111L);
JobExecution jobExecution = new JobExecution(new JobInstance(123L, jobParameters, job.getName()), 111L);
jobExecution.createStepExecution(new StepSupport("step1"));
jobExecution.createStepExecution(new StepSupport("step2"));
jobExecution.getStepExecutions().iterator().next().setId(21L);
EasyMock.expectLastCall().andReturn(jobExecution);
EasyMock.replay(batchMetaDataExplorer);
EasyMock.replay(jobExplorer);
Map<Long, String> value = jobOperator.getStepExecutionSummaries(111L);
assertEquals(2, value.size());
EasyMock.verify(batchMetaDataExplorer);
EasyMock.verify(jobExplorer);
}
@Test
public void testFindRunningExecutionsSunnyDay() throws Exception {
jobParameters = new JobParameters();
batchMetaDataExplorer.findRunningJobExecutions("foo");
jobExplorer.findRunningJobExecutions("foo");
JobExecution jobExecution = new JobExecution(new JobInstance(123L, jobParameters, job.getName()), 111L);
EasyMock.expectLastCall().andReturn(Collections.singleton(jobExecution));
EasyMock.replay(batchMetaDataExplorer);
EasyMock.replay(jobExplorer);
Set<Long> value = jobOperator.getRunningExecutions("foo");
assertEquals(111L, value.iterator().next().longValue());
EasyMock.verify(batchMetaDataExplorer);
EasyMock.verify(jobExplorer);
}
@Test
public void testGetJobParametersSunnyDay() throws Exception {
final JobParameters jobParameters = new JobParameters();
batchMetaDataExplorer.getJobExecution(111L);
jobExplorer.getJobExecution(111L);
EasyMock.expectLastCall()
.andReturn(new JobExecution(new JobInstance(123L, jobParameters, job.getName()), 111L));
EasyMock.replay(batchMetaDataExplorer);
EasyMock.replay(jobExplorer);
String value = jobOperator.getParameters(111L);
assertEquals("a=b", value);
EasyMock.verify(batchMetaDataExplorer);
EasyMock.verify(jobExplorer);
}
@Test
public void testGetLastInstancesSunnyDay() throws Exception {
jobParameters = new JobParameters();
batchMetaDataExplorer.getLastJobInstances("foo",2);
jobExplorer.getLastJobInstances("foo",2);
JobInstance jobInstance = new JobInstance(123L, jobParameters, job.getName());
EasyMock.expectLastCall().andReturn(Collections.singletonList(jobInstance));
EasyMock.replay(batchMetaDataExplorer);
EasyMock.replay(jobExplorer);
List<Long> value = jobOperator.getLastInstances("foo",2);
assertEquals(123L, value.get(0).longValue());
EasyMock.verify(batchMetaDataExplorer);
EasyMock.verify(jobExplorer);
}
@Test
@@ -275,15 +275,15 @@ public class SimpleJobOperatorTests {
@Test
public void testGetExecutionsSunnyDay() throws Exception {
JobInstance jobInstance = new JobInstance(123L, jobParameters, job.getName());
batchMetaDataExplorer.getJobInstance(123L);
jobExplorer.getJobInstance(123L);
EasyMock.expectLastCall().andReturn(jobInstance);
JobExecution jobExecution = new JobExecution(jobInstance, 111L);
batchMetaDataExplorer.findJobExecutions(jobInstance);
jobExplorer.findJobExecutions(jobInstance);
EasyMock.expectLastCall().andReturn(Collections.singletonList(jobExecution));
EasyMock.replay(batchMetaDataExplorer);
EasyMock.replay(jobExplorer);
List<Long> value = jobOperator.getExecutions(123L);
assertEquals(111L, value.iterator().next().longValue());
EasyMock.verify(batchMetaDataExplorer);
EasyMock.verify(jobExplorer);
}
}