Refactored building of TaskLifecycleListener
When using Spring Boot's datasource initialization features, there was the possibility that the datasource used by the TaskLifecycleListener was not ready by the time it was needed for regular injection. This commit addresses that by obtaining the datasource at the last possible moment. Resolves spring-cloud/spring-cloud-task#83 Updates per code review
This commit is contained in:
committed by
Glenn Renfro
parent
3b252680e7
commit
27c8c6d76d
@@ -18,15 +18,15 @@ package org.springframework.cloud.task.configuration;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.task.repository.TaskExplorer;
|
||||
import org.springframework.cloud.task.repository.TaskRepository;
|
||||
import org.springframework.cloud.task.repository.dao.JdbcTaskExecutionDao;
|
||||
import org.springframework.cloud.task.repository.dao.MapTaskExecutionDao;
|
||||
import org.springframework.cloud.task.repository.dao.TaskExecutionDao;
|
||||
import org.springframework.cloud.task.repository.support.SimpleTaskExplorer;
|
||||
import org.springframework.cloud.task.repository.support.SimpleTaskRepository;
|
||||
import org.springframework.cloud.task.repository.support.TaskExecutionDaoFactoryBean;
|
||||
import org.springframework.cloud.task.repository.support.TaskRepositoryInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
@@ -38,7 +38,7 @@ import org.springframework.transaction.PlatformTransactionManager;
|
||||
*/
|
||||
|
||||
@Configuration
|
||||
public class TestConfiguration {
|
||||
public class TestConfiguration implements InitializingBean {
|
||||
|
||||
@Autowired(required = false)
|
||||
private DataSource dataSource;
|
||||
@@ -46,6 +46,11 @@ public class TestConfiguration {
|
||||
@Autowired(required = false)
|
||||
private ResourceLoader resourceLoader;
|
||||
|
||||
@Autowired
|
||||
private ConfigurableApplicationContext applicationContext;
|
||||
|
||||
private TaskExecutionDaoFactoryBean taskExecutionDaoFactoryBean;
|
||||
|
||||
@Bean
|
||||
public TaskRepositoryInitializer taskRepositoryInitializer() throws Exception {
|
||||
TaskRepositoryInitializer taskRepositoryInitializer = new TaskRepositoryInitializer();
|
||||
@@ -57,8 +62,13 @@ public class TestConfiguration {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TaskRepository taskRepository(TaskExecutionDao taskExecutionDao){
|
||||
return new SimpleTaskRepository(taskExecutionDao);
|
||||
public TaskExplorer taskExplorer() throws Exception {
|
||||
return new SimpleTaskExplorer(this.taskExecutionDaoFactoryBean);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TaskRepository taskRepository(){
|
||||
return new SimpleTaskRepository(this.taskExecutionDaoFactoryBean);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@@ -71,18 +81,8 @@ public class TestConfiguration {
|
||||
}
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TaskExplorer taskExplorer(TaskExecutionDao taskExecutionDao) {
|
||||
return new SimpleTaskExplorer(taskExecutionDao);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TaskExecutionDao taskExecutionDao() {
|
||||
if(dataSource != null) {
|
||||
return new JdbcTaskExecutionDao(dataSource);
|
||||
}
|
||||
else {
|
||||
return new MapTaskExecutionDao();
|
||||
}
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
this.taskExecutionDaoFactoryBean = new TaskExecutionDaoFactoryBean(this.applicationContext);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,9 +21,7 @@ import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -105,15 +103,6 @@ public class TaskLifecycleListenerTests {
|
||||
verifyTaskExecution(0, true, 1, exception);
|
||||
}
|
||||
|
||||
private static String stackTraceToString(Throwable exception) {
|
||||
StringWriter writer = new StringWriter();
|
||||
PrintWriter printWriter = new PrintWriter(writer);
|
||||
|
||||
exception.printStackTrace(printWriter);
|
||||
|
||||
return writer.toString();
|
||||
}
|
||||
|
||||
private void verifyTaskExecution(int numberOfParams, boolean update, Integer exitCode, Throwable exception) {
|
||||
this.taskExplorer = context.getBean(TaskExplorer.class);
|
||||
|
||||
@@ -195,7 +184,7 @@ public class TaskLifecycleListenerTests {
|
||||
|
||||
@Override
|
||||
public List<String> getOptionValues(String s) {
|
||||
return Arrays.asList(this.args.get(s));
|
||||
return Collections.singletonList(this.args.get(s));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -16,10 +16,10 @@
|
||||
|
||||
package org.springframework.cloud.task.repository.support;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertNotNull;
|
||||
import static junit.framework.Assert.assertNull;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -34,8 +34,6 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
@@ -51,11 +49,10 @@ import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfigurati
|
||||
import org.springframework.cloud.task.configuration.TestConfiguration;
|
||||
import org.springframework.cloud.task.repository.TaskExecution;
|
||||
import org.springframework.cloud.task.repository.TaskExplorer;
|
||||
import org.springframework.cloud.task.repository.dao.JdbcTaskExecutionDao;
|
||||
import org.springframework.cloud.task.repository.dao.TaskExecutionDao;
|
||||
import org.springframework.cloud.task.util.TestDBUtils;
|
||||
import org.springframework.cloud.task.repository.TaskRepository;
|
||||
import org.springframework.cloud.task.util.TestVerifierUtils;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
@@ -68,14 +65,11 @@ public class SimpleTaskExplorerTests {
|
||||
|
||||
private AnnotationConfigApplicationContext context;
|
||||
|
||||
@Autowired
|
||||
private TaskExecutionDao dao;
|
||||
|
||||
@Autowired
|
||||
private TaskExplorer taskExplorer;
|
||||
|
||||
@Autowired(required = false)
|
||||
private DataSource dataSource;
|
||||
@Autowired
|
||||
private TaskRepository taskRepository;
|
||||
|
||||
private DaoType testType;
|
||||
|
||||
@@ -95,17 +89,12 @@ public class SimpleTaskExplorerTests {
|
||||
@Before
|
||||
public void testDefaultContext() throws Exception {
|
||||
|
||||
if (testType == DaoType.jdbc) {
|
||||
if (this.testType == DaoType.jdbc) {
|
||||
initializeJdbcExplorerTest();
|
||||
dao = new JdbcTaskExecutionDao(dataSource);
|
||||
((JdbcTaskExecutionDao)dao).
|
||||
setTaskIncrementer(TestDBUtils.getIncrementer(dataSource));
|
||||
}
|
||||
else {
|
||||
initializeMapExplorerTest();
|
||||
}
|
||||
|
||||
taskExplorer = new SimpleTaskExplorer(dao);
|
||||
}
|
||||
|
||||
@After
|
||||
@@ -176,7 +165,7 @@ public class SimpleTaskExplorerTests {
|
||||
|
||||
for (; i < (COMPLETE_COUNT + TEST_COUNT); i++) {
|
||||
TaskExecution expectedTaskExecution = new TaskExecution(i, 0, TASK_NAME, new Date(), null, null, new ArrayList<String>(0));
|
||||
dao.saveTaskExecution(expectedTaskExecution);
|
||||
this.taskRepository.createTaskExecution(expectedTaskExecution);
|
||||
expectedResults.put(expectedTaskExecution.getExecutionId(), expectedTaskExecution);
|
||||
}
|
||||
Pageable pageable = new PageRequest(0, 10);
|
||||
@@ -211,7 +200,7 @@ public class SimpleTaskExplorerTests {
|
||||
for (int i = 0; i < TEST_COUNT; i++) {
|
||||
TaskExecution expectedTaskExecution = TestVerifierUtils.createSampleTaskExecutionNoParam();
|
||||
expectedTaskExecution.setTaskName(TASK_NAME);
|
||||
dao.saveTaskExecution(expectedTaskExecution);
|
||||
this.taskRepository.createTaskExecution(expectedTaskExecution);
|
||||
expectedResults.put(expectedTaskExecution.getExecutionId(), expectedTaskExecution);
|
||||
}
|
||||
|
||||
@@ -318,7 +307,7 @@ public class SimpleTaskExplorerTests {
|
||||
|
||||
private TaskExecution createAndSaveTaskExecution(int i) {
|
||||
TaskExecution taskExecution = TestVerifierUtils.createSampleTaskExecution(i);
|
||||
dao.saveTaskExecution(taskExecution);
|
||||
this.taskRepository.createTaskExecution(taskExecution);
|
||||
return taskExecution;
|
||||
}
|
||||
|
||||
@@ -378,4 +367,10 @@ public class SimpleTaskExplorerTests {
|
||||
}
|
||||
|
||||
private enum DaoType{jdbc, map}
|
||||
|
||||
@Configuration
|
||||
public static class DataSourceConfiguration{}
|
||||
|
||||
@Configuration
|
||||
public static class EmptyConfiguration{}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,9 @@ import org.springframework.cloud.task.repository.TaskRepository;
|
||||
import org.springframework.cloud.task.repository.dao.MapTaskExecutionDao;
|
||||
import org.springframework.cloud.task.util.TaskExecutionCreator;
|
||||
import org.springframework.cloud.task.util.TestVerifierUtils;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Tests for the SimpleTaskRepository that uses Map as a datastore.
|
||||
@@ -38,9 +41,8 @@ public class SimpleTaskRepositoryMapTests {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MapTaskRepositoryFactoryBean factoryBean =
|
||||
new MapTaskRepositoryFactoryBean();
|
||||
taskRepository = factoryBean.getObject();
|
||||
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(EmptyConfiguration.class);
|
||||
this.taskRepository = new SimpleTaskRepository(new TaskExecutionDaoFactoryBean(context));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -80,4 +82,7 @@ public class SimpleTaskRepositoryMapTests {
|
||||
taskMap.containsKey(taskExecutionId));
|
||||
return taskMap.get(taskExecutionId);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class EmptyConfiguration{}
|
||||
}
|
||||
|
||||
@@ -69,7 +69,8 @@ public class TaskDatabaseInitializerTests {
|
||||
|
||||
@Test
|
||||
public void testNoDatabase() throws Exception {
|
||||
SimpleTaskRepository repository = new SimpleTaskRepository(new MapTaskExecutionDao());
|
||||
this.context = new AnnotationConfigApplicationContext(EmptyConfiguration.class);
|
||||
SimpleTaskRepository repository = new SimpleTaskRepository(new TaskExecutionDaoFactoryBean(this.context));
|
||||
assertThat(repository.getTaskExecutionDao(), instanceOf(MapTaskExecutionDao.class));
|
||||
MapTaskExecutionDao dao = (MapTaskExecutionDao) repository.getTaskExecutionDao();
|
||||
assertEquals(0, dao.getTaskExecutions().size());
|
||||
|
||||
@@ -0,0 +1,214 @@
|
||||
/*
|
||||
* Copyright 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.repository.support;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.cloud.task.repository.dao.JdbcTaskExecutionDao;
|
||||
import org.springframework.cloud.task.repository.dao.MapTaskExecutionDao;
|
||||
import org.springframework.cloud.task.repository.dao.TaskExecutionDao;
|
||||
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.context.support.GenericApplicationContext;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
/**
|
||||
* @author Michael Minella
|
||||
*/
|
||||
public class TaskExecutionDaoFactoryBeanTests {
|
||||
|
||||
private ConfigurableApplicationContext context;
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
if(this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetObjectType() {
|
||||
assertEquals(new TaskExecutionDaoFactoryBean().getObjectType(), TaskExecutionDao.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsSingleton() {
|
||||
assertTrue(new TaskExecutionDaoFactoryBean().isSingleton());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testConstructorValidation() {
|
||||
new TaskExecutionDaoFactoryBean(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapTaskExecutionDaoWithAppContext() throws Exception {
|
||||
this.context = new GenericApplicationContext();
|
||||
this.context.refresh();
|
||||
|
||||
TaskExecutionDaoFactoryBean factoryBean = new TaskExecutionDaoFactoryBean(this.context);
|
||||
TaskExecutionDao taskExecutionDao = factoryBean.getObject();
|
||||
|
||||
assertTrue(taskExecutionDao instanceof MapTaskExecutionDao);
|
||||
|
||||
TaskExecutionDao taskExecutionDao2 = factoryBean.getObject();
|
||||
|
||||
assertTrue(taskExecutionDao == taskExecutionDao2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapTaskExecutionDaoWithoutAppContext() throws Exception {
|
||||
TaskExecutionDaoFactoryBean factoryBean = new TaskExecutionDaoFactoryBean();
|
||||
TaskExecutionDao taskExecutionDao = factoryBean.getObject();
|
||||
|
||||
assertTrue(taskExecutionDao instanceof MapTaskExecutionDao);
|
||||
|
||||
TaskExecutionDao taskExecutionDao2 = factoryBean.getObject();
|
||||
|
||||
assertTrue(taskExecutionDao == taskExecutionDao2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultDataSourceConfiguration() throws Exception {
|
||||
this.context = new AnnotationConfigApplicationContext(DefaultDataSourceConfiguration.class);
|
||||
|
||||
TaskExecutionDaoFactoryBean factoryBean = new TaskExecutionDaoFactoryBean(this.context);
|
||||
TaskExecutionDao taskExecutionDao = factoryBean.getObject();
|
||||
|
||||
assertTrue(taskExecutionDao instanceof JdbcTaskExecutionDao);
|
||||
|
||||
TaskExecutionDao taskExecutionDao2 = factoryBean.getObject();
|
||||
|
||||
assertTrue(taskExecutionDao == taskExecutionDao2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNonDefaultNameDataSourceConfiguration() throws Exception {
|
||||
this.context = new AnnotationConfigApplicationContext(AlternativeDataSourceConfiguration.class);
|
||||
|
||||
TaskExecutionDaoFactoryBean factoryBean = new TaskExecutionDaoFactoryBean(this.context);
|
||||
TaskExecutionDao taskExecutionDao = factoryBean.getObject();
|
||||
|
||||
assertTrue(taskExecutionDao instanceof JdbcTaskExecutionDao);
|
||||
|
||||
TaskExecutionDao taskExecutionDao2 = factoryBean.getObject();
|
||||
|
||||
assertTrue(taskExecutionDao == taskExecutionDao2);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testMissingCustomDataSourceNameConfiguration() throws Exception {
|
||||
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(AlternativeDataSourceConfiguration.class);
|
||||
|
||||
TaskExecutionDaoFactoryBean factoryBean = new TaskExecutionDaoFactoryBean(context);
|
||||
factoryBean.setDataSourceName("wrongName");
|
||||
factoryBean.getObject();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomDataSourceNameConfiguration() throws Exception {
|
||||
this.context = new AnnotationConfigApplicationContext(AlternativeDataSourceConfiguration.class);
|
||||
|
||||
TaskExecutionDaoFactoryBean factoryBean = new TaskExecutionDaoFactoryBean(this.context);
|
||||
factoryBean.setDataSourceName("notDataSource");
|
||||
TaskExecutionDao taskExecutionDao = factoryBean.getObject();
|
||||
|
||||
assertTrue(taskExecutionDao instanceof JdbcTaskExecutionDao);
|
||||
|
||||
TaskExecutionDao taskExecutionDao2 = factoryBean.getObject();
|
||||
|
||||
assertTrue(taskExecutionDao == taskExecutionDao2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomDataSourceNameConfigurationWithMultipleDataSources() throws Exception {
|
||||
this.context = new AnnotationConfigApplicationContext(MultipleDataSourceConfiguration.class);
|
||||
|
||||
TaskExecutionDaoFactoryBean factoryBean = new TaskExecutionDaoFactoryBean(this.context);
|
||||
factoryBean.setDataSourceName("useThisDataSource");
|
||||
JdbcTaskExecutionDao taskExecutionDao = (JdbcTaskExecutionDao) factoryBean.getObject();
|
||||
|
||||
Object usedDataSource = ReflectionTestUtils.getField(taskExecutionDao, "dataSource");
|
||||
|
||||
assertTrue(usedDataSource == this.context.getBean("useThisDataSource"));
|
||||
|
||||
TaskExecutionDao taskExecutionDao2 = factoryBean.getObject();
|
||||
|
||||
assertTrue(taskExecutionDao == taskExecutionDao2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSettingTablePrefix() throws Exception {
|
||||
this.context = new AnnotationConfigApplicationContext(DefaultDataSourceConfiguration.class);
|
||||
|
||||
TaskExecutionDaoFactoryBean factoryBean = new TaskExecutionDaoFactoryBean(this.context);
|
||||
factoryBean.setTablePrefix("foo_");
|
||||
TaskExecutionDao taskExecutionDao = factoryBean.getObject();
|
||||
|
||||
assertEquals("foo_", ReflectionTestUtils.getField(taskExecutionDao, "tablePrefix"));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class DefaultDataSourceConfiguration {
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource() {
|
||||
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2);
|
||||
return builder.build();
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class AlternativeDataSourceConfiguration {
|
||||
|
||||
@Bean
|
||||
public DataSource notDataSource() {
|
||||
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2);
|
||||
return builder.build();
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class MultipleDataSourceConfiguration {
|
||||
|
||||
@Bean
|
||||
public DataSource useThisDataSource() {
|
||||
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
|
||||
.setType(EmbeddedDatabaseType.H2)
|
||||
.setName("useThisDataSource");
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataSource dontUseThisDataSource() {
|
||||
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
|
||||
.setType(EmbeddedDatabaseType.H2)
|
||||
.setName("dontUseThisDataSource");
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
/*
|
||||
* Copyright 2015 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.repository.support;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.cloud.task.repository.TaskRepository;
|
||||
import org.springframework.cloud.task.repository.dao.JdbcTaskExecutionDao;
|
||||
import org.springframework.cloud.task.repository.dao.MapTaskExecutionDao;
|
||||
import org.springframework.cloud.task.util.TestDBUtils;
|
||||
|
||||
/**
|
||||
* Tests that the TaskRepositoryFactoryBeans produce the correct repositories.
|
||||
*
|
||||
* @author Glenn Renfro
|
||||
*/
|
||||
|
||||
public class TaskRepositoryFactoryBeanTests {
|
||||
|
||||
@Test
|
||||
public void testJdbcTaskRepositoryFactoryBean() throws Exception{
|
||||
DataSource dataSource = TestDBUtils.getMockDataSource("HSQL Database Engine");
|
||||
JdbcTaskRepositoryFactoryBean factory = new JdbcTaskRepositoryFactoryBean(dataSource);
|
||||
TaskRepository repository = factory.getObject();
|
||||
assertThat(repository, instanceOf(SimpleTaskRepository.class));
|
||||
assertThat(((SimpleTaskRepository) repository).getTaskExecutionDao(),
|
||||
instanceOf(JdbcTaskExecutionDao.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMapTaskRepositoryFactoryBean() {
|
||||
MapTaskRepositoryFactoryBean factory = new MapTaskRepositoryFactoryBean();
|
||||
TaskRepository repository = factory.getObject();
|
||||
assertThat(repository, instanceOf(SimpleTaskRepository.class));
|
||||
assertThat(((SimpleTaskRepository) repository).getTaskExecutionDao(),
|
||||
instanceOf(MapTaskExecutionDao.class));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,16 +16,18 @@
|
||||
|
||||
package org.springframework.cloud.task.util;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.cloud.task.listener.TaskLifecycleListener;
|
||||
import org.springframework.cloud.task.repository.TaskExplorer;
|
||||
import org.springframework.cloud.task.repository.TaskNameResolver;
|
||||
import org.springframework.cloud.task.repository.TaskRepository;
|
||||
import org.springframework.cloud.task.repository.dao.MapTaskExecutionDao;
|
||||
import org.springframework.cloud.task.repository.support.SimpleTaskExplorer;
|
||||
import org.springframework.cloud.task.repository.support.SimpleTaskNameResolver;
|
||||
import org.springframework.cloud.task.repository.support.SimpleTaskRepository;
|
||||
import org.springframework.cloud.task.repository.support.TaskExecutionDaoFactoryBean;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@@ -35,20 +37,27 @@ import org.springframework.context.annotation.Configuration;
|
||||
* @author Glenn Renfro
|
||||
*/
|
||||
@Configuration
|
||||
public class TestDefaultConfiguration {
|
||||
public class TestDefaultConfiguration implements InitializingBean {
|
||||
|
||||
private MapTaskExecutionDao dao;
|
||||
private TaskExecutionDaoFactoryBean factoryBean;
|
||||
|
||||
@Autowired(required = false)
|
||||
private ApplicationArguments applicationArguments;
|
||||
|
||||
@Autowired
|
||||
private ConfigurableApplicationContext context;
|
||||
|
||||
public TestDefaultConfiguration() {
|
||||
this.dao = new MapTaskExecutionDao();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TaskRepository taskRepository(){
|
||||
return new SimpleTaskRepository(this.dao);
|
||||
return new SimpleTaskRepository(this.factoryBean);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TaskExplorer taskExplorer() throws Exception {
|
||||
return new SimpleTaskExplorer(this.factoryBean);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@@ -61,8 +70,8 @@ public class TestDefaultConfiguration {
|
||||
return new TaskLifecycleListener(taskRepository(), taskNameResolver(), applicationArguments);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TaskExplorer taskExplorer() {
|
||||
return new SimpleTaskExplorer(this.dao);
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
this.factoryBean = new TaskExecutionDaoFactoryBean(this.context);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user