@Override public ExitStatus afterStep(StepExecution stepExecution) { if (stepExecution.getReadCount() == 0) { return ExitStatus.FAILED; } return null; }
@Override public void update(StepExecution stepExecution) { updateCount++; if (updateCount <= 3) { assertEquals(updateCount, stepExecution.getReadCount() + 1); } }
@Test public void testCommitIntervalJobParameter() throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext("/org/springframework/batch/core/scope/context/CommitIntervalJobParameter-context.xml"); Job job = context.getBean(Job.class); JobLauncher launcher = context.getBean(JobLauncher.class); JobExecution execution = launcher.run(job, new JobParametersBuilder().addLong("commit.interval", 1l).toJobParameters()); assertEquals(BatchStatus.COMPLETED, execution.getStatus()); assertEquals(2, execution.getStepExecutions().iterator().next().getReadCount()); assertEquals(2, execution.getStepExecutions().iterator().next().getWriteCount()); }
@Test public void testLaunchJob() throws Exception { int before = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STAGING"); JobExecution execution = jobLauncherTestUtils.launchJob(); int after = JdbcTestUtils.countRowsInTable(jdbcTemplate, "BATCH_STAGING"); assertEquals(BatchStatus.COMPLETED, execution.getStatus()); assertEquals(after - before, execution.getStepExecutions().iterator().next().getReadCount()); }
@Test public void testFailedStep() throws Exception { JobExecution jobExecution = jobLauncher.run(job, new JobParameters(Collections.singletonMap("item.three", new JobParameter("fail")))); assertEquals(BatchStatus.FAILED, jobExecution.getStatus()); StepExecution stepExecution = jobExecution.getStepExecutions().iterator().next(); assertEquals(9, stepExecution.getReadCount()); // In principle the write count could be more than 2 and less than 9... assertEquals(7, stepExecution.getWriteCount()); }
@Test public void testSunnyDayFaultTolerant() throws Exception { JobExecution jobExecution = jobLauncher.run(job, new JobParameters(Collections.singletonMap("item.three", new JobParameter("3")))); assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); StepExecution stepExecution = jobExecution.getStepExecutions().iterator().next(); assertEquals(9, stepExecution.getReadCount()); assertEquals(9, stepExecution.getWriteCount()); }
@Test public void testFailedStepOnError() throws Exception { JobExecution jobExecution = jobLauncher.run(job, new JobParameters(Collections.singletonMap("item.three", new JobParameter("error")))); assertEquals(BatchStatus.FAILED, jobExecution.getStatus()); StepExecution stepExecution = jobExecution.getStepExecutions().iterator().next(); assertEquals(9, stepExecution.getReadCount()); // In principle the write count could be more than 2 and less than 9... assertEquals(7, stepExecution.getWriteCount()); }
@Test public void testSunnyDayFaultTolerant() throws Exception { JobExecution jobExecution = jobLauncher.run(job, new JobParameters(Collections.singletonMap("item.three", new JobParameter("3")))); assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); StepExecution stepExecution = jobExecution.getStepExecutions().iterator().next(); assertEquals(9, stepExecution.getReadCount()); assertEquals(9, stepExecution.getWriteCount()); }
@Test public void testFailedStep() throws Exception { JobExecution jobExecution = jobLauncher.run(job, new JobParameters(Collections.singletonMap("item.three", new JobParameter("unsupported")))); assertEquals(BatchStatus.FAILED, jobExecution.getStatus()); StepExecution stepExecution = jobExecution.getStepExecutions().iterator().next(); assertEquals(9, stepExecution.getReadCount()); // In principle the write count could be more than 2 and less than 9... assertEquals(7, stepExecution.getWriteCount()); }
@Test public void testSunnyDaySimpleStep() throws Exception { JobExecution jobExecution = jobLauncher.run(job, new JobParameters(Collections.singletonMap("item.three", new JobParameter("3")))); assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); StepExecution stepExecution = jobExecution.getStepExecutions().iterator().next(); assertEquals(9, stepExecution.getReadCount()); assertEquals(9, stepExecution.getWriteCount()); }
@Test public void testVanillaIteration() throws Exception { factory.setItemReader(new ListItemReader<>(Arrays.asList(StringUtils .commaDelimitedListToStringArray("1,2,3,4,5,6")))); Step step = factory.getObject(); StepExecution stepExecution = getStepExecution(step); step.execute(stepExecution); waitForResults(6, 10); assertEquals(6, TestItemWriter.count); assertEquals(6, stepExecution.getReadCount()); }
@Test public void testFailedStepOnError() throws Exception { JobExecution jobExecution = jobLauncher.run(job, new JobParameters(Collections.singletonMap("item.three", new JobParameter("error")))); assertEquals(BatchStatus.FAILED, jobExecution.getStatus()); StepExecution stepExecution = jobExecution.getStepExecutions().iterator().next(); assertEquals(9, stepExecution.getReadCount()); // In principle the write count could be more than 2 and less than 9... assertEquals(7, stepExecution.getWriteCount()); }
@Test @DirtiesContext public void testSunnyDayFaultTolerant() throws Exception { JobExecution jobExecution = jobLauncher.run(job, new JobParameters(Collections.singletonMap("item.three", new JobParameter("3")))); assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); StepExecution stepExecution = jobExecution.getStepExecutions().iterator().next(); assertEquals(9, stepExecution.getReadCount()); assertEquals(9, stepExecution.getWriteCount()); }
@Test @DirtiesContext public void testFailedStep() throws Exception { JobExecution jobExecution = jobLauncher.run(job, new JobParameters(Collections.singletonMap("item.three", new JobParameter("unsupported")))); assertEquals(BatchStatus.FAILED, jobExecution.getStatus()); StepExecution stepExecution = jobExecution.getStepExecutions().iterator().next(); assertEquals(9, stepExecution.getReadCount()); // In principle the write count could be more than 2 and less than 9... assertEquals(7, stepExecution.getWriteCount()); }
@Test @DirtiesContext public void testFailedStepOnError() throws Exception { JobExecution jobExecution = jobLauncher.run(job, new JobParameters(Collections.singletonMap("item.three", new JobParameter("error")))); assertEquals(BatchStatus.FAILED, jobExecution.getStatus()); StepExecution stepExecution = jobExecution.getStepExecutions().iterator().next(); assertEquals(9, stepExecution.getReadCount()); // In principle the write count could be more than 2 and less than 9... assertEquals(7, stepExecution.getWriteCount()); }
@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 testSkipsInWriter() throws Exception { JobExecution jobExecution = jobLauncher.run(job, new JobParametersBuilder().addString("item.three", "fail") .addLong("run.id", 1L).toJobParameters()); assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); StepExecution stepExecution = jobExecution.getStepExecutions().iterator().next(); assertEquals(9, stepExecution.getReadCount()); assertEquals(7, stepExecution.getWriteCount()); // The whole chunk gets skipped... assertEquals(2, stepExecution.getWriteSkipCount()); } }
@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 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()); }