16
16
17
17
package org .springframework .boot .context .properties ;
18
18
19
+ import java .util .Arrays ;
20
+ import java .util .function .Consumer ;
21
+
19
22
import org .junit .jupiter .api .Test ;
20
23
24
+ import org .springframework .aot .test .generator .compile .CompileWithTargetClassAccess ;
25
+ import org .springframework .aot .test .generator .compile .TestCompiler ;
21
26
import org .springframework .beans .factory .aot .AotServices ;
22
27
import org .springframework .beans .factory .aot .BeanRegistrationAotContribution ;
23
28
import org .springframework .beans .factory .aot .BeanRegistrationAotProcessor ;
24
29
import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
25
30
import org .springframework .beans .factory .support .RegisteredBean ;
26
31
import org .springframework .beans .factory .support .RootBeanDefinition ;
32
+ import org .springframework .boot .context .properties .scan .valid .b .BScanConfiguration ;
33
+ import org .springframework .boot .context .properties .scan .valid .b .BScanConfiguration .BFirstProperties ;
34
+ import org .springframework .boot .context .properties .scan .valid .b .BScanConfiguration .BSecondProperties ;
35
+ import org .springframework .context .ApplicationContextInitializer ;
36
+ import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
37
+ import org .springframework .context .annotation .Configuration ;
38
+ import org .springframework .context .aot .ApplicationContextAotGenerator ;
39
+ import org .springframework .context .support .GenericApplicationContext ;
40
+ import org .springframework .javapoet .ClassName ;
41
+ import org .springframework .test .aot .generate .TestGenerationContext ;
42
+ import org .springframework .test .context .support .TestPropertySourceUtils ;
27
43
28
44
import static org .assertj .core .api .Assertions .assertThat ;
29
45
@@ -73,6 +89,78 @@ private BeanRegistrationAotContribution process(Class<?> type) {
73
89
return this .processor .processAheadOfTime (registeredBean );
74
90
}
75
91
92
+ @ Test
93
+ @ CompileWithTargetClassAccess
94
+ void aotContributedInitializerBindsValueObject () {
95
+ compile (createContext (ValueObjectSampleBeanConfiguration .class ), (freshContext ) -> {
96
+ TestPropertySourceUtils .addInlinedPropertiesToEnvironment (freshContext , "test.name=Hello" );
97
+ freshContext .refresh ();
98
+ ValueObjectSampleBean bean = freshContext .getBean (ValueObjectSampleBean .class );
99
+ assertThat (bean .name ).isEqualTo ("Hello" );
100
+ });
101
+ }
102
+
103
+ @ Test
104
+ @ CompileWithTargetClassAccess
105
+ void aotContributedInitializerBindsJavaBean () {
106
+ compile (createContext (JavaBeanSampleBeanConfiguration .class ), (freshContext ) -> {
107
+ TestPropertySourceUtils .addInlinedPropertiesToEnvironment (freshContext , "test.name=Hello" );
108
+ freshContext .refresh ();
109
+ JavaBeanSampleBean bean = freshContext .getBean (JavaBeanSampleBean .class );
110
+ assertThat (bean .getName ()).isEqualTo ("Hello" );
111
+ });
112
+ }
113
+
114
+ @ Test
115
+ @ CompileWithTargetClassAccess
116
+ void aotContributedInitializerBindsScannedValueObject () {
117
+ compile (createContext (ScanTestConfiguration .class ), (freshContext ) -> {
118
+ TestPropertySourceUtils .addInlinedPropertiesToEnvironment (freshContext , "b.first.name=Hello" );
119
+ freshContext .refresh ();
120
+ BFirstProperties bean = freshContext .getBean (BFirstProperties .class );
121
+ assertThat (bean .getName ()).isEqualTo ("Hello" );
122
+ });
123
+ }
124
+
125
+ @ Test
126
+ @ CompileWithTargetClassAccess
127
+ void aotContributedInitializerBindsScannedJavaBean () {
128
+ compile (createContext (ScanTestConfiguration .class ), (freshContext ) -> {
129
+ TestPropertySourceUtils .addInlinedPropertiesToEnvironment (freshContext , "b.second.number=42" );
130
+ freshContext .refresh ();
131
+ BSecondProperties bean = freshContext .getBean (BSecondProperties .class );
132
+ assertThat (bean .getNumber ()).isEqualTo (42 );
133
+ });
134
+ }
135
+
136
+ private GenericApplicationContext createContext (Class <?>... types ) {
137
+ GenericApplicationContext context = new AnnotationConfigApplicationContext ();
138
+ context .registerBean (JavaBeanSampleBeanConfiguration .class );
139
+ Arrays .stream (types ).forEach ((type ) -> context .registerBean (type ));
140
+ return context ;
141
+ }
142
+
143
+ @ SuppressWarnings ("unchecked" )
144
+ private void compile (GenericApplicationContext context , Consumer <GenericApplicationContext > freshContext ) {
145
+ TestGenerationContext generationContext = new TestGenerationContext (TestTarget .class );
146
+ ClassName className = new ApplicationContextAotGenerator ().generateApplicationContext (context ,
147
+ generationContext );
148
+ generationContext .writeGeneratedContent ();
149
+ TestCompiler .forSystem ().withFiles (generationContext .getGeneratedFiles ()).compile ((compiled ) -> {
150
+ GenericApplicationContext freshApplicationContext = new GenericApplicationContext ();
151
+ ApplicationContextInitializer <GenericApplicationContext > initializer = compiled
152
+ .getInstance (ApplicationContextInitializer .class , className .toString ());
153
+ initializer .initialize (freshApplicationContext );
154
+ freshContext .accept (freshApplicationContext );
155
+ });
156
+ }
157
+
158
+ @ Configuration (proxyBeanMethods = false )
159
+ @ EnableConfigurationProperties (JavaBeanSampleBean .class )
160
+ static class JavaBeanSampleBeanConfiguration {
161
+
162
+ }
163
+
76
164
@ ConfigurationProperties ("test" )
77
165
public static class JavaBeanSampleBean {
78
166
@@ -88,6 +176,12 @@ public void setName(String name) {
88
176
89
177
}
90
178
179
+ @ Configuration (proxyBeanMethods = false )
180
+ @ EnableConfigurationProperties (ValueObjectSampleBean .class )
181
+ static class ValueObjectSampleBeanConfiguration {
182
+
183
+ }
184
+
91
185
@ ConfigurationProperties ("test" )
92
186
public static class ValueObjectSampleBean {
93
187
@@ -100,4 +194,14 @@ public static class ValueObjectSampleBean {
100
194
101
195
}
102
196
197
+ @ Configuration (proxyBeanMethods = false )
198
+ @ ConfigurationPropertiesScan (basePackageClasses = BScanConfiguration .class )
199
+ static class ScanTestConfiguration {
200
+
201
+ }
202
+
203
+ static class TestTarget {
204
+
205
+ }
206
+
103
207
}
0 commit comments