@Override public void run() { try { jobRepository.add(stepExecution); step.execute(stepExecution); } catch (JobInterruptedException e) { // do nothing... } } };
@Test public void testRepositoryErrorOnExecutionContext() throws Exception { taskletStep.setTasklet(new Tasklet() { @Override public RepeatStatus execute(StepContribution contribution, ChunkContext attributes) throws Exception { return RepeatStatus.FINISHED; } }); jobRepository.setFailOnUpdateExecutionContext(true); taskletStep.execute(stepExecution); assertEquals(UNKNOWN, stepExecution.getStatus()); Throwable e = stepExecution.getFailureExceptions().get(0); assertEquals("Expected exception in step execution context persistence", e.getMessage()); }
@Test public void testSuccessfulExecutionWithExecutionContext() throws Exception { final JobExecution jobExecution = new JobExecution(jobInstance, jobParameters); final StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); step.setJobRepository(new JobRepositorySupport() { @Override public void updateExecutionContext(StepExecution stepExecution) { list.add(stepExecution); } }); step.execute(stepExecution); // context saved before looping and updated once for every processing // loop (once in this case) assertEquals(3, list.size()); }
@Test public void testOpenFailure() throws Exception { final RuntimeException exception = new RuntimeException(); taskletStep.setStreams(new ItemStream[] { new ItemStreamSupport() { @Override public void open(ExecutionContext executionContext) throws ItemStreamException { throw exception; } } }); taskletStep.execute(stepExecution); assertEquals(FAILED, stepExecution.getStatus()); assertTrue(stepExecution.getFailureExceptions().contains(exception)); assertEquals(2, jobRepository.getUpdateCount()); }
@Test public void testRepositoryErrorOnUpdateStepExecutionInTransaction() throws Exception { taskletStep.setTasklet(new Tasklet() { @Override public RepeatStatus execute(StepContribution contribution, ChunkContext attributes) throws Exception { return RepeatStatus.FINISHED; } }); jobRepository.setFailOnUpdateStepExecution(1); jobRepository.setFailInTransaction(true); taskletStep.execute(stepExecution); assertEquals(FAILED, stepExecution.getStatus()); Throwable e = stepExecution.getFailureExceptions().get(0); assertEquals("JobRepository failure forcing rollback", e.getMessage()); }
@Test public void testRestartJobOnNonRestartableTasklet() throws Exception { step.setTasklet(new TestingChunkOrientedTasklet<>(new ItemReader<String>() { @Override public String read() throws Exception { return "foo"; } }, itemWriter)); JobExecution jobExecution = new JobExecution(jobInstance, jobParameters); StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); step.execute(stepExecution); }
@Test public void testStepExecutor() throws Exception { JobExecution jobExecutionContext = new JobExecution(jobInstance, jobParameters); StepExecution stepExecution = new StepExecution(step.getName(), jobExecutionContext); step.execute(stepExecution); assertEquals(1, processed.size()); assertEquals(1, stepExecution.getReadCount()); assertEquals(1, stepExecution.getCommitCount()); }
@Test public void testApplicationException() throws Exception { taskletStep.execute(stepExecution); assertEquals(FAILED, stepExecution.getStatus()); assertEquals(FAILED.toString(), stepExecution.getExitStatus().getExitCode()); }
@Test public void testTaskletExecuteReturnNull() throws Exception { step.setTasklet(new Tasklet() { @Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { return null; } }); JobExecution jobExecutionContext = new JobExecution(jobInstance, jobParameters); StepExecution stepExecution = new StepExecution(step.getName(), jobExecutionContext); step.execute(stepExecution); assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus()); }
@Test public void testStepToCompletion() throws Exception { RepeatTemplate template = new RepeatTemplate(); // process all items: template.setCompletionPolicy(new DefaultResultCompletionPolicy()); step.setStepOperations(template); JobExecution jobExecutionContext = new JobExecution(jobInstance, jobParameters); StepExecution stepExecution = new StepExecution(step.getName(), jobExecutionContext); step.execute(stepExecution); assertEquals(3, processed.size()); assertEquals(3, stepExecution.getReadCount()); }
@Test public void testInterruptedWithCustomStatus() throws Exception { taskletStep.setTasklet(new Tasklet() { @Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { contribution.setExitStatus(new ExitStatus("FUNNY")); throw new JobInterruptedException("Planned"); } }); taskletStep.execute(stepExecution); assertEquals(STOPPED, stepExecution.getStatus()); assertEquals("FUNNY", stepExecution.getExitStatus().getExitCode()); }
@Test public void testCommitCount_Even() throws Exception { JobExecution jobExecutionContext = new JobExecution(jobInstance, jobParameters); step = getStep(new String[] { "foo", "bar", "spam", "eggs" }, 2); step.setTransactionManager(transactionManager); StepExecution stepExecution = new StepExecution(step.getName(), jobExecutionContext); step.execute(stepExecution); assertEquals(4, processed.size()); assertEquals(4, stepExecution.getReadCount()); assertEquals(4, stepExecution.getWriteCount()); assertEquals(3, stepExecution.getCommitCount()); //the empty chunk is the 3rd commit }
@Test public void testCommitCount_Uneven() throws Exception { JobExecution jobExecutionContext = new JobExecution(jobInstance, jobParameters); step = getStep(new String[] { "foo", "bar", "spam" }, 2); step.setTransactionManager(transactionManager); StepExecution stepExecution = new StepExecution(step.getName(), jobExecutionContext); step.execute(stepExecution); assertEquals(3, processed.size()); assertEquals(3, stepExecution.getReadCount()); assertEquals(3, stepExecution.getWriteCount()); assertEquals(2, stepExecution.getCommitCount()); }
@Test public void testInterrupted() throws Exception { taskletStep.setStepExecutionListeners(new StepExecutionListener[] { new InterruptionListener() }); taskletStep.execute(stepExecution); assertEquals(STOPPED, stepExecution.getStatus()); assertEquals(STOPPED.toString(), stepExecution.getExitStatus().getExitCode()); }
/** * Failure to update StepExecution after chunk commit is fatal. */ @Test public void testStepExecutionUpdateFailure() throws Exception { JobExecution jobExecution = new JobExecution(jobInstance, jobParameters); StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); JobRepository repository = new JobRepositoryFailedUpdateStub(); step.setJobRepository(repository); step.afterPropertiesSet(); step.execute(stepExecution); assertEquals(BatchStatus.UNKNOWN, stepExecution.getStatus()); }
@Test public void testNoSaveExecutionAttributesRestartableJob() { MockRestartableItemReader tasklet = new MockRestartableItemReader(); step.setTasklet(new TestingChunkOrientedTasklet<>(tasklet, itemWriter)); JobExecution jobExecutionContext = new JobExecution(jobInstance, jobParameters); StepExecution stepExecution = new StepExecution(step.getName(), jobExecutionContext); try { step.execute(stepExecution); } catch (Throwable t) { fail(); } assertFalse(tasklet.isRestoreFromCalled()); }
@Test public void testRepository() throws Exception { SimpleJobRepository repository = new SimpleJobRepository(new MapJobInstanceDao(), new MapJobExecutionDao(), new MapStepExecutionDao(), new MapExecutionContextDao()); step.setJobRepository(repository); JobExecution jobExecution = repository.createJobExecution(job.getName(), jobParameters); StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); repository.add(stepExecution); step.execute(stepExecution); assertEquals(1, processed.size()); }
@Test public void testDirectlyInjectedItemStream() throws Exception { step.setStreams(new ItemStream[] { new ItemStreamSupport() { @Override public void update(ExecutionContext executionContext) { super.update(executionContext); executionContext.putString("foo", "bar"); } } }); JobExecution jobExecution = new JobExecution(jobInstance, jobParameters); StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); assertEquals(false, stepExecution.getExecutionContext().containsKey("foo")); step.execute(stepExecution); assertEquals("bar", stepExecution.getExecutionContext().getString("foo")); }
@Test public void testNonRestartedJob() throws Exception { MockRestartableItemReader tasklet = new MockRestartableItemReader(); step.setTasklet(new TestingChunkOrientedTasklet<>(tasklet, itemWriter)); step.registerStream(tasklet); JobExecution jobExecutionContext = new JobExecution(jobInstance, jobParameters); StepExecution stepExecution = new StepExecution(step.getName(), jobExecutionContext); step.execute(stepExecution); assertFalse(tasklet.isRestoreFromCalled()); assertTrue(tasklet.isGetExecutionAttributesCalled()); }
@Test public void test() throws Exception { JobRepository jobRepository = new MapJobRepositoryFactoryBean().getObject(); StepExecution execution = jobRepository.createJobExecution("foo", new JobParameters()).createStepExecution( "step"); jobRepository.add(execution); PlatformTransactionManager transactionManager = new ResourcelessTransactionManager(); TaskletStepBuilder builder = new StepBuilder("step").repository(jobRepository) .transactionManager(transactionManager).tasklet((contribution, chunkContext) -> null); builder.build().execute(execution); assertEquals(BatchStatus.COMPLETED, execution.getStatus()); }