IN PROGRESS - issue BATCH-264: Dependencies among jobs
Refactored SimpleJob and AbstractJob. Also had to fix JobRegistryBackgroundJobRunner to not use stdin for blocking.
This commit is contained in:
@@ -15,41 +15,38 @@
|
||||
*/
|
||||
package org.springframework.batch.core.job;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.Step;
|
||||
import org.springframework.batch.core.step.StepSupport;
|
||||
import org.springframework.batch.core.JobExecutionException;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class AbstractJobTests extends TestCase {
|
||||
public class AbstractJobTests {
|
||||
|
||||
AbstractJob job = new AbstractJob("job") {
|
||||
public void execute(JobExecution execution) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
};
|
||||
AbstractJob job = new StubJob("job");
|
||||
|
||||
/**
|
||||
* Test method for {@link org.springframework.batch.core.job.AbstractJob#getName()}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetName() {
|
||||
job = new AbstractJob(){
|
||||
public void execute(JobExecution execution) {
|
||||
// No-op
|
||||
}
|
||||
};
|
||||
job = new StubJob();
|
||||
assertNull(job.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link org.springframework.batch.core.job.AbstractJob#setBeanName(java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testSetBeanName() {
|
||||
job.setBeanName("foo");
|
||||
assertEquals("job", job.getName());
|
||||
@@ -58,53 +55,32 @@ public class AbstractJobTests extends TestCase {
|
||||
/**
|
||||
* Test method for {@link org.springframework.batch.core.job.AbstractJob#setBeanName(java.lang.String)}.
|
||||
*/
|
||||
@Test
|
||||
public void testSetBeanNameWithNullName() {
|
||||
job = new AbstractJob(null) {
|
||||
public void execute(JobExecution execution) {
|
||||
// NO-OP
|
||||
}
|
||||
};
|
||||
job = new StubJob(null);
|
||||
assertEquals(null, job.getName());
|
||||
job.setBeanName("foo");
|
||||
assertEquals("foo", job.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link org.springframework.batch.core.job.AbstractJob#setSteps(java.util.List)}.
|
||||
*/
|
||||
public void testSetSteps() {
|
||||
job.setSteps(Collections.singletonList((Step) new StepSupport("step")));
|
||||
assertEquals(1, job.getSteps().size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.core.job.AbstractJob#addStep(org.springframework.batch.core.Step)}.
|
||||
*/
|
||||
public void testAddStep() {
|
||||
job.addStep(new StepSupport("step"));
|
||||
assertEquals(1, job.getSteps().size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link org.springframework.batch.core.job.AbstractJob#setRestartable(boolean)}.
|
||||
*/
|
||||
@Test
|
||||
public void testSetRestartable() {
|
||||
assertFalse(job.isRestartable());
|
||||
job.setRestartable(true);
|
||||
assertTrue(job.isRestartable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToString() throws Exception {
|
||||
String value = job.toString();
|
||||
assertTrue("Should contain name: " + value, value.indexOf("name=") >= 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAfterPropertiesSet() throws Exception {
|
||||
AbstractJob job = new AbstractJob() {
|
||||
public void execute(JobExecution execution) {
|
||||
}
|
||||
};
|
||||
job.setJobRepository(null);
|
||||
try {
|
||||
job.afterPropertiesSet();
|
||||
@@ -114,5 +90,31 @@ public class AbstractJobTests extends TestCase {
|
||||
assertTrue(e.getMessage().contains("JobRepository"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
private static class StubJob extends AbstractJob {
|
||||
/**
|
||||
* @param name
|
||||
*/
|
||||
private StubJob(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* No-name constructor
|
||||
*/
|
||||
public StubJob() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StepExecution doExecute(JobExecution execution) throws JobExecutionException {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,14 +19,21 @@ package org.springframework.batch.core.job;
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
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.JobExecutionException;
|
||||
@@ -58,7 +65,7 @@ import org.springframework.batch.repeat.ExitStatus;
|
||||
*
|
||||
* @author Lucas Ward
|
||||
*/
|
||||
public class SimpleJobTests extends TestCase {
|
||||
public class SimpleJobTests {
|
||||
|
||||
private JobRepository jobRepository;
|
||||
|
||||
@@ -88,8 +95,8 @@ public class SimpleJobTests extends TestCase {
|
||||
|
||||
private SimpleJob job;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
|
||||
MapJobInstanceDao.clear();
|
||||
MapJobExecutionDao.clear();
|
||||
@@ -132,7 +139,30 @@ public class SimpleJobTests extends TestCase {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link SimpleJob#setSteps(java.util.List)}.
|
||||
*/
|
||||
@Test
|
||||
public void testSetSteps() {
|
||||
job.setSteps(Collections.singletonList((Step) new StepSupport("step")));
|
||||
job.execute(jobExecution);
|
||||
assertEquals(1, jobExecution.getStepExecutions().size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for
|
||||
* {@link SimpleJob#addStep(org.springframework.batch.core.Step)}.
|
||||
*/
|
||||
@Test
|
||||
public void testAddStep() {
|
||||
job.setSteps(Collections.<Step> emptyList());
|
||||
job.addStep(new StepSupport("step"));
|
||||
job.execute(jobExecution);
|
||||
assertEquals(1, jobExecution.getStepExecutions().size());
|
||||
}
|
||||
|
||||
// Test to ensure the exit status returned by the last step is returned
|
||||
@Test
|
||||
public void testExitStatusReturned() throws JobExecutionException {
|
||||
|
||||
final ExitStatus customStatus = new ExitStatus(true, "test");
|
||||
@@ -162,10 +192,7 @@ public class SimpleJobTests extends TestCase {
|
||||
assertEquals(customStatus, jobExecution.getExitStatus());
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRunNormally() throws Exception {
|
||||
step1.setStartLimit(5);
|
||||
step2.setStartLimit(5);
|
||||
@@ -179,6 +206,7 @@ public class SimpleJobTests extends TestCase {
|
||||
assertFalse(step2.passedInJobContext.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRunNormallyWithListener() throws Exception {
|
||||
job.setJobExecutionListeners(new JobExecutionListenerSupport[] { new JobExecutionListenerSupport() {
|
||||
public void beforeJob(JobExecution jobExecution) {
|
||||
@@ -193,6 +221,7 @@ public class SimpleJobTests extends TestCase {
|
||||
assertEquals(4, list.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRunWithSimpleStepExecutor() throws Exception {
|
||||
|
||||
job.setJobRepository(jobRepository);
|
||||
@@ -205,6 +234,7 @@ public class SimpleJobTests extends TestCase {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecutionContextIsSet() throws Exception {
|
||||
testRunNormally();
|
||||
assertEquals(jobInstance, jobExecution.getJobInstance());
|
||||
@@ -213,6 +243,7 @@ public class SimpleJobTests extends TestCase {
|
||||
assertEquals(step2.getName(), stepExecution2.getStepName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInterrupted() throws Exception {
|
||||
step1.setStartLimit(5);
|
||||
step2.setStartLimit(5);
|
||||
@@ -225,6 +256,7 @@ public class SimpleJobTests extends TestCase {
|
||||
checkRepository(BatchStatus.STOPPED, ExitStatus.FAILED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFailed() throws Exception {
|
||||
step1.setStartLimit(5);
|
||||
step2.setStartLimit(5);
|
||||
@@ -239,6 +271,7 @@ public class SimpleJobTests extends TestCase {
|
||||
checkRepository(BatchStatus.FAILED, ExitStatus.FAILED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFailedWithListener() throws Exception {
|
||||
job.setJobExecutionListeners(new JobExecutionListenerSupport[] { new JobExecutionListenerSupport() {
|
||||
public void afterJob(JobExecution jobExecution) {
|
||||
@@ -255,6 +288,7 @@ public class SimpleJobTests extends TestCase {
|
||||
checkRepository(BatchStatus.FAILED, ExitStatus.FAILED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFailedWithError() throws Exception {
|
||||
step1.setStartLimit(5);
|
||||
step2.setStartLimit(5);
|
||||
@@ -268,6 +302,7 @@ public class SimpleJobTests extends TestCase {
|
||||
checkRepository(BatchStatus.FAILED, ExitStatus.FAILED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStepShouldNotStart() throws Exception {
|
||||
// Start policy will return false, keeping the step from being started.
|
||||
step1.setStartLimit(0);
|
||||
@@ -280,13 +315,14 @@ public class SimpleJobTests extends TestCase {
|
||||
.indexOf("start limit exceeded") >= 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoSteps() throws Exception {
|
||||
job.setSteps(new ArrayList<Step>());
|
||||
|
||||
job.execute(jobExecution);
|
||||
ExitStatus exitStatus = jobExecution.getExitStatus();
|
||||
assertTrue("Wrong message in execution: " + exitStatus, exitStatus.getExitDescription().indexOf(
|
||||
"No steps configured") >= 0);
|
||||
"no steps configured") >= 0);
|
||||
}
|
||||
|
||||
// public void testNoStepsExecuted() throws Exception {
|
||||
@@ -302,6 +338,7 @@ public class SimpleJobTests extends TestCase {
|
||||
// "steps already completed"));
|
||||
// }
|
||||
|
||||
@Test
|
||||
public void testNotExecutedIfAlreadyStopped() throws Exception {
|
||||
jobExecution.stop();
|
||||
job.execute(jobExecution);
|
||||
@@ -312,6 +349,7 @@ public class SimpleJobTests extends TestCase {
|
||||
assertEquals(ExitStatus.NOOP.getExitCode(), exitStatus.getExitCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRestart() throws Exception {
|
||||
step1.setAllowStartIfComplete(true);
|
||||
final RuntimeException exception = new RuntimeException("Foo!");
|
||||
@@ -328,6 +366,7 @@ public class SimpleJobTests extends TestCase {
|
||||
assertFalse(step2.passedInStepContext.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInterruptWithListener() throws Exception {
|
||||
step1.setProcessException(new JobInterruptedException("job interrupted!"));
|
||||
|
||||
@@ -347,6 +386,7 @@ public class SimpleJobTests extends TestCase {
|
||||
/**
|
||||
* Execution context should be restored on restart.
|
||||
*/
|
||||
@Test
|
||||
public void testRestartScenario() throws Exception {
|
||||
|
||||
job.setRestartable(true);
|
||||
@@ -375,6 +415,7 @@ public class SimpleJobTests extends TestCase {
|
||||
assertFalse(step2.passedInJobContext.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInterruptJob() throws Exception {
|
||||
|
||||
step1 = new StubStep("interruptStep") {
|
||||
|
||||
@@ -50,5 +50,6 @@ public class JobRegistryBackgroundJobRunnerTests {
|
||||
public void tearDown() throws Exception {
|
||||
System.clearProperty(JobRegistryBackgroundJobRunner.EMBEDDED);
|
||||
JobRegistryBackgroundJobRunner.getErrors().clear();
|
||||
JobRegistryBackgroundJobRunner.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,6 @@ import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.Step;
|
||||
import org.springframework.batch.core.StepListener;
|
||||
import org.springframework.batch.core.job.AbstractJob;
|
||||
import org.springframework.batch.core.job.SimpleJob;
|
||||
import org.springframework.batch.core.listener.ItemListenerSupport;
|
||||
import org.springframework.batch.core.repository.dao.MapExecutionContextDao;
|
||||
@@ -71,15 +70,12 @@ public class SimpleStepFactoryBeanTests {
|
||||
|
||||
private ItemReader<String> reader;
|
||||
|
||||
private AbstractJob job = new SimpleJob() {
|
||||
{
|
||||
setBeanName("simpleJob");
|
||||
}
|
||||
};
|
||||
private SimpleJob job = new SimpleJob();
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
job.setJobRepository(repository);
|
||||
job.setBeanName("simpleJob");
|
||||
MapJobInstanceDao.clear();
|
||||
MapJobExecutionDao.clear();
|
||||
MapStepExecutionDao.clear();
|
||||
@@ -151,7 +147,7 @@ public class SimpleStepFactoryBeanTests {
|
||||
job.setSteps(Collections.singletonList(step));
|
||||
|
||||
JobExecution jobExecution = repository.createJobExecution(job.getName(), new JobParameters());
|
||||
|
||||
|
||||
job.execute(jobExecution);
|
||||
assertEquals("Error!", jobExecution.getAllFailureExceptions().get(0).getMessage());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user