|
| 1 | +/* |
| 2 | + * Copyright 2025-present 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 | +package org.springframework.data.mongodb.repository.aot; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.springframework.aot.generate.GeneratedFiles.Kind; |
| 25 | +import org.springframework.aot.test.generate.TestGenerationContext; |
| 26 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 27 | +import org.springframework.context.annotation.Configuration; |
| 28 | +import org.springframework.context.aot.ApplicationContextAotGenerator; |
| 29 | +import org.springframework.core.io.InputStreamResource; |
| 30 | +import org.springframework.core.io.InputStreamSource; |
| 31 | +import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author Christoph Strobl |
| 35 | + */ |
| 36 | +class MongoRepositoryContributorConfigurationTests { |
| 37 | + |
| 38 | + @Configuration |
| 39 | + @EnableMongoRepositories(basePackages = "example.aot") |
| 40 | + static class ConfigurationWithoutAnythingSpecial { |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + @Configuration |
| 45 | + @EnableMongoRepositories(mongoTemplateRef = "template-2", basePackages = "example.aot") |
| 46 | + static class ConfigurationWithTemplateRef { |
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | + @Test // GH-5107 |
| 51 | + void usesPrimaryMongoOperationsBeanReferenceByDefault() throws IOException { |
| 52 | + |
| 53 | + TestGenerationContext testContext = generate(ConfigurationWithoutAnythingSpecial.class); |
| 54 | + InputStreamSource file = testContext.getGeneratedFiles().getGeneratedFile(Kind.SOURCE, |
| 55 | + "example/aot/UserRepository__BeanDefinitions.java"); |
| 56 | + |
| 57 | + InputStreamResource isr = new InputStreamResource(file); |
| 58 | + String sourceCode = isr.getContentAsString(StandardCharsets.UTF_8); |
| 59 | + |
| 60 | + assertThat(sourceCode).contains("operations = beanFactory.getBean(MongoOperations.class)"); |
| 61 | + } |
| 62 | + |
| 63 | + @Test // GH-5107 |
| 64 | + void shouldConsiderMongoTemplateReferenceIfPresent() throws IOException { |
| 65 | + |
| 66 | + TestGenerationContext testContext = generate(ConfigurationWithTemplateRef.class); |
| 67 | + InputStreamSource file = testContext.getGeneratedFiles().getGeneratedFile(Kind.SOURCE, |
| 68 | + "example/aot/UserRepository__BeanDefinitions.java"); |
| 69 | + |
| 70 | + InputStreamResource isr = new InputStreamResource(file); |
| 71 | + String sourceCode = isr.getContentAsString(StandardCharsets.UTF_8); |
| 72 | + |
| 73 | + assertThat(sourceCode).contains("operations = beanFactory.getBean(\"template-2\", MongoOperations.class)"); |
| 74 | + } |
| 75 | + |
| 76 | + private static TestGenerationContext generate(Class<?>... configurationClasses) { |
| 77 | + |
| 78 | + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); |
| 79 | + context.register(configurationClasses); |
| 80 | + |
| 81 | + ApplicationContextAotGenerator generator = new ApplicationContextAotGenerator(); |
| 82 | + |
| 83 | + TestGenerationContext generationContext = new TestGenerationContext(); |
| 84 | + generator.processAheadOfTime(context, generationContext); |
| 85 | + generationContext.writeGeneratedContent(); |
| 86 | + return generationContext; |
| 87 | + } |
| 88 | +} |
0 commit comments