47
47
import org .springframework .beans .testfixture .beans .factory .aot .InnerBeanConfiguration ;
48
48
import org .springframework .beans .testfixture .beans .factory .aot .MockBeanRegistrationsCode ;
49
49
import org .springframework .beans .testfixture .beans .factory .aot .SimpleBean ;
50
+ import org .springframework .beans .testfixture .beans .factory .aot .SimpleBeanConfiguration ;
50
51
import org .springframework .beans .testfixture .beans .factory .aot .TestHierarchy ;
51
52
import org .springframework .beans .testfixture .beans .factory .aot .TestHierarchy .Implementation ;
52
53
import org .springframework .beans .testfixture .beans .factory .aot .TestHierarchy .One ;
@@ -92,9 +93,27 @@ class BeanDefinitionMethodGeneratorTests {
92
93
this .beanRegistrationsCode = new MockBeanRegistrationsCode (this .generationContext );
93
94
}
94
95
96
+ @ Test
97
+ void generateWithBeanClassSetsOnlyBeanClass () {
98
+ RootBeanDefinition beanDefinition = new RootBeanDefinition (TestBean .class );
99
+ RegisteredBean registeredBean = registerBean (beanDefinition );
100
+ BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator (
101
+ this .methodGeneratorFactory , registeredBean , null ,
102
+ Collections .emptyList ());
103
+ MethodReference method = generator .generateBeanDefinitionMethod (
104
+ this .generationContext , this .beanRegistrationsCode );
105
+ compile (method , (actual , compiled ) -> {
106
+ SourceFile sourceFile = compiled .getSourceFile (".*BeanDefinitions" );
107
+ assertThat (sourceFile ).contains ("Get the bean definition for 'testBean'" );
108
+ assertThat (sourceFile ).contains ("new RootBeanDefinition(TestBean.class)" );
109
+ assertThat (sourceFile ).doesNotContain ("setTargetType(" );
110
+ assertThat (sourceFile ).contains ("setInstanceSupplier(TestBean::new)" );
111
+ assertThat (actual ).isInstanceOf (RootBeanDefinition .class );
112
+ });
113
+ }
95
114
96
115
@ Test
97
- void generateBeanDefinitionMethodWithOnlyTargetTypeDoesNotSetBeanClass () {
116
+ void generateWithTargetTypeWithNoGenericSetsOnlyBeanClass () {
98
117
RootBeanDefinition beanDefinition = new RootBeanDefinition ();
99
118
beanDefinition .setTargetType (TestBean .class );
100
119
RegisteredBean registeredBean = registerBean (beanDefinition );
@@ -106,34 +125,79 @@ void generateBeanDefinitionMethodWithOnlyTargetTypeDoesNotSetBeanClass() {
106
125
compile (method , (actual , compiled ) -> {
107
126
SourceFile sourceFile = compiled .getSourceFile (".*BeanDefinitions" );
108
127
assertThat (sourceFile ).contains ("Get the bean definition for 'testBean'" );
109
- assertThat (sourceFile ).contains ("new RootBeanDefinition()" );
110
- assertThat (sourceFile ).contains ("setTargetType(TestBean.class)" );
128
+ assertThat (sourceFile ).contains ("new RootBeanDefinition(TestBean.class)" );
111
129
assertThat (sourceFile ).contains ("setInstanceSupplier(TestBean::new)" );
112
130
assertThat (actual ).isInstanceOf (RootBeanDefinition .class );
113
131
});
114
132
}
115
133
116
134
@ Test
117
- void generateBeanDefinitionMethodSpecifiesBeanClassIfSet () {
118
- RootBeanDefinition beanDefinition = new RootBeanDefinition (TestBean .class );
135
+ void generateWithTargetTypeUsingGenericsSetsBothBeanClassAndTargetType () {
136
+ RootBeanDefinition beanDefinition = new RootBeanDefinition ();
137
+ beanDefinition .setTargetType (ResolvableType .forClassWithGenerics (GenericBean .class , Integer .class ));
119
138
RegisteredBean registeredBean = registerBean (beanDefinition );
120
139
BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator (
121
140
this .methodGeneratorFactory , registeredBean , null ,
122
141
Collections .emptyList ());
123
142
MethodReference method = generator .generateBeanDefinitionMethod (
124
143
this .generationContext , this .beanRegistrationsCode );
125
144
compile (method , (actual , compiled ) -> {
145
+ assertThat (actual .getResolvableType ().resolve ()).isEqualTo (GenericBean .class );
126
146
SourceFile sourceFile = compiled .getSourceFile (".*BeanDefinitions" );
127
147
assertThat (sourceFile ).contains ("Get the bean definition for 'testBean'" );
128
- assertThat (sourceFile ).contains ("new RootBeanDefinition(TestBean.class)" );
148
+ assertThat (sourceFile ).contains ("new RootBeanDefinition(GenericBean.class)" );
149
+ assertThat (sourceFile ).contains (
150
+ "setTargetType(ResolvableType.forClassWithGenerics(GenericBean.class, Integer.class))" );
151
+ assertThat (sourceFile ).contains ("setInstanceSupplier(GenericBean::new)" );
152
+ assertThat (actual ).isInstanceOf (RootBeanDefinition .class );
153
+ });
154
+ }
155
+
156
+ @ Test
157
+ void generateWithBeanClassAndFactoryMethodNameSetsTargetTypeAndBeanClass () {
158
+ this .beanFactory .registerSingleton ("factory" , new SimpleBeanConfiguration ());
159
+ RootBeanDefinition beanDefinition = new RootBeanDefinition (SimpleBean .class );
160
+ beanDefinition .setFactoryBeanName ("factory" );
161
+ beanDefinition .setFactoryMethodName ("simpleBean" );
162
+ RegisteredBean registeredBean = registerBean (beanDefinition );
163
+ BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator (
164
+ this .methodGeneratorFactory , registeredBean , null ,
165
+ Collections .emptyList ());
166
+ MethodReference method = generator .generateBeanDefinitionMethod (
167
+ this .generationContext , this .beanRegistrationsCode );
168
+ compile (method , (actual , compiled ) -> {
169
+ SourceFile sourceFile = compiled .getSourceFile (".*BeanDefinitions" );
170
+ assertThat (sourceFile ).contains ("Get the bean definition for 'testBean'" );
171
+ assertThat (sourceFile ).contains ("new RootBeanDefinition(SimpleBean.class)" );
172
+ assertThat (sourceFile ).contains ("setTargetType(SimpleBean.class)" );
173
+ assertThat (actual ).isInstanceOf (RootBeanDefinition .class );
174
+ });
175
+ }
176
+
177
+ @ Test
178
+ void generateWithTargetTypeAndFactoryMethodNameSetsOnlyBeanClass () {
179
+ this .beanFactory .registerSingleton ("factory" , new SimpleBeanConfiguration ());
180
+ RootBeanDefinition beanDefinition = new RootBeanDefinition ();
181
+ beanDefinition .setTargetType (SimpleBean .class );
182
+ beanDefinition .setFactoryBeanName ("factory" );
183
+ beanDefinition .setFactoryMethodName ("simpleBean" );
184
+ RegisteredBean registeredBean = registerBean (beanDefinition );
185
+ BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator (
186
+ this .methodGeneratorFactory , registeredBean , null ,
187
+ Collections .emptyList ());
188
+ MethodReference method = generator .generateBeanDefinitionMethod (
189
+ this .generationContext , this .beanRegistrationsCode );
190
+ compile (method , (actual , compiled ) -> {
191
+ SourceFile sourceFile = compiled .getSourceFile (".*BeanDefinitions" );
192
+ assertThat (sourceFile ).contains ("Get the bean definition for 'testBean'" );
193
+ assertThat (sourceFile ).contains ("new RootBeanDefinition(SimpleBean.class)" );
129
194
assertThat (sourceFile ).doesNotContain ("setTargetType(" );
130
- assertThat (sourceFile ).contains ("setInstanceSupplier(TestBean::new)" );
131
195
assertThat (actual ).isInstanceOf (RootBeanDefinition .class );
132
196
});
133
197
}
134
198
135
199
@ Test
136
- void generateBeanDefinitionMethodSpecifiesBeanClassAndTargetTypIfDifferent () {
200
+ void generateWithBeanClassAndTargetTypeDifferentSetsBoth () {
137
201
RootBeanDefinition beanDefinition = new RootBeanDefinition (One .class );
138
202
beanDefinition .setTargetType (Implementation .class );
139
203
beanDefinition .setResolvedFactoryMethod (ReflectionUtils .findMethod (TestHierarchy .class , "oneBean" ));
@@ -152,6 +216,28 @@ void generateBeanDefinitionMethodSpecifiesBeanClassAndTargetTypIfDifferent() {
152
216
});
153
217
}
154
218
219
+ @ Test
220
+ void generateWithBeanClassAndTargetTypWithGenericSetsBoth () {
221
+ RootBeanDefinition beanDefinition = new RootBeanDefinition (Integer .class );
222
+ beanDefinition .setTargetType (ResolvableType .forClassWithGenerics (GenericBean .class , Integer .class ));
223
+ RegisteredBean registeredBean = registerBean (beanDefinition );
224
+ BeanDefinitionMethodGenerator generator = new BeanDefinitionMethodGenerator (
225
+ this .methodGeneratorFactory , registeredBean , null ,
226
+ Collections .emptyList ());
227
+ MethodReference method = generator .generateBeanDefinitionMethod (
228
+ this .generationContext , this .beanRegistrationsCode );
229
+ compile (method , (actual , compiled ) -> {
230
+ assertThat (actual .getResolvableType ().resolve ()).isEqualTo (GenericBean .class );
231
+ SourceFile sourceFile = compiled .getSourceFile (".*BeanDefinitions" );
232
+ assertThat (sourceFile ).contains ("Get the bean definition for 'testBean'" );
233
+ assertThat (sourceFile ).contains ("new RootBeanDefinition(Integer.class)" );
234
+ assertThat (sourceFile ).contains (
235
+ "setTargetType(ResolvableType.forClassWithGenerics(GenericBean.class, Integer.class))" );
236
+ assertThat (sourceFile ).contains ("setInstanceSupplier(GenericBean::new)" );
237
+ assertThat (actual ).isInstanceOf (RootBeanDefinition .class );
238
+ });
239
+ }
240
+
155
241
@ Test
156
242
void generateBeanDefinitionMethodUSeBeanClassNameIfNotReachable () {
157
243
RootBeanDefinition beanDefinition = new RootBeanDefinition (PackagePrivateTestBean .class );
0 commit comments