|
19 | 19 | import java.util.Collections;
|
20 | 20 |
|
21 | 21 | import com.datastax.oss.driver.api.core.CqlSession;
|
22 |
| -import org.junit.jupiter.api.AfterEach; |
23 | 22 | import org.junit.jupiter.api.Test;
|
24 | 23 |
|
| 24 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
25 | 25 | import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration;
|
26 | 26 | import org.springframework.boot.autoconfigure.data.cassandra.city.City;
|
27 | 27 | import org.springframework.boot.autoconfigure.domain.EntityScan;
|
28 |
| -import org.springframework.boot.test.util.TestPropertyValues; |
| 28 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
29 | 29 | import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
30 | 30 | import org.springframework.context.annotation.Bean;
|
31 | 31 | import org.springframework.context.annotation.Configuration;
|
|
36 | 36 | import org.springframework.data.cassandra.core.cql.CqlTemplate;
|
37 | 37 | import org.springframework.data.cassandra.core.mapping.CassandraMappingContext;
|
38 | 38 | import org.springframework.data.cassandra.core.mapping.SimpleUserTypeResolver;
|
39 |
| -import org.springframework.data.domain.ManagedTypes; |
40 |
| -import org.springframework.test.util.ReflectionTestUtils; |
41 |
| -import org.springframework.util.ObjectUtils; |
42 | 39 |
|
43 | 40 | import static org.assertj.core.api.Assertions.assertThat;
|
44 | 41 |
|
|
51 | 48 | */
|
52 | 49 | class CassandraDataAutoConfigurationTests {
|
53 | 50 |
|
54 |
| - private AnnotationConfigApplicationContext context; |
| 51 | + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 52 | + .withPropertyValues("spring.cassandra.keyspaceName=boot_test") |
| 53 | + .withUserConfiguration(CassandraMockConfiguration.class) |
| 54 | + .withConfiguration( |
| 55 | + AutoConfigurations.of(CassandraAutoConfiguration.class, CassandraDataAutoConfiguration.class)); |
55 | 56 |
|
56 |
| - @AfterEach |
57 |
| - void close() { |
58 |
| - if (this.context != null) { |
59 |
| - this.context.close(); |
60 |
| - } |
| 57 | + @Test |
| 58 | + void cqlTemplateExists() { |
| 59 | + this.contextRunner.run((context) -> assertThat(context).hasSingleBean(CqlTemplate.class)); |
61 | 60 | }
|
62 | 61 |
|
63 | 62 | @Test
|
64 | 63 | void templateExists() {
|
65 |
| - load(CassandraMockConfiguration.class); |
66 |
| - assertThat(this.context.getBeanNamesForType(CassandraTemplate.class)).hasSize(1); |
| 64 | + this.contextRunner.run((context) -> assertThat(context).hasSingleBean(CassandraTemplate.class)); |
67 | 65 | }
|
68 | 66 |
|
69 | 67 | @Test
|
70 |
| - void cqlTemplateExists() { |
71 |
| - load(CassandraMockConfiguration.class); |
72 |
| - assertThat(this.context.getBeanNamesForType(CqlTemplate.class)).hasSize(1); |
| 68 | + void templateUsesCqlTemplate() { |
| 69 | + this.contextRunner.run((context) -> { |
| 70 | + assertThat(context).hasSingleBean(CassandraTemplate.class); |
| 71 | + assertThat(context.getBean(CassandraTemplate.class).getCqlOperations()) |
| 72 | + .isSameAs(context.getBean(CqlTemplate.class)); |
| 73 | + }); |
73 | 74 | }
|
74 | 75 |
|
75 | 76 | @Test
|
76 | 77 | void entityScanShouldSetManagedTypes() {
|
77 |
| - load(EntityScanConfig.class); |
78 |
| - CassandraMappingContext mappingContext = this.context.getBean(CassandraMappingContext.class); |
79 |
| - ManagedTypes managedTypes = (ManagedTypes) ReflectionTestUtils.getField(mappingContext, "managedTypes"); |
80 |
| - assertThat(managedTypes.toList()).containsOnly(City.class); |
| 78 | + this.contextRunner.withUserConfiguration(EntityScanConfig.class).run((context) -> { |
| 79 | + assertThat(context).hasSingleBean(CassandraMappingContext.class); |
| 80 | + CassandraMappingContext mappingContext = context.getBean(CassandraMappingContext.class); |
| 81 | + assertThat(mappingContext.getManagedTypes()).singleElement() |
| 82 | + .satisfies((typeInformation) -> assertThat(typeInformation.getType()).isEqualTo(City.class)); |
| 83 | + }); |
81 | 84 | }
|
82 | 85 |
|
83 | 86 | @Test
|
84 | 87 | void userTypeResolverShouldBeSet() {
|
85 |
| - load(); |
86 |
| - CassandraConverter cassandraConverter = this.context.getBean(CassandraConverter.class); |
87 |
| - assertThat(cassandraConverter).extracting("userTypeResolver").isInstanceOf(SimpleUserTypeResolver.class); |
| 88 | + this.contextRunner.run((context) -> { |
| 89 | + assertThat(context).hasSingleBean(CassandraConverter.class); |
| 90 | + assertThat(context.getBean(CassandraConverter.class)).extracting("userTypeResolver") |
| 91 | + .isInstanceOf(SimpleUserTypeResolver.class); |
| 92 | + }); |
88 | 93 | }
|
89 | 94 |
|
90 | 95 | @Test
|
91 | 96 | void codecRegistryShouldBeSet() {
|
92 |
| - load(); |
93 |
| - CassandraConverter cassandraConverter = this.context.getBean(CassandraConverter.class); |
94 |
| - assertThat(cassandraConverter.getCodecRegistry()) |
95 |
| - .isSameAs(this.context.getBean(CassandraMockConfiguration.class).codecRegistry); |
| 97 | + this.contextRunner.run((context) -> { |
| 98 | + assertThat(context).hasSingleBean(CassandraConverter.class); |
| 99 | + assertThat(context.getBean(CassandraConverter.class).getCodecRegistry()) |
| 100 | + .isSameAs(context.getBean(CassandraMockConfiguration.class).codecRegistry); |
| 101 | + }); |
96 | 102 | }
|
97 | 103 |
|
98 | 104 | @Test
|
99 | 105 | void defaultConversions() {
|
100 |
| - load(); |
101 |
| - CassandraTemplate template = this.context.getBean(CassandraTemplate.class); |
102 |
| - assertThat(template.getConverter().getConversionService().canConvert(Person.class, String.class)).isFalse(); |
| 106 | + this.contextRunner.run((context) -> { |
| 107 | + CassandraTemplate template = context.getBean(CassandraTemplate.class); |
| 108 | + assertThat(template.getConverter().getConversionService().canConvert(Person.class, String.class)).isFalse(); |
| 109 | + }); |
103 | 110 | }
|
104 | 111 |
|
105 | 112 | @Test
|
106 | 113 | void customConversions() {
|
107 |
| - load(CustomConversionConfig.class); |
108 |
| - CassandraTemplate template = this.context.getBean(CassandraTemplate.class); |
109 |
| - assertThat(template.getConverter().getConversionService().canConvert(Person.class, String.class)).isTrue(); |
| 114 | + this.contextRunner.withUserConfiguration(CustomConversionConfig.class).run((context) -> { |
| 115 | + CassandraTemplate template = context.getBean(CassandraTemplate.class); |
| 116 | + assertThat(template.getConverter().getConversionService().canConvert(Person.class, String.class)).isTrue(); |
| 117 | + }); |
110 | 118 | }
|
111 | 119 |
|
112 | 120 | @Test
|
113 | 121 | void clusterDoesNotExist() {
|
114 |
| - this.context = new AnnotationConfigApplicationContext(CassandraDataAutoConfiguration.class); |
115 |
| - assertThat(this.context.getBeansOfType(CqlSession.class)).isEmpty(); |
116 |
| - } |
117 |
| - |
118 |
| - void load(Class<?>... config) { |
119 |
| - AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); |
120 |
| - TestPropertyValues.of("spring.cassandra.keyspaceName:boot_test").applyTo(ctx); |
121 |
| - if (!ObjectUtils.isEmpty(config)) { |
122 |
| - ctx.register(config); |
| 122 | + try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( |
| 123 | + CassandraDataAutoConfiguration.class)) { |
| 124 | + assertThat(context.getBeansOfType(CqlSession.class)).isEmpty(); |
123 | 125 | }
|
124 |
| - ctx.register(CassandraMockConfiguration.class, CassandraAutoConfiguration.class, |
125 |
| - CassandraDataAutoConfiguration.class); |
126 |
| - ctx.refresh(); |
127 |
| - this.context = ctx; |
128 | 126 | }
|
129 | 127 |
|
130 | 128 | @Configuration(proxyBeanMethods = false)
|
|
0 commit comments