OPEN - issue BATCH-791: Ditch Tasklet (StepHandler is more flexible)
Done. Use StepHandler instead.
This commit is contained in:
@@ -4,7 +4,7 @@ import java.io.File;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@@ -17,10 +17,9 @@ import org.springframework.util.Assert;
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration()
|
||||
public class TaskletJobFunctionalTests extends AbstractValidatingBatchLauncherTests {
|
||||
public class HandlerJobFunctionalTests extends AbstractValidatingBatchLauncherTests {
|
||||
|
||||
@Autowired
|
||||
private Resource directory;
|
||||
private Resource directory = new FileSystemResource("target/test-outputs/test-dir");
|
||||
|
||||
/*
|
||||
* Create the directory and some files in it.
|
||||
@@ -3,8 +3,8 @@ package org.springframework.batch.sample;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.sample.tasklet.DummyMessageReceivingTasklet;
|
||||
import org.springframework.batch.sample.tasklet.DummyMessageSendingTasklet;
|
||||
import org.springframework.batch.sample.tasklet.DummyMessageReceivingStepHandler;
|
||||
import org.springframework.batch.sample.tasklet.DummyMessageSendingStepHandler;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@@ -14,10 +14,10 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
public class JobExecutionContextSampleFunctionalTests extends AbstractValidatingBatchLauncherTests {
|
||||
|
||||
@Autowired
|
||||
private DummyMessageSendingTasklet sender;
|
||||
private DummyMessageSendingStepHandler sender;
|
||||
|
||||
@Autowired
|
||||
private DummyMessageReceivingTasklet receiver;
|
||||
private DummyMessageReceivingStepHandler receiver;
|
||||
|
||||
protected void validatePostConditions() throws Exception {
|
||||
assertEquals(sender.getMessage(), receiver.getReceivedMessage());
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
package org.springframework.batch.sample.common;
|
||||
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.repeat.ExitStatus;
|
||||
|
||||
/**
|
||||
* Tests for {@link ConfigurableSystemProcessExitCodeMapper}
|
||||
*/
|
||||
public class ConfigurableSystemProcessExitCodeMapperTests {
|
||||
|
||||
private ConfigurableSystemProcessExitCodeMapper mapper = new ConfigurableSystemProcessExitCodeMapper();
|
||||
|
||||
/**
|
||||
* Regular usage scenario - mapping adheres to injected values
|
||||
*/
|
||||
@Test
|
||||
public void testMapping() {
|
||||
Map<Object, ExitStatus> mappings = new HashMap<Object, ExitStatus>() {{
|
||||
put(0, ExitStatus.FINISHED);
|
||||
put(1, ExitStatus.FAILED);
|
||||
put(2, ExitStatus.CONTINUABLE);
|
||||
put(3, ExitStatus.NOOP);
|
||||
put(4, ExitStatus.UNKNOWN);
|
||||
put(ConfigurableSystemProcessExitCodeMapper.ELSE_KEY, ExitStatus.UNKNOWN);
|
||||
}};
|
||||
|
||||
mapper.setMappings(mappings);
|
||||
|
||||
//check explicitly defined values
|
||||
for (Map.Entry<Object, ExitStatus> entry : mappings.entrySet()) {
|
||||
if (entry.getKey().equals(ConfigurableSystemProcessExitCodeMapper.ELSE_KEY)) continue;
|
||||
|
||||
int exitCode = (Integer) entry.getKey();
|
||||
assertSame(entry.getValue(), mapper.getExitStatus(exitCode));
|
||||
}
|
||||
|
||||
//check the else clause
|
||||
assertSame(mappings.get(ConfigurableSystemProcessExitCodeMapper.ELSE_KEY),
|
||||
mapper.getExitStatus(5));
|
||||
}
|
||||
|
||||
/**
|
||||
* Else clause is required in the injected map - setter checks its presence.
|
||||
*/
|
||||
@Test
|
||||
public void testSetMappingsMissingElseClause() {
|
||||
Map<Object, ExitStatus> missingElse = new HashMap<Object, ExitStatus>();
|
||||
try {
|
||||
mapper.setMappings(missingElse);
|
||||
fail();
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
}
|
||||
|
||||
Map<Object, ExitStatus> containsElse = new HashMap<Object, ExitStatus>() {{
|
||||
put(ConfigurableSystemProcessExitCodeMapper.ELSE_KEY, ExitStatus.FAILED);
|
||||
}};
|
||||
// no error expected now
|
||||
mapper.setMappings(containsElse);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package org.springframework.batch.sample.common;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.repeat.ExitStatus;
|
||||
|
||||
/**
|
||||
* Tests for {@link SimpleSystemProcessExitCodeMapper}.
|
||||
*/
|
||||
public class SimpleSystemProcessExitCodeMapperTests {
|
||||
|
||||
private SimpleSystemProcessExitCodeMapper mapper = new SimpleSystemProcessExitCodeMapper();
|
||||
|
||||
/**
|
||||
* 0 -> ExitStatus.FINISHED
|
||||
* else -> ExitStatus.FAILED
|
||||
*/
|
||||
@Test
|
||||
public void testMapping() {
|
||||
assertEquals(ExitStatus.FINISHED, mapper.getExitStatus(0));
|
||||
assertEquals(ExitStatus.FAILED, mapper.getExitStatus(1));
|
||||
assertEquals(ExitStatus.FAILED, mapper.getExitStatus(-1));
|
||||
}
|
||||
}
|
||||
@@ -1,212 +0,0 @@
|
||||
package org.springframework.batch.sample.common;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobInstance;
|
||||
import org.springframework.batch.core.JobInterruptedException;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.repeat.ExitStatus;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Tests for {@link SystemCommandTasklet}.
|
||||
*/
|
||||
public class SystemCommandTaskletIntegrationTests {
|
||||
|
||||
private static final Log log = LogFactory.getLog(SystemCommandTaskletIntegrationTests.class);
|
||||
|
||||
private SystemCommandTasklet tasklet = new SystemCommandTasklet();
|
||||
|
||||
private StepExecution stepExecution = new StepExecution("systemCommandStep", new JobExecution(new JobInstance(
|
||||
1L, new JobParameters(), "systemCommandJob")));
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
tasklet.setEnvironmentParams(null); // inherit from parent process
|
||||
tasklet.setWorkingDirectory(null); // inherit from parent process
|
||||
tasklet.setSystemProcessExitCodeMapper(new TestExitCodeMapper());
|
||||
tasklet.setTimeout(5000); // long enough timeout
|
||||
tasklet.setTerminationCheckInterval(500);
|
||||
tasklet.setCommand("invalid command, change value for successful execution");
|
||||
tasklet.afterPropertiesSet();
|
||||
|
||||
tasklet.beforeStep(stepExecution);
|
||||
}
|
||||
|
||||
/*
|
||||
* Regular usage scenario - successful execution of system command.
|
||||
*/
|
||||
@Test
|
||||
public void testExecute() throws Exception {
|
||||
String command = "java -version";
|
||||
tasklet.setCommand(command);
|
||||
tasklet.afterPropertiesSet();
|
||||
|
||||
log.info("Executing command: " + command);
|
||||
ExitStatus exitStatus = tasklet.execute();
|
||||
|
||||
assertEquals(ExitStatus.FINISHED, exitStatus);
|
||||
}
|
||||
|
||||
/*
|
||||
* Failed execution scenario - error exit code returned by system command.
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteFailure() throws Exception {
|
||||
String command = "java org.springframework.batch.sample.tasklet.UnknownClass";
|
||||
tasklet.setCommand(command);
|
||||
tasklet.afterPropertiesSet();
|
||||
|
||||
log.info("Executing command: " + command);
|
||||
ExitStatus exitStatus = tasklet.execute();
|
||||
|
||||
assertEquals(ExitStatus.FAILED, exitStatus);
|
||||
}
|
||||
|
||||
/*
|
||||
* Failed execution scenario - execution time exceeds timeout.
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteTimeout() throws Exception {
|
||||
String command = "sleep 3";
|
||||
tasklet.setCommand(command);
|
||||
tasklet.setTimeout(10);
|
||||
tasklet.afterPropertiesSet();
|
||||
|
||||
log.info("Executing command: " + command);
|
||||
try {
|
||||
tasklet.execute();
|
||||
fail();
|
||||
}
|
||||
catch (SystemCommandException e) {
|
||||
assertTrue(e.getMessage().indexOf("did not finish successfully within the timeout") > 0);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Job interrupted scenario.
|
||||
*/
|
||||
@Test
|
||||
public void testInterruption() throws Exception {
|
||||
String command = "sleep 5";
|
||||
tasklet.setCommand(command);
|
||||
tasklet.setTerminationCheckInterval(10);
|
||||
tasklet.afterPropertiesSet();
|
||||
|
||||
stepExecution.setTerminateOnly();
|
||||
try {
|
||||
tasklet.execute();
|
||||
fail();
|
||||
}
|
||||
catch (JobInterruptedException e) {
|
||||
System.out.println(e.getMessage());
|
||||
assertTrue(e.getMessage().indexOf("Job interrupted while executing system command") > -1);
|
||||
assertTrue(e.getMessage().indexOf(command) > -1);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Command property value is required to be set.
|
||||
*/
|
||||
@Test
|
||||
public void testCommandNotSet() throws Exception {
|
||||
tasklet.setCommand(null);
|
||||
try {
|
||||
tasklet.afterPropertiesSet();
|
||||
fail();
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
}
|
||||
|
||||
tasklet.setCommand("");
|
||||
try {
|
||||
tasklet.afterPropertiesSet();
|
||||
fail();
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Timeout must be set to non-zero value.
|
||||
*/
|
||||
@Test
|
||||
public void testTimeoutNotSet() throws Exception {
|
||||
tasklet.setCommand("not-empty placeholder");
|
||||
tasklet.setTimeout(0);
|
||||
try {
|
||||
tasklet.afterPropertiesSet();
|
||||
fail();
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Working directory property must point to an existing location and it must
|
||||
* be a directory
|
||||
*/
|
||||
@Test
|
||||
public void testWorkingDirectory() throws Exception {
|
||||
File notExistingFile = new File("not-existing-path");
|
||||
Assert.state(!notExistingFile.exists());
|
||||
|
||||
try {
|
||||
tasklet.setWorkingDirectory(notExistingFile.getCanonicalPath());
|
||||
fail();
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
}
|
||||
|
||||
File notDirectory = File.createTempFile(this.getClass().getName(), null);
|
||||
Assert.state(notDirectory.exists());
|
||||
Assert.state(!notDirectory.isDirectory());
|
||||
|
||||
try {
|
||||
tasklet.setWorkingDirectory(notDirectory.getCanonicalPath());
|
||||
fail();
|
||||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
}
|
||||
|
||||
File directory = notDirectory.getParentFile();
|
||||
Assert.state(directory.exists());
|
||||
Assert.state(directory.isDirectory());
|
||||
|
||||
// no error expected now
|
||||
tasklet.setWorkingDirectory(directory.getCanonicalPath());
|
||||
}
|
||||
|
||||
/**
|
||||
* Exit code mapper containing mapping logic expected by the tests. 0 means
|
||||
* finished successfully, other value means failure.
|
||||
*/
|
||||
private static class TestExitCodeMapper implements SystemProcessExitCodeMapper {
|
||||
|
||||
public ExitStatus getExitStatus(int exitCode) {
|
||||
if (exitCode == 0) {
|
||||
return ExitStatus.FINISHED;
|
||||
}
|
||||
else {
|
||||
return ExitStatus.FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,6 +5,6 @@
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
|
||||
|
||||
<import resource="classpath:/simple-job-launcher-context.xml" />
|
||||
<import resource="classpath:/jobs/taskletJob.xml" />
|
||||
<import resource="classpath:/jobs/handlerJob.xml" />
|
||||
|
||||
</beans>
|
||||
Reference in New Issue
Block a user