|
18 | 18 |
|
19 | 19 | import java.lang.annotation.Retention;
|
20 | 20 | import java.lang.annotation.RetentionPolicy;
|
| 21 | +import java.util.function.BiConsumer; |
21 | 22 |
|
22 | 23 | import org.junit.jupiter.api.AfterEach;
|
23 | 24 | import org.junit.jupiter.api.Test;
|
24 | 25 | import org.testcontainers.containers.Container;
|
25 | 26 | import org.testcontainers.containers.PostgreSQLContainer;
|
26 | 27 |
|
| 28 | +import org.springframework.aot.test.generate.TestGenerationContext; |
27 | 29 | import org.springframework.boot.testcontainers.beans.TestcontainerBeanDefinition;
|
28 | 30 | import org.springframework.boot.testcontainers.context.ImportTestcontainers;
|
29 | 31 | import org.springframework.boot.testsupport.container.DisabledIfDockerUnavailable;
|
30 | 32 | import org.springframework.boot.testsupport.container.TestImage;
|
| 33 | +import org.springframework.context.ApplicationContextInitializer; |
31 | 34 | import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
| 35 | +import org.springframework.context.aot.ApplicationContextAotGenerator; |
| 36 | +import org.springframework.context.support.GenericApplicationContext; |
| 37 | +import org.springframework.core.test.tools.CompileWithForkedClassLoader; |
| 38 | +import org.springframework.core.test.tools.Compiled; |
| 39 | +import org.springframework.core.test.tools.TestCompiler; |
| 40 | +import org.springframework.javapoet.ClassName; |
32 | 41 | import org.springframework.test.context.DynamicPropertyRegistry;
|
33 | 42 | import org.springframework.test.context.DynamicPropertySource;
|
34 | 43 |
|
|
43 | 52 | @DisabledIfDockerUnavailable
|
44 | 53 | class ImportTestcontainersTests {
|
45 | 54 |
|
| 55 | + private final TestGenerationContext generationContext = new TestGenerationContext(); |
| 56 | + |
46 | 57 | private AnnotationConfigApplicationContext applicationContext;
|
47 | 58 |
|
48 | 59 | @AfterEach
|
@@ -122,6 +133,81 @@ void importWhenHasBadArgsDynamicPropertySourceMethod() {
|
122 | 133 | .withMessage("@DynamicPropertySource method 'containerProperties' must be static");
|
123 | 134 | }
|
124 | 135 |
|
| 136 | + @Test |
| 137 | + @CompileWithForkedClassLoader |
| 138 | + void importTestcontainersImportWithoutValueAotContribution() { |
| 139 | + this.applicationContext = new AnnotationConfigApplicationContext(); |
| 140 | + this.applicationContext.register(ImportWithoutValue.class); |
| 141 | + compile((freshContext, compiled) -> { |
| 142 | + PostgreSQLContainer<?> container = freshContext.getBean(PostgreSQLContainer.class); |
| 143 | + assertThat(container).isSameAs(ImportWithoutValue.container); |
| 144 | + }); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + @CompileWithForkedClassLoader |
| 149 | + void importTestcontainersImportWithValueAotContribution() { |
| 150 | + this.applicationContext = new AnnotationConfigApplicationContext(); |
| 151 | + this.applicationContext.register(ImportWithValue.class); |
| 152 | + compile((freshContext, compiled) -> { |
| 153 | + PostgreSQLContainer<?> container = freshContext.getBean(PostgreSQLContainer.class); |
| 154 | + assertThat(container).isSameAs(ContainerDefinitions.container); |
| 155 | + }); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + @CompileWithForkedClassLoader |
| 160 | + void importTestcontainersWithDynamicPropertySourceAotContribution() { |
| 161 | + this.applicationContext = new AnnotationConfigApplicationContext(); |
| 162 | + this.applicationContext.register(ContainerDefinitionsWithDynamicPropertySource.class); |
| 163 | + compile((freshContext, compiled) -> { |
| 164 | + PostgreSQLContainer<?> container = freshContext.getBean(PostgreSQLContainer.class); |
| 165 | + assertThat(container).isSameAs(ContainerDefinitionsWithDynamicPropertySource.container); |
| 166 | + }); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + @CompileWithForkedClassLoader |
| 171 | + void importTestcontainersWithCustomPostgreSQLContainerAotContribution() { |
| 172 | + this.applicationContext = new AnnotationConfigApplicationContext(); |
| 173 | + this.applicationContext.register(CustomPostgreSQLContainerDefinitions.class); |
| 174 | + compile((freshContext, compiled) -> { |
| 175 | + CustomPostgreSQLContainer container = freshContext.getBean(CustomPostgreSQLContainer.class); |
| 176 | + assertThat(container).isSameAs(CustomPostgreSQLContainerDefinitions.container); |
| 177 | + }); |
| 178 | + } |
| 179 | + |
| 180 | + @Test |
| 181 | + @CompileWithForkedClassLoader |
| 182 | + void importTestcontainersWithNotAccessibleContainerAotContribution() { |
| 183 | + this.applicationContext = new AnnotationConfigApplicationContext(); |
| 184 | + this.applicationContext.register(ImportNotAccessibleContainer.class); |
| 185 | + compile((freshContext, compiled) -> { |
| 186 | + PostgreSQLContainer<?> container = freshContext.getBean(PostgreSQLContainer.class); |
| 187 | + assertThat(container).isSameAs(ImportNotAccessibleContainer.container); |
| 188 | + }); |
| 189 | + } |
| 190 | + |
| 191 | + @SuppressWarnings("unchecked") |
| 192 | + private void compile(BiConsumer<GenericApplicationContext, Compiled> result) { |
| 193 | + ClassName className = processAheadOfTime(); |
| 194 | + TestCompiler.forSystem().with(this.generationContext).compile((compiled) -> { |
| 195 | + GenericApplicationContext freshApplicationContext = new GenericApplicationContext(); |
| 196 | + ApplicationContextInitializer<GenericApplicationContext> initializer = compiled |
| 197 | + .getInstance(ApplicationContextInitializer.class, className.toString()); |
| 198 | + initializer.initialize(freshApplicationContext); |
| 199 | + freshApplicationContext.refresh(); |
| 200 | + result.accept(freshApplicationContext, compiled); |
| 201 | + }); |
| 202 | + } |
| 203 | + |
| 204 | + private ClassName processAheadOfTime() { |
| 205 | + ClassName className = new ApplicationContextAotGenerator().processAheadOfTime(this.applicationContext, |
| 206 | + this.generationContext); |
| 207 | + this.generationContext.writeGeneratedContent(); |
| 208 | + return className; |
| 209 | + } |
| 210 | + |
125 | 211 | @ImportTestcontainers
|
126 | 212 | static class ImportWithoutValue {
|
127 | 213 |
|
@@ -196,4 +282,26 @@ void containerProperties() {
|
196 | 282 |
|
197 | 283 | }
|
198 | 284 |
|
| 285 | + @ImportTestcontainers |
| 286 | + static class CustomPostgreSQLContainerDefinitions { |
| 287 | + |
| 288 | + static CustomPostgreSQLContainer container = new CustomPostgreSQLContainer(); |
| 289 | + |
| 290 | + } |
| 291 | + |
| 292 | + static class CustomPostgreSQLContainer extends PostgreSQLContainer<CustomPostgreSQLContainer> { |
| 293 | + |
| 294 | + CustomPostgreSQLContainer() { |
| 295 | + super("postgres:14"); |
| 296 | + } |
| 297 | + |
| 298 | + } |
| 299 | + |
| 300 | + @ImportTestcontainers |
| 301 | + static class ImportNotAccessibleContainer { |
| 302 | + |
| 303 | + private static final PostgreSQLContainer<?> container = TestImage.container(PostgreSQLContainer.class); |
| 304 | + |
| 305 | + } |
| 306 | + |
199 | 307 | }
|
0 commit comments