36
36
import org .springframework .beans .factory .BeanFactoryAware ;
37
37
import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
38
38
import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
39
- import org .springframework .boot .autoconfigure .freemarker .FreeMarkerAutoConfiguration ;
40
- import org .springframework .boot .autoconfigure .mustache .MustacheAutoConfiguration ;
41
- import org .springframework .boot .autoconfigure .thymeleaf .ThymeleafAutoConfiguration ;
42
39
import org .springframework .boot .context .annotation .ImportCandidates ;
40
+ import org .springframework .boot .testsupport .classpath .resources .WithResource ;
43
41
import org .springframework .context .annotation .Configuration ;
44
42
import org .springframework .context .annotation .DeferredImportSelector .Group ;
45
43
import org .springframework .context .annotation .DeferredImportSelector .Group .Entry ;
57
55
* @author Stephane Nicoll
58
56
* @author Madhura Bhave
59
57
*/
58
+ @ WithResource (name = "META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports" , content = """
59
+ com.example.one.FirstAutoConfiguration
60
+ com.example.two.SecondAutoConfiguration
61
+ com.example.three.ThirdAutoConfiguration
62
+ com.example.four.FourthAutoConfiguration
63
+ com.example.five.FifthAutoConfiguration
64
+ com.example.six.SixthAutoConfiguration
65
+ org.springframework.boot.autoconfigure.AutoConfigurationImportSelectorTests$SeventhAutoConfiguration
66
+ """ )
60
67
class AutoConfigurationImportSelectorTests {
61
68
62
69
private final TestAutoConfigurationImportSelector importSelector = new TestAutoConfigurationImportSelector (null );
@@ -84,78 +91,78 @@ void classExclusionsAreApplied() {
84
91
String [] imports = selectImports (EnableAutoConfigurationWithClassExclusions .class );
85
92
assertThat (imports ).hasSize (getAutoConfigurationClassNames ().size () - 1 );
86
93
assertThat (this .importSelector .getLastEvent ().getExclusions ())
87
- .contains (FreeMarkerAutoConfiguration .class .getName ());
94
+ .contains (SeventhAutoConfiguration .class .getName ());
88
95
}
89
96
90
97
@ Test
91
98
void classExclusionsAreAppliedWhenUsingSpringBootApplication () {
92
99
String [] imports = selectImports (SpringBootApplicationWithClassExclusions .class );
93
100
assertThat (imports ).hasSize (getAutoConfigurationClassNames ().size () - 1 );
94
101
assertThat (this .importSelector .getLastEvent ().getExclusions ())
95
- .contains (FreeMarkerAutoConfiguration .class .getName ());
102
+ .contains (SeventhAutoConfiguration .class .getName ());
96
103
}
97
104
98
105
@ Test
99
106
void classNamesExclusionsAreApplied () {
100
107
String [] imports = selectImports (EnableAutoConfigurationWithClassNameExclusions .class );
101
108
assertThat (imports ).hasSize (getAutoConfigurationClassNames ().size () - 1 );
102
109
assertThat (this .importSelector .getLastEvent ().getExclusions ())
103
- .contains (MustacheAutoConfiguration . class . getName () );
110
+ .contains ("com.example.one.FirstAutoConfiguration" );
104
111
}
105
112
106
113
@ Test
107
114
void classNamesExclusionsAreAppliedWhenUsingSpringBootApplication () {
108
115
String [] imports = selectImports (SpringBootApplicationWithClassNameExclusions .class );
109
116
assertThat (imports ).hasSize (getAutoConfigurationClassNames ().size () - 1 );
110
117
assertThat (this .importSelector .getLastEvent ().getExclusions ())
111
- .contains (MustacheAutoConfiguration . class . getName () );
118
+ .contains ("com.example.three.ThirdAutoConfiguration" );
112
119
}
113
120
114
121
@ Test
115
122
void propertyExclusionsAreApplied () {
116
- this .environment .setProperty ("spring.autoconfigure.exclude" , FreeMarkerAutoConfiguration . class . getName () );
123
+ this .environment .setProperty ("spring.autoconfigure.exclude" , "com.example.three.ThirdAutoConfiguration" );
117
124
String [] imports = selectImports (BasicEnableAutoConfiguration .class );
118
125
assertThat (imports ).hasSize (getAutoConfigurationClassNames ().size () - 1 );
119
126
assertThat (this .importSelector .getLastEvent ().getExclusions ())
120
- .contains (FreeMarkerAutoConfiguration . class . getName () );
127
+ .contains ("com.example.three.ThirdAutoConfiguration" );
121
128
}
122
129
123
130
@ Test
124
131
void severalPropertyExclusionsAreApplied () {
125
132
this .environment .setProperty ("spring.autoconfigure.exclude" ,
126
- FreeMarkerAutoConfiguration . class . getName () + "," + MustacheAutoConfiguration . class . getName () );
133
+ "com.example.two.SecondAutoConfiguration,com.example.four.FourthAutoConfiguration" );
127
134
testSeveralPropertyExclusionsAreApplied ();
128
135
}
129
136
130
137
@ Test
131
138
void severalPropertyExclusionsAreAppliedWithExtraSpaces () {
132
139
this .environment .setProperty ("spring.autoconfigure.exclude" ,
133
- FreeMarkerAutoConfiguration . class . getName () + " , " + MustacheAutoConfiguration . class . getName () + " " );
140
+ "com.example.two.SecondAutoConfiguration , com.example.four.FourthAutoConfiguration " );
134
141
testSeveralPropertyExclusionsAreApplied ();
135
142
}
136
143
137
144
@ Test
138
145
void severalPropertyYamlExclusionsAreApplied () {
139
- this .environment .setProperty ("spring.autoconfigure.exclude[0]" , FreeMarkerAutoConfiguration . class . getName () );
140
- this .environment .setProperty ("spring.autoconfigure.exclude[1]" , MustacheAutoConfiguration . class . getName () );
146
+ this .environment .setProperty ("spring.autoconfigure.exclude[0]" , "com.example.two.SecondAutoConfiguration" );
147
+ this .environment .setProperty ("spring.autoconfigure.exclude[1]" , "com.example.four.FourthAutoConfiguration" );
141
148
testSeveralPropertyExclusionsAreApplied ();
142
149
}
143
150
144
151
private void testSeveralPropertyExclusionsAreApplied () {
145
152
String [] imports = selectImports (BasicEnableAutoConfiguration .class );
146
153
assertThat (imports ).hasSize (getAutoConfigurationClassNames ().size () - 2 );
147
154
assertThat (this .importSelector .getLastEvent ().getExclusions ())
148
- .contains (FreeMarkerAutoConfiguration . class . getName (), MustacheAutoConfiguration . class . getName () );
155
+ .contains ("com.example.two.SecondAutoConfiguration" , "com.example.four.FourthAutoConfiguration" );
149
156
}
150
157
151
158
@ Test
152
159
void combinedExclusionsAreApplied () {
153
- this .environment .setProperty ("spring.autoconfigure.exclude" , ThymeleafAutoConfiguration . class . getName () );
160
+ this .environment .setProperty ("spring.autoconfigure.exclude" , "com.example.one.FirstAutoConfiguration" );
154
161
String [] imports = selectImports (EnableAutoConfigurationWithClassAndClassNameExclusions .class );
155
162
assertThat (imports ).hasSize (getAutoConfigurationClassNames ().size () - 3 );
156
163
assertThat (this .importSelector .getLastEvent ().getExclusions ()).contains (
157
- FreeMarkerAutoConfiguration . class . getName (), MustacheAutoConfiguration . class . getName () ,
158
- ThymeleafAutoConfiguration .class .getName ());
164
+ "com.example.one.FirstAutoConfiguration" , "com.example.five.FifthAutoConfiguration" ,
165
+ SeventhAutoConfiguration .class .getName ());
159
166
}
160
167
161
168
@ Test
@@ -247,13 +254,15 @@ private String[] selectImports(Class<?> source) {
247
254
}
248
255
249
256
private List <String > getAutoConfigurationClassNames () {
250
- return ImportCandidates .load (AutoConfiguration .class , getClass ().getClassLoader ()).getCandidates ();
257
+ return ImportCandidates .load (AutoConfiguration .class , Thread .currentThread ().getContextClassLoader ())
258
+ .getCandidates ();
251
259
}
252
260
253
261
private void setupImportSelector (TestAutoConfigurationImportSelector importSelector ) {
254
262
importSelector .setBeanFactory (this .beanFactory );
255
263
importSelector .setEnvironment (this .environment );
256
264
importSelector .setResourceLoader (new DefaultResourceLoader ());
265
+ importSelector .setBeanClassLoader (Thread .currentThread ().getContextClassLoader ());
257
266
}
258
267
259
268
private final class TestAutoConfigurationImportSelector extends AutoConfigurationImportSelector {
@@ -322,23 +331,23 @@ private final class BasicEnableAutoConfiguration {
322
331
323
332
}
324
333
325
- @ EnableAutoConfiguration (exclude = FreeMarkerAutoConfiguration .class )
334
+ @ EnableAutoConfiguration (exclude = SeventhAutoConfiguration .class )
326
335
private final class EnableAutoConfigurationWithClassExclusions {
327
336
328
337
}
329
338
330
- @ SpringBootApplication (exclude = FreeMarkerAutoConfiguration .class )
339
+ @ SpringBootApplication (exclude = SeventhAutoConfiguration .class )
331
340
private final class SpringBootApplicationWithClassExclusions {
332
341
333
342
}
334
343
335
- @ EnableAutoConfiguration (excludeName = "org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration " )
344
+ @ EnableAutoConfiguration (excludeName = "com.example.one.FirstAutoConfiguration " )
336
345
private final class EnableAutoConfigurationWithClassNameExclusions {
337
346
338
347
}
339
348
340
- @ EnableAutoConfiguration (exclude = MustacheAutoConfiguration .class ,
341
- excludeName = "org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration " )
349
+ @ EnableAutoConfiguration (exclude = SeventhAutoConfiguration .class ,
350
+ excludeName = "com.example.five.FifthAutoConfiguration " )
342
351
private final class EnableAutoConfigurationWithClassAndClassNameExclusions {
343
352
344
353
}
@@ -359,7 +368,7 @@ private final class EnableAutoConfigurationWithAbsentClassNameExclude {
359
368
360
369
}
361
370
362
- @ SpringBootApplication (excludeName = "org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration " )
371
+ @ SpringBootApplication (excludeName = "com.example.three.ThirdAutoConfiguration " )
363
372
private final class SpringBootApplicationWithClassNameExclusions {
364
373
365
374
}
@@ -383,4 +392,9 @@ static class AfterDeprecatedAutoConfiguration {
383
392
384
393
}
385
394
395
+ @ AutoConfiguration
396
+ static class SeventhAutoConfiguration {
397
+
398
+ }
399
+
386
400
}
0 commit comments