|
1 | 1 | /* |
2 | | - * Copyright 2012-2016 the original author or authors. |
| 2 | + * Copyright 2012-2017 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
22 | 22 | import java.util.Map; |
23 | 23 |
|
24 | 24 | import com.hazelcast.core.HazelcastInstance; |
25 | | -import org.junit.After; |
26 | 25 | import org.junit.Test; |
27 | 26 |
|
| 27 | +import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
| 28 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
28 | 29 | import org.springframework.boot.autoconfigure.data.jpa.EntityManagerFactoryDependsOnPostProcessor; |
29 | 30 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; |
30 | | -import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration; |
31 | 31 | 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; |
33 | 34 | import org.springframework.context.annotation.Bean; |
34 | 35 | import org.springframework.context.annotation.Configuration; |
35 | 36 |
|
|
43 | 44 | */ |
44 | 45 | public class HazelcastJpaDependencyAutoConfigurationTests { |
45 | 46 |
|
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"); |
54 | 53 |
|
55 | 54 | @Test |
56 | 55 | 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 | + }); |
61 | 63 | } |
62 | 64 |
|
63 | 65 | @Test |
64 | 66 | 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 | + }); |
70 | 74 | } |
71 | 75 |
|
72 | 76 | @Test |
73 | 77 | 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 | + }); |
79 | 84 | } |
80 | 85 |
|
81 | 86 | @Test |
82 | 87 | 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")); |
89 | 93 | } |
90 | 94 |
|
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); |
94 | 98 | } |
95 | 99 |
|
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(); |
99 | 104 | return dependsOn != null ? Arrays.asList(dependsOn) |
100 | 105 | : Collections.<String>emptyList(); |
101 | 106 | } |
102 | 107 |
|
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 | | - } |
114 | 108 |
|
115 | 109 | @Configuration |
116 | 110 | static class HazelcastConfiguration { |
|
0 commit comments