Repository is not initialized if tablePrefix is set.

resolves #311
This commit is contained in:
Glenn Renfro
2017-05-04 17:52:05 -04:00
committed by Michael Minella
parent b03f8398a7
commit 6c47223859
3 changed files with 53 additions and 2 deletions

View File

@@ -17,6 +17,7 @@
package org.springframework.cloud.task.configuration;
import java.util.Collection;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;

View File

@@ -29,6 +29,7 @@ import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.jdbc.support.MetaDataAccessException;
import org.springframework.util.StringUtils;
/**
* Utility for initializing the Task Repository's datasource. If a single
@@ -63,6 +64,9 @@ public final class TaskRepositoryInitializer implements InitializingBean {
@Value("${spring.cloud.task.initialize.enable:true}")
private boolean taskInitializationEnable;
@Value("${spring.cloud.task.tablePrefix:#{null}}")
private String tablePrefix;
public TaskRepositoryInitializer(){
}
@@ -86,7 +90,9 @@ public final class TaskRepositoryInitializer implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
if (dataSource != null && taskInitializationEnable) {
if (dataSource != null &&
taskInitializationEnable &&
!StringUtils.hasText(this.tablePrefix)) {
String platform = getDatabaseType(dataSource);
if ("hsql".equals(platform)) {
platform = "hsqldb";

View File

@@ -16,6 +16,8 @@
package org.springframework.cloud.task;
import java.util.Properties;
import javax.sql.DataSource;
import org.junit.After;
@@ -23,20 +25,28 @@ import org.junit.Test;
import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
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.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.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
/**
* Verifies that the beans created by the SimpleTaskConfiguration.
@@ -69,6 +79,40 @@ public class SimpleTaskConfigurationTests {
assertEquals(targetClass, 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(), is(equalTo(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;
}
assertTrue("Expected ApplicationContextException to be thrown", wasExceptionThrown);
}
@Test(expected = BeanCreationException.class)
public void testMultipleConfigurers() {
this.context = new AnnotationConfigApplicationContext(MultipleConfigurers.class,