1
1
/*
2
- * Copyright 2012-2019 the original author or authors.
2
+ * Copyright 2012-2020 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .boot .autoconfigure .condition ;
18
18
19
- import org .junit .jupiter .api .AfterEach ;
20
19
import org .junit .jupiter .api .Test ;
21
20
22
- import org .springframework .context .annotation . AnnotationConfigApplicationContext ;
21
+ import org .springframework .boot . test . context .runner . ApplicationContextRunner ;
23
22
import org .springframework .context .annotation .Bean ;
24
23
import org .springframework .context .annotation .Configuration ;
25
24
import org .springframework .context .annotation .Primary ;
26
25
27
26
import static org .assertj .core .api .Assertions .assertThat ;
28
- import static org .assertj .core .api .Assertions .assertThatIllegalStateException ;
29
27
30
28
/**
31
29
* Tests for {@link ConditionalOnSingleCandidate @ConditionalOnSingleCandidate}.
35
33
*/
36
34
class ConditionalOnSingleCandidateTests {
37
35
38
- private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ();
39
-
40
- @ AfterEach
41
- void close () {
42
- if (this .context != null ) {
43
- this .context .close ();
44
- }
45
- }
36
+ private final ApplicationContextRunner contextRunner = new ApplicationContextRunner ();
46
37
47
38
@ Test
48
39
void singleCandidateNoCandidate () {
49
- load (OnBeanSingleCandidateConfiguration .class );
50
- assertThat (this . context . containsBean ( "baz" )). isFalse ( );
40
+ this . contextRunner . withUserConfiguration (OnBeanSingleCandidateConfiguration .class )
41
+ . run (( context ) -> assertThat (context ). doesNotHaveBean ( "consumer" ) );
51
42
}
52
43
53
44
@ Test
54
45
void singleCandidateOneCandidate () {
55
- load (FooConfiguration .class , OnBeanSingleCandidateConfiguration .class );
56
- assertThat (this .context .containsBean ("baz" )).isTrue ();
57
- assertThat (this .context .getBean ("baz" )).isEqualTo ("foo" );
46
+ this .contextRunner .withUserConfiguration (AlphaConfiguration .class , OnBeanSingleCandidateConfiguration .class )
47
+ .run ((context ) -> {
48
+ assertThat (context ).hasBean ("consumer" );
49
+ assertThat (context .getBean ("consumer" )).isEqualTo ("alpha" );
50
+ });
58
51
}
59
52
60
53
@ Test
61
54
void singleCandidateInAncestorsOneCandidateInCurrent () {
62
- load ();
63
- AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext ();
64
- child .register (FooConfiguration .class , OnBeanSingleCandidateInAncestorsConfiguration .class );
65
- child .setParent (this .context );
66
- child .refresh ();
67
- assertThat (child .containsBean ("baz" )).isFalse ();
68
- child .close ();
55
+ this .contextRunner .run ((parent ) -> this .contextRunner
56
+ .withUserConfiguration (AlphaConfiguration .class , OnBeanSingleCandidateInAncestorsConfiguration .class )
57
+ .withParent (parent ).run ((child ) -> assertThat (child ).doesNotHaveBean ("consumer" )));
69
58
}
70
59
71
60
@ Test
72
61
void singleCandidateInAncestorsOneCandidateInParent () {
73
- load (FooConfiguration .class );
74
- AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext ();
75
- child .register (OnBeanSingleCandidateInAncestorsConfiguration .class );
76
- child .setParent (this .context );
77
- child .refresh ();
78
- assertThat (child .containsBean ("baz" )).isTrue ();
79
- assertThat (child .getBean ("baz" )).isEqualTo ("foo" );
80
- child .close ();
62
+ this .contextRunner .withUserConfiguration (AlphaConfiguration .class )
63
+ .run ((parent ) -> this .contextRunner
64
+ .withUserConfiguration (OnBeanSingleCandidateInAncestorsConfiguration .class ).withParent (parent )
65
+ .run ((child ) -> {
66
+ assertThat (child ).hasBean ("consumer" );
67
+ assertThat (child .getBean ("consumer" )).isEqualTo ("alpha" );
68
+ }));
81
69
}
82
70
83
71
@ Test
84
72
void singleCandidateInAncestorsOneCandidateInGrandparent () {
85
- load (FooConfiguration .class );
86
- AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext ();
87
- parent .setParent (this .context );
88
- parent .refresh ();
89
- AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext ();
90
- child .register (OnBeanSingleCandidateInAncestorsConfiguration .class );
91
- child .setParent (parent );
92
- child .refresh ();
93
- assertThat (child .containsBean ("baz" )).isTrue ();
94
- assertThat (child .getBean ("baz" )).isEqualTo ("foo" );
95
- child .close ();
96
- parent .close ();
73
+ this .contextRunner .withUserConfiguration (AlphaConfiguration .class )
74
+ .run ((grandparent ) -> this .contextRunner .withParent (grandparent )
75
+ .run ((parent ) -> this .contextRunner
76
+ .withUserConfiguration (OnBeanSingleCandidateInAncestorsConfiguration .class )
77
+ .withParent (parent ).run ((child ) -> {
78
+ assertThat (child ).hasBean ("consumer" );
79
+ assertThat (child .getBean ("consumer" )).isEqualTo ("alpha" );
80
+ })));
97
81
}
98
82
99
83
@ Test
100
84
void singleCandidateMultipleCandidates () {
101
- load (FooConfiguration .class , BarConfiguration .class , OnBeanSingleCandidateConfiguration .class );
102
- assertThat (this .context .containsBean ("baz" )).isFalse ();
85
+ this .contextRunner
86
+ .withUserConfiguration (AlphaConfiguration .class , BravoConfiguration .class ,
87
+ OnBeanSingleCandidateConfiguration .class )
88
+ .run ((context ) -> assertThat (context ).doesNotHaveBean ("consumer" ));
103
89
}
104
90
105
91
@ Test
106
92
void singleCandidateMultipleCandidatesOnePrimary () {
107
- load (FooPrimaryConfiguration .class , BarConfiguration .class , OnBeanSingleCandidateConfiguration .class );
108
- assertThat (this .context .containsBean ("baz" )).isTrue ();
109
- assertThat (this .context .getBean ("baz" )).isEqualTo ("foo" );
93
+ this .contextRunner .withUserConfiguration (AlphaPrimaryConfiguration .class , BravoConfiguration .class ,
94
+ OnBeanSingleCandidateConfiguration .class ).run ((context ) -> {
95
+ assertThat (context ).hasBean ("consumer" );
96
+ assertThat (context .getBean ("consumer" )).isEqualTo ("alpha" );
97
+ });
110
98
}
111
99
112
100
@ Test
113
101
void singleCandidateMultipleCandidatesMultiplePrimary () {
114
- load (FooPrimaryConfiguration .class , BarPrimaryConfiguration .class , OnBeanSingleCandidateConfiguration .class );
115
- assertThat (this .context .containsBean ("baz" )).isFalse ();
102
+ this .contextRunner
103
+ .withUserConfiguration (AlphaPrimaryConfiguration .class , BravoPrimaryConfiguration .class ,
104
+ OnBeanSingleCandidateConfiguration .class )
105
+ .run ((context ) -> assertThat (context ).doesNotHaveBean ("consumer" ));
116
106
}
117
107
118
108
@ Test
119
109
void invalidAnnotationTwoTypes () {
120
- assertThatIllegalStateException ().isThrownBy (() -> load (OnBeanSingleCandidateTwoTypesConfiguration .class ))
121
- .withCauseInstanceOf (IllegalArgumentException .class )
122
- .withMessageContaining (OnBeanSingleCandidateTwoTypesConfiguration .class .getName ());
110
+ this .contextRunner .withUserConfiguration (OnBeanSingleCandidateTwoTypesConfiguration .class ).run ((context ) -> {
111
+ assertThat (context ).hasFailed ();
112
+ assertThat (context ).getFailure ().hasCauseInstanceOf (IllegalArgumentException .class )
113
+ .hasMessageContaining (OnBeanSingleCandidateTwoTypesConfiguration .class .getName ());
114
+ });
123
115
}
124
116
125
117
@ Test
126
118
void invalidAnnotationNoType () {
127
- assertThatIllegalStateException ().isThrownBy (() -> load (OnBeanSingleCandidateNoTypeConfiguration .class ))
128
- .withCauseInstanceOf (IllegalArgumentException .class )
129
- .withMessageContaining (OnBeanSingleCandidateNoTypeConfiguration .class .getName ());
119
+ this .contextRunner .withUserConfiguration (OnBeanSingleCandidateNoTypeConfiguration .class ).run ((context ) -> {
120
+ assertThat (context ).hasFailed ();
121
+ assertThat (context ).getFailure ().hasCauseInstanceOf (IllegalArgumentException .class )
122
+ .hasMessageContaining (OnBeanSingleCandidateNoTypeConfiguration .class .getName ());
123
+ });
130
124
}
131
125
132
126
@ Test
133
127
void singleCandidateMultipleCandidatesInContextHierarchy () {
134
- load (FooPrimaryConfiguration .class , BarConfiguration .class );
135
- try (AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext ()) {
136
- child .setParent (this .context );
137
- child .register (OnBeanSingleCandidateConfiguration .class );
138
- child .refresh ();
139
- assertThat (child .containsBean ("baz" )).isTrue ();
140
- assertThat (child .getBean ("baz" )).isEqualTo ("foo" );
141
- }
142
- }
143
-
144
- private void load (Class <?>... classes ) {
145
- if (classes .length > 0 ) {
146
- this .context .register (classes );
147
- }
148
- this .context .refresh ();
128
+ this .contextRunner .withUserConfiguration (AlphaPrimaryConfiguration .class , BravoConfiguration .class )
129
+ .run ((parent ) -> this .contextRunner .withUserConfiguration (OnBeanSingleCandidateConfiguration .class )
130
+ .withParent (parent ).run ((child ) -> {
131
+ assertThat (child ).hasBean ("consumer" );
132
+ assertThat (child .getBean ("consumer" )).isEqualTo ("alpha" );
133
+ }));
149
134
}
150
135
151
136
@ Configuration (proxyBeanMethods = false )
152
137
@ ConditionalOnSingleCandidate (String .class )
153
138
static class OnBeanSingleCandidateConfiguration {
154
139
155
140
@ Bean
156
- String baz (String s ) {
141
+ String consumer (String s ) {
157
142
return s ;
158
143
}
159
144
@@ -164,7 +149,7 @@ String baz(String s) {
164
149
static class OnBeanSingleCandidateInAncestorsConfiguration {
165
150
166
151
@ Bean
167
- String baz (String s ) {
152
+ String consumer (String s ) {
168
153
return s ;
169
154
}
170
155
@@ -183,43 +168,43 @@ static class OnBeanSingleCandidateNoTypeConfiguration {
183
168
}
184
169
185
170
@ Configuration (proxyBeanMethods = false )
186
- static class FooConfiguration {
171
+ static class AlphaConfiguration {
187
172
188
173
@ Bean
189
- String foo () {
190
- return "foo " ;
174
+ String alpha () {
175
+ return "alpha " ;
191
176
}
192
177
193
178
}
194
179
195
180
@ Configuration (proxyBeanMethods = false )
196
- static class FooPrimaryConfiguration {
181
+ static class AlphaPrimaryConfiguration {
197
182
198
183
@ Bean
199
184
@ Primary
200
- String foo () {
201
- return "foo " ;
185
+ String alpha () {
186
+ return "alpha " ;
202
187
}
203
188
204
189
}
205
190
206
191
@ Configuration (proxyBeanMethods = false )
207
- static class BarConfiguration {
192
+ static class BravoConfiguration {
208
193
209
194
@ Bean
210
- String bar () {
211
- return "bar " ;
195
+ String bravo () {
196
+ return "bravo " ;
212
197
}
213
198
214
199
}
215
200
216
201
@ Configuration (proxyBeanMethods = false )
217
- static class BarPrimaryConfiguration {
202
+ static class BravoPrimaryConfiguration {
218
203
219
204
@ Bean
220
205
@ Primary
221
- String bar () {
222
- return "bar " ;
206
+ String bravo () {
207
+ return "bravo " ;
223
208
}
224
209
225
210
}
0 commit comments