BATCH-2006: Completed implementation of JobContext and StepContext

* Implemented the interfaces per the JSR-352 spec
* Added a ne StepContextFactoryBean used to add the StepContext (one per step) into the step scope to be available for injection by batch artifacts.
* Updated the StepParser to utilize the new StepContextFactoryBean.
This commit is contained in:
Michael Minella
2013-08-04 19:08:56 -05:00
parent e1b1d3a43f
commit 35c9e2ea89
4 changed files with 188 additions and 3 deletions

View File

@@ -1,6 +1,8 @@
package org.springframework.batch.core.jsr;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.Properties;
@@ -15,11 +17,13 @@ import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.converter.JobParametersConverterSupport;
import org.springframework.batch.item.ExecutionContext;
public class StepContextTests {
private StepExecution stepExecution;
private StepContext stepContext;
private ExecutionContext executionContext;
@Before
public void setUp() throws Exception {
@@ -37,6 +41,8 @@ public class StepContextTests {
stepExecution.setRollbackCount(6);
stepExecution.setWriteCount(7);
stepExecution.setWriteSkipCount(8);
executionContext = new ExecutionContext();
stepExecution.setExecutionContext(executionContext);
stepContext = new StepContext(stepExecution, new JobParametersConverterSupport());
stepContext.setTransientUserData("This is my transient data");
@@ -92,4 +98,38 @@ public class StepContextTests {
stepContext.setExitStatus("new Exit Status");
assertEquals("new Exit Status", stepExecution.getExitStatus().getExitCode());
}
@Test
public void testPersistentUserData() {
String data = "saved data";
stepContext.setPersistentUserData(data);
assertEquals(data, stepContext.getPersistentUserData());
assertEquals(data, executionContext.get("batch_jsr_persistentUserData"));
}
@Test
public void testGetExceptionEmpty() {
assertNull(stepContext.getException());
}
@Test
public void testGetExceptionException() {
stepExecution.addFailureException(new Exception("expected"));
assertEquals("expected", stepContext.getException().getMessage());
}
@Test
public void testGetExceptionThrowable() {
stepExecution.addFailureException(new Throwable("expected"));
assertTrue(stepContext.getException().getMessage().endsWith("expected"));
}
@Test
public void testGetExceptionMultiple() {
stepExecution.addFailureException(new Exception("not me"));
stepExecution.addFailureException(new Exception("not me either"));
stepExecution.addFailureException(new Exception("me"));
assertEquals("me", stepContext.getException().getMessage());
}
}