2929import org .springframework .beans .factory .BeanFactoryAware ;
3030import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
3131import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
32- import org .springframework .boot .autoconfigure .freemarker .FreeMarkerAutoConfiguration ;
33- import org .springframework .boot .autoconfigure .mustache .MustacheAutoConfiguration ;
34- import org .springframework .boot .autoconfigure .thymeleaf .ThymeleafAutoConfiguration ;
3532import org .springframework .boot .context .annotation .ImportCandidates ;
33+ import org .springframework .boot .testsupport .classpath .resources .WithResource ;
3634import org .springframework .context .annotation .Configuration ;
3735import org .springframework .core .io .DefaultResourceLoader ;
3836import org .springframework .core .type .AnnotationMetadata ;
4846 * @author Stephane Nicoll
4947 * @author Madhura Bhave
5048 */
49+ @ WithResource (name = "META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports" , content = """
50+ com.example.one.FirstAutoConfiguration
51+ com.example.two.SecondAutoConfiguration
52+ com.example.three.ThirdAutoConfiguration
53+ com.example.four.FourthAutoConfiguration
54+ com.example.five.FifthAutoConfiguration
55+ com.example.six.SixthAutoConfiguration
56+ org.springframework.boot.autoconfigure.AutoConfigurationImportSelectorTests$SeventhAutoConfiguration
57+ """ )
5158class AutoConfigurationImportSelectorTests {
5259
5360 private final TestAutoConfigurationImportSelector importSelector = new TestAutoConfigurationImportSelector ();
@@ -63,6 +70,7 @@ void setup() {
6370 this .importSelector .setBeanFactory (this .beanFactory );
6471 this .importSelector .setEnvironment (this .environment );
6572 this .importSelector .setResourceLoader (new DefaultResourceLoader ());
73+ this .importSelector .setBeanClassLoader (Thread .currentThread ().getContextClassLoader ());
6674 }
6775
6876 @ Test
@@ -77,78 +85,78 @@ void classExclusionsAreApplied() {
7785 String [] imports = selectImports (EnableAutoConfigurationWithClassExclusions .class );
7886 assertThat (imports ).hasSize (getAutoConfigurationClassNames ().size () - 1 );
7987 assertThat (this .importSelector .getLastEvent ().getExclusions ())
80- .contains (FreeMarkerAutoConfiguration .class .getName ());
88+ .contains (SeventhAutoConfiguration .class .getName ());
8189 }
8290
8391 @ Test
8492 void classExclusionsAreAppliedWhenUsingSpringBootApplication () {
8593 String [] imports = selectImports (SpringBootApplicationWithClassExclusions .class );
8694 assertThat (imports ).hasSize (getAutoConfigurationClassNames ().size () - 1 );
8795 assertThat (this .importSelector .getLastEvent ().getExclusions ())
88- .contains (FreeMarkerAutoConfiguration .class .getName ());
96+ .contains (SeventhAutoConfiguration .class .getName ());
8997 }
9098
9199 @ Test
92100 void classNamesExclusionsAreApplied () {
93101 String [] imports = selectImports (EnableAutoConfigurationWithClassNameExclusions .class );
94102 assertThat (imports ).hasSize (getAutoConfigurationClassNames ().size () - 1 );
95103 assertThat (this .importSelector .getLastEvent ().getExclusions ())
96- .contains (MustacheAutoConfiguration . class . getName () );
104+ .contains ("com.example.one.FirstAutoConfiguration" );
97105 }
98106
99107 @ Test
100108 void classNamesExclusionsAreAppliedWhenUsingSpringBootApplication () {
101109 String [] imports = selectImports (SpringBootApplicationWithClassNameExclusions .class );
102110 assertThat (imports ).hasSize (getAutoConfigurationClassNames ().size () - 1 );
103111 assertThat (this .importSelector .getLastEvent ().getExclusions ())
104- .contains (MustacheAutoConfiguration . class . getName () );
112+ .contains ("com.example.three.ThirdAutoConfiguration" );
105113 }
106114
107115 @ Test
108116 void propertyExclusionsAreApplied () {
109- this .environment .setProperty ("spring.autoconfigure.exclude" , FreeMarkerAutoConfiguration . class . getName () );
117+ this .environment .setProperty ("spring.autoconfigure.exclude" , "com.example.three.ThirdAutoConfiguration" );
110118 String [] imports = selectImports (BasicEnableAutoConfiguration .class );
111119 assertThat (imports ).hasSize (getAutoConfigurationClassNames ().size () - 1 );
112120 assertThat (this .importSelector .getLastEvent ().getExclusions ())
113- .contains (FreeMarkerAutoConfiguration . class . getName () );
121+ .contains ("com.example.three.ThirdAutoConfiguration" );
114122 }
115123
116124 @ Test
117125 void severalPropertyExclusionsAreApplied () {
118126 this .environment .setProperty ("spring.autoconfigure.exclude" ,
119- FreeMarkerAutoConfiguration . class . getName () + "," + MustacheAutoConfiguration . class . getName () );
127+ "com.example.two.SecondAutoConfiguration,com.example.four.FourthAutoConfiguration" );
120128 testSeveralPropertyExclusionsAreApplied ();
121129 }
122130
123131 @ Test
124132 void severalPropertyExclusionsAreAppliedWithExtraSpaces () {
125133 this .environment .setProperty ("spring.autoconfigure.exclude" ,
126- FreeMarkerAutoConfiguration . class . getName () + " , " + MustacheAutoConfiguration . class . getName () + " " );
134+ "com.example.two.SecondAutoConfiguration , com.example.four.FourthAutoConfiguration " );
127135 testSeveralPropertyExclusionsAreApplied ();
128136 }
129137
130138 @ Test
131139 void severalPropertyYamlExclusionsAreApplied () {
132- this .environment .setProperty ("spring.autoconfigure.exclude[0]" , FreeMarkerAutoConfiguration . class . getName () );
133- this .environment .setProperty ("spring.autoconfigure.exclude[1]" , MustacheAutoConfiguration . class . getName () );
140+ this .environment .setProperty ("spring.autoconfigure.exclude[0]" , "com.example.two.SecondAutoConfiguration" );
141+ this .environment .setProperty ("spring.autoconfigure.exclude[1]" , "com.example.four.FourthAutoConfiguration" );
134142 testSeveralPropertyExclusionsAreApplied ();
135143 }
136144
137145 private void testSeveralPropertyExclusionsAreApplied () {
138146 String [] imports = selectImports (BasicEnableAutoConfiguration .class );
139147 assertThat (imports ).hasSize (getAutoConfigurationClassNames ().size () - 2 );
140148 assertThat (this .importSelector .getLastEvent ().getExclusions ())
141- .contains (FreeMarkerAutoConfiguration . class . getName (), MustacheAutoConfiguration . class . getName () );
149+ .contains ("com.example.two.SecondAutoConfiguration" , "com.example.four.FourthAutoConfiguration" );
142150 }
143151
144152 @ Test
145153 void combinedExclusionsAreApplied () {
146- this .environment .setProperty ("spring.autoconfigure.exclude" , ThymeleafAutoConfiguration . class . getName () );
154+ this .environment .setProperty ("spring.autoconfigure.exclude" , "com.example.one.FirstAutoConfiguration" );
147155 String [] imports = selectImports (EnableAutoConfigurationWithClassAndClassNameExclusions .class );
148156 assertThat (imports ).hasSize (getAutoConfigurationClassNames ().size () - 3 );
149157 assertThat (this .importSelector .getLastEvent ().getExclusions ()).contains (
150- FreeMarkerAutoConfiguration . class . getName (), MustacheAutoConfiguration . class . getName () ,
151- ThymeleafAutoConfiguration .class .getName ());
158+ "com.example.one.FirstAutoConfiguration" , "com.example.five.FifthAutoConfiguration" ,
159+ SeventhAutoConfiguration .class .getName ());
152160 }
153161
154162 @ Test
@@ -213,7 +221,8 @@ private String[] selectImports(Class<?> source) {
213221 }
214222
215223 private List <String > getAutoConfigurationClassNames () {
216- return ImportCandidates .load (AutoConfiguration .class , getClass ().getClassLoader ()).getCandidates ();
224+ return ImportCandidates .load (AutoConfiguration .class , Thread .currentThread ().getContextClassLoader ())
225+ .getCandidates ();
217226 }
218227
219228 private final class TestAutoConfigurationImportSelector extends AutoConfigurationImportSelector {
@@ -278,23 +287,23 @@ private final class BasicEnableAutoConfiguration {
278287
279288 }
280289
281- @ EnableAutoConfiguration (exclude = FreeMarkerAutoConfiguration .class )
290+ @ EnableAutoConfiguration (exclude = SeventhAutoConfiguration .class )
282291 private final class EnableAutoConfigurationWithClassExclusions {
283292
284293 }
285294
286- @ SpringBootApplication (exclude = FreeMarkerAutoConfiguration .class )
295+ @ SpringBootApplication (exclude = SeventhAutoConfiguration .class )
287296 private final class SpringBootApplicationWithClassExclusions {
288297
289298 }
290299
291- @ EnableAutoConfiguration (excludeName = "org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration " )
300+ @ EnableAutoConfiguration (excludeName = "com.example.one.FirstAutoConfiguration " )
292301 private final class EnableAutoConfigurationWithClassNameExclusions {
293302
294303 }
295304
296- @ EnableAutoConfiguration (exclude = MustacheAutoConfiguration .class ,
297- excludeName = "org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration " )
305+ @ EnableAutoConfiguration (exclude = SeventhAutoConfiguration .class ,
306+ excludeName = "com.example.five.FifthAutoConfiguration " )
298307 private final class EnableAutoConfigurationWithClassAndClassNameExclusions {
299308
300309 }
@@ -315,9 +324,14 @@ private final class EnableAutoConfigurationWithAbsentClassNameExclude {
315324
316325 }
317326
318- @ SpringBootApplication (excludeName = "org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration " )
327+ @ SpringBootApplication (excludeName = "com.example.three.ThirdAutoConfiguration " )
319328 private final class SpringBootApplicationWithClassNameExclusions {
320329
321330 }
322331
332+ @ AutoConfiguration
333+ static class SeventhAutoConfiguration {
334+
335+ }
336+
323337}
0 commit comments