3636import org .springframework .beans .factory .BeanFactoryAware ;
3737import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
3838import 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 ;
4239import org .springframework .boot .context .annotation .ImportCandidates ;
40+ import org .springframework .boot .testsupport .classpath .resources .WithResource ;
4341import org .springframework .context .annotation .Configuration ;
4442import org .springframework .context .annotation .DeferredImportSelector .Group ;
4543import org .springframework .context .annotation .DeferredImportSelector .Group .Entry ;
5755 * @author Stephane Nicoll
5856 * @author Madhura Bhave
5957 */
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+ """ )
6067class AutoConfigurationImportSelectorTests {
6168
6269 private final TestAutoConfigurationImportSelector importSelector = new TestAutoConfigurationImportSelector (null );
@@ -84,78 +91,78 @@ void classExclusionsAreApplied() {
8491 String [] imports = selectImports (EnableAutoConfigurationWithClassExclusions .class );
8592 assertThat (imports ).hasSize (getAutoConfigurationClassNames ().size () - 1 );
8693 assertThat (this .importSelector .getLastEvent ().getExclusions ())
87- .contains (FreeMarkerAutoConfiguration .class .getName ());
94+ .contains (SeventhAutoConfiguration .class .getName ());
8895 }
8996
9097 @ Test
9198 void classExclusionsAreAppliedWhenUsingSpringBootApplication () {
9299 String [] imports = selectImports (SpringBootApplicationWithClassExclusions .class );
93100 assertThat (imports ).hasSize (getAutoConfigurationClassNames ().size () - 1 );
94101 assertThat (this .importSelector .getLastEvent ().getExclusions ())
95- .contains (FreeMarkerAutoConfiguration .class .getName ());
102+ .contains (SeventhAutoConfiguration .class .getName ());
96103 }
97104
98105 @ Test
99106 void classNamesExclusionsAreApplied () {
100107 String [] imports = selectImports (EnableAutoConfigurationWithClassNameExclusions .class );
101108 assertThat (imports ).hasSize (getAutoConfigurationClassNames ().size () - 1 );
102109 assertThat (this .importSelector .getLastEvent ().getExclusions ())
103- .contains (MustacheAutoConfiguration . class . getName () );
110+ .contains ("com.example.one.FirstAutoConfiguration" );
104111 }
105112
106113 @ Test
107114 void classNamesExclusionsAreAppliedWhenUsingSpringBootApplication () {
108115 String [] imports = selectImports (SpringBootApplicationWithClassNameExclusions .class );
109116 assertThat (imports ).hasSize (getAutoConfigurationClassNames ().size () - 1 );
110117 assertThat (this .importSelector .getLastEvent ().getExclusions ())
111- .contains (MustacheAutoConfiguration . class . getName () );
118+ .contains ("com.example.three.ThirdAutoConfiguration" );
112119 }
113120
114121 @ Test
115122 void propertyExclusionsAreApplied () {
116- this .environment .setProperty ("spring.autoconfigure.exclude" , FreeMarkerAutoConfiguration . class . getName () );
123+ this .environment .setProperty ("spring.autoconfigure.exclude" , "com.example.three.ThirdAutoConfiguration" );
117124 String [] imports = selectImports (BasicEnableAutoConfiguration .class );
118125 assertThat (imports ).hasSize (getAutoConfigurationClassNames ().size () - 1 );
119126 assertThat (this .importSelector .getLastEvent ().getExclusions ())
120- .contains (FreeMarkerAutoConfiguration . class . getName () );
127+ .contains ("com.example.three.ThirdAutoConfiguration" );
121128 }
122129
123130 @ Test
124131 void severalPropertyExclusionsAreApplied () {
125132 this .environment .setProperty ("spring.autoconfigure.exclude" ,
126- FreeMarkerAutoConfiguration . class . getName () + "," + MustacheAutoConfiguration . class . getName () );
133+ "com.example.two.SecondAutoConfiguration,com.example.four.FourthAutoConfiguration" );
127134 testSeveralPropertyExclusionsAreApplied ();
128135 }
129136
130137 @ Test
131138 void severalPropertyExclusionsAreAppliedWithExtraSpaces () {
132139 this .environment .setProperty ("spring.autoconfigure.exclude" ,
133- FreeMarkerAutoConfiguration . class . getName () + " , " + MustacheAutoConfiguration . class . getName () + " " );
140+ "com.example.two.SecondAutoConfiguration , com.example.four.FourthAutoConfiguration " );
134141 testSeveralPropertyExclusionsAreApplied ();
135142 }
136143
137144 @ Test
138145 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" );
141148 testSeveralPropertyExclusionsAreApplied ();
142149 }
143150
144151 private void testSeveralPropertyExclusionsAreApplied () {
145152 String [] imports = selectImports (BasicEnableAutoConfiguration .class );
146153 assertThat (imports ).hasSize (getAutoConfigurationClassNames ().size () - 2 );
147154 assertThat (this .importSelector .getLastEvent ().getExclusions ())
148- .contains (FreeMarkerAutoConfiguration . class . getName (), MustacheAutoConfiguration . class . getName () );
155+ .contains ("com.example.two.SecondAutoConfiguration" , "com.example.four.FourthAutoConfiguration" );
149156 }
150157
151158 @ Test
152159 void combinedExclusionsAreApplied () {
153- this .environment .setProperty ("spring.autoconfigure.exclude" , ThymeleafAutoConfiguration . class . getName () );
160+ this .environment .setProperty ("spring.autoconfigure.exclude" , "com.example.one.FirstAutoConfiguration" );
154161 String [] imports = selectImports (EnableAutoConfigurationWithClassAndClassNameExclusions .class );
155162 assertThat (imports ).hasSize (getAutoConfigurationClassNames ().size () - 3 );
156163 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 ());
159166 }
160167
161168 @ Test
@@ -247,13 +254,15 @@ private String[] selectImports(Class<?> source) {
247254 }
248255
249256 private List <String > getAutoConfigurationClassNames () {
250- return ImportCandidates .load (AutoConfiguration .class , getClass ().getClassLoader ()).getCandidates ();
257+ return ImportCandidates .load (AutoConfiguration .class , Thread .currentThread ().getContextClassLoader ())
258+ .getCandidates ();
251259 }
252260
253261 private void setupImportSelector (TestAutoConfigurationImportSelector importSelector ) {
254262 importSelector .setBeanFactory (this .beanFactory );
255263 importSelector .setEnvironment (this .environment );
256264 importSelector .setResourceLoader (new DefaultResourceLoader ());
265+ importSelector .setBeanClassLoader (Thread .currentThread ().getContextClassLoader ());
257266 }
258267
259268 private final class TestAutoConfigurationImportSelector extends AutoConfigurationImportSelector {
@@ -322,23 +331,23 @@ private final class BasicEnableAutoConfiguration {
322331
323332 }
324333
325- @ EnableAutoConfiguration (exclude = FreeMarkerAutoConfiguration .class )
334+ @ EnableAutoConfiguration (exclude = SeventhAutoConfiguration .class )
326335 private final class EnableAutoConfigurationWithClassExclusions {
327336
328337 }
329338
330- @ SpringBootApplication (exclude = FreeMarkerAutoConfiguration .class )
339+ @ SpringBootApplication (exclude = SeventhAutoConfiguration .class )
331340 private final class SpringBootApplicationWithClassExclusions {
332341
333342 }
334343
335- @ EnableAutoConfiguration (excludeName = "org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration " )
344+ @ EnableAutoConfiguration (excludeName = "com.example.one.FirstAutoConfiguration " )
336345 private final class EnableAutoConfigurationWithClassNameExclusions {
337346
338347 }
339348
340- @ EnableAutoConfiguration (exclude = MustacheAutoConfiguration .class ,
341- excludeName = "org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration " )
349+ @ EnableAutoConfiguration (exclude = SeventhAutoConfiguration .class ,
350+ excludeName = "com.example.five.FifthAutoConfiguration " )
342351 private final class EnableAutoConfigurationWithClassAndClassNameExclusions {
343352
344353 }
@@ -359,7 +368,7 @@ private final class EnableAutoConfigurationWithAbsentClassNameExclude {
359368
360369 }
361370
362- @ SpringBootApplication (excludeName = "org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration " )
371+ @ SpringBootApplication (excludeName = "com.example.three.ThirdAutoConfiguration " )
363372 private final class SpringBootApplicationWithClassNameExclusions {
364373
365374 }
@@ -383,4 +392,9 @@ static class AfterDeprecatedAutoConfiguration {
383392
384393 }
385394
395+ @ AutoConfiguration
396+ static class SeventhAutoConfiguration {
397+
398+ }
399+
386400}
0 commit comments