This commit adds a test case for building a flow
job with a job-scoped step.

Issue #857
This commit is contained in:
Mahmoud Ben Hassine
2020-10-27 14:40:39 +01:00
parent 0c500794f6
commit 791cfd5e8a

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,6 +15,8 @@
*/
package org.springframework.batch.core.job.builder;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
@@ -25,19 +27,33 @@ import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInterruptedException;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.UnexpectedJobExecutionException;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.JobScope;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.core.job.flow.FlowExecutionStatus;
import org.springframework.batch.core.job.flow.JobExecutionDecider;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
import org.springframework.batch.core.step.StepSupport;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.lang.Nullable;
/**
* @author Dave Syer
* @author Mahmoud Ben Hassine
*
*/
public class FlowJobBuilderTests {
@@ -238,4 +254,45 @@ public class FlowJobBuilderTests {
assertEquals("step2", execution.getStepExecutions().iterator().next().getStepName());
}
@Test
public void testBuildWithJobScopedStep() throws Exception {
// given
ApplicationContext context = new AnnotationConfigApplicationContext(JobConfiguration.class);
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
Job job = context.getBean(Job.class);
JobParameters jobParameters = new JobParametersBuilder()
.addLong("chunkSize", 2L)
.toJobParameters();
// when
JobExecution jobExecution = jobLauncher.run(job, jobParameters);
// then
assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
}
@EnableBatchProcessing
@Configuration
static class JobConfiguration {
@Bean
@JobScope
public Step step(StepBuilderFactory stepBuilderFactory,
@Value("#{jobParameters['chunkSize']}") Integer chunkSize) {
return stepBuilderFactory.get("step")
.<Integer, Integer>chunk(chunkSize)
.reader(new ListItemReader<>(Arrays.asList(1, 2, 3, 4)))
.writer(items -> {})
.build();
}
@Bean
public Job job(JobBuilderFactory jobBuilderFactory) {
return jobBuilderFactory.get("job")
.flow(step(null, null))
.build()
.build();
}
}
}