Commit ebbe29cc authored by Stephane Nicoll's avatar Stephane Nicoll

Migrate HazelcastJpaDependencyAutoConfigurationTests to context runner

This commit also makes sure to generate unique embedded data source and
disable datasource initialization as this is not required by those
tests.

See gh-9889
parent 49d249bd
/* /*
* Copyright 2012-2016 the original author or authors. * Copyright 2012-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -22,14 +22,15 @@ import java.util.List; ...@@ -22,14 +22,15 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import com.hazelcast.core.HazelcastInstance; import com.hazelcast.core.HazelcastInstance;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.data.jpa.EntityManagerFactoryDependsOnPostProcessor; import org.springframework.boot.autoconfigure.data.jpa.EntityManagerFactoryDependsOnPostProcessor;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
...@@ -43,74 +44,67 @@ import static org.mockito.Mockito.mock; ...@@ -43,74 +44,67 @@ import static org.mockito.Mockito.mock;
*/ */
public class HazelcastJpaDependencyAutoConfigurationTests { public class HazelcastJpaDependencyAutoConfigurationTests {
private AnnotationConfigApplicationContext context; private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class,
@After HibernateJpaAutoConfiguration.class,
public void closeContext() { HazelcastJpaDependencyAutoConfiguration.class))
if (this.context != null) { .withPropertyValues("spring.datasource.generate-unique-name=true",
this.context.close(); "spring.datasource.initialize=false");
}
}
@Test @Test
public void registrationIfHazelcastInstanceHasRegularBeanName() { public void registrationIfHazelcastInstanceHasRegularBeanName() {
load(HazelcastConfiguration.class); this.contextRunner.withUserConfiguration(
assertThat(getPostProcessor()) HazelcastConfiguration.class).run((context) -> {
.containsKey("hazelcastInstanceJpaDependencyPostProcessor"); assertThat(postProcessors(context))
assertThat(getEntityManagerFactoryDependencies()).contains("hazelcastInstance"); .containsKey("hazelcastInstanceJpaDependencyPostProcessor");
assertThat(entityManagerFactoryDependencies(context)).contains(
"hazelcastInstance");
});
} }
@Test @Test
public void noRegistrationIfHazelcastInstanceHasCustomBeanName() { public void noRegistrationIfHazelcastInstanceHasCustomBeanName() {
load(HazelcastCustomNameConfiguration.class); this.contextRunner.withUserConfiguration(
assertThat(getEntityManagerFactoryDependencies()) HazelcastCustomNameConfiguration.class).run((context) -> {
.doesNotContain("hazelcastInstance"); assertThat(entityManagerFactoryDependencies(context))
assertThat(getPostProcessor()) .doesNotContain("hazelcastInstance");
.doesNotContainKey("hazelcastInstanceJpaDependencyPostProcessor"); assertThat(postProcessors(context))
.doesNotContainKey("hazelcastInstanceJpaDependencyPostProcessor");
});
} }
@Test @Test
public void noRegistrationWithNoHazelcastInstance() { public void noRegistrationWithNoHazelcastInstance() {
load(null); this.contextRunner.run((context) -> {
assertThat(getEntityManagerFactoryDependencies()) assertThat(entityManagerFactoryDependencies(context))
.doesNotContain("hazelcastInstance"); .doesNotContain("hazelcastInstance");
assertThat(getPostProcessor()) assertThat(postProcessors(context))
.doesNotContainKey("hazelcastInstanceJpaDependencyPostProcessor"); .doesNotContainKey("hazelcastInstanceJpaDependencyPostProcessor");
});
} }
@Test @Test
public void noRegistrationWithNoEntityManagerFactory() { public void noRegistrationWithNoEntityManagerFactory() {
this.context = new AnnotationConfigApplicationContext(); new ApplicationContextRunner().withUserConfiguration(HazelcastConfiguration.class)
this.context.register(HazelcastConfiguration.class, .withConfiguration(AutoConfigurations.of(
HazelcastJpaDependencyAutoConfiguration.class); HazelcastJpaDependencyAutoConfiguration.class))
this.context.refresh(); .run((context) -> assertThat(postProcessors(context)).doesNotContainKey(
assertThat(getPostProcessor()) "hazelcastInstanceJpaDependencyPostProcessor"));
.doesNotContainKey("hazelcastInstanceJpaDependencyPostProcessor");
} }
private Map<String, EntityManagerFactoryDependsOnPostProcessor> getPostProcessor() { private Map<String, EntityManagerFactoryDependsOnPostProcessor> postProcessors(
return this.context AssertableApplicationContext context) {
.getBeansOfType(EntityManagerFactoryDependsOnPostProcessor.class); return context.getBeansOfType(EntityManagerFactoryDependsOnPostProcessor.class);
} }
private List<String> getEntityManagerFactoryDependencies() { private List<String> entityManagerFactoryDependencies(
String[] dependsOn = this.context.getBeanDefinition("entityManagerFactory") AssertableApplicationContext context) {
.getDependsOn(); String[] dependsOn = ((BeanDefinitionRegistry) context.getSourceApplicationContext())
.getBeanDefinition("entityManagerFactory").getDependsOn();
return dependsOn != null ? Arrays.asList(dependsOn) return dependsOn != null ? Arrays.asList(dependsOn)
: Collections.<String>emptyList(); : Collections.<String>emptyList();
} }
public void load(Class<?> config) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
if (config != null) {
ctx.register(config);
}
ctx.register(EmbeddedDataSourceConfiguration.class,
DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class);
ctx.register(HazelcastJpaDependencyAutoConfiguration.class);
ctx.refresh();
this.context = ctx;
}
@Configuration @Configuration
static class HazelcastConfiguration { static class HazelcastConfiguration {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment