Update Task to BOOT 2.1.M1

Migrating to use ApplicationContextRunner or ImportAutoConfiguration with SpringApp.run, instead of SpringApplicationBuilder, because builder does not handle AutoConfiguration properly

SimpleTaskAutoConfiguration now has an annotation AutoConfigureBefore the BatchTaskAutoConfig so that it is processed prior.  THis is so that that BatchTaskAutoConfig can create the appropriate beans

SimpleTaskAutoConfiguration has new annotations so that it is AutoConfigured after BindingServiceConfiguration and after SimpleTaskAutoConfiguration.  This is so that it does not attempt to start emitting messages before stream is ready and it can create the appropriate beans after SimpleTaskAutoConfiguration has run.

Renamed SimpleTaskConfiguration to SimpleTaskAutoConfiguration.

Task version updated to  2.1.0

Added missing headers

Updated documentation.

Deprecated EnableTask

Added ability to disable Task autoconfiguration.

Removed @EnableTask from tests

Resolves #439
Resolves #440
Resolves #448
Resolves #466
This commit is contained in:
Glenn Renfro
2018-08-09 17:54:56 -04:00
committed by Michael Minella
parent d2c90c5256
commit d2bc2530cc
70 changed files with 1381 additions and 801 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2018 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.
@@ -17,17 +17,13 @@
package org.springframework.cloud.task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.task.configuration.SingleTaskConfiguration;
import org.springframework.cloud.task.configuration.SimpleTaskConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.task.configuration.SimpleTaskAutoConfiguration;
import org.springframework.cloud.task.configuration.SingleInstanceTaskListener;
import org.springframework.cloud.task.configuration.TaskProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.cloud.task.configuration.SingleTaskConfiguration;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@@ -39,24 +35,23 @@ import static org.junit.Assert.assertNotNull;
* @author Glenn Renfro
* @since 2.0.0
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {TaskProperties.class, SimpleTaskConfiguration.class, SingleTaskConfiguration.class})
@TestPropertySource(properties = {
"spring.cloud.task.single-instance-enabled=true",
})
public class SimpleSingleTaskAutoConfigurationTests {
@Autowired
private ConfigurableApplicationContext context;
@Test
public void testConfiguration() throws Exception {
SingleInstanceTaskListener singleInstanceTaskListener = this.context.getBean(SingleInstanceTaskListener.class);
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
PropertyPlaceholderAutoConfiguration.class,
SimpleTaskAutoConfiguration.class,
SingleTaskConfiguration.class))
.withPropertyValues("spring.cloud.task.singleInstanceEnabled=true");
applicationContextRunner.run((context) -> {
SingleInstanceTaskListener singleInstanceTaskListener = context.getBean(SingleInstanceTaskListener.class);
assertNotNull("singleInstanceTaskListener should not be null", singleInstanceTaskListener);
assertEquals(singleInstanceTaskListener.getClass(), SingleInstanceTaskListener.class);
assertNotNull("singleInstanceTaskListener should not be null", singleInstanceTaskListener);
assertEquals(singleInstanceTaskListener.getClass(), SingleInstanceTaskListener.class); });
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017 the original author or authors.
* Copyright 2017-2018 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.
@@ -17,17 +17,14 @@
package org.springframework.cloud.task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.cloud.task.configuration.SingleTaskConfiguration;
import org.springframework.cloud.task.configuration.SimpleTaskConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.task.configuration.SimpleTaskAutoConfiguration;
import org.springframework.cloud.task.configuration.SingleInstanceTaskListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.cloud.task.configuration.SingleTaskConfiguration;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@@ -39,25 +36,24 @@ import static org.junit.Assert.assertNotNull;
* @author Glenn Renfro
* @since 2.0.0
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {SimpleTaskConfiguration.class,
SingleTaskConfiguration.class,
EmbeddedDataSourceConfiguration.class})
@TestPropertySource(properties = {
"spring.cloud.task.single-instance-enabled=true",
})
public class SimpleSingleTaskAutoConfigurationWithDataSourceTests {
@Autowired
private ConfigurableApplicationContext context;
@Test
public void testConfiguration() throws Exception {
SingleInstanceTaskListener singleInstanceTaskListener = this.context.getBean(SingleInstanceTaskListener.class);
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
PropertyPlaceholderAutoConfiguration.class,
SimpleTaskAutoConfiguration.class,
SingleTaskConfiguration.class,
EmbeddedDataSourceConfiguration.class))
.withPropertyValues("spring.cloud.task.singleInstanceEnabled=true");
applicationContextRunner.run((context) -> {
SingleInstanceTaskListener singleInstanceTaskListener = context.getBean(SingleInstanceTaskListener.class);
assertNotNull("singleInstanceTaskListener should not be null", singleInstanceTaskListener);
assertNotNull("singleInstanceTaskListener should not be null", singleInstanceTaskListener);
assertEquals(singleInstanceTaskListener.getClass(), SingleInstanceTaskListener.class);
assertEquals(singleInstanceTaskListener.getClass(), SingleInstanceTaskListener.class);
});
}
}

View File

@@ -0,0 +1,251 @@
/*
* Copyright 2015-2018 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
*
* 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.
*/
package org.springframework.cloud.task;
import javax.sql.DataSource;
import org.junit.After;
import org.junit.Test;
import org.junit.jupiter.api.function.Executable;
import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.cloud.task.configuration.DefaultTaskConfigurer;
import org.springframework.cloud.task.configuration.SimpleTaskAutoConfiguration;
import org.springframework.cloud.task.configuration.SingleTaskConfiguration;
import org.springframework.cloud.task.configuration.TaskConfigurer;
import org.springframework.cloud.task.repository.TaskExplorer;
import org.springframework.cloud.task.repository.TaskRepository;
import org.springframework.cloud.task.repository.support.SimpleTaskRepository;
import org.springframework.context.ApplicationContextException;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
/**
* Verifies that the beans created by the SimpleTaskAutoConfiguration.
*
* @author Glenn Renfro
* @author Michael Minella
*/
public class SimpleTaskAutoConfigurationTests {
private ConfigurableApplicationContext context;
@After
public void tearDown() {
if (this.context != null) {
this.context.close();
}
}
@Test
public void testRepository() throws Exception {
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
PropertyPlaceholderAutoConfiguration.class,
SimpleTaskAutoConfiguration.class,
SingleTaskConfiguration.class));
applicationContextRunner.run((context) -> {
TaskRepository taskRepository = context.getBean(TaskRepository.class);
assertThat(taskRepository).isNotNull();
Class<?> targetClass = AopProxyUtils.ultimateTargetClass(taskRepository);
assertThat(targetClass).isEqualTo(SimpleTaskRepository.class);
});
}
@Test
public void testAutoConfigurationDisabled() throws Exception {
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
PropertyPlaceholderAutoConfiguration.class,
SimpleTaskAutoConfiguration.class,
SingleTaskConfiguration.class))
.withPropertyValues("spring.cloud.task.autoconfiguration.enabled=false");
Executable executable = () -> {
applicationContextRunner.run((context) -> {
context.getBean(TaskRepository.class);
});
};
verifyExceptionThrown(NoSuchBeanDefinitionException.class, "No qualifying " +
"bean of type 'org.springframework.cloud.task.repository.TaskRepository' " +
"available", executable);
}
@Test
public void testRepositoryInitialized() throws Exception {
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(EmbeddedDataSourceConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
SimpleTaskAutoConfiguration.class,
SingleTaskConfiguration.class));
applicationContextRunner.run((context) -> {
TaskExplorer taskExplorer = context.getBean(TaskExplorer.class);
assertThat(taskExplorer.getTaskExecutionCount()).isEqualTo(1l);
});
}
@Test
public void testRepositoryNotInitialized() throws Exception {
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(EmbeddedDataSourceConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
SimpleTaskAutoConfiguration.class,
SingleTaskConfiguration.class))
.withPropertyValues("spring.cloud.task.tablePrefix=foobarless");
verifyExceptionThrownDefaultExecutable(ApplicationContextException.class, "Failed to start " +
"bean 'taskLifecycleListener'; nested exception is " +
"org.springframework.dao.DataAccessResourceFailureException: " +
"Could not obtain sequence value; nested exception is org.h2.jdbc.JdbcSQLException: " +
"Syntax error in SQL statement \"SELECT FOOBARLESSSEQ.NEXTVAL FROM[*] DUAL \"; " +
"expected \"identifier\"; SQL statement:\n" +
"select foobarlessSEQ.nextval from dual [42001-197]", applicationContextRunner);
}
@Test
public void testMultipleConfigurers() {
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
PropertyPlaceholderAutoConfiguration.class,
SimpleTaskAutoConfiguration.class,
SingleTaskConfiguration.class))
.withUserConfiguration(MultipleConfigurers.class);
verifyExceptionThrownDefaultExecutable(BeanCreationException.class, "Error creating bean " +
"with name 'simpleTaskAutoConfiguration': Invocation of init " +
"method failed; nested exception is java.lang.IllegalStateException:" +
" Expected one TaskConfigurer but found 2", applicationContextRunner);
}
@Test
public void testMultipleDataSources() {
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
SimpleTaskAutoConfiguration.class,
SingleTaskConfiguration.class))
.withUserConfiguration(MultipleDataSources.class);
verifyExceptionThrownDefaultExecutable(BeanCreationException.class, "Error creating bean " +
"with name 'simpleTaskAutoConfiguration': Invocation of init method " +
"failed; nested exception is java.lang.IllegalStateException: To use " +
"the default TaskConfigurer the context must contain no more than " +
"one DataSource, found 2", applicationContextRunner);
}
public void verifyExceptionThrownDefaultExecutable(Class classToCheck, String message,
ApplicationContextRunner applicationContextRunner) {
Executable executable = () -> {
applicationContextRunner.run((context) -> {
Throwable expectedException = context.getStartupFailure();
assertThat(expectedException).isNotNull();
throw expectedException;
});
};
verifyExceptionThrown(classToCheck, message, executable);
}
public void verifyExceptionThrown(Class classToCheck, String message, Executable executable) {
Throwable exception = assertThrows(classToCheck, executable);
assertThat(exception.getMessage()).isEqualTo(message);
}
/**
* Verify that the verifyEnvironment method skips DataSource Proxy Beans when determining
* the number of available dataSources.
*/
@Test
public void testWithDataSourceProxy() {
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(
EmbeddedDataSourceConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
SimpleTaskAutoConfiguration.class,
SingleTaskConfiguration.class))
.withUserConfiguration(DataSourceProxyConfiguration.class);
applicationContextRunner.run((context) -> {
assertThat(context.getBeanNamesForType(DataSource.class).length).isEqualTo(2);
SimpleTaskAutoConfiguration taskConfiguration = context.getBean(SimpleTaskAutoConfiguration.class);
assertThat(taskConfiguration).isNotNull();
assertThat(taskConfiguration.taskExplorer()).isNotNull();
});
}
@Configuration
public static class MultipleConfigurers {
@Bean
public TaskConfigurer taskConfigurer1() {
return new DefaultTaskConfigurer((DataSource) null);
}
@Bean
public TaskConfigurer taskConfigurer2() {
return new DefaultTaskConfigurer((DataSource) null);
}
}
@Configuration
public static class MultipleDataSources {
@Bean
public DataSource dataSource() {
return mock(DataSource.class);
};
@Bean
public DataSource dataSource2() {
return mock(DataSource.class);
};
}
@Configuration
public static class DataSourceProxyConfiguration {
@Autowired
private ConfigurableApplicationContext context;
@Bean
public BeanDefinitionHolder proxyDataSource() {
GenericBeanDefinition proxyBeanDefinition = new GenericBeanDefinition();
proxyBeanDefinition.setBeanClassName("javax.sql.DataSource");
BeanDefinitionHolder myDataSource = new BeanDefinitionHolder(proxyBeanDefinition, "dataSource2");
ScopedProxyUtils.createScopedProxy(myDataSource, (BeanDefinitionRegistry) this.context.getBeanFactory(),
true);
return myDataSource;
}
}
}

View File

@@ -1,194 +0,0 @@
/*
* Copyright 2015-2016 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
*
* 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.
*/
package org.springframework.cloud.task;
import java.util.Properties;
import javax.sql.DataSource;
import org.junit.After;
import org.junit.Test;
import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.cloud.task.configuration.DefaultTaskConfigurer;
import org.springframework.cloud.task.configuration.EnableTask;
import org.springframework.cloud.task.configuration.SimpleTaskConfiguration;
import org.springframework.cloud.task.configuration.TaskConfigurer;
import org.springframework.cloud.task.configuration.TaskProperties;
import org.springframework.cloud.task.repository.TaskExplorer;
import org.springframework.cloud.task.repository.TaskRepository;
import org.springframework.cloud.task.repository.support.SimpleTaskRepository;
import org.springframework.context.ApplicationContextException;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.PropertiesPropertySource;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Verifies that the beans created by the SimpleTaskConfiguration.
*
* @author Glenn Renfro
* @author Michael Minella
*/
public class SimpleTaskConfigurationTests {
private ConfigurableApplicationContext context;
@After
public void tearDown() {
if(this.context != null) {
this.context.close();
}
}
@Test
public void testRepository() throws Exception {
this.context = new AnnotationConfigApplicationContext(SimpleTaskConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
TaskRepository taskRepository = this.context.getBean(TaskRepository.class);
assertThat(taskRepository).isNotNull();
Class<?> targetClass = AopProxyUtils.ultimateTargetClass(taskRepository);
assertThat(targetClass).isEqualTo(SimpleTaskRepository.class);
}
@Test
public void testRepositoryInitialized() throws Exception {
this.context = new AnnotationConfigApplicationContext(EmbeddedDataSourceConfiguration.class, SimpleTaskConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
TaskExplorer taskExplorer = this.context.getBean(TaskExplorer.class);
assertThat(taskExplorer.getTaskExecutionCount()).isEqualTo(1l);
}
@Test
public void testRepositoryNotInitialized() throws Exception {
Properties properties = new Properties();
properties.put("spring.cloud.task.tablePrefix", "foobarless");
PropertiesPropertySource propertiesSource = new PropertiesPropertySource("test", properties);
this.context = new AnnotationConfigApplicationContext();
this.context.getEnvironment().getPropertySources().addLast(propertiesSource);
((AnnotationConfigApplicationContext)context).register(SimpleTaskConfiguration.class);
((AnnotationConfigApplicationContext)context).register(PropertyPlaceholderAutoConfiguration.class);
((AnnotationConfigApplicationContext)context).register(EmbeddedDataSourceConfiguration.class);
boolean wasExceptionThrown = false;
try {
this.context.refresh();
}
catch (ApplicationContextException ex) {
wasExceptionThrown = true;
}
assertThat( wasExceptionThrown).isTrue();
}
@Test(expected = BeanCreationException.class)
public void testMultipleConfigurers() {
this.context = new AnnotationConfigApplicationContext(MultipleConfigurers.class,
PropertyPlaceholderAutoConfiguration.class);
}
@Test(expected = BeanCreationException.class)
public void testMultipleDataSources() {
this.context = new AnnotationConfigApplicationContext(
MultipleDataSources.class, SimpleTaskConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
}
/**
* Verify that the verifyEnvironment method skips DataSource Proxy Beans
* when determining the number of available dataSources.
*/
@Test
public void testWithDataSourceProxy() {
this.context = new AnnotationConfigApplicationContext(EmbeddedDataSourceConfiguration.class, TaskProperties.class,
PropertyPlaceholderAutoConfiguration.class, DataSourceProxyConfiguration.class
);
assertThat(this.context.getBeanNamesForType(DataSource.class).length).isEqualTo(2);
SimpleTaskConfiguration taskConfiguration = this.context.getBean(SimpleTaskConfiguration.class);
assertThat(taskConfiguration).isNotNull();
assertThat(taskConfiguration.taskExplorer()).isNotNull();
}
@Configuration
@EnableTask
public static class MultipleConfigurers {
@Bean
public TaskConfigurer taskConfigurer1() {
return new DefaultTaskConfigurer((DataSource) null);
}
@Bean
public TaskConfigurer taskConfigurer2() {
return new DefaultTaskConfigurer((DataSource) null);
}
}
@Configuration
public static class MultipleDataSources {
@Bean
public DataSource dataSource() {
return mock(DataSource.class);
};
@Bean
public DataSource dataSource2() {
return mock(DataSource.class);
};
}
@Configuration
public static class DataSourceProxyConfiguration {
@Autowired
private ConfigurableApplicationContext context;
@Bean
public SimpleTaskConfiguration simpleTaskConfiguration() {
GenericBeanDefinition proxyBeanDefinition = new GenericBeanDefinition();
proxyBeanDefinition.setBeanClassName("javax.sql.DataSource");
BeanDefinitionHolder myDataSource = new BeanDefinitionHolder(proxyBeanDefinition,"dataSource2");
ScopedProxyUtils.createScopedProxy(myDataSource, (BeanDefinitionRegistry) this.context.getBeanFactory(), true);
return new SimpleTaskConfiguration();
}
}
}

View File

@@ -1,3 +1,19 @@
/*
* Copyright 2015-2018 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
*
* 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.
*/
package org.springframework.cloud.task;
import org.junit.After;
@@ -5,14 +21,15 @@ import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.rule.OutputCapture;
import org.springframework.cloud.task.configuration.EnableTask;
import org.springframework.cloud.task.configuration.SimpleTaskAutoConfiguration;
import org.springframework.context.ApplicationContextException;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.junit.Assert.assertTrue;
@@ -48,11 +65,31 @@ public class TaskCoreTests {
@Test
public void successfulTaskTest() {
applicationContext = new SpringApplicationBuilder().sources(new Class[]{TaskConfiguration.class,
PropertyPlaceholderAutoConfiguration.class}).build().run(new String[]{
"--spring.cloud.task.closecontext.enable=false",
"--spring.cloud.task.name=" + TASK_NAME,
"--spring.main.web-environment=false"});
this.applicationContext = SpringApplication.run( TaskConfiguration.class,
new String[] {
"--spring.cloud.task.closecontext.enable=false",
"--spring.cloud.task.name=" + TASK_NAME,
"--spring.main.web-environment=false" });
String output = this.outputCapture.toString();
assertTrue("Test results do not show create task message: " + output,
output.contains(CREATE_TASK_MESSAGE));
assertTrue("Test results do not show success message: " + output,
output.contains(UPDATE_TASK_MESSAGE));
assertTrue("Test results have incorrect exit code: " + output,
output.contains(SUCCESS_EXIT_CODE_MESSAGE));
}
/**
* Test to verify that deprecated annotation does not affect task execution.
*/
@Test
public void successfulTaskTestWithAnnotation() {
this.applicationContext = SpringApplication.run( TaskConfigurationWithAnotation.class,
new String[] {
"--spring.cloud.task.closecontext.enable=false",
"--spring.cloud.task.name=" + TASK_NAME,
"--spring.main.web-environment=false" });
String output = this.outputCapture.toString();
assertTrue("Test results do not show create task message: " + output,
@@ -67,11 +104,11 @@ public class TaskCoreTests {
public void exceptionTaskTest() {
boolean exceptionFired = false;
try {
applicationContext = new SpringApplicationBuilder().sources(TaskExceptionConfiguration.class,
PropertyPlaceholderAutoConfiguration.class).build().run(new String[]{
"--spring.cloud.task.closecontext.enable=false",
"--spring.cloud.task.name=" + TASK_NAME,
"--spring.main.web-environment=false"});
this.applicationContext = SpringApplication.run( TaskExceptionConfiguration.class,
new String[] {
"--spring.cloud.task.closecontext.enable=false",
"--spring.cloud.task.name=" + TASK_NAME,
"--spring.main.web-environment=false" });
}
catch (IllegalStateException exception) {
exceptionFired = true;
@@ -95,8 +132,8 @@ public class TaskCoreTests {
public void invalidExecutionId() {
boolean exceptionFired = false;
try {
applicationContext = new SpringApplicationBuilder().sources(TaskExceptionConfiguration.class,
PropertyPlaceholderAutoConfiguration.class).build().run(new String[]{
applicationContext = this.applicationContext = SpringApplication.run(
TaskExceptionConfiguration.class, new String[]{
"--spring.cloud.task.closecontext.enable=false",
"--spring.cloud.task.name=" + TASK_NAME,
"--spring.main.web-environment=false",
@@ -112,8 +149,7 @@ public class TaskCoreTests {
output.contains(EXCEPTION_INVALID_TASK_EXECUTION_ID));
}
@Configuration
@EnableTask
@ImportAutoConfiguration({SimpleTaskAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class})
public static class TaskConfiguration {
@Bean
@@ -126,8 +162,21 @@ public class TaskCoreTests {
}
}
@Configuration
@EnableTask
@ImportAutoConfiguration({SimpleTaskAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class})
public static class TaskConfigurationWithAnotation {
@Bean
public CommandLineRunner commandLineRunner() {
return new CommandLineRunner() {
@Override
public void run(String... strings) throws Exception {
}
};
}
}
@ImportAutoConfiguration({SimpleTaskAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class})
public static class TaskExceptionConfiguration {
@Bean

View File

@@ -26,7 +26,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.cloud.task.configuration.SimpleTaskConfiguration;
import org.springframework.cloud.task.configuration.SimpleTaskAutoConfiguration;
import org.springframework.cloud.task.configuration.TaskConfigurer;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
@@ -42,7 +42,7 @@ import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
* @since 2.0.0
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {SimpleTaskConfiguration.class,
@ContextConfiguration(classes = {SimpleTaskAutoConfiguration.class,
EmbeddedDataSourceConfiguration.class})
public class TaskRepositoryInitializerDefaultTaskConfigurerTests {

View File

@@ -27,7 +27,8 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.cloud.task.configuration.DefaultTaskConfigurer;
import org.springframework.cloud.task.configuration.SimpleTaskConfiguration;
import org.springframework.cloud.task.configuration.SimpleTaskAutoConfiguration;
import org.springframework.cloud.task.configuration.SingleTaskConfiguration;
import org.springframework.cloud.task.configuration.TaskConfigurer;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
@@ -43,7 +44,7 @@ import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
* @since 2.0.0
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {SimpleTaskConfiguration.class,
@ContextConfiguration(classes = {SimpleTaskAutoConfiguration.class, SingleTaskConfiguration.class,
EmbeddedDataSourceConfiguration.class,
DefaultTaskConfigurer.class})
public class TaskRepositoryInitializerNoDataSourceTaskConfigurerTests {

View File

@@ -1,3 +1,19 @@
/*
* Copyright 2017-2018 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
*
* 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.
*/
package org.springframework.cloud.task.configuration;
import org.junit.Test;
@@ -7,10 +23,6 @@ import org.junit.runners.Suite.SuiteClasses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.task.repository.TaskRepository;
import org.springframework.cloud.task.repository.support.SimpleTaskRepository;
import org.springframework.cloud.task.repository.support.TaskExecutionDaoFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
@@ -21,7 +33,7 @@ import static org.junit.Assert.assertThat;
@RunWith(Suite.class)
@SuiteClasses({
TaskPropertiesTests.CloseContextEnabledTest.class,
TaskPropertiesTests.CloseContextEnabledTest.class
})
@@ -36,21 +48,15 @@ public class TaskPropertiesTests {
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes={TaskPropertiesTests.Config.class}, properties = { "spring.cloud.task.closecontextEnabled=false" })
@SpringBootTest(classes={TaskPropertiesTests.Config.class,
SimpleTaskAutoConfiguration.class, SingleTaskConfiguration.class},
properties = { "spring.cloud.task.closecontextEnabled=false" })
@DirtiesContext
public static class CloseContextEnabledTest extends TaskPropertiesTests {}
@Configuration
@EnableTask
public static class Config {
@Bean
TaskExecutionDaoFactoryBean taskExecutionDaoFactoryBean() {
return new TaskExecutionDaoFactoryBean();
}
@Bean
public TaskRepository taskRepository(TaskExecutionDaoFactoryBean tefb) {
return new SimpleTaskRepository(tefb);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016 the original author or authors.
* Copyright 2016-2018 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.
@@ -29,11 +29,9 @@ import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.cloud.task.listener.annotation.AfterTask;
import org.springframework.cloud.task.listener.annotation.BeforeTask;
import org.springframework.cloud.task.listener.annotation.FailedTask;
import org.springframework.cloud.task.listener.annotation.TaskListenerExecutorFactoryBean;
import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.cloud.task.util.TestDefaultConfiguration;
import org.springframework.cloud.task.util.TestListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -290,12 +288,6 @@ public class TaskExecutionListenerTests {
return new AnnotatedTaskListener();
}
@Bean
public TaskListenerExecutorFactoryBean taskListenerExecutor(ConfigurableApplicationContext context) throws Exception
{
return new TaskListenerExecutorFactoryBean(context);
}
public static class AnnotatedTaskListener extends TestListener {
@BeforeTask
@@ -331,12 +323,6 @@ public class TaskExecutionListenerTests {
return new AnnotatedTaskListener();
}
@Bean
public TaskListenerExecutorFactoryBean taskListenerExecutor(ConfigurableApplicationContext context) throws Exception
{
return new TaskListenerExecutorFactoryBean(context);
}
public static class AnnotatedTaskListener {
@BeforeTask
@@ -365,11 +351,6 @@ public class TaskExecutionListenerTests {
return new AnnotatedTaskListener();
}
@Bean
public TaskListenerExecutorFactoryBean taskListenerExecutor(ConfigurableApplicationContext context) throws Exception
{
return new TaskListenerExecutorFactoryBean(context);
}
public static class AnnotatedTaskListener {
@@ -400,12 +381,6 @@ public class TaskExecutionListenerTests {
return new AnnotatedTaskListener();
}
@Bean
public TaskListenerExecutorFactoryBean taskListenerExecutor(ConfigurableApplicationContext context) throws Exception
{
return new TaskListenerExecutorFactoryBean(context);
}
public static class AnnotatedTaskListener extends TestListener{
@BeforeTask

View File

@@ -0,0 +1,141 @@
/*
* Copyright 2018 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
*
* 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.
*/
package org.springframework.cloud.task.listener;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.task.listener.annotation.AfterTask;
import org.springframework.cloud.task.listener.annotation.BeforeTask;
import org.springframework.cloud.task.listener.annotation.FailedTask;
import org.springframework.cloud.task.listener.annotation.TaskListenerExecutor;
import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Verifies that the {@link TaskListenerExecutorObjectFactory} retrieves the
* {@link TaskListenerExecutor}.
*
* @author Glenn Renfro
* @since 2.1.0
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = { TaskListenerExecutorObjectFactoryTests.TaskExecutionListenerConfiguration.class })
public class TaskListenerExecutorObjectFactoryTests {
public static final String BEFORE_LISTENER = "BEFORE LISTENER";
public static final String AFTER_LISTENER = "AFTER LISTENER";
public static final String FAIL_LISTENER = "FAIL LISTENER";
public static List<TaskExecution> taskExecutionListenerResults = new ArrayList<>(3);
@Autowired
private ConfigurableApplicationContext context;
private TaskListenerExecutor taskListenerExecutor;
private TaskListenerExecutorObjectFactory taskListenerExecutorObjectFactory;
@Before
public void setup() {
taskExecutionListenerResults.clear();
this.taskListenerExecutorObjectFactory = new TaskListenerExecutorObjectFactory(this.context);
this.taskListenerExecutor = this.taskListenerExecutorObjectFactory.getObject();
}
@Test
public void verifyTaskStartupListener() {
this.taskListenerExecutor.onTaskStartup(createSampleTaskExecution(BEFORE_LISTENER));
validateSingleEntry(BEFORE_LISTENER);
}
@Test
public void verifyTaskFailedListener() {
this.taskListenerExecutor.onTaskFailed(createSampleTaskExecution(FAIL_LISTENER),
new IllegalStateException("oops"));
validateSingleEntry(FAIL_LISTENER);
}
@Test
public void verifyTaskEndListener() {
this.taskListenerExecutor.onTaskEnd(createSampleTaskExecution(AFTER_LISTENER));
validateSingleEntry(AFTER_LISTENER);
}
@Test
public void verifyAllListener() {
this.taskListenerExecutor.onTaskStartup(createSampleTaskExecution(BEFORE_LISTENER));
this.taskListenerExecutor.onTaskFailed(createSampleTaskExecution(FAIL_LISTENER),
new IllegalStateException("oops"));
this.taskListenerExecutor.onTaskEnd(createSampleTaskExecution(AFTER_LISTENER));
assertThat(taskExecutionListenerResults.size()).isEqualTo(3);
assertThat(taskExecutionListenerResults.get(0).getTaskName()).isEqualTo(BEFORE_LISTENER);
assertThat(taskExecutionListenerResults.get(1).getTaskName()).isEqualTo(FAIL_LISTENER);
assertThat(taskExecutionListenerResults.get(2).getTaskName()).isEqualTo(AFTER_LISTENER);
}
private TaskExecution createSampleTaskExecution(String taskName) {
TaskExecution taskExecution = new TaskExecution();
taskExecution.setTaskName(taskName);
return taskExecution;
}
private void validateSingleEntry(String event) {
assertThat(taskExecutionListenerResults.size()).isEqualTo(1);
assertThat(taskExecutionListenerResults.get(0).getTaskName()).isEqualTo(event);
}
@Configuration
public static class TaskExecutionListenerConfiguration {
@Bean
public TaskRunComponent taskRunComponent() {
return new TaskRunComponent();
}
}
public static class TaskRunComponent {
@BeforeTask
public void initBeforeListener(TaskExecution taskExecution) {
TaskListenerExecutorObjectFactoryTests.taskExecutionListenerResults.add(taskExecution);
}
@AfterTask
public void initAfterListener(TaskExecution taskExecution) {
TaskListenerExecutorObjectFactoryTests.taskExecutionListenerResults.add(taskExecution);
}
@FailedTask
public void initFailedListener(TaskExecution taskExecution, Throwable exception) {
TaskListenerExecutorObjectFactoryTests.taskExecutionListenerResults.add(taskExecution);
}
}
}

View File

@@ -28,7 +28,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.cloud.task.configuration.SimpleTaskConfiguration;
import org.springframework.cloud.task.configuration.SimpleTaskAutoConfiguration;
import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.cloud.task.repository.TaskExplorer;
import org.springframework.cloud.task.repository.TaskRepository;
@@ -49,7 +49,7 @@ import static org.junit.Assert.assertEquals;
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {EmbeddedDataSourceConfiguration.class,
SimpleTaskConfiguration.class,
SimpleTaskAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class})
public class SimpleTaskRepositoryJdbcTests {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015-2016 the original author or authors.
* Copyright 2015-2018 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.
@@ -26,7 +26,7 @@ import org.junit.rules.ExpectedException;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.cloud.task.configuration.SimpleTaskConfiguration;
import org.springframework.cloud.task.configuration.SimpleTaskAutoConfiguration;
import org.springframework.cloud.task.configuration.TestConfiguration;
import org.springframework.cloud.task.repository.dao.MapTaskExecutionDao;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -89,7 +89,7 @@ public class TaskDatabaseInitializerTests {
@Test(expected = BeanCreationException.class)
public void testMultipleDataSourcesContext() throws Exception {
this.context = new AnnotationConfigApplicationContext();
this.context.register( SimpleTaskConfiguration.class,
this.context.register( SimpleTaskAutoConfiguration.class,
EmbeddedDataSourceConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
DataSource dataSource = mock(DataSource.class);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2015-2016 the original author or authors.
* Copyright 2015-2018 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.
@@ -24,6 +24,7 @@ import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.task.configuration.TaskProperties;
import org.springframework.cloud.task.listener.TaskLifecycleListener;
import org.springframework.cloud.task.listener.TaskListenerExecutorObjectFactory;
import org.springframework.cloud.task.repository.TaskExplorer;
import org.springframework.cloud.task.repository.TaskNameResolver;
import org.springframework.cloud.task.repository.TaskRepository;
@@ -74,10 +75,15 @@ public class TestDefaultConfiguration implements InitializingBean {
return new SimpleTaskNameResolver();
}
@Bean
public TaskListenerExecutorObjectFactory taskListenerExecutorObjectProvider(ConfigurableApplicationContext context) {
return new TaskListenerExecutorObjectFactory(context);
}
@Bean
public TaskLifecycleListener taskHandler(TaskExplorer taskExplorer){
return new TaskLifecycleListener(taskRepository(), taskNameResolver(),
applicationArguments, taskExplorer, taskProperties);
applicationArguments, taskExplorer, taskProperties, taskListenerExecutorObjectProvider(context));
}
@Override