|
| 1 | +/* |
| 2 | + * Copyright 2014-2024 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.cloud.config.server.aot; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.nio.charset.Charset; |
| 21 | +import java.util.Optional; |
| 22 | +import java.util.Set; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import org.springframework.aot.generate.GeneratedFiles; |
| 27 | +import org.springframework.aot.hint.ReflectionHints; |
| 28 | +import org.springframework.aot.test.generate.TestGenerationContext; |
| 29 | +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; |
| 30 | +import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext; |
| 31 | +import org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentProperties; |
| 32 | +import org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentRepository; |
| 33 | +import org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentRepositoryFactory; |
| 34 | +import org.springframework.cloud.config.server.environment.SvnEnvironmentRepositoryFactory; |
| 35 | +import org.springframework.cloud.config.server.environment.SvnKitEnvironmentProperties; |
| 36 | +import org.springframework.cloud.config.server.environment.SvnKitEnvironmentRepository; |
| 37 | +import org.springframework.cloud.config.server.test.TestConfigServerApplication; |
| 38 | +import org.springframework.context.aot.ApplicationContextAotGenerator; |
| 39 | +import org.springframework.context.support.GenericApplicationContext; |
| 40 | +import org.springframework.javapoet.ClassName; |
| 41 | + |
| 42 | +import static org.assertj.core.api.Assertions.assertThat; |
| 43 | + |
| 44 | +/** |
| 45 | + * Tests for {@link CompositeEnvironmentBeanFactoryInitializationAotProcessor}. |
| 46 | + * |
| 47 | + * @author Olga Maciaszek-Sharma |
| 48 | + */ |
| 49 | +class CompositeEnvironmentBeanFactoryInitializationAotProcessorTests { |
| 50 | + |
| 51 | + @Test |
| 52 | + void shouldCreateCompositeEnvironmentBeansRegistrationContribution() { |
| 53 | + Set<Class<?>> hintClasses = Set.of(MultipleJGitEnvironmentRepository.class, |
| 54 | + SvnKitEnvironmentRepository.class, SvnKitEnvironmentProperties.class, |
| 55 | + MultipleJGitEnvironmentRepositoryFactory.class, SvnEnvironmentRepositoryFactory.class, |
| 56 | + MultipleJGitEnvironmentProperties.class); |
| 57 | + new WebApplicationContextRunner(AnnotationConfigServletWebApplicationContext::new) |
| 58 | + .withUserConfiguration(TestConfigServerApplication.class) |
| 59 | + .withPropertyValues("spring.cloud.refresh.enabled=false", |
| 60 | + "spring.cloud.config.server.composite[0].uri:file:./target/repos/config-repo", |
| 61 | + "spring.cloud.config.server.composite[0].type:git", |
| 62 | + "spring.cloud.config.server.composite[1].uri:file:///./target/repos/svn-config-repo", |
| 63 | + "spring.cloud.config.server.composite[1].type:svn", "spring.profiles.active:test,composite") |
| 64 | + .prepare(context -> { |
| 65 | + TestGenerationContext generationContext = new TestGenerationContext(TestTarget.class); |
| 66 | + ClassName className = new ApplicationContextAotGenerator().processAheadOfTime( |
| 67 | + (GenericApplicationContext) context.getSourceApplicationContext(), generationContext); |
| 68 | + generationContext.writeGeneratedContent(); |
| 69 | + Optional<String> source = getGeneratedSource(generationContext, className.simpleName()); |
| 70 | + assertThat(source).isNotEmpty(); |
| 71 | + assertThat(source.get()).contains("beanFactory.registerBeanDefinition(\"git-env-repo-properties0\", propertiesDefinition0);", |
| 72 | + "beanFactory.registerBeanDefinition(\"git-env-repo0\", repoBeanDefinition0);", |
| 73 | + "beanFactory.registerBeanDefinition(\"svn-env-repo-properties1\", propertiesDefinition1);", |
| 74 | + "beanFactory.registerBeanDefinition(\"svn-env-repo1\", repoBeanDefinition1);"); |
| 75 | + ReflectionHints hints = generationContext.getRuntimeHints().reflection(); |
| 76 | + hintClasses.forEach(clazz -> assertThat(hints.getTypeHint(clazz)).isNotNull()); |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + private static Optional<String> getGeneratedSource(TestGenerationContext generationContext, String simpleClassName) { |
| 81 | + return generationContext. |
| 82 | + getGeneratedFiles() |
| 83 | + .getGeneratedFiles(GeneratedFiles.Kind.SOURCE) |
| 84 | + .values() |
| 85 | + .stream() |
| 86 | + .map(inputStreamSource -> { |
| 87 | + try { |
| 88 | + return new String(inputStreamSource.getInputStream() |
| 89 | + .readAllBytes(), Charset.defaultCharset()); |
| 90 | + } |
| 91 | + catch (IOException e) { |
| 92 | + throw new RuntimeException(e); |
| 93 | + } |
| 94 | + }) |
| 95 | + .filter(source -> |
| 96 | + source.contains(simpleClassName)) |
| 97 | + .findAny(); |
| 98 | + } |
| 99 | + |
| 100 | + static class TestTarget { } |
| 101 | + |
| 102 | +} |
0 commit comments