42
42
import org .springframework .core .test .tools .Compiled ;
43
43
import org .springframework .core .test .tools .SourceFile ;
44
44
import org .springframework .core .test .tools .TestCompiler ;
45
+ import org .springframework .javapoet .ClassName ;
45
46
import org .springframework .javapoet .CodeBlock ;
46
47
import org .springframework .javapoet .MethodSpec ;
47
48
import org .springframework .javapoet .ParameterizedTypeName ;
49
+ import org .springframework .util .LinkedMultiValueMap ;
50
+ import org .springframework .util .MultiValueMap ;
48
51
49
52
import static org .assertj .core .api .Assertions .assertThat ;
50
53
51
54
/**
52
55
* Tests for {@link BeanRegistrationsAotContribution}.
53
56
*
54
57
* @author Phillip Webb
58
+ * @author Sebastien Deleuze
55
59
*/
56
60
class BeanRegistrationsAotContributionTests {
57
61
@@ -84,7 +88,7 @@ void applyToAppliesContribution() {
84
88
Collections .emptyList ());
85
89
registrations .put ("testBean" , generator );
86
90
BeanRegistrationsAotContribution contribution = new BeanRegistrationsAotContribution (
87
- registrations );
91
+ registrations , new LinkedMultiValueMap <>() );
88
92
contribution .applyTo (this .generationContext , this .beanFactoryInitializationCode );
89
93
compile ((consumer , compiled ) -> {
90
94
DefaultListableBeanFactory freshBeanFactory = new DefaultListableBeanFactory ();
@@ -93,6 +97,27 @@ void applyToAppliesContribution() {
93
97
});
94
98
}
95
99
100
+ @ Test
101
+ void applyToAppliesContributionWithAliases () {
102
+ Map <String , BeanDefinitionMethodGenerator > registrations = new LinkedHashMap <>();
103
+ RegisteredBean registeredBean = registerBean (
104
+ new RootBeanDefinition (TestBean .class ));
105
+ BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator (
106
+ this .methodGeneratorFactory , registeredBean , null ,
107
+ Collections .emptyList ());
108
+ registrations .put ("testBean" , generator );
109
+ MultiValueMap <String , String > aliases = new LinkedMultiValueMap <>();
110
+ aliases .add ("testBean" , "testAlias" );
111
+ BeanRegistrationsAotContribution contribution = new BeanRegistrationsAotContribution (
112
+ registrations , aliases );
113
+ contribution .applyTo (this .generationContext , this .beanFactoryInitializationCode );
114
+ compile ((consumer , compiled ) -> {
115
+ DefaultListableBeanFactory freshBeanFactory = new DefaultListableBeanFactory ();
116
+ consumer .accept (freshBeanFactory );
117
+ assertThat (freshBeanFactory .getAliases ("testBean" )).containsExactly ("testAlias" );
118
+ });
119
+ }
120
+
96
121
@ Test
97
122
void applyToWhenHasNameGeneratesPrefixedFeatureName () {
98
123
this .generationContext = new TestGenerationContext (
@@ -106,7 +131,7 @@ void applyToWhenHasNameGeneratesPrefixedFeatureName() {
106
131
Collections .emptyList ());
107
132
registrations .put ("testBean" , generator );
108
133
BeanRegistrationsAotContribution contribution = new BeanRegistrationsAotContribution (
109
- registrations );
134
+ registrations , new LinkedMultiValueMap <>() );
110
135
contribution .applyTo (this .generationContext , this .beanFactoryInitializationCode );
111
136
compile ((consumer , compiled ) -> {
112
137
SourceFile sourceFile = compiled .getSourceFile (".*BeanDefinitions" );
@@ -136,7 +161,7 @@ MethodReference generateBeanDefinitionMethod(
136
161
};
137
162
registrations .put ("testBean" , generator );
138
163
BeanRegistrationsAotContribution contribution = new BeanRegistrationsAotContribution (
139
- registrations );
164
+ registrations , new LinkedMultiValueMap <>() );
140
165
contribution .applyTo (this .generationContext , this .beanFactoryInitializationCode );
141
166
assertThat (beanRegistrationsCodes ).hasSize (1 );
142
167
BeanRegistrationsCode actual = beanRegistrationsCodes .get (0 );
@@ -152,17 +177,21 @@ private RegisteredBean registerBean(RootBeanDefinition rootBeanDefinition) {
152
177
@ SuppressWarnings ({ "unchecked" , "cast" })
153
178
private void compile (
154
179
BiConsumer <Consumer <DefaultListableBeanFactory >, Compiled > result ) {
155
- MethodReference methodReference = this .beanFactoryInitializationCode
180
+ MethodReference beanRegistrationsMethodReference = this .beanFactoryInitializationCode
156
181
.getInitializers ().get (0 );
182
+ MethodReference aliasesMethodReference = this .beanFactoryInitializationCode
183
+ .getInitializers ().get (1 );
157
184
this .beanFactoryInitializationCode .getTypeBuilder ().set (type -> {
158
- CodeBlock methodInvocation = methodReference .toInvokeCodeBlock (
159
- ArgumentCodeGenerator .of (DefaultListableBeanFactory .class , "beanFactory" ),
160
- this .beanFactoryInitializationCode .getClassName ());
185
+ ArgumentCodeGenerator beanFactory = ArgumentCodeGenerator .of (DefaultListableBeanFactory .class , "beanFactory" );
186
+ ClassName className = this .beanFactoryInitializationCode .getClassName ();
187
+ CodeBlock beanRegistrationsMethodInvocation = beanRegistrationsMethodReference .toInvokeCodeBlock (beanFactory , className );
188
+ CodeBlock aliasesMethodInvocation = aliasesMethodReference .toInvokeCodeBlock (beanFactory , className );
161
189
type .addModifiers (Modifier .PUBLIC );
162
190
type .addSuperinterface (ParameterizedTypeName .get (Consumer .class , DefaultListableBeanFactory .class ));
163
191
type .addMethod (MethodSpec .methodBuilder ("accept" ).addModifiers (Modifier .PUBLIC )
164
192
.addParameter (DefaultListableBeanFactory .class , "beanFactory" )
165
- .addStatement (methodInvocation )
193
+ .addStatement (beanRegistrationsMethodInvocation )
194
+ .addStatement (aliasesMethodInvocation )
166
195
.build ());
167
196
});
168
197
this .generationContext .writeGeneratedContent ();
0 commit comments