Skip to content

Commit 7a35ad7

Browse files
committed
Add test for CompositeEnvironmentBeanFactoryInitializationAotProcessor. Add javadocs.
1 parent 4bdb292 commit 7a35ad7

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

spring-cloud-config-server/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,11 @@
238238
<artifactId>spring-cloud-aws-s3</artifactId>
239239
<scope>test</scope>
240240
</dependency>
241+
<dependency>
242+
<groupId>org.springframework</groupId>
243+
<artifactId>spring-core-test</artifactId>
244+
<scope>test</scope>
245+
</dependency>
241246
</dependencies>
242247

243248
<properties>

spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/aot/CompositeEnvironmentBeanFactoryInitializationAotProcessor.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@
5555
import org.springframework.util.ClassUtils;
5656

5757
/**
58+
* A {@link BeanFactoryInitializationAotProcessor} implementation that generates code
59+
* for registering composite environment repository beans.
60+
*
5861
* @author Olga Maciaszek-Sharma
62+
* @since 4.1.2
5963
*/
6064
public class CompositeEnvironmentBeanFactoryInitializationAotProcessor
6165
implements BeanFactoryInitializationAotProcessor, BeanRegistrationExcludeFilter {
@@ -138,7 +142,7 @@ private void generateRuntimeHints(RuntimeHints runtimeHints) {
138142
@SuppressWarnings("unchecked")
139143
private void generateRegisterBeanDefinitionsMethod(MethodSpec.Builder method) {
140144
method.addJavadoc(
141-
"Register the EnvironmentRepositoryProperties bean definitions for composite config data sources.");
145+
"Register composite environment repository bean definitions for composite config data sources.");
142146
method.addModifiers(Modifier.PUBLIC);
143147
method.addParameter(DefaultListableBeanFactory.class, "beanFactory");
144148
method.addParameter(Environment.class, "environment");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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

Comments
 (0)