|
| 1 | +/* |
| 2 | + * Copyright 2012-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.cassandra; |
| 18 | + |
| 19 | +import java.time.Duration; |
| 20 | + |
| 21 | +import com.datastax.oss.driver.api.core.CqlSession; |
| 22 | +import com.datastax.oss.driver.api.core.config.DriverConfigLoader; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.testcontainers.containers.CassandraContainer; |
| 25 | +import org.testcontainers.junit.jupiter.Container; |
| 26 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 27 | + |
| 28 | +import org.springframework.beans.BeansException; |
| 29 | +import org.springframework.beans.factory.config.BeanPostProcessor; |
| 30 | +import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
| 31 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 32 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 33 | +import org.springframework.boot.testsupport.testcontainers.DockerImageNames; |
| 34 | +import org.springframework.context.annotation.Bean; |
| 35 | +import org.springframework.context.annotation.Configuration; |
| 36 | + |
| 37 | +import static org.assertj.core.api.Assertions.assertThat; |
| 38 | +import static org.mockito.Mockito.spy; |
| 39 | +import static org.mockito.Mockito.verify; |
| 40 | + |
| 41 | +/** |
| 42 | + * Integration tests for {@link CassandraAutoConfiguration}. |
| 43 | + * |
| 44 | + * @author Andy Wilkinson |
| 45 | + */ |
| 46 | +@Testcontainers(disabledWithoutDocker = true) |
| 47 | +class CassandraAutoConfigurationIntegrationTests { |
| 48 | + |
| 49 | + @Container |
| 50 | + static final CassandraContainer<?> cassandra = new CassandraContainer<>(DockerImageNames.cassandra()) |
| 51 | + .withStartupAttempts(5).withStartupTimeout(Duration.ofMinutes(10)); |
| 52 | + |
| 53 | + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 54 | + .withConfiguration(AutoConfigurations.of(CassandraAutoConfiguration.class)).withPropertyValues( |
| 55 | + "spring.data.cassandra.contact-points:" + cassandra.getHost() + ":" |
| 56 | + + cassandra.getFirstMappedPort(), |
| 57 | + "spring.data.cassandra.local-datacenter=datacenter1", "spring.data.cassandra.request.timeout=20s", |
| 58 | + "spring.data.cassandra.connection.init-query-timeout=10s"); |
| 59 | + |
| 60 | + @Test |
| 61 | + void whenTheContextIsClosedThenTheDriverConfigLoaderIsClosed() { |
| 62 | + this.contextRunner.withUserConfiguration(DriverConfigLoaderSpyConfiguration.class).run((context) -> { |
| 63 | + assertThat(((BeanDefinitionRegistry) context.getSourceApplicationContext()) |
| 64 | + .getBeanDefinition("cassandraDriverConfigLoader").getDestroyMethodName()).isEmpty(); |
| 65 | + // Initialize lazy bean |
| 66 | + context.getBean(CqlSession.class); |
| 67 | + DriverConfigLoader driverConfigLoader = context.getBean(DriverConfigLoader.class); |
| 68 | + context.close(); |
| 69 | + verify(driverConfigLoader).close(); |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + @Configuration(proxyBeanMethods = false) |
| 74 | + static class DriverConfigLoaderSpyConfiguration { |
| 75 | + |
| 76 | + @Bean |
| 77 | + static BeanPostProcessor driverConfigLoaderSpy() { |
| 78 | + return new BeanPostProcessor() { |
| 79 | + |
| 80 | + @Override |
| 81 | + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { |
| 82 | + if (bean instanceof DriverConfigLoader) { |
| 83 | + return spy(bean); |
| 84 | + } |
| 85 | + return bean; |
| 86 | + } |
| 87 | + |
| 88 | + }; |
| 89 | + } |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments