pull/4399/head
Phillip Webb 9 years ago
parent ce73dec992
commit e06198e9cb

@ -33,15 +33,16 @@ import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
/**
* Additional configuration to ensure that {@link EntityManagerFactory} beans
* depend-on the {@code hazelcastInstance} bean.
* Additional configuration to ensure that {@link EntityManagerFactory} beans depend-on
* the {@code hazelcastInstance} bean.
*
* @author Stephane Nicoll
* @since 1.3.0
*/
@Configuration
@ConditionalOnClass({ HazelcastInstance.class, LocalContainerEntityManagerFactoryBean.class })
@AutoConfigureAfter({ HazelcastAutoConfiguration.class, HibernateJpaAutoConfiguration.class })
@ConditionalOnClass({ HazelcastInstance.class,
LocalContainerEntityManagerFactoryBean.class })
@AutoConfigureAfter({ HazelcastAutoConfiguration.class,
HibernateJpaAutoConfiguration.class })
class HazelcastJpaDependencyAutoConfiguration {
@Bean
@ -56,6 +57,7 @@ class HazelcastJpaDependencyAutoConfiguration {
HazelcastInstanceJpaDependencyPostProcessor() {
super("hazelcastInstance");
}
}
static class OnHazelcastAndJpaCondition extends AllNestedConditions {

@ -382,8 +382,8 @@ public class CacheAutoConfigurationTests {
public void hazelcastCacheWithMainHazelcastAutoConfiguration() throws IOException {
String mainConfig = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml";
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(applicationContext, "spring.cache.type=hazelcast",
"spring.hazelcast.config=" + mainConfig);
EnvironmentTestUtils.addEnvironment(applicationContext,
"spring.cache.type=hazelcast", "spring.hazelcast.config=" + mainConfig);
applicationContext.register(DefaultCacheConfiguration.class);
applicationContext.register(HazelcastAndCacheConfiguration.class);
applicationContext.refresh();
@ -404,14 +404,14 @@ public class CacheAutoConfigurationTests {
String mainConfig = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml";
String cacheConfig = "org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml";
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(applicationContext, "spring.cache.type=hazelcast",
EnvironmentTestUtils.addEnvironment(applicationContext,
"spring.cache.type=hazelcast",
"spring.cache.hazelcast.config=" + cacheConfig,
"spring.hazelcast.config=" + mainConfig);
applicationContext.register(DefaultCacheConfiguration.class);
applicationContext.register(HazelcastAndCacheConfiguration.class);
applicationContext.refresh();
this.context = applicationContext;
HazelcastInstance hazelcastInstance = this.context
.getBean(HazelcastInstance.class);
HazelcastCacheManager cacheManager = validateCacheManager(
@ -681,7 +681,8 @@ public class CacheAutoConfigurationTests {
}
@Configuration
@ImportAutoConfiguration({CacheAutoConfiguration.class, HazelcastAutoConfiguration.class})
@ImportAutoConfiguration({ CacheAutoConfiguration.class,
HazelcastAutoConfiguration.class })
static class HazelcastAndCacheConfiguration {
}

@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.hazelcast;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.hazelcast.core.HazelcastInstance;
import org.junit.After;
@ -57,7 +58,7 @@ public class HazelcastJpaDependencyAutoConfigurationTests {
@Test
public void registrationIfHazelcastInstanceHasRegularBeanName() {
load(HazelcastConfiguration.class);
assertThat(this.context.getBeansOfType(EntityManagerFactoryDependsOnPostProcessor.class),
assertThat(getPostProcessor(),
hasKey("hazelcastInstanceJpaDependencyPostProcessor"));
assertThat(getEntityManagerFactoryDependencies(), hasItem("hazelcastInstance"));
}
@ -65,16 +66,18 @@ public class HazelcastJpaDependencyAutoConfigurationTests {
@Test
public void noRegistrationIfHazelcastInstanceHasCustomBeanName() {
load(HazelcastCustomNameConfiguration.class);
assertThat(getEntityManagerFactoryDependencies(), not(hasItem("hazelcastInstance")));
assertThat(this.context.getBeansOfType(EntityManagerFactoryDependsOnPostProcessor.class),
assertThat(getEntityManagerFactoryDependencies(),
not(hasItem("hazelcastInstance")));
assertThat(getPostProcessor(),
not(hasKey("hazelcastInstanceJpaDependencyPostProcessor")));
}
@Test
public void noRegistrationWithNoHazelcastInstance() {
load(null);
assertThat(getEntityManagerFactoryDependencies(), not(hasItem("hazelcastInstance")));
assertThat(this.context.getBeansOfType(EntityManagerFactoryDependsOnPostProcessor.class),
assertThat(getEntityManagerFactoryDependencies(),
not(hasItem("hazelcastInstance")));
assertThat(getPostProcessor(),
not(hasKey("hazelcastInstanceJpaDependencyPostProcessor")));
}
@ -84,13 +87,20 @@ public class HazelcastJpaDependencyAutoConfigurationTests {
this.context.register(HazelcastConfiguration.class,
HazelcastJpaDependencyAutoConfiguration.class);
this.context.refresh();
assertThat(this.context.getBeansOfType(EntityManagerFactoryDependsOnPostProcessor.class),
assertThat(getPostProcessor(),
not(hasKey("hazelcastInstanceJpaDependencyPostProcessor")));
}
private Map<String, EntityManagerFactoryDependsOnPostProcessor> getPostProcessor() {
return this.context
.getBeansOfType(EntityManagerFactoryDependsOnPostProcessor.class);
}
private List<String> getEntityManagerFactoryDependencies() {
String[] dependsOn = this.context.getBeanDefinition("entityManagerFactory").getDependsOn();
return dependsOn != null ? Arrays.asList(dependsOn) : Collections.<String>emptyList();
String[] dependsOn = this.context.getBeanDefinition("entityManagerFactory")
.getDependsOn();
return dependsOn != null ? Arrays.asList(dependsOn)
: Collections.<String>emptyList();
}
public void load(Class<?> config) {

Loading…
Cancel
Save