Skip to content

Commit ebbe29c

Browse files
committed
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
1 parent 49d249b commit ebbe29c

File tree

1 file changed

+43
-49
lines changed

1 file changed

+43
-49
lines changed

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastJpaDependencyAutoConfigurationTests.java

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,14 +22,15 @@
2222
import java.util.Map;
2323

2424
import com.hazelcast.core.HazelcastInstance;
25-
import org.junit.After;
2625
import org.junit.Test;
2726

27+
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
28+
import org.springframework.boot.autoconfigure.AutoConfigurations;
2829
import org.springframework.boot.autoconfigure.data.jpa.EntityManagerFactoryDependsOnPostProcessor;
2930
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
30-
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
3131
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
32-
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
32+
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
33+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3334
import org.springframework.context.annotation.Bean;
3435
import org.springframework.context.annotation.Configuration;
3536

@@ -43,74 +44,67 @@
4344
*/
4445
public class HazelcastJpaDependencyAutoConfigurationTests {
4546

46-
private AnnotationConfigApplicationContext context;
47-
48-
@After
49-
public void closeContext() {
50-
if (this.context != null) {
51-
this.context.close();
52-
}
53-
}
47+
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
48+
.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class,
49+
HibernateJpaAutoConfiguration.class,
50+
HazelcastJpaDependencyAutoConfiguration.class))
51+
.withPropertyValues("spring.datasource.generate-unique-name=true",
52+
"spring.datasource.initialize=false");
5453

5554
@Test
5655
public void registrationIfHazelcastInstanceHasRegularBeanName() {
57-
load(HazelcastConfiguration.class);
58-
assertThat(getPostProcessor())
59-
.containsKey("hazelcastInstanceJpaDependencyPostProcessor");
60-
assertThat(getEntityManagerFactoryDependencies()).contains("hazelcastInstance");
56+
this.contextRunner.withUserConfiguration(
57+
HazelcastConfiguration.class).run((context) -> {
58+
assertThat(postProcessors(context))
59+
.containsKey("hazelcastInstanceJpaDependencyPostProcessor");
60+
assertThat(entityManagerFactoryDependencies(context)).contains(
61+
"hazelcastInstance");
62+
});
6163
}
6264

6365
@Test
6466
public void noRegistrationIfHazelcastInstanceHasCustomBeanName() {
65-
load(HazelcastCustomNameConfiguration.class);
66-
assertThat(getEntityManagerFactoryDependencies())
67-
.doesNotContain("hazelcastInstance");
68-
assertThat(getPostProcessor())
69-
.doesNotContainKey("hazelcastInstanceJpaDependencyPostProcessor");
67+
this.contextRunner.withUserConfiguration(
68+
HazelcastCustomNameConfiguration.class).run((context) -> {
69+
assertThat(entityManagerFactoryDependencies(context))
70+
.doesNotContain("hazelcastInstance");
71+
assertThat(postProcessors(context))
72+
.doesNotContainKey("hazelcastInstanceJpaDependencyPostProcessor");
73+
});
7074
}
7175

7276
@Test
7377
public void noRegistrationWithNoHazelcastInstance() {
74-
load(null);
75-
assertThat(getEntityManagerFactoryDependencies())
76-
.doesNotContain("hazelcastInstance");
77-
assertThat(getPostProcessor())
78-
.doesNotContainKey("hazelcastInstanceJpaDependencyPostProcessor");
78+
this.contextRunner.run((context) -> {
79+
assertThat(entityManagerFactoryDependencies(context))
80+
.doesNotContain("hazelcastInstance");
81+
assertThat(postProcessors(context))
82+
.doesNotContainKey("hazelcastInstanceJpaDependencyPostProcessor");
83+
});
7984
}
8085

8186
@Test
8287
public void noRegistrationWithNoEntityManagerFactory() {
83-
this.context = new AnnotationConfigApplicationContext();
84-
this.context.register(HazelcastConfiguration.class,
85-
HazelcastJpaDependencyAutoConfiguration.class);
86-
this.context.refresh();
87-
assertThat(getPostProcessor())
88-
.doesNotContainKey("hazelcastInstanceJpaDependencyPostProcessor");
88+
new ApplicationContextRunner().withUserConfiguration(HazelcastConfiguration.class)
89+
.withConfiguration(AutoConfigurations.of(
90+
HazelcastJpaDependencyAutoConfiguration.class))
91+
.run((context) -> assertThat(postProcessors(context)).doesNotContainKey(
92+
"hazelcastInstanceJpaDependencyPostProcessor"));
8993
}
9094

91-
private Map<String, EntityManagerFactoryDependsOnPostProcessor> getPostProcessor() {
92-
return this.context
93-
.getBeansOfType(EntityManagerFactoryDependsOnPostProcessor.class);
95+
private Map<String, EntityManagerFactoryDependsOnPostProcessor> postProcessors(
96+
AssertableApplicationContext context) {
97+
return context.getBeansOfType(EntityManagerFactoryDependsOnPostProcessor.class);
9498
}
9599

96-
private List<String> getEntityManagerFactoryDependencies() {
97-
String[] dependsOn = this.context.getBeanDefinition("entityManagerFactory")
98-
.getDependsOn();
100+
private List<String> entityManagerFactoryDependencies(
101+
AssertableApplicationContext context) {
102+
String[] dependsOn = ((BeanDefinitionRegistry) context.getSourceApplicationContext())
103+
.getBeanDefinition("entityManagerFactory").getDependsOn();
99104
return dependsOn != null ? Arrays.asList(dependsOn)
100105
: Collections.<String>emptyList();
101106
}
102107

103-
public void load(Class<?> config) {
104-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
105-
if (config != null) {
106-
ctx.register(config);
107-
}
108-
ctx.register(EmbeddedDataSourceConfiguration.class,
109-
DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class);
110-
ctx.register(HazelcastJpaDependencyAutoConfiguration.class);
111-
ctx.refresh();
112-
this.context = ctx;
113-
}
114108

115109
@Configuration
116110
static class HazelcastConfiguration {

0 commit comments

Comments
 (0)