Added checkstyle

This commit is contained in:
Marcin Grzejszczak
2019-02-03 19:27:07 +01:00
parent 4d0acf120c
commit 60f1e21d03
249 changed files with 7450 additions and 5857 deletions

View File

@@ -1,17 +1,17 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2015-2019 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.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.configuration;
@@ -35,4 +35,5 @@ import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
@Documented
@ImportAutoConfiguration
public @interface TaskBatchTest {
}

View File

@@ -1,17 +1,17 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2015-2019 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.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.configuration;
@@ -33,33 +33,33 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class TaskJobLauncherAutoConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner().
withUserConfiguration(TaskBatchExecutionListenerTests.JobConfiguration.class,
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withUserConfiguration(TaskBatchExecutionListenerTests.JobConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
EmbeddedDataSourceConfiguration.class,
BatchAutoConfiguration.class,
EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class,
TaskJobLauncherAutoConfiguration.class);
@Test
public void testAutoBuiltDataSourceWithTaskJobLauncherCLR() {
this.contextRunner.withPropertyValues("spring.cloud.task.batch.fail-on-job-failure=true").run(context -> {
assertThat(context).hasSingleBean(TaskJobLauncherCommandLineRunner.class);
assertThat(context.getBean(TaskJobLauncherCommandLineRunner.class)
.getOrder())
.isEqualTo(0);
});
this.contextRunner
.withPropertyValues("spring.cloud.task.batch.fail-on-job-failure=true")
.run(context -> {
assertThat(context)
.hasSingleBean(TaskJobLauncherCommandLineRunner.class);
assertThat(context.getBean(TaskJobLauncherCommandLineRunner.class)
.getOrder()).isEqualTo(0);
});
}
@Test
public void testAutoBuiltDataSourceWithTaskJobLauncherCLROrder() {
this.contextRunner.
withPropertyValues("spring.cloud.task.batch.fail-on-job-failure=true",
"spring.cloud.task.batch.commandLineRunnerOrder=100").
run(context -> {
this.contextRunner
.withPropertyValues("spring.cloud.task.batch.fail-on-job-failure=true",
"spring.cloud.task.batch.commandLineRunnerOrder=100")
.run(context -> {
assertThat(context.getBean(TaskJobLauncherCommandLineRunner.class)
.getOrder())
.isEqualTo(100);
});
.getOrder()).isEqualTo(100);
});
}
@Test
@@ -69,5 +69,5 @@ public class TaskJobLauncherAutoConfigurationTests {
assertThat(context).doesNotHaveBean(TaskJobLauncherCommandLineRunner.class);
});
}
}
}

View File

@@ -1,23 +1,21 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2015-2019 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.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.handler;
import org.assertj.core.api.AssertionsForClassTypes;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.function.Executable;
@@ -55,13 +53,14 @@ import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.PlatformTransactionManager;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* @author Glenn Renfro
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {TaskJobLauncherCommandLineRunnerCoreTests.BatchConfiguration.class})
@ContextConfiguration(classes = {
TaskJobLauncherCommandLineRunnerCoreTests.BatchConfiguration.class })
public class TaskJobLauncherCommandLineRunnerCoreTests {
@Autowired
@@ -93,11 +92,11 @@ public class TaskJobLauncherCommandLineRunnerCoreTests {
Tasklet tasklet = (contribution, chunkContext) -> RepeatStatus.FINISHED;
this.step = this.steps.get("step").tasklet(tasklet).build();
this.job = this.jobs.get("job").start(this.step).build();
this.runner = new TaskJobLauncherCommandLineRunner(this.jobLauncher, this.jobExplorer, jobRepository, new TaskBatchProperties());
this.runner = new TaskJobLauncherCommandLineRunner(this.jobLauncher,
this.jobExplorer, this.jobRepository, new TaskBatchProperties());
}
@DirtiesContext
@Test
public void basicExecution() throws Exception {
@@ -146,7 +145,6 @@ public class TaskJobLauncherCommandLineRunnerCoreTests {
assertThat(this.jobExplorer.getJobInstances("job", 0, 100)).hasSize(2);
}
@DirtiesContext
@Test
public void retryFailedExecutionOnNonRestartableJob() throws Exception {
@@ -162,9 +160,9 @@ public class TaskJobLauncherCommandLineRunnerCoreTests {
// try to re-run a failed execution
Executable executable = () -> this.runner.execute(this.job,
new JobParametersBuilder().addLong("run.id", 1L).toJobParameters());
Throwable exception = assertThrows(JobRestartException.class, executable);
AssertionsForClassTypes.assertThat(exception.getMessage())
.isEqualTo("JobInstance already exists and is not restartable");
assertThatExceptionOfType(JobRestartException.class)
.isThrownBy(executable::execute)
.withMessage("JobInstance already exists and is not restartable");
}
@DirtiesContext
@@ -177,12 +175,11 @@ public class TaskJobLauncherCommandLineRunnerCoreTests {
.addLong("foo", 2L, false).toJobParameters();
runFailedJob(jobParameters);
assertThat(this.jobExplorer.getJobInstances("job", 0, 100)).hasSize(1);
runFailedJob(new JobParametersBuilder(jobParameters)
.addLong("run.id", 1L).toJobParameters());
runFailedJob(new JobParametersBuilder(jobParameters).addLong("run.id", 1L)
.toJobParameters());
assertThat(this.jobExplorer.getJobInstances("job", 0, 100)).hasSize(1);
}
@DirtiesContext
@Test
public void retryFailedExecutionWithDifferentNonIdentifyingParametersFromPreviousExecution()
@@ -195,7 +192,7 @@ public class TaskJobLauncherCommandLineRunnerCoreTests {
runFailedJob(jobParameters);
assertThat(this.jobExplorer.getJobInstances("job", 0, 100)).hasSize(1);
// try to re-run a failed execution with non identifying parameters
runFailedJob( new JobParametersBuilder().addLong("run.id", 1L)
runFailedJob(new JobParametersBuilder().addLong("run.id", 1L)
.addLong("id", 2L, false).addLong("foo", 3L, false).toJobParameters());
assertThat(this.jobExplorer.getJobInstances("job", 0, 100)).hasSize(1);
JobInstance jobInstance = this.jobExplorer.getJobInstance(0L);
@@ -216,7 +213,6 @@ public class TaskJobLauncherCommandLineRunnerCoreTests {
assertThat(parameters.getLong("foo")).isEqualTo(3L);
}
private Tasklet throwingTasklet() {
return (contribution, chunkContext) -> {
throw new RuntimeException("Planned");
@@ -238,13 +234,11 @@ public class TaskJobLauncherCommandLineRunnerCoreTests {
@EnableBatchProcessing
protected static class BatchConfiguration implements BatchConfigurer {
private ResourcelessTransactionManager transactionManager =
new ResourcelessTransactionManager();
private ResourcelessTransactionManager transactionManager = new ResourcelessTransactionManager();
private JobRepository jobRepository;
private MapJobRepositoryFactoryBean jobRepositoryFactory =
new MapJobRepositoryFactoryBean(
private MapJobRepositoryFactoryBean jobRepositoryFactory = new MapJobRepositoryFactoryBean(
this.transactionManager);
public BatchConfiguration() throws Exception {
@@ -275,4 +269,5 @@ public class TaskJobLauncherCommandLineRunnerCoreTests {
}
}
}

View File

@@ -1,17 +1,17 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2015-2019 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.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.handler;
@@ -20,6 +20,7 @@ import java.util.Set;
import javax.sql.DataSource;
import org.assertj.core.api.Condition;
import org.junit.After;
import org.junit.Test;
import org.junit.jupiter.api.function.Executable;
@@ -59,18 +60,18 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* @author Glenn Renfro
*/
public class TaskJobLauncherCommandLineRunnerTests {
private ConfigurableApplicationContext applicationContext;
private static final String DEFAULT_ERROR_MESSAGE = "The following Jobs have failed: \n"
+ "Job jobA failed during execution for job instance id 1 with jobExecutionId of 1 \n";
private static final String DEFAULT_ERROR_MESSAGE = "The following Jobs have failed: \n" +
"Job jobA failed during execution for job instance id 1 with jobExecutionId of 1 \n";
private ConfigurableApplicationContext applicationContext;
@After
public void tearDown() {
@@ -82,20 +83,22 @@ public class TaskJobLauncherCommandLineRunnerTests {
@Test
public void testTaskJobLauncherCLRSuccessFail() {
String[] enabledArgs = new String[] {
"--spring.cloud.task.batch.failOnJobFailure=true"};
validateForFail(DEFAULT_ERROR_MESSAGE, TaskJobLauncherCommandLineRunnerTests.JobWithFailureConfiguration.class,
"--spring.cloud.task.batch.failOnJobFailure=true" };
validateForFail(DEFAULT_ERROR_MESSAGE,
TaskJobLauncherCommandLineRunnerTests.JobWithFailureConfiguration.class,
enabledArgs);
}
/**
* Verifies that the task will return an exit code other than zero if the
* job fails with the deprecated EnableTask annotation.
* Verifies that the task will return an exit code other than zero if the job fails
* with the deprecated EnableTask annotation.
*/
@Test
public void testTaskJobLauncherCLRSuccessFailWithAnnotation() {
String[] enabledArgs = new String[] {
"--spring.cloud.task.batch.failOnJobFailure=true"};
validateForFail(DEFAULT_ERROR_MESSAGE, TaskJobLauncherCommandLineRunnerTests.JobWithFailureAnnotatedConfiguration.class,
"--spring.cloud.task.batch.failOnJobFailure=true" };
validateForFail(DEFAULT_ERROR_MESSAGE,
TaskJobLauncherCommandLineRunnerTests.JobWithFailureAnnotatedConfiguration.class,
enabledArgs);
}
@@ -103,8 +106,9 @@ public class TaskJobLauncherCommandLineRunnerTests {
public void testTaskJobLauncherCLRSuccessFailWithTaskExecutor() {
String[] enabledArgs = new String[] {
"--spring.cloud.task.batch.failOnJobFailure=true",
"--spring.cloud.task.batch.failOnJobFailurePollInterval=500"};
validateForFail(DEFAULT_ERROR_MESSAGE, TaskJobLauncherCommandLineRunnerTests.JobWithFailureTaskExecutorConfiguration.class,
"--spring.cloud.task.batch.failOnJobFailurePollInterval=500" };
validateForFail(DEFAULT_ERROR_MESSAGE,
TaskJobLauncherCommandLineRunnerTests.JobWithFailureTaskExecutorConfiguration.class,
enabledArgs);
}
@@ -112,11 +116,12 @@ public class TaskJobLauncherCommandLineRunnerTests {
public void testTaskJobLauncherPickOneJob() {
String[] enabledArgs = new String[] {
"--spring.cloud.task.batch.fail-on-job-failure=true",
"--spring.cloud.task.batch.jobNames=jobSucceed"};
"--spring.cloud.task.batch.jobNames=jobSucceed" };
boolean isExceptionThrown = false;
try {
this.applicationContext = SpringApplication
.run(new Class[] { TaskJobLauncherCommandLineRunnerTests.JobWithFailureConfiguration.class }, enabledArgs);
this.applicationContext = SpringApplication.run(new Class[] {
TaskJobLauncherCommandLineRunnerTests.JobWithFailureConfiguration.class },
enabledArgs);
}
catch (IllegalStateException exception) {
isExceptionThrown = true;
@@ -128,40 +133,51 @@ public class TaskJobLauncherCommandLineRunnerTests {
@Test
public void testCommandLineRunnerSetToFalse() {
String[] enabledArgs = new String[] {};
this.applicationContext = SpringApplication
.run(new Class[] { TaskJobLauncherCommandLineRunnerTests.JobConfiguration.class }, enabledArgs);
this.applicationContext = SpringApplication.run(
new Class[] {
TaskJobLauncherCommandLineRunnerTests.JobConfiguration.class },
enabledArgs);
validateContext();
assertThat(applicationContext.getBean(JobLauncherCommandLineRunner.class)).isNotNull();
assertThat(this.applicationContext.getBean(JobLauncherCommandLineRunner.class))
.isNotNull();
Executable executable = () -> applicationContext.getBean(TaskJobLauncherCommandLineRunner.class);
Executable executable = () -> this.applicationContext
.getBean(TaskJobLauncherCommandLineRunner.class);
Throwable exception = assertThrows(NoSuchBeanDefinitionException.class, executable);
assertThat(exception.getMessage()).isEqualTo("No qualifying bean of type " +
"'org.springframework.cloud.task.batch.handler.TaskJobLauncherCommandLineRunner' available");
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
.isThrownBy(executable::execute).withMessage("No qualifying bean of type "
+ "'org.springframework.cloud.task.batch.handler.TaskJobLauncherCommandLineRunner' available");
validateContext();
}
private void validateContext() {
TaskExplorer taskExplorer = this.applicationContext.getBean(TaskExplorer.class);
Page<TaskExecution> page = taskExplorer.findTaskExecutionsByName("application", PageRequest.of(0, 1));
Page<TaskExecution> page = taskExplorer.findTaskExecutionsByName("application",
PageRequest.of(0, 1));
Set<Long> jobExecutionIds = taskExplorer
.getJobExecutionIdsByTaskExecutionId(page.iterator().next().getExecutionId());
Set<Long> jobExecutionIds = taskExplorer.getJobExecutionIdsByTaskExecutionId(
page.iterator().next().getExecutionId());
assertThat(jobExecutionIds.size()).isEqualTo(1);
assertThat(taskExplorer.getTaskExecution(jobExecutionIds.iterator().next()).getExecutionId()).isEqualTo(1);
assertThat(taskExplorer.getTaskExecution(jobExecutionIds.iterator().next())
.getExecutionId()).isEqualTo(1);
}
private void validateForFail(String errorMessage, Class clazz, String [] enabledArgs) {
Executable executable = () -> this.applicationContext = SpringApplication
.run(new Class[] { clazz,PropertyPlaceholderAutoConfiguration.class}, enabledArgs);
private void validateForFail(String errorMessage, Class clazz, String[] enabledArgs) {
Executable executable = () -> this.applicationContext = SpringApplication.run(
new Class[] { clazz, PropertyPlaceholderAutoConfiguration.class },
enabledArgs);
Throwable exception = assertThrows(IllegalStateException.class, executable);
assertThat(exception.getCause().getMessage()).isEqualTo(errorMessage);
assertThatExceptionOfType(IllegalStateException.class)
.isThrownBy(executable::execute).has(new Condition<Throwable>() {
@Override
public boolean matches(Throwable value) {
return errorMessage.equals(value.getCause().getMessage());
}
});
}
@EnableBatchProcessing
@TaskBatchTest
@Import(EmbeddedDataSourceConfiguration.class)
@@ -176,25 +192,23 @@ public class TaskJobLauncherCommandLineRunnerTests {
@Bean
public Job job() {
return jobBuilderFactory.get("job")
.start(stepBuilderFactory.get("step1").tasklet(new Tasklet() {
return this.jobBuilderFactory.get("job")
.start(this.stepBuilderFactory.get("step1").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
public RepeatStatus execute(StepContribution contribution,
ChunkContext chunkContext) {
System.out.println("Executed");
return RepeatStatus.FINISHED;
}
}).build())
.build();
}).build()).build();
}
}
@EnableBatchProcessing
@ImportAutoConfiguration({
PropertyPlaceholderAutoConfiguration.class,
BatchAutoConfiguration.class,
TaskBatchAutoConfiguration.class,
TaskJobLauncherAutoConfiguration.class,
SingleTaskConfiguration.class,
@ImportAutoConfiguration({ PropertyPlaceholderAutoConfiguration.class,
BatchAutoConfiguration.class, TaskBatchAutoConfiguration.class,
TaskJobLauncherAutoConfiguration.class, SingleTaskConfiguration.class,
SimpleTaskAutoConfiguration.class })
@Import(EmbeddedDataSourceConfiguration.class)
@EnableTask
@@ -208,50 +222,54 @@ public class TaskJobLauncherCommandLineRunnerTests {
@Bean
public Job jobFail() {
return jobBuilderFactory.get("jobA")
.start(stepBuilderFactory.get("step1").tasklet(new Tasklet() {
return this.jobBuilderFactory.get("jobA")
.start(this.stepBuilderFactory.get("step1").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution,
ChunkContext chunkContext)
throws Exception {
ChunkContext chunkContext) throws Exception {
System.out.println("Executed");
throw new IllegalStateException("WHOOPS");
throw new IllegalStateException("WHOOPS");
}
}).build())
.build();
}).build()).build();
}
@Bean
public Job jobFun() {
return jobBuilderFactory.get("jobSucceed")
.start(stepBuilderFactory.get("step1Succeed").tasklet(new Tasklet() {
return this.jobBuilderFactory.get("jobSucceed").start(
this.stepBuilderFactory.get("step1Succeed").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution,
ChunkContext chunkContext) {
System.out.println("Executed");
return RepeatStatus.FINISHED;
}
}).build())
.build();
}).build()).build();
}
}
@EnableTask
public static class JobWithFailureAnnotatedConfiguration extends JobWithFailureConfiguration{
public static class JobWithFailureAnnotatedConfiguration
extends JobWithFailureConfiguration {
}
public static class JobWithFailureTaskExecutorConfiguration extends JobWithFailureConfiguration{
public static class JobWithFailureTaskExecutorConfiguration
extends JobWithFailureConfiguration {
@Bean
public BatchConfigurer batchConfigurer(DataSource dataSource) {
return new TestBatchConfigurer(dataSource);
}
}
private static class TestBatchConfigurer extends DefaultBatchConfigurer{
public TestBatchConfigurer(DataSource dataSource) {
private static class TestBatchConfigurer extends DefaultBatchConfigurer {
TestBatchConfigurer(DataSource dataSource) {
super(dataSource);
}
protected JobLauncher createJobLauncher() throws Exception {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(getJobRepository());
@@ -259,5 +277,7 @@ public class TaskJobLauncherCommandLineRunnerTests {
jobLauncher.afterPropertiesSet();
return jobLauncher;
}
}
}

View File

@@ -1,22 +1,23 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2015-2019 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.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.listener;
import java.util.Set;
import javax.sql.DataSource;
import org.junit.After;
@@ -56,8 +57,8 @@ public class PrefixTests {
@Test
public void testPrefix() {
this.applicationContext = SpringApplication.run(
JobConfiguration.class, "--spring.cloud.task.tablePrefix=FOO_");
this.applicationContext = SpringApplication.run(JobConfiguration.class,
"--spring.cloud.task.tablePrefix=FOO_");
TaskExplorer taskExplorer = this.applicationContext.getBean(TaskExplorer.class);
@@ -80,22 +81,19 @@ public class PrefixTests {
@Bean
public Job job() {
return jobBuilderFactory.get("job")
.start(stepBuilderFactory.get("step1")
.tasklet((contribution, chunkContext) -> {
return this.jobBuilderFactory.get("job").start(this.stepBuilderFactory
.get("step1").tasklet((contribution, chunkContext) -> {
System.out.println("Executed");
return RepeatStatus.FINISHED;
}).build())
.build();
}).build()).build();
}
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.addScript("classpath:schema-h2.sql")
.setType(EmbeddedDatabaseType.H2)
.build();
return new EmbeddedDatabaseBuilder().addScript("classpath:schema-h2.sql")
.setType(EmbeddedDatabaseType.H2).build();
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 the original author or authors.
* Copyright 2015-2019 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.listener;
import java.util.ArrayList;
@@ -61,7 +62,7 @@ import org.springframework.data.domain.PageRequest;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Michael Minella
@@ -75,15 +76,14 @@ public class TaskBatchExecutionListenerTests {
@After
public void tearDown() {
if(this.applicationContext != null && this.applicationContext.isActive()) {
if (this.applicationContext != null && this.applicationContext.isActive()) {
this.applicationContext.close();
}
}
@Test
public void testAutobuiltDataSource() {
this.applicationContext = SpringApplication.run(JobConfiguration.class ,
ARGS);
this.applicationContext = SpringApplication.run(JobConfiguration.class, ARGS);
validateContext();
}
@@ -96,28 +96,28 @@ public class TaskBatchExecutionListenerTests {
@Test(expected = AssertionError.class)
public void testNoAutoConfigurationEnable() {
this.applicationContext = SpringApplication.run(JobConfiguration.class ,
this.applicationContext = SpringApplication.run(JobConfiguration.class,
"--spring.cloud.task.batch.listener.enable=false");
validateContext();
}
@Test(expected = AssertionError.class)
public void testNoAutoConfigurationBothDisabled() {
this.applicationContext = SpringApplication.run(JobConfiguration.class ,
this.applicationContext = SpringApplication.run(JobConfiguration.class,
"--spring.cloud.task.batch.listener.enable=false --spring.cloud.task.batch.listener.enabled=false");
validateContext();
}
@Test
public void testAutoConfigurationEnable() {
this.applicationContext = SpringApplication.run(JobConfiguration.class ,
this.applicationContext = SpringApplication.run(JobConfiguration.class,
"--spring.cloud.task.batch.listener.enable=true");
validateContext();
}
@Test
public void testAutoConfigurationEnabled() {
this.applicationContext = SpringApplication.run(JobConfiguration.class ,
this.applicationContext = SpringApplication.run(JobConfiguration.class,
"--spring.cloud.task.batch.listener.enabled=true");
validateContext();
}
@@ -132,26 +132,34 @@ public class TaskBatchExecutionListenerTests {
private void validateContext() {
TaskExplorer taskExplorer = this.applicationContext.getBean(TaskExplorer.class);
Page<TaskExecution> page = taskExplorer.findTaskExecutionsByName("application", PageRequest.of(0, 1));
Page<TaskExecution> page = taskExplorer.findTaskExecutionsByName("application",
PageRequest.of(0, 1));
Set<Long> jobExecutionIds = taskExplorer.getJobExecutionIdsByTaskExecutionId(page.iterator().next().getExecutionId());
Set<Long> jobExecutionIds = taskExplorer.getJobExecutionIdsByTaskExecutionId(
page.iterator().next().getExecutionId());
assertEquals(1, jobExecutionIds.size());
assertEquals(1, taskExplorer.getTaskExecution(jobExecutionIds.iterator().next()).getExecutionId());
assertThat(jobExecutionIds.size()).isEqualTo(1);
assertThat(taskExplorer.getTaskExecution(jobExecutionIds.iterator().next())
.getExecutionId()).isEqualTo(1);
}
@Test
public void testMultipleDataSources() {
this.applicationContext = SpringApplication.run(JobConfigurationMultipleDataSources.class, ARGS);
this.applicationContext = SpringApplication
.run(JobConfigurationMultipleDataSources.class, ARGS);
TaskExplorer taskExplorer = this.applicationContext.getBean(TaskExplorer.class);
Page<TaskExecution> page = taskExplorer.findTaskExecutionsByName("application", PageRequest.of(0, 1));
Page<TaskExecution> page = taskExplorer.findTaskExecutionsByName("application",
PageRequest.of(0, 1));
Set<Long> jobExecutionIds = taskExplorer.getJobExecutionIdsByTaskExecutionId(page.iterator().next().getExecutionId());
Set<Long> jobExecutionIds = taskExplorer.getJobExecutionIdsByTaskExecutionId(
page.iterator().next().getExecutionId());
assertEquals(1, jobExecutionIds.size());
assertEquals(1, taskExplorer.getTaskExecution(jobExecutionIds.iterator().next()).getExecutionId());
assertThat(jobExecutionIds.size()).isEqualTo(1);
assertThat(taskExplorer.getTaskExecution(jobExecutionIds.iterator().next())
.getExecutionId()).isEqualTo(1);
}
@Test
@@ -160,11 +168,13 @@ public class TaskBatchExecutionListenerTests {
TaskExplorer taskExplorer = this.applicationContext.getBean(TaskExplorer.class);
Page<TaskExecution> page = taskExplorer.findTaskExecutionsByName("application", PageRequest.of(0, 1));
Page<TaskExecution> page = taskExplorer.findTaskExecutionsByName("application",
PageRequest.of(0, 1));
Set<Long> jobExecutionIds = taskExplorer.getJobExecutionIdsByTaskExecutionId(page.iterator().next().getExecutionId());
Set<Long> jobExecutionIds = taskExplorer.getJobExecutionIdsByTaskExecutionId(
page.iterator().next().getExecutionId());
assertEquals(0, jobExecutionIds.size());
assertThat(jobExecutionIds.size()).isEqualTo(0);
}
@Test
@@ -173,28 +183,39 @@ public class TaskBatchExecutionListenerTests {
TaskExplorer taskExplorer = this.applicationContext.getBean(TaskExplorer.class);
Page<TaskExecution> page = taskExplorer.findTaskExecutionsByName("application", PageRequest.of(0, 1));
Page<TaskExecution> page = taskExplorer.findTaskExecutionsByName("application",
PageRequest.of(0, 1));
Set<Long> jobExecutionIds = taskExplorer.getJobExecutionIdsByTaskExecutionId(page.iterator().next().getExecutionId());
Set<Long> jobExecutionIds = taskExplorer.getJobExecutionIdsByTaskExecutionId(
page.iterator().next().getExecutionId());
assertEquals(1, jobExecutionIds.size());
assertEquals(1, (long) taskExplorer.getTaskExecutionIdByJobExecutionId(jobExecutionIds.iterator().next()));
assertThat(jobExecutionIds.size()).isEqualTo(1);
assertThat((long) taskExplorer
.getTaskExecutionIdByJobExecutionId(jobExecutionIds.iterator().next()))
.isEqualTo(1);
}
@Test
public void testMultipleJobs() {
this.applicationContext = SpringApplication.run(MultipleJobConfiguration.class, ARGS);
this.applicationContext = SpringApplication.run(MultipleJobConfiguration.class,
ARGS);
TaskExplorer taskExplorer = this.applicationContext.getBean(TaskExplorer.class);
Page<TaskExecution> page = taskExplorer.findTaskExecutionsByName("application", PageRequest.of(0, 1));
Page<TaskExecution> page = taskExplorer.findTaskExecutionsByName("application",
PageRequest.of(0, 1));
Set<Long> jobExecutionIds = taskExplorer.getJobExecutionIdsByTaskExecutionId(page.iterator().next().getExecutionId());
Set<Long> jobExecutionIds = taskExplorer.getJobExecutionIdsByTaskExecutionId(
page.iterator().next().getExecutionId());
assertEquals(2, jobExecutionIds.size());
assertThat(jobExecutionIds.size()).isEqualTo(2);
Iterator<Long> jobExecutionIdsIterator = jobExecutionIds.iterator();
assertEquals(1, (long) taskExplorer.getTaskExecutionIdByJobExecutionId(jobExecutionIdsIterator.next()));
assertEquals(1, (long) taskExplorer.getTaskExecutionIdByJobExecutionId(jobExecutionIdsIterator.next()));
assertThat((long) taskExplorer
.getTaskExecutionIdByJobExecutionId(jobExecutionIdsIterator.next()))
.isEqualTo(1);
assertThat((long) taskExplorer
.getTaskExecutionIdByJobExecutionId(jobExecutionIdsIterator.next()))
.isEqualTo(1);
}
@Test
@@ -204,27 +225,26 @@ public class TaskBatchExecutionListenerTests {
jobNames.add("job2");
jobNames.add("TESTOBJECT");
TaskBatchExecutionListenerBeanPostProcessor beanPostProcessor =
beanPostProcessor(jobNames);
TaskBatchExecutionListenerBeanPostProcessor beanPostProcessor = beanPostProcessor(
jobNames);
SimpleJob testObject = new SimpleJob();
SimpleJob bean = (SimpleJob) beanPostProcessor.
postProcessBeforeInitialization(testObject,"TESTOBJECT");
assertEquals(testObject,bean);
SimpleJob bean = (SimpleJob) beanPostProcessor
.postProcessBeforeInitialization(testObject, "TESTOBJECT");
assertThat(bean).isEqualTo(testObject);
}
@Test
public void testBatchExecutionListenerBeanPostProcessorWithEmptyJobNames() {
TaskBatchExecutionListenerBeanPostProcessor beanPostProcessor =
beanPostProcessor(Collections.emptyList());
TaskBatchExecutionListenerBeanPostProcessor beanPostProcessor = beanPostProcessor(
Collections.emptyList());
SimpleJob testObject = new SimpleJob();
SimpleJob bean = (SimpleJob) beanPostProcessor.
postProcessBeforeInitialization(testObject,"TESTOBJECT");
assertEquals(testObject,bean);
SimpleJob bean = (SimpleJob) beanPostProcessor
.postProcessBeforeInitialization(testObject, "TESTOBJECT");
assertThat(bean).isEqualTo(testObject);
}
@Test(expected = IllegalArgumentException.class)
public void testBatchExecutionListenerBeanPostProcessorNullJobNames() {
beanPostProcessor(null);
@@ -232,16 +252,14 @@ public class TaskBatchExecutionListenerTests {
private TaskBatchExecutionListenerBeanPostProcessor beanPostProcessor(
List<String> jobNames) {
this.applicationContext = SpringApplication.run(new Class[] {JobConfiguration.class,
PropertyPlaceholderAutoConfiguration.class, EmbeddedDataSourceConfiguration.class,
BatchAutoConfiguration.class,
TaskBatchAutoConfiguration.class,
SimpleTaskAutoConfiguration.class,
this.applicationContext = SpringApplication.run(new Class[] {
JobConfiguration.class, PropertyPlaceholderAutoConfiguration.class,
EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class,
TaskBatchAutoConfiguration.class, SimpleTaskAutoConfiguration.class,
SingleTaskConfiguration.class }, ARGS);
TaskBatchExecutionListenerBeanPostProcessor beanPostProcessor =
this.applicationContext.getBean(
TaskBatchExecutionListenerBeanPostProcessor.class);
TaskBatchExecutionListenerBeanPostProcessor beanPostProcessor = this.applicationContext
.getBean(TaskBatchExecutionListenerBeanPostProcessor.class);
beanPostProcessor.setJobNames(jobNames);
return beanPostProcessor;
@@ -269,16 +287,17 @@ public class TaskBatchExecutionListenerTests {
@Bean
public Job job() {
return jobBuilderFactory.get("job")
.start(stepBuilderFactory.get("step1").tasklet(new Tasklet() {
return this.jobBuilderFactory.get("job")
.start(this.stepBuilderFactory.get("step1").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
public RepeatStatus execute(StepContribution contribution,
ChunkContext chunkContext) throws Exception {
System.out.println("Executed");
return RepeatStatus.FINISHED;
}
}).build())
.build();
}).build()).build();
}
}
@EnableBatchProcessing
@@ -298,14 +317,18 @@ public class TaskBatchExecutionListenerTests {
return new FactoryBean<Job>() {
@Override
public Job getObject() throws Exception {
return jobBuilderFactory.get("job")
.start(stepBuilderFactory.get("step1").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("Executed");
return RepeatStatus.FINISHED;
}
}).build())
return JobFactoryBeanConfiguration.this.jobBuilderFactory.get("job")
.start(JobFactoryBeanConfiguration.this.stepBuilderFactory
.get("step1").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(
StepContribution contribution,
ChunkContext chunkContext)
throws Exception {
System.out.println("Executed");
return RepeatStatus.FINISHED;
}
}).build())
.build();
}
@@ -320,6 +343,7 @@ public class TaskBatchExecutionListenerTests {
}
};
}
}
@EnableBatchProcessing
@@ -336,34 +360,31 @@ public class TaskBatchExecutionListenerTests {
@Bean
public Job job() {
return jobBuilderFactory.get("job")
.start(stepBuilderFactory.get("step1").tasklet(new Tasklet() {
return this.jobBuilderFactory.get("job")
.start(this.stepBuilderFactory.get("step1").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
public RepeatStatus execute(StepContribution contribution,
ChunkContext chunkContext) throws Exception {
System.out.println("Executed");
return RepeatStatus.FINISHED;
}
}).build())
.build();
}).build()).build();
}
@Bean
@Primary
public DataSource myDataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.setName("myDataSource");
.setType(EmbeddedDatabaseType.H2).setName("myDataSource");
return builder.build();
}
@Bean
public DataSource incorrectDataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.setName("incorrectDataSource");
.setType(EmbeddedDatabaseType.H2).setName("incorrectDataSource");
return builder.build();
}
@Bean
public TaskConfigurer taskConfigurer() {
@@ -374,6 +395,7 @@ public class TaskBatchExecutionListenerTests {
public DefaultBatchConfigurer batchConfigurer() {
return new DefaultBatchConfigurer(myDataSource());
}
}
@EnableBatchProcessing
@@ -390,28 +412,30 @@ public class TaskBatchExecutionListenerTests {
@Bean
public Job job1() {
return jobBuilderFactory.get("job1")
.start(stepBuilderFactory.get("job1step1").tasklet(new Tasklet() {
return this.jobBuilderFactory.get("job1").start(
this.stepBuilderFactory.get("job1step1").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
public RepeatStatus execute(StepContribution contribution,
ChunkContext chunkContext) throws Exception {
System.out.println("Executed job1");
return RepeatStatus.FINISHED;
}
}).build())
.build();
}).build()).build();
}
@Bean
public Job job2() {
return jobBuilderFactory.get("job2")
.start(stepBuilderFactory.get("job2step1").tasklet(new Tasklet() {
return this.jobBuilderFactory.get("job2").start(
this.stepBuilderFactory.get("job2step1").tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
public RepeatStatus execute(StepContribution contribution,
ChunkContext chunkContext) throws Exception {
System.out.println("Executed job2");
return RepeatStatus.FINISHED;
}
}).build())
.build();
}).build()).build();
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2015-2019 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.partition;
import java.util.ArrayList;
@@ -50,9 +51,7 @@ import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.mock.env.MockEnvironment;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
@@ -64,6 +63,9 @@ import static org.mockito.Mockito.when;
*/
public class DeployerPartitionHandlerTests {
@Captor
ArgumentCaptor<AppDeploymentRequest> appDeploymentRequestArgumentCaptor;
@Mock
private TaskLauncher taskLauncher;
@@ -81,8 +83,6 @@ public class DeployerPartitionHandlerTests {
private Environment environment;
@Captor ArgumentCaptor<AppDeploymentRequest> appDeploymentRequestArgumentCaptor;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
@@ -91,27 +91,33 @@ public class DeployerPartitionHandlerTests {
@Test
public void testConstructorValidation() {
validateConstructorValidation(null, null, null, null, "A taskLauncher is required");
validateConstructorValidation(this.taskLauncher, null, null, null, "A jobExplorer is required");
validateConstructorValidation(this.taskLauncher, this.jobExplorer, null, null, "A resource is required");
validateConstructorValidation(this.taskLauncher, this.jobExplorer, this.resource, null, "A step name is required");
validateConstructorValidation(null, null, null, null,
"A taskLauncher is required");
validateConstructorValidation(this.taskLauncher, null, null, null,
"A jobExplorer is required");
validateConstructorValidation(this.taskLauncher, this.jobExplorer, null, null,
"A resource is required");
validateConstructorValidation(this.taskLauncher, this.jobExplorer, this.resource,
null, "A step name is required");
new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step-name");
new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource,
"step-name");
}
@Test
public void testNoPartitions() throws Exception {
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step1");
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher,
this.jobExplorer, this.resource, "step1");
handler.setEnvironment(this.environment);
StepExecution stepExecution = new StepExecution("step1", new JobExecution(1L));
when(this.splitter.split(stepExecution, 1)).thenReturn(new HashSet<StepExecution>());
when(this.splitter.split(stepExecution, 1)).thenReturn(new HashSet<>());
Collection<StepExecution> results = handler.handle(this.splitter, stepExecution);
verify(this.taskLauncher, never()).launch((AppDeploymentRequest) any());
assertTrue(results.isEmpty());
assertThat(results.isEmpty()).isTrue();
}
@Test
@@ -121,9 +127,11 @@ public class DeployerPartitionHandlerTests {
JobExecution jobExecution = masterStepExecution.getJobExecution();
StepExecution workerStepExecutionStart = getStepExecutionStart(jobExecution, 4L);
StepExecution workerStepExecutionFinish = getStepExecutionFinish(workerStepExecutionStart, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish = getStepExecutionFinish(
workerStepExecutionStart, BatchStatus.COMPLETED);
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step1");
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher,
this.jobExplorer, this.resource, "step1");
handler.setEnvironment(this.environment);
TaskExecution taskExecution = new TaskExecution();
@@ -133,32 +141,40 @@ public class DeployerPartitionHandlerTests {
stepExecutions.add(workerStepExecutionStart);
when(this.splitter.split(masterStepExecution, 1)).thenReturn(stepExecutions);
when(this.jobExplorer.getStepExecution(1L, 4L)).thenReturn(workerStepExecutionFinish);
when(this.jobExplorer.getStepExecution(1L, 4L))
.thenReturn(workerStepExecutionFinish);
handler.afterPropertiesSet();
handler.beforeTask(taskExecution);
Collection<StepExecution> results = handler.handle(this.splitter, masterStepExecution);
Collection<StepExecution> results = handler.handle(this.splitter,
masterStepExecution);
verify(this.taskLauncher).launch(this.appDeploymentRequestArgumentCaptor.capture());
verify(this.taskLauncher)
.launch(this.appDeploymentRequestArgumentCaptor.capture());
AppDeploymentRequest request = this.appDeploymentRequestArgumentCaptor.getValue();
assertEquals(this.resource, request.getResource());
assertEquals(0, request.getDeploymentProperties().size());
assertThat(request.getResource()).isEqualTo(this.resource);
assertThat(request.getDeploymentProperties().size()).isEqualTo(0);
AppDefinition appDefinition = request.getDefinition();
assertEquals("partitionedJobTask", appDefinition.getName());
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID, "1")));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID, "4")));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME, "step1")));
assertThat(appDefinition.getName()).isEqualTo("partitionedJobTask");
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID, "1")))
.isTrue();
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID, "4")))
.isTrue();
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME, "step1"))).isTrue();
assertEquals(1, results.size());
assertThat(results.size()).isEqualTo(1);
StepExecution resultStepExecution = results.iterator().next();
assertEquals(BatchStatus.COMPLETED, resultStepExecution.getStatus());
assertEquals("step1:partition1", resultStepExecution.getStepName());
assertThat(resultStepExecution.getStatus()).isEqualTo(BatchStatus.COMPLETED);
assertThat(resultStepExecution.getStepName()).isEqualTo("step1:partition1");
}
@Test
@@ -168,49 +184,64 @@ public class DeployerPartitionHandlerTests {
JobExecution jobExecution = masterStepExecution.getJobExecution();
StepExecution workerStepExecutionStart = getStepExecutionStart(jobExecution, 4L);
StepExecution workerStepExecutionFinish = getStepExecutionFinish(workerStepExecutionStart, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish = getStepExecutionFinish(
workerStepExecutionStart, BatchStatus.COMPLETED);
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step1");
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher,
this.jobExplorer, this.resource, "step1");
handler.setEnvironment(this.environment);
handler.setDefaultArgsAsEnvironmentVars(true);
TaskExecution taskExecution = new TaskExecution(55, null, null, null,
null, null, new ArrayList<String>(), null, null);
TaskExecution taskExecution = new TaskExecution(55, null, null, null, null, null,
new ArrayList<>(), null, null);
taskExecution.setTaskName("partitionedJobTask");
Set<StepExecution> stepExecutions = new HashSet<>();
stepExecutions.add(workerStepExecutionStart);
when(this.splitter.split(masterStepExecution, 1)).thenReturn(stepExecutions);
when(this.jobExplorer.getStepExecution(1L, 4L)).thenReturn(workerStepExecutionFinish);
when(this.jobExplorer.getStepExecution(1L, 4L))
.thenReturn(workerStepExecutionFinish);
handler.afterPropertiesSet();
handler.beforeTask(taskExecution);
Collection<StepExecution> results = handler.handle(this.splitter, masterStepExecution);
Collection<StepExecution> results = handler.handle(this.splitter,
masterStepExecution);
verify(this.taskLauncher).launch(this.appDeploymentRequestArgumentCaptor.capture());
verify(this.taskLauncher)
.launch(this.appDeploymentRequestArgumentCaptor.capture());
AppDeploymentRequest request = this.appDeploymentRequestArgumentCaptor.getValue();
assertEquals(this.resource, request.getResource());
assertEquals(0, request.getDeploymentProperties().size());
assertThat(request.getResource()).isEqualTo(this.resource);
assertThat(request.getDeploymentProperties().size()).isEqualTo(0);
AppDefinition appDefinition = request.getDefinition();
assertEquals("partitionedJobTask", appDefinition.getName());
assertTrue(request.getCommandlineArguments().isEmpty());
assertEquals("1", request.getDefinition().getProperties().get(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID));
assertEquals("4", request.getDefinition().getProperties().get(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID));
assertEquals("step1", request.getDefinition().getProperties().get(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME));
assertEquals("partitionedJobTask_partitionedJob_step1:partition1", request.getDefinition().getProperties().get(DeployerPartitionHandler.SPRING_CLOUD_TASK_NAME));
assertEquals("55", request.getDefinition().getProperties().get(DeployerPartitionHandler.SPRING_CLOUD_TASK_PARENT_EXECUTION_ID));
assertThat(appDefinition.getName()).isEqualTo("partitionedJobTask");
assertThat(request.getCommandlineArguments().isEmpty()).isTrue();
assertThat(request.getDefinition().getProperties()
.get(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID))
.isEqualTo("1");
assertThat(request.getDefinition().getProperties()
.get(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID))
.isEqualTo("4");
assertThat(request.getDefinition().getProperties()
.get(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME))
.isEqualTo("step1");
assertThat(request.getDefinition().getProperties()
.get(DeployerPartitionHandler.SPRING_CLOUD_TASK_NAME))
.isEqualTo("partitionedJobTask_partitionedJob_step1:partition1");
assertThat(request.getDefinition().getProperties()
.get(DeployerPartitionHandler.SPRING_CLOUD_TASK_PARENT_EXECUTION_ID))
.isEqualTo("55");
assertEquals(1, results.size());
assertThat(results.size()).isEqualTo(1);
StepExecution resultStepExecution = results.iterator().next();
assertEquals(BatchStatus.COMPLETED, resultStepExecution.getStatus());
assertEquals("step1:partition1", resultStepExecution.getStepName());
assertThat(resultStepExecution.getStatus()).isEqualTo(BatchStatus.COMPLETED);
assertThat(resultStepExecution.getStepName()).isEqualTo("step1:partition1");
}
@Test
@@ -220,13 +251,15 @@ public class DeployerPartitionHandlerTests {
JobExecution jobExecution = masterStepExecution.getJobExecution();
StepExecution workerStepExecutionStart = getStepExecutionStart(jobExecution, 4L);
StepExecution workerStepExecutionFinish = getStepExecutionFinish(workerStepExecutionStart, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish = getStepExecutionFinish(
workerStepExecutionStart, BatchStatus.COMPLETED);
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step1");
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher,
this.jobExplorer, this.resource, "step1");
handler.setEnvironment(this.environment);
TaskExecution taskExecution = new TaskExecution(55, null, null, null,
null, null, new ArrayList<String>(), null, null);
TaskExecution taskExecution = new TaskExecution(55, null, null, null, null, null,
new ArrayList<>(), null, null);
taskExecution.setTaskName("partitionedJobTask");
@@ -234,7 +267,8 @@ public class DeployerPartitionHandlerTests {
stepExecutions.add(workerStepExecutionStart);
when(this.splitter.split(masterStepExecution, 1)).thenReturn(stepExecutions);
when(this.jobExplorer.getStepExecution(1L, 4L)).thenReturn(workerStepExecutionFinish);
when(this.jobExplorer.getStepExecution(1L, 4L))
.thenReturn(workerStepExecutionFinish);
handler.afterPropertiesSet();
@@ -242,10 +276,13 @@ public class DeployerPartitionHandlerTests {
handler.handle(this.splitter, masterStepExecution);
verify(this.taskLauncher).launch(this.appDeploymentRequestArgumentCaptor.capture());
verify(this.taskLauncher)
.launch(this.appDeploymentRequestArgumentCaptor.capture());
AppDeploymentRequest request = this.appDeploymentRequestArgumentCaptor.getValue();
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_PARENT_EXECUTION_ID, "55")));
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_PARENT_EXECUTION_ID, "55")))
.isTrue();
}
@Test
@@ -255,15 +292,19 @@ public class DeployerPartitionHandlerTests {
JobExecution jobExecution = masterStepExecution.getJobExecution();
StepExecution workerStepExecutionStart1 = getStepExecutionStart(jobExecution, 4L);
StepExecution workerStepExecutionFinish1 = getStepExecutionFinish(workerStepExecutionStart1, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish1 = getStepExecutionFinish(
workerStepExecutionStart1, BatchStatus.COMPLETED);
StepExecution workerStepExecutionStart2 = getStepExecutionStart(jobExecution, 5L);
StepExecution workerStepExecutionFinish2 = getStepExecutionFinish(workerStepExecutionStart2, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish2 = getStepExecutionFinish(
workerStepExecutionStart2, BatchStatus.COMPLETED);
StepExecution workerStepExecutionStart3 = getStepExecutionStart(jobExecution, 6L);
StepExecution workerStepExecutionFinish3 = getStepExecutionFinish(workerStepExecutionStart3, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish3 = getStepExecutionFinish(
workerStepExecutionStart3, BatchStatus.COMPLETED);
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step1");
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher,
this.jobExplorer, this.resource, "step1");
handler.setEnvironment(this.environment);
TaskExecution taskExecution = new TaskExecution();
@@ -277,18 +318,24 @@ public class DeployerPartitionHandlerTests {
when(this.splitter.split(masterStepExecution, 1)).thenReturn(stepExecutions);
when(this.jobExplorer.getStepExecution(1L, 4L)).thenReturn(workerStepExecutionFinish1);
when(this.jobExplorer.getStepExecution(1L, 5L)).thenReturn(workerStepExecutionFinish2);
when(this.jobExplorer.getStepExecution(1L, 6L)).thenReturn(workerStepExecutionFinish3);
when(this.jobExplorer.getStepExecution(1L, 4L))
.thenReturn(workerStepExecutionFinish1);
when(this.jobExplorer.getStepExecution(1L, 5L))
.thenReturn(workerStepExecutionFinish2);
when(this.jobExplorer.getStepExecution(1L, 6L))
.thenReturn(workerStepExecutionFinish3);
handler.afterPropertiesSet();
handler.beforeTask(taskExecution);
Collection<StepExecution> results = handler.handle(this.splitter, masterStepExecution);
Collection<StepExecution> results = handler.handle(this.splitter,
masterStepExecution);
verify(this.taskLauncher, times(3)).launch(this.appDeploymentRequestArgumentCaptor.capture());
verify(this.taskLauncher, times(3))
.launch(this.appDeploymentRequestArgumentCaptor.capture());
List<AppDeploymentRequest> allValues = this.appDeploymentRequestArgumentCaptor.getAllValues();
List<AppDeploymentRequest> allValues = this.appDeploymentRequestArgumentCaptor
.getAllValues();
validateAppDeploymentRequests(allValues, 3);
@@ -302,15 +349,19 @@ public class DeployerPartitionHandlerTests {
JobExecution jobExecution = masterStepExecution.getJobExecution();
StepExecution workerStepExecutionStart1 = getStepExecutionStart(jobExecution, 4L);
StepExecution workerStepExecutionFinish1 = getStepExecutionFinish(workerStepExecutionStart1, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish1 = getStepExecutionFinish(
workerStepExecutionStart1, BatchStatus.COMPLETED);
StepExecution workerStepExecutionStart2 = getStepExecutionStart(jobExecution, 5L);
StepExecution workerStepExecutionFinish2 = getStepExecutionFinish(workerStepExecutionStart2, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish2 = getStepExecutionFinish(
workerStepExecutionStart2, BatchStatus.COMPLETED);
StepExecution workerStepExecutionStart3 = getStepExecutionStart(jobExecution, 6L);
StepExecution workerStepExecutionFinish3 = getStepExecutionFinish(workerStepExecutionStart3, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish3 = getStepExecutionFinish(
workerStepExecutionStart3, BatchStatus.COMPLETED);
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step1");
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher,
this.jobExplorer, this.resource, "step1");
handler.setEnvironment(this.environment);
handler.setMaxWorkers(2);
@@ -325,18 +376,24 @@ public class DeployerPartitionHandlerTests {
when(this.splitter.split(masterStepExecution, 1)).thenReturn(stepExecutions);
when(this.jobExplorer.getStepExecution(1L, 4L)).thenReturn(workerStepExecutionFinish1);
when(this.jobExplorer.getStepExecution(1L, 5L)).thenReturn(workerStepExecutionFinish2);
when(this.jobExplorer.getStepExecution(1L, 6L)).thenReturn(workerStepExecutionFinish3);
when(this.jobExplorer.getStepExecution(1L, 4L))
.thenReturn(workerStepExecutionFinish1);
when(this.jobExplorer.getStepExecution(1L, 5L))
.thenReturn(workerStepExecutionFinish2);
when(this.jobExplorer.getStepExecution(1L, 6L))
.thenReturn(workerStepExecutionFinish3);
handler.afterPropertiesSet();
handler.beforeTask(taskExecution);
Collection<StepExecution> results = handler.handle(this.splitter, masterStepExecution);
Collection<StepExecution> results = handler.handle(this.splitter,
masterStepExecution);
verify(this.taskLauncher, times(3)).launch(this.appDeploymentRequestArgumentCaptor.capture());
verify(this.taskLauncher, times(3))
.launch(this.appDeploymentRequestArgumentCaptor.capture());
List<AppDeploymentRequest> allValues = this.appDeploymentRequestArgumentCaptor.getAllValues();
List<AppDeploymentRequest> allValues = this.appDeploymentRequestArgumentCaptor
.getAllValues();
validateAppDeploymentRequests(allValues, 3);
@@ -350,15 +407,19 @@ public class DeployerPartitionHandlerTests {
JobExecution jobExecution = masterStepExecution.getJobExecution();
StepExecution workerStepExecutionStart1 = getStepExecutionStart(jobExecution, 4L);
StepExecution workerStepExecutionFinish1 = getStepExecutionFinish(workerStepExecutionStart1, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish1 = getStepExecutionFinish(
workerStepExecutionStart1, BatchStatus.COMPLETED);
StepExecution workerStepExecutionStart2 = getStepExecutionStart(jobExecution, 5L);
StepExecution workerStepExecutionFinish2 = getStepExecutionFinish(workerStepExecutionStart2, BatchStatus.FAILED);
StepExecution workerStepExecutionFinish2 = getStepExecutionFinish(
workerStepExecutionStart2, BatchStatus.FAILED);
StepExecution workerStepExecutionStart3 = getStepExecutionStart(jobExecution, 6L);
StepExecution workerStepExecutionFinish3 = getStepExecutionFinish(workerStepExecutionStart3, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish3 = getStepExecutionFinish(
workerStepExecutionStart3, BatchStatus.COMPLETED);
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step1");
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher,
this.jobExplorer, this.resource, "step1");
handler.setEnvironment(this.environment);
handler.setMaxWorkers(2);
@@ -373,18 +434,24 @@ public class DeployerPartitionHandlerTests {
when(this.splitter.split(masterStepExecution, 1)).thenReturn(stepExecutions);
when(this.jobExplorer.getStepExecution(1L, 4L)).thenReturn(workerStepExecutionFinish1);
when(this.jobExplorer.getStepExecution(1L, 5L)).thenReturn(workerStepExecutionFinish2);
when(this.jobExplorer.getStepExecution(1L, 6L)).thenReturn(workerStepExecutionFinish3);
when(this.jobExplorer.getStepExecution(1L, 4L))
.thenReturn(workerStepExecutionFinish1);
when(this.jobExplorer.getStepExecution(1L, 5L))
.thenReturn(workerStepExecutionFinish2);
when(this.jobExplorer.getStepExecution(1L, 6L))
.thenReturn(workerStepExecutionFinish3);
handler.afterPropertiesSet();
handler.beforeTask(taskExecution);
Collection<StepExecution> results = handler.handle(this.splitter, masterStepExecution);
Collection<StepExecution> results = handler.handle(this.splitter,
masterStepExecution);
verify(this.taskLauncher, times(3)).launch(this.appDeploymentRequestArgumentCaptor.capture());
verify(this.taskLauncher, times(3))
.launch(this.appDeploymentRequestArgumentCaptor.capture());
List<AppDeploymentRequest> allValues = this.appDeploymentRequestArgumentCaptor.getAllValues();
List<AppDeploymentRequest> allValues = this.appDeploymentRequestArgumentCaptor
.getAllValues();
validateAppDeploymentRequests(allValues, 3);
@@ -395,13 +462,13 @@ public class DeployerPartitionHandlerTests {
StepExecution curResult = resultsIterator.next();
if (curResult.getStepName().equals("step1:partition2")) {
assertEquals(BatchStatus.FAILED, curResult.getStatus());
assertThat(curResult.getStatus()).isEqualTo(BatchStatus.FAILED);
}
else {
assertEquals(BatchStatus.COMPLETED, curResult.getStatus());
assertThat(curResult.getStatus()).isEqualTo(BatchStatus.COMPLETED);
}
assertTrue(!names.contains(curResult.getStepName()));
assertThat(!names.contains(curResult.getStepName())).isTrue();
names.add(curResult.getStepName());
}
}
@@ -413,15 +480,18 @@ public class DeployerPartitionHandlerTests {
JobExecution jobExecution = masterStepExecution.getJobExecution();
StepExecution workerStepExecutionStart = getStepExecutionStart(jobExecution, 4L);
StepExecution workerStepExecutionFinish = getStepExecutionFinish(workerStepExecutionStart, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish = getStepExecutionFinish(
workerStepExecutionStart, BatchStatus.COMPLETED);
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step1");
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher,
this.jobExplorer, this.resource, "step1");
Map<String, String> environmentParameters = new HashMap<>(2);
environmentParameters.put("foo", "bar");
environmentParameters.put("baz", "qux");
SimpleEnvironmentVariablesProvider environmentVariablesProvider = new SimpleEnvironmentVariablesProvider(this.environment);
SimpleEnvironmentVariablesProvider environmentVariablesProvider = new SimpleEnvironmentVariablesProvider(
this.environment);
environmentVariablesProvider.setEnvironmentProperties(environmentParameters);
handler.setEnvironmentVariablesProvider(environmentVariablesProvider);
@@ -432,33 +502,41 @@ public class DeployerPartitionHandlerTests {
stepExecutions.add(workerStepExecutionStart);
when(this.splitter.split(masterStepExecution, 1)).thenReturn(stepExecutions);
when(this.jobExplorer.getStepExecution(1L, 4L)).thenReturn(workerStepExecutionFinish);
when(this.jobExplorer.getStepExecution(1L, 4L))
.thenReturn(workerStepExecutionFinish);
handler.afterPropertiesSet();
handler.beforeTask(taskExecution);
Collection<StepExecution> results = handler.handle(this.splitter, masterStepExecution);
Collection<StepExecution> results = handler.handle(this.splitter,
masterStepExecution);
verify(this.taskLauncher).launch(this.appDeploymentRequestArgumentCaptor.capture());
verify(this.taskLauncher)
.launch(this.appDeploymentRequestArgumentCaptor.capture());
AppDeploymentRequest request = this.appDeploymentRequestArgumentCaptor.getValue();
assertEquals(this.resource, request.getResource());
assertEquals(2, request.getDefinition().getProperties().size());
assertEquals("bar", request.getDefinition().getProperties().get("foo"));
assertEquals("qux", request.getDefinition().getProperties().get("baz"));
assertThat(request.getResource()).isEqualTo(this.resource);
assertThat(request.getDefinition().getProperties().size()).isEqualTo(2);
assertThat(request.getDefinition().getProperties().get("foo")).isEqualTo("bar");
assertThat(request.getDefinition().getProperties().get("baz")).isEqualTo("qux");
AppDefinition appDefinition = request.getDefinition();
assertEquals("partitionedJobTask", appDefinition.getName());
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID, "1")));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID, "4")));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME, "step1")));
assertThat(appDefinition.getName()).isEqualTo("partitionedJobTask");
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID, "1")))
.isTrue();
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID, "4")))
.isTrue();
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME, "step1"))).isTrue();
assertEquals(1, results.size());
assertThat(results.size()).isEqualTo(1);
StepExecution resultStepExecution = results.iterator().next();
assertEquals(BatchStatus.COMPLETED, resultStepExecution.getStatus());
assertEquals("step1:partition1", resultStepExecution.getStepName());
assertThat(resultStepExecution.getStatus()).isEqualTo(BatchStatus.COMPLETED);
assertThat(resultStepExecution.getStepName()).isEqualTo("step1:partition1");
}
@Test
@@ -471,16 +549,19 @@ public class DeployerPartitionHandlerTests {
JobExecution jobExecution = masterStepExecution.getJobExecution();
StepExecution workerStepExecutionStart = getStepExecutionStart(jobExecution, 4L);
StepExecution workerStepExecutionFinish = getStepExecutionFinish(workerStepExecutionStart, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish = getStepExecutionFinish(
workerStepExecutionStart, BatchStatus.COMPLETED);
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step1");
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher,
this.jobExplorer, this.resource, "step1");
handler.setEnvironment(this.environment);
Map<String, String> environmentParameters = new HashMap<>(2);
environmentParameters.put("foo", "bar");
environmentParameters.put("baz", "qux");
SimpleEnvironmentVariablesProvider environmentVariablesProvider = new SimpleEnvironmentVariablesProvider(this.environment);
SimpleEnvironmentVariablesProvider environmentVariablesProvider = new SimpleEnvironmentVariablesProvider(
this.environment);
environmentVariablesProvider.setEnvironmentProperties(environmentParameters);
handler.setEnvironmentVariablesProvider(environmentVariablesProvider);
@@ -491,34 +572,43 @@ public class DeployerPartitionHandlerTests {
stepExecutions.add(workerStepExecutionStart);
when(this.splitter.split(masterStepExecution, 1)).thenReturn(stepExecutions);
when(this.jobExplorer.getStepExecution(1L, 4L)).thenReturn(workerStepExecutionFinish);
when(this.jobExplorer.getStepExecution(1L, 4L))
.thenReturn(workerStepExecutionFinish);
handler.afterPropertiesSet();
handler.beforeTask(taskExecution);
Collection<StepExecution> results = handler.handle(this.splitter, masterStepExecution);
Collection<StepExecution> results = handler.handle(this.splitter,
masterStepExecution);
verify(this.taskLauncher).launch(this.appDeploymentRequestArgumentCaptor.capture());
verify(this.taskLauncher)
.launch(this.appDeploymentRequestArgumentCaptor.capture());
AppDeploymentRequest request = this.appDeploymentRequestArgumentCaptor.getValue();
assertEquals(this.resource, request.getResource());
assertEquals(3, request.getDefinition().getProperties().size());
assertEquals("bar", request.getDefinition().getProperties().get("foo"));
assertEquals("qux", request.getDefinition().getProperties().get("baz"));
assertEquals("batch", request.getDefinition().getProperties().get("task"));
assertThat(request.getResource()).isEqualTo(this.resource);
assertThat(request.getDefinition().getProperties().size()).isEqualTo(3);
assertThat(request.getDefinition().getProperties().get("foo")).isEqualTo("bar");
assertThat(request.getDefinition().getProperties().get("baz")).isEqualTo("qux");
assertThat(request.getDefinition().getProperties().get("task"))
.isEqualTo("batch");
AppDefinition appDefinition = request.getDefinition();
assertEquals("partitionedJobTask", appDefinition.getName());
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID, "1")));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID, "4")));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME, "step1")));
assertThat(appDefinition.getName()).isEqualTo("partitionedJobTask");
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID, "1")))
.isTrue();
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID, "4")))
.isTrue();
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME, "step1"))).isTrue();
assertEquals(1, results.size());
assertThat(results.size()).isEqualTo(1);
StepExecution resultStepExecution = results.iterator().next();
assertEquals(BatchStatus.COMPLETED, resultStepExecution.getStatus());
assertEquals("step1:partition1", resultStepExecution.getStepName());
assertThat(resultStepExecution.getStatus()).isEqualTo(BatchStatus.COMPLETED);
assertThat(resultStepExecution.getStepName()).isEqualTo("step1:partition1");
}
@Test
@@ -528,12 +618,15 @@ public class DeployerPartitionHandlerTests {
JobExecution jobExecution = masterStepExecution.getJobExecution();
StepExecution workerStepExecutionStart1 = getStepExecutionStart(jobExecution, 4L);
StepExecution workerStepExecutionFinish1 = getStepExecutionFinish(workerStepExecutionStart1, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish1 = getStepExecutionFinish(
workerStepExecutionStart1, BatchStatus.COMPLETED);
StepExecution workerStepExecutionStart2 = getStepExecutionStart(jobExecution, 5L);
StepExecution workerStepExecutionFinish2 = getStepExecutionFinish(workerStepExecutionStart2, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish2 = getStepExecutionFinish(
workerStepExecutionStart2, BatchStatus.COMPLETED);
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step1");
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher,
this.jobExplorer, this.resource, "step1");
handler.setEnvironment(this.environment);
handler.setPollInterval(20000L);
@@ -547,26 +640,33 @@ public class DeployerPartitionHandlerTests {
stepExecutions.add(workerStepExecutionStart2);
when(this.splitter.split(masterStepExecution, 1)).thenReturn(stepExecutions);
when(this.jobExplorer.getStepExecution(1L, 4L)).thenReturn(workerStepExecutionFinish1);
when(this.jobExplorer.getStepExecution(1L, 5L)).thenReturn(workerStepExecutionFinish2);
when(this.jobExplorer.getStepExecution(1L, 4L))
.thenReturn(workerStepExecutionFinish1);
when(this.jobExplorer.getStepExecution(1L, 5L))
.thenReturn(workerStepExecutionFinish2);
handler.afterPropertiesSet();
handler.beforeTask(taskExecution);
Date startTime = new Date();
Collection<StepExecution> results = handler.handle(this.splitter, masterStepExecution);
Collection<StepExecution> results = handler.handle(this.splitter,
masterStepExecution);
Date endTime = new Date();
verify(this.taskLauncher, times(2)).launch(this.appDeploymentRequestArgumentCaptor.capture());
verify(this.taskLauncher, times(2))
.launch(this.appDeploymentRequestArgumentCaptor.capture());
List<AppDeploymentRequest> allRequests = this.appDeploymentRequestArgumentCaptor.getAllValues();
List<AppDeploymentRequest> allRequests = this.appDeploymentRequestArgumentCaptor
.getAllValues();
validateAppDeploymentRequests(allRequests, 2);
validateStepExecutionResults(results);
assertTrue("Time difference was too small: " + (endTime.getTime() - startTime.getTime()),
endTime.getTime() - startTime.getTime() >= 19999);
assertThat(endTime.getTime() - startTime.getTime() >= 19999)
.as("Time difference was too small: "
+ (endTime.getTime() - startTime.getTime()))
.isTrue();
}
@Test(expected = TimeoutException.class)
@@ -576,12 +676,15 @@ public class DeployerPartitionHandlerTests {
JobExecution jobExecution = masterStepExecution.getJobExecution();
StepExecution workerStepExecutionStart1 = getStepExecutionStart(jobExecution, 4L);
StepExecution workerStepExecutionFinish1 = getStepExecutionFinish(workerStepExecutionStart1, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish1 = getStepExecutionFinish(
workerStepExecutionStart1, BatchStatus.COMPLETED);
StepExecution workerStepExecutionStart2 = getStepExecutionStart(jobExecution, 5L);
StepExecution workerStepExecutionFinish2 = getStepExecutionFinish(workerStepExecutionStart2, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish2 = getStepExecutionFinish(
workerStepExecutionStart2, BatchStatus.COMPLETED);
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step1");
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher,
this.jobExplorer, this.resource, "step1");
handler.setEnvironment(this.environment);
handler.setPollInterval(20000L);
@@ -596,8 +699,10 @@ public class DeployerPartitionHandlerTests {
stepExecutions.add(workerStepExecutionStart2);
when(this.splitter.split(masterStepExecution, 1)).thenReturn(stepExecutions);
when(this.jobExplorer.getStepExecution(1L, 4L)).thenReturn(workerStepExecutionFinish1);
when(this.jobExplorer.getStepExecution(1L, 5L)).thenReturn(workerStepExecutionFinish2);
when(this.jobExplorer.getStepExecution(1L, 4L))
.thenReturn(workerStepExecutionFinish1);
when(this.jobExplorer.getStepExecution(1L, 5L))
.thenReturn(workerStepExecutionFinish2);
handler.afterPropertiesSet();
@@ -613,12 +718,15 @@ public class DeployerPartitionHandlerTests {
JobExecution jobExecution = masterStepExecution.getJobExecution();
StepExecution workerStepExecutionStart1 = getStepExecutionStart(jobExecution, 4L);
StepExecution workerStepExecutionFinish1 = getStepExecutionFinish(workerStepExecutionStart1, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish1 = getStepExecutionFinish(
workerStepExecutionStart1, BatchStatus.COMPLETED);
StepExecution workerStepExecutionStart2 = getStepExecutionStart(jobExecution, 5L);
StepExecution workerStepExecutionFinish2 = getStepExecutionFinish(workerStepExecutionStart2, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish2 = getStepExecutionFinish(
workerStepExecutionStart2, BatchStatus.COMPLETED);
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step1");
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher,
this.jobExplorer, this.resource, "step1");
handler.setEnvironment(this.environment);
handler.setGridSize(2);
@@ -631,18 +739,23 @@ public class DeployerPartitionHandlerTests {
stepExecutions.add(workerStepExecutionStart2);
when(this.splitter.split(masterStepExecution, 2)).thenReturn(stepExecutions);
when(this.jobExplorer.getStepExecution(1L, 4L)).thenReturn(workerStepExecutionFinish1);
when(this.jobExplorer.getStepExecution(1L, 5L)).thenReturn(workerStepExecutionFinish2);
when(this.jobExplorer.getStepExecution(1L, 4L))
.thenReturn(workerStepExecutionFinish1);
when(this.jobExplorer.getStepExecution(1L, 5L))
.thenReturn(workerStepExecutionFinish2);
handler.afterPropertiesSet();
handler.beforeTask(taskExecution);
Collection<StepExecution> results = handler.handle(this.splitter, masterStepExecution);
Collection<StepExecution> results = handler.handle(this.splitter,
masterStepExecution);
verify(this.taskLauncher, times(2)).launch(this.appDeploymentRequestArgumentCaptor.capture());
verify(this.taskLauncher, times(2))
.launch(this.appDeploymentRequestArgumentCaptor.capture());
List<AppDeploymentRequest> allRequests = this.appDeploymentRequestArgumentCaptor.getAllValues();
List<AppDeploymentRequest> allRequests = this.appDeploymentRequestArgumentCaptor
.getAllValues();
validateAppDeploymentRequests(allRequests, 2);
@@ -656,9 +769,11 @@ public class DeployerPartitionHandlerTests {
JobExecution jobExecution = masterStepExecution.getJobExecution();
StepExecution workerStepExecutionStart = getStepExecutionStart(jobExecution, 4L);
StepExecution workerStepExecutionFinish = getStepExecutionFinish(workerStepExecutionStart, BatchStatus.COMPLETED);
StepExecution workerStepExecutionFinish = getStepExecutionFinish(
workerStepExecutionStart, BatchStatus.COMPLETED);
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher, this.jobExplorer, this.resource, "step1");
DeployerPartitionHandler handler = new DeployerPartitionHandler(this.taskLauncher,
this.jobExplorer, this.resource, "step1");
handler.setEnvironment(this.environment);
Map<String, String> deploymentProperties = new HashMap<>(2);
@@ -674,48 +789,59 @@ public class DeployerPartitionHandlerTests {
stepExecutions.add(workerStepExecutionStart);
when(this.splitter.split(masterStepExecution, 1)).thenReturn(stepExecutions);
when(this.jobExplorer.getStepExecution(1L, 4L)).thenReturn(workerStepExecutionFinish);
when(this.jobExplorer.getStepExecution(1L, 4L))
.thenReturn(workerStepExecutionFinish);
handler.afterPropertiesSet();
handler.beforeTask(taskExecution);
Collection<StepExecution> results = handler.handle(this.splitter, masterStepExecution);
Collection<StepExecution> results = handler.handle(this.splitter,
masterStepExecution);
verify(this.taskLauncher).launch(this.appDeploymentRequestArgumentCaptor.capture());
verify(this.taskLauncher)
.launch(this.appDeploymentRequestArgumentCaptor.capture());
AppDeploymentRequest request = this.appDeploymentRequestArgumentCaptor.getValue();
assertEquals(this.resource, request.getResource());
assertEquals(2, request.getDeploymentProperties().size());
assertEquals("bar", request.getDeploymentProperties().get("foo"));
assertEquals("qux", request.getDeploymentProperties().get("baz"));
assertThat(request.getResource()).isEqualTo(this.resource);
assertThat(request.getDeploymentProperties().size()).isEqualTo(2);
assertThat(request.getDeploymentProperties().get("foo")).isEqualTo("bar");
assertThat(request.getDeploymentProperties().get("baz")).isEqualTo("qux");
AppDefinition appDefinition = request.getDefinition();
assertEquals("partitionedJobTask", appDefinition.getName());
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID, "1")));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID, "4")));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME, "step1")));
assertThat(appDefinition.getName()).isEqualTo("partitionedJobTask");
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID, "1")))
.isTrue();
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID, "4")))
.isTrue();
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME, "step1"))).isTrue();
assertEquals(1, results.size());
assertThat(results.size()).isEqualTo(1);
StepExecution resultStepExecution = results.iterator().next();
assertEquals(BatchStatus.COMPLETED, resultStepExecution.getStatus());
assertEquals("step1:partition1", resultStepExecution.getStepName());
assertThat(resultStepExecution.getStatus()).isEqualTo(BatchStatus.COMPLETED);
assertThat(resultStepExecution.getStepName()).isEqualTo("step1:partition1");
}
private String formatArgs(String key, String value) {
return String.format("--%s=%s", key, value);
}
private StepExecution getStepExecutionFinish(StepExecution stepExecutionStart, BatchStatus status) {
StepExecution workerStepExecutionFinish = new StepExecution(stepExecutionStart.getStepName(), stepExecutionStart.getJobExecution());
private StepExecution getStepExecutionFinish(StepExecution stepExecutionStart,
BatchStatus status) {
StepExecution workerStepExecutionFinish = new StepExecution(
stepExecutionStart.getStepName(), stepExecutionStart.getJobExecution());
workerStepExecutionFinish.setId(stepExecutionStart.getId());
workerStepExecutionFinish.setStatus(status);
return workerStepExecutionFinish;
}
private StepExecution getStepExecutionStart(JobExecution jobExecution, long id) {
StepExecution workerStepExecutionStart = new StepExecution("step1:partition" + (id - 3), jobExecution);
StepExecution workerStepExecutionStart = new StepExecution(
"step1:partition" + (id - 3), jobExecution);
workerStepExecutionStart.setId(id);
return workerStepExecutionStart;
}
@@ -738,14 +864,15 @@ public class DeployerPartitionHandlerTests {
while (resultsIterator.hasNext()) {
StepExecution curResult = resultsIterator.next();
assertEquals(BatchStatus.COMPLETED, curResult.getStatus());
assertThat(curResult.getStatus()).isEqualTo(BatchStatus.COMPLETED);
assertTrue(!names.contains(curResult.getStepName()));
assertThat(!names.contains(curResult.getStepName())).isTrue();
names.add(curResult.getStepName());
}
}
private void validateAppDeploymentRequests(List<AppDeploymentRequest> allRequests, int numberOfPartitions) {
private void validateAppDeploymentRequests(List<AppDeploymentRequest> allRequests,
int numberOfPartitions) {
Collections.sort(allRequests, new Comparator<AppDeploymentRequest>() {
@Override
public int compare(AppDeploymentRequest o1, AppDeploymentRequest o2) {
@@ -753,7 +880,8 @@ public class DeployerPartitionHandlerTests {
String o1Command = "";
for (String commandlineArgument : commandlineArguments) {
if(commandlineArgument.contains(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID)) {
if (commandlineArgument.contains(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID)) {
o1Command = commandlineArgument;
break;
}
@@ -763,7 +891,8 @@ public class DeployerPartitionHandlerTests {
String o2Command = "";
for (String commandlineArgument : commandlineArguments) {
if(commandlineArgument.contains(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID)) {
if (commandlineArgument.contains(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID)) {
o2Command = commandlineArgument;
break;
}
@@ -775,23 +904,33 @@ public class DeployerPartitionHandlerTests {
for (int i = 4; i < (numberOfPartitions + 4); i++) {
AppDeploymentRequest request = allRequests.get(i - 4);
assertEquals(this.resource, request.getResource());
assertEquals(0, request.getDeploymentProperties().size());
assertThat(request.getResource()).isEqualTo(this.resource);
assertThat(request.getDeploymentProperties().size()).isEqualTo(0);
AppDefinition appDefinition = request.getDefinition();
assertEquals("partitionedJobTask", appDefinition.getName());
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID, "1")));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID, String.valueOf(i))));
assertTrue(request.getCommandlineArguments().contains(formatArgs(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME, "step1")));
assertThat(appDefinition.getName()).isEqualTo("partitionedJobTask");
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID, "1")))
.isTrue();
assertThat(request.getCommandlineArguments()
.contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID,
String.valueOf(i)))).isTrue();
assertThat(request.getCommandlineArguments().contains(formatArgs(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME, "step1")))
.isTrue();
}
}
private void validateConstructorValidation(TaskLauncher taskLauncher, JobExplorer jobExplorer, Resource resource, String stepName, String expectedMessage) {
private void validateConstructorValidation(TaskLauncher taskLauncher,
JobExplorer jobExplorer, Resource resource, String stepName,
String expectedMessage) {
try {
new DeployerPartitionHandler(taskLauncher, jobExplorer, resource, stepName);
}
catch (IllegalArgumentException iae) {
assertEquals(expectedMessage, iae.getMessage());
assertThat(iae.getMessage()).isEqualTo(expectedMessage);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2015-2019 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.partition;
import org.junit.Before;
@@ -35,7 +36,7 @@ import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.core.env.Environment;
import org.springframework.test.util.ReflectionTestUtils;
import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
@@ -70,7 +71,8 @@ public class DeployerStepExecutionHandlerTests {
public void setUp() {
MockitoAnnotations.initMocks(this);
this.handler = new DeployerStepExecutionHandler(this.beanFactory, this.jobExplorer, this.jobRepository);
this.handler = new DeployerStepExecutionHandler(this.beanFactory,
this.jobExplorer, this.jobRepository);
ReflectionTestUtils.setField(this.handler, "environment", this.environment);
}
@@ -78,69 +80,116 @@ public class DeployerStepExecutionHandlerTests {
@Test
public void testConstructorValidation() {
validateConstructorValidation(null, null, null, "A beanFactory is required");
validateConstructorValidation(this.beanFactory, null, null, "A jobExplorer is required");
validateConstructorValidation(this.beanFactory, this.jobExplorer, null, "A jobRepository is required");
validateConstructorValidation(this.beanFactory, null, null,
"A jobExplorer is required");
validateConstructorValidation(this.beanFactory, this.jobExplorer, null,
"A jobRepository is required");
new DeployerStepExecutionHandler(this.beanFactory, this.jobExplorer, this.jobRepository);
new DeployerStepExecutionHandler(this.beanFactory, this.jobExplorer,
this.jobRepository);
}
@Test
public void testValidationOfRequestValuesExist() throws Exception {
validateEnvironmentConfiguration("A job execution id is required", new String[0]);
validateEnvironmentConfiguration("A step execution id is required", new String[] {DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID});
validateEnvironmentConfiguration("A step name is required", new String[] {DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID, DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID});
validateEnvironmentConfiguration("A step execution id is required", new String[] {
DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID });
validateEnvironmentConfiguration("A step name is required",
new String[] {
DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID,
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID });
}
@Test
public void testValidationOfRequestStepFound() throws Exception {
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID)).thenReturn(true);
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID)).thenReturn(true);
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME)).thenReturn(true);
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME)).thenReturn("foo");
when(this.beanFactory.getBeanNamesForType(Step.class)).thenReturn(new String[] {"bar", "baz"});
when(this.environment.containsProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID))
.thenReturn(true);
when(this.environment.containsProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID))
.thenReturn(true);
when(this.environment
.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME))
.thenReturn(true);
when(this.environment
.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME))
.thenReturn("foo");
when(this.beanFactory.getBeanNamesForType(Step.class))
.thenReturn(new String[] { "bar", "baz" });
try {
this.handler.run();
}
catch (IllegalArgumentException iae) {
assertEquals("The step requested cannot be found in the provided BeanFactory", iae.getMessage());
assertThat(iae.getMessage()).isEqualTo(
"The step requested cannot be found in the provided BeanFactory");
}
}
@Test
public void testMissingStepExecution() throws Exception {
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID)).thenReturn(true);
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID)).thenReturn(true);
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME)).thenReturn(true);
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME)).thenReturn("foo");
when(this.beanFactory.getBeanNamesForType(Step.class)).thenReturn(new String[] {"foo", "bar", "baz"});
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID)).thenReturn("2");
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID)).thenReturn("1");
when(this.environment.containsProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID))
.thenReturn(true);
when(this.environment.containsProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID))
.thenReturn(true);
when(this.environment
.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME))
.thenReturn(true);
when(this.environment
.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME))
.thenReturn("foo");
when(this.beanFactory.getBeanNamesForType(Step.class))
.thenReturn(new String[] { "foo", "bar", "baz" });
when(this.environment.getProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID))
.thenReturn("2");
when(this.environment
.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID))
.thenReturn("1");
try {
this.handler.run();
}
catch (NoSuchStepException nsse) {
assertEquals("No StepExecution could be located for step execution id 2 within job execution 1", nsse.getMessage());
assertThat(nsse.getMessage()).isEqualTo(
"No StepExecution could be located for step execution id 2 within job execution 1");
}
}
@Test
public void testRunSuccessful() throws Exception {
StepExecution workerStep = new StepExecution("workerStep", new JobExecution(1L), 2L);
StepExecution workerStep = new StepExecution("workerStep", new JobExecution(1L),
2L);
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID)).thenReturn(true);
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID)).thenReturn(true);
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME)).thenReturn(true);
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME)).thenReturn("workerStep");
when(this.beanFactory.getBeanNamesForType(Step.class)).thenReturn(new String[] {"workerStep", "foo", "bar"});
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID)).thenReturn("2");
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID)).thenReturn("1");
when(this.environment.containsProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID))
.thenReturn(true);
when(this.environment.containsProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID))
.thenReturn(true);
when(this.environment
.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME))
.thenReturn(true);
when(this.environment
.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME))
.thenReturn("workerStep");
when(this.beanFactory.getBeanNamesForType(Step.class))
.thenReturn(new String[] { "workerStep", "foo", "bar" });
when(this.environment.getProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID))
.thenReturn("2");
when(this.environment
.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID))
.thenReturn("1");
when(this.jobExplorer.getStepExecution(1L, 2L)).thenReturn(workerStep);
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME)).thenReturn("workerStep");
when(this.environment
.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME))
.thenReturn("workerStep");
when(this.beanFactory.getBean("workerStep", Step.class)).thenReturn(this.step);
handler.run();
this.handler.run();
verify(this.step).execute(workerStep);
verifyZeroInteractions(this.jobRepository);
@@ -148,51 +197,87 @@ public class DeployerStepExecutionHandlerTests {
@Test
public void testJobInterruptedException() throws Exception {
StepExecution workerStep = new StepExecution("workerStep", new JobExecution(1L), 2L);
StepExecution workerStep = new StepExecution("workerStep", new JobExecution(1L),
2L);
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID)).thenReturn(true);
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID)).thenReturn(true);
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME)).thenReturn(true);
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME)).thenReturn("workerStep");
when(this.beanFactory.getBeanNamesForType(Step.class)).thenReturn(new String[] {"workerStep", "foo", "bar"});
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID)).thenReturn("2");
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID)).thenReturn("1");
when(this.environment.containsProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID))
.thenReturn(true);
when(this.environment.containsProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID))
.thenReturn(true);
when(this.environment
.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME))
.thenReturn(true);
when(this.environment
.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME))
.thenReturn("workerStep");
when(this.beanFactory.getBeanNamesForType(Step.class))
.thenReturn(new String[] { "workerStep", "foo", "bar" });
when(this.environment.getProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID))
.thenReturn("2");
when(this.environment
.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID))
.thenReturn("1");
when(this.jobExplorer.getStepExecution(1L, 2L)).thenReturn(workerStep);
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME)).thenReturn("workerStep");
when(this.environment
.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME))
.thenReturn("workerStep");
when(this.beanFactory.getBean("workerStep", Step.class)).thenReturn(this.step);
doThrow(new JobInterruptedException("expected")).when(this.step).execute(workerStep);
doThrow(new JobInterruptedException("expected")).when(this.step)
.execute(workerStep);
handler.run();
this.handler.run();
verify(this.jobRepository).update(this.stepExecutionArgumentCaptor.capture());
assertEquals(BatchStatus.STOPPED, this.stepExecutionArgumentCaptor.getValue().getStatus());
assertThat(this.stepExecutionArgumentCaptor.getValue().getStatus())
.isEqualTo(BatchStatus.STOPPED);
}
@Test
public void testRuntimeException() throws Exception {
StepExecution workerStep = new StepExecution("workerStep", new JobExecution(1L), 2L);
StepExecution workerStep = new StepExecution("workerStep", new JobExecution(1L),
2L);
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID)).thenReturn(true);
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID)).thenReturn(true);
when(this.environment.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME)).thenReturn(true);
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME)).thenReturn("workerStep");
when(this.beanFactory.getBeanNamesForType(Step.class)).thenReturn(new String[] {"workerStep", "foo", "bar"});
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID)).thenReturn("2");
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID)).thenReturn("1");
when(this.environment.containsProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID))
.thenReturn(true);
when(this.environment.containsProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID))
.thenReturn(true);
when(this.environment
.containsProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME))
.thenReturn(true);
when(this.environment
.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME))
.thenReturn("workerStep");
when(this.beanFactory.getBeanNamesForType(Step.class))
.thenReturn(new String[] { "workerStep", "foo", "bar" });
when(this.environment.getProperty(
DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_EXECUTION_ID))
.thenReturn("2");
when(this.environment
.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_JOB_EXECUTION_ID))
.thenReturn("1");
when(this.jobExplorer.getStepExecution(1L, 2L)).thenReturn(workerStep);
when(this.environment.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME)).thenReturn("workerStep");
when(this.environment
.getProperty(DeployerPartitionHandler.SPRING_CLOUD_TASK_STEP_NAME))
.thenReturn("workerStep");
when(this.beanFactory.getBean("workerStep", Step.class)).thenReturn(this.step);
doThrow(new RuntimeException("expected")).when(this.step).execute(workerStep);
handler.run();
this.handler.run();
verify(this.jobRepository).update(this.stepExecutionArgumentCaptor.capture());
assertEquals(BatchStatus.FAILED, this.stepExecutionArgumentCaptor.getValue().getStatus());
assertThat(this.stepExecutionArgumentCaptor.getValue().getStatus())
.isEqualTo(BatchStatus.FAILED);
}
private void validateEnvironmentConfiguration(String errorMessage, String[] properties) throws Exception {
private void validateEnvironmentConfiguration(String errorMessage,
String[] properties) throws Exception {
for (String property : properties) {
when(this.environment.containsProperty(property)).thenReturn(true);
@@ -202,17 +287,18 @@ public class DeployerStepExecutionHandlerTests {
this.handler.run();
}
catch (IllegalArgumentException iae) {
assertEquals(errorMessage, iae.getMessage());
assertThat(iae.getMessage()).isEqualTo(errorMessage);
}
}
private void validateConstructorValidation(BeanFactory beanFactory, JobExplorer jobExplorer, JobRepository jobRepository, String message) {
private void validateConstructorValidation(BeanFactory beanFactory,
JobExplorer jobExplorer, JobRepository jobRepository, String message) {
try {
new DeployerStepExecutionHandler(beanFactory, jobExplorer, jobRepository);
}
catch (IllegalArgumentException iae) {
assertEquals(message, iae.getMessage());
assertThat(iae.getMessage()).isEqualTo(message);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2015-2019 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.
@@ -13,13 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.partition;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Michael Minella
@@ -35,11 +37,14 @@ public class NoOpEnvironmentVariablesProviderTests {
@Test
public void test() {
Map<String, String> environmentVariables = this.provider.getEnvironmentVariables(null);
assertNotNull(environmentVariables);
assertTrue(environmentVariables.isEmpty());
Map<String, String> environmentVariables = this.provider
.getEnvironmentVariables(null);
assertThat(environmentVariables).isNotNull();
assertThat(environmentVariables.isEmpty()).isTrue();
Map<String, String> environmentVariables2 = this.provider.getEnvironmentVariables(null);
assertTrue(environmentVariables == environmentVariables2);
Map<String, String> environmentVariables2 = this.provider
.getEnvironmentVariables(null);
assertThat(environmentVariables == environmentVariables2).isTrue();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2015-2019 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.
@@ -13,13 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.partition;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Michael Minella
@@ -39,8 +41,9 @@ public class PassThroughCommandLineArgsProviderTests {
List<String> commandLineArgs = provider.getCommandLineArgs(null);
assertEquals("foo", commandLineArgs.get(0));
assertEquals("bar", commandLineArgs.get(1));
assertEquals("baz", commandLineArgs.get(2));
assertThat(commandLineArgs.get(0)).isEqualTo("foo");
assertThat(commandLineArgs.get(1)).isEqualTo("bar");
assertThat(commandLineArgs.get(2)).isEqualTo("baz");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 the original author or authors.
* Copyright 2015-2019 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.task.batch.partition;
import java.util.ArrayList;
@@ -23,7 +24,7 @@ import org.junit.Test;
import org.springframework.cloud.task.repository.TaskExecution;
import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Michael Minella
@@ -35,13 +36,14 @@ public class SimpleCommandLineArgsProviderTests {
TaskExecution taskExecution = new TaskExecution();
taskExecution.setArguments(Arrays.asList("foo", "bar", "baz"));
SimpleCommandLineArgsProvider provider = new SimpleCommandLineArgsProvider(taskExecution);
SimpleCommandLineArgsProvider provider = new SimpleCommandLineArgsProvider(
taskExecution);
List<String> commandLineArgs = provider.getCommandLineArgs(null);
assertEquals("foo", commandLineArgs.get(0));
assertEquals("bar", commandLineArgs.get(1));
assertEquals("baz", commandLineArgs.get(2));
assertThat(commandLineArgs.get(0)).isEqualTo("foo");
assertThat(commandLineArgs.get(1)).isEqualTo("bar");
assertThat(commandLineArgs.get(2)).isEqualTo("baz");
}
@Test
@@ -54,17 +56,18 @@ public class SimpleCommandLineArgsProviderTests {
TaskExecution taskExecution = new TaskExecution();
taskExecution.setArguments(Arrays.asList("foo", "bar", "baz"));
SimpleCommandLineArgsProvider provider = new SimpleCommandLineArgsProvider(taskExecution);
SimpleCommandLineArgsProvider provider = new SimpleCommandLineArgsProvider(
taskExecution);
provider.setAppendedArgs(appendedValues);
List<String> commandLineArgs = provider.getCommandLineArgs(null);
assertEquals("foo", commandLineArgs.get(0));
assertEquals("bar", commandLineArgs.get(1));
assertEquals("baz", commandLineArgs.get(2));
assertEquals("one", commandLineArgs.get(3));
assertEquals("two", commandLineArgs.get(4));
assertEquals("three", commandLineArgs.get(5));
assertThat(commandLineArgs.get(0)).isEqualTo("foo");
assertThat(commandLineArgs.get(1)).isEqualTo("bar");
assertThat(commandLineArgs.get(2)).isEqualTo("baz");
assertThat(commandLineArgs.get(3)).isEqualTo("one");
assertThat(commandLineArgs.get(4)).isEqualTo("two");
assertThat(commandLineArgs.get(5)).isEqualTo("three");
}
@Test
@@ -73,14 +76,16 @@ public class SimpleCommandLineArgsProviderTests {
TaskExecution taskExecution = new TaskExecution();
taskExecution.setArguments(Arrays.asList("foo", "bar", "baz"));
SimpleCommandLineArgsProvider provider = new SimpleCommandLineArgsProvider(taskExecution);
SimpleCommandLineArgsProvider provider = new SimpleCommandLineArgsProvider(
taskExecution);
provider.setAppendedArgs(null);
List<String> commandLineArgs = provider.getCommandLineArgs(null);
assertEquals(3, commandLineArgs.size());
assertEquals("foo", commandLineArgs.get(0));
assertEquals("bar", commandLineArgs.get(1));
assertEquals("baz", commandLineArgs.get(2));
assertThat(commandLineArgs.size()).isEqualTo(3);
assertThat(commandLineArgs.get(0)).isEqualTo("foo");
assertThat(commandLineArgs.get(1)).isEqualTo("bar");
assertThat(commandLineArgs.get(2)).isEqualTo("baz");
}
}