29
29
import org .springframework .beans .factory .BeanFactoryAware ;
30
30
import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
31
31
import 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 ;
35
32
import org .springframework .boot .context .annotation .ImportCandidates ;
33
+ import org .springframework .boot .testsupport .classpath .resources .WithResource ;
36
34
import org .springframework .context .annotation .Configuration ;
37
35
import org .springframework .core .io .DefaultResourceLoader ;
38
36
import org .springframework .core .type .AnnotationMetadata ;
48
46
* @author Stephane Nicoll
49
47
* @author Madhura Bhave
50
48
*/
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
+ """ )
51
58
class AutoConfigurationImportSelectorTests {
52
59
53
60
private final TestAutoConfigurationImportSelector importSelector = new TestAutoConfigurationImportSelector ();
@@ -63,6 +70,7 @@ void setup() {
63
70
this .importSelector .setBeanFactory (this .beanFactory );
64
71
this .importSelector .setEnvironment (this .environment );
65
72
this .importSelector .setResourceLoader (new DefaultResourceLoader ());
73
+ this .importSelector .setBeanClassLoader (Thread .currentThread ().getContextClassLoader ());
66
74
}
67
75
68
76
@ Test
@@ -77,78 +85,78 @@ void classExclusionsAreApplied() {
77
85
String [] imports = selectImports (EnableAutoConfigurationWithClassExclusions .class );
78
86
assertThat (imports ).hasSize (getAutoConfigurationClassNames ().size () - 1 );
79
87
assertThat (this .importSelector .getLastEvent ().getExclusions ())
80
- .contains (FreeMarkerAutoConfiguration .class .getName ());
88
+ .contains (SeventhAutoConfiguration .class .getName ());
81
89
}
82
90
83
91
@ Test
84
92
void classExclusionsAreAppliedWhenUsingSpringBootApplication () {
85
93
String [] imports = selectImports (SpringBootApplicationWithClassExclusions .class );
86
94
assertThat (imports ).hasSize (getAutoConfigurationClassNames ().size () - 1 );
87
95
assertThat (this .importSelector .getLastEvent ().getExclusions ())
88
- .contains (FreeMarkerAutoConfiguration .class .getName ());
96
+ .contains (SeventhAutoConfiguration .class .getName ());
89
97
}
90
98
91
99
@ Test
92
100
void classNamesExclusionsAreApplied () {
93
101
String [] imports = selectImports (EnableAutoConfigurationWithClassNameExclusions .class );
94
102
assertThat (imports ).hasSize (getAutoConfigurationClassNames ().size () - 1 );
95
103
assertThat (this .importSelector .getLastEvent ().getExclusions ())
96
- .contains (MustacheAutoConfiguration . class . getName () );
104
+ .contains ("com.example.one.FirstAutoConfiguration" );
97
105
}
98
106
99
107
@ Test
100
108
void classNamesExclusionsAreAppliedWhenUsingSpringBootApplication () {
101
109
String [] imports = selectImports (SpringBootApplicationWithClassNameExclusions .class );
102
110
assertThat (imports ).hasSize (getAutoConfigurationClassNames ().size () - 1 );
103
111
assertThat (this .importSelector .getLastEvent ().getExclusions ())
104
- .contains (MustacheAutoConfiguration . class . getName () );
112
+ .contains ("com.example.three.ThirdAutoConfiguration" );
105
113
}
106
114
107
115
@ Test
108
116
void propertyExclusionsAreApplied () {
109
- this .environment .setProperty ("spring.autoconfigure.exclude" , FreeMarkerAutoConfiguration . class . getName () );
117
+ this .environment .setProperty ("spring.autoconfigure.exclude" , "com.example.three.ThirdAutoConfiguration" );
110
118
String [] imports = selectImports (BasicEnableAutoConfiguration .class );
111
119
assertThat (imports ).hasSize (getAutoConfigurationClassNames ().size () - 1 );
112
120
assertThat (this .importSelector .getLastEvent ().getExclusions ())
113
- .contains (FreeMarkerAutoConfiguration . class . getName () );
121
+ .contains ("com.example.three.ThirdAutoConfiguration" );
114
122
}
115
123
116
124
@ Test
117
125
void severalPropertyExclusionsAreApplied () {
118
126
this .environment .setProperty ("spring.autoconfigure.exclude" ,
119
- FreeMarkerAutoConfiguration . class . getName () + "," + MustacheAutoConfiguration . class . getName () );
127
+ "com.example.two.SecondAutoConfiguration,com.example.four.FourthAutoConfiguration" );
120
128
testSeveralPropertyExclusionsAreApplied ();
121
129
}
122
130
123
131
@ Test
124
132
void severalPropertyExclusionsAreAppliedWithExtraSpaces () {
125
133
this .environment .setProperty ("spring.autoconfigure.exclude" ,
126
- FreeMarkerAutoConfiguration . class . getName () + " , " + MustacheAutoConfiguration . class . getName () + " " );
134
+ "com.example.two.SecondAutoConfiguration , com.example.four.FourthAutoConfiguration " );
127
135
testSeveralPropertyExclusionsAreApplied ();
128
136
}
129
137
130
138
@ Test
131
139
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" );
134
142
testSeveralPropertyExclusionsAreApplied ();
135
143
}
136
144
137
145
private void testSeveralPropertyExclusionsAreApplied () {
138
146
String [] imports = selectImports (BasicEnableAutoConfiguration .class );
139
147
assertThat (imports ).hasSize (getAutoConfigurationClassNames ().size () - 2 );
140
148
assertThat (this .importSelector .getLastEvent ().getExclusions ())
141
- .contains (FreeMarkerAutoConfiguration . class . getName (), MustacheAutoConfiguration . class . getName () );
149
+ .contains ("com.example.two.SecondAutoConfiguration" , "com.example.four.FourthAutoConfiguration" );
142
150
}
143
151
144
152
@ Test
145
153
void combinedExclusionsAreApplied () {
146
- this .environment .setProperty ("spring.autoconfigure.exclude" , ThymeleafAutoConfiguration . class . getName () );
154
+ this .environment .setProperty ("spring.autoconfigure.exclude" , "com.example.one.FirstAutoConfiguration" );
147
155
String [] imports = selectImports (EnableAutoConfigurationWithClassAndClassNameExclusions .class );
148
156
assertThat (imports ).hasSize (getAutoConfigurationClassNames ().size () - 3 );
149
157
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 ());
152
160
}
153
161
154
162
@ Test
@@ -213,7 +221,8 @@ private String[] selectImports(Class<?> source) {
213
221
}
214
222
215
223
private List <String > getAutoConfigurationClassNames () {
216
- return ImportCandidates .load (AutoConfiguration .class , getClass ().getClassLoader ()).getCandidates ();
224
+ return ImportCandidates .load (AutoConfiguration .class , Thread .currentThread ().getContextClassLoader ())
225
+ .getCandidates ();
217
226
}
218
227
219
228
private final class TestAutoConfigurationImportSelector extends AutoConfigurationImportSelector {
@@ -278,23 +287,23 @@ private final class BasicEnableAutoConfiguration {
278
287
279
288
}
280
289
281
- @ EnableAutoConfiguration (exclude = FreeMarkerAutoConfiguration .class )
290
+ @ EnableAutoConfiguration (exclude = SeventhAutoConfiguration .class )
282
291
private final class EnableAutoConfigurationWithClassExclusions {
283
292
284
293
}
285
294
286
- @ SpringBootApplication (exclude = FreeMarkerAutoConfiguration .class )
295
+ @ SpringBootApplication (exclude = SeventhAutoConfiguration .class )
287
296
private final class SpringBootApplicationWithClassExclusions {
288
297
289
298
}
290
299
291
- @ EnableAutoConfiguration (excludeName = "org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration " )
300
+ @ EnableAutoConfiguration (excludeName = "com.example.one.FirstAutoConfiguration " )
292
301
private final class EnableAutoConfigurationWithClassNameExclusions {
293
302
294
303
}
295
304
296
- @ EnableAutoConfiguration (exclude = MustacheAutoConfiguration .class ,
297
- excludeName = "org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration " )
305
+ @ EnableAutoConfiguration (exclude = SeventhAutoConfiguration .class ,
306
+ excludeName = "com.example.five.FifthAutoConfiguration " )
298
307
private final class EnableAutoConfigurationWithClassAndClassNameExclusions {
299
308
300
309
}
@@ -315,9 +324,14 @@ private final class EnableAutoConfigurationWithAbsentClassNameExclude {
315
324
316
325
}
317
326
318
- @ SpringBootApplication (excludeName = "org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration " )
327
+ @ SpringBootApplication (excludeName = "com.example.three.ThirdAutoConfiguration " )
319
328
private final class SpringBootApplicationWithClassNameExclusions {
320
329
321
330
}
322
331
332
+ @ AutoConfiguration
333
+ static class SeventhAutoConfiguration {
334
+
335
+ }
336
+
323
337
}
0 commit comments