1
1
/*
2
- * Copyright 2012-2019 the original author or authors.
2
+ * Copyright 2012-2021 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.
19
19
import java .util .Map ;
20
20
import java .util .Properties ;
21
21
22
- import org .junit .jupiter .api .AfterEach ;
23
22
import org .junit .jupiter .api .Test ;
24
23
25
24
import org .springframework .boot .actuate .info .BuildInfoContributor ;
26
25
import org .springframework .boot .actuate .info .EnvironmentInfoContributor ;
27
26
import org .springframework .boot .actuate .info .GitInfoContributor ;
28
27
import org .springframework .boot .actuate .info .Info ;
29
28
import org .springframework .boot .actuate .info .InfoContributor ;
30
- import org .springframework .boot .actuate .info .java . JavaInfo ;
31
- import org .springframework .boot .actuate . info . java . JavaInfoContributor ;
29
+ import org .springframework .boot .actuate .info .JavaInfoContributor ;
30
+ import org .springframework .boot .autoconfigure . AutoConfigurations ;
32
31
import org .springframework .boot .info .BuildProperties ;
33
32
import org .springframework .boot .info .GitProperties ;
34
- import org .springframework .boot .test . util . TestPropertyValues ;
35
- import org .springframework .context .annotation . AnnotationConfigApplicationContext ;
33
+ import org .springframework .boot .info . JavaInfo ;
34
+ import org .springframework .boot . test . context .runner . ApplicationContextRunner ;
36
35
import org .springframework .context .annotation .Bean ;
37
36
import org .springframework .context .annotation .Configuration ;
38
37
46
45
*/
47
46
class InfoContributorAutoConfigurationTests {
48
47
49
- private AnnotationConfigApplicationContext context ;
50
-
51
- @ AfterEach
52
- void close () {
53
- if (this .context != null ) {
54
- this .context .close ();
55
- }
56
- }
48
+ private final ApplicationContextRunner contextRunner = new ApplicationContextRunner ()
49
+ .withConfiguration (AutoConfigurations .of (InfoContributorAutoConfiguration .class ));
57
50
58
51
@ Test
59
52
void disableEnvContributor () {
60
- load ("management.info.env.enabled:false" );
61
- Map <String , InfoContributor > beans = this .context .getBeansOfType (InfoContributor .class );
62
- assertThat (beans ).extractingFromEntries (Map .Entry ::getValue ).allSatisfy (
63
- (infoContributor ) -> assertThat (infoContributor ).isNotInstanceOf (EnvironmentInfoContributor .class ));
53
+ this .contextRunner .withPropertyValues ("management.info.env.enabled=false" )
54
+ .run ((context ) -> assertThat (context ).doesNotHaveBean (EnvironmentInfoContributor .class ));
64
55
}
65
56
66
57
@ Test
67
58
void disableJavaContributor () {
68
- load ("management.info.java.enabled:false" );
69
- Map <String , InfoContributor > beans = this .context .getBeansOfType (InfoContributor .class );
70
- assertThat (beans ).extractingFromEntries (Map .Entry ::getValue ).allSatisfy (
71
- (infoContributor ) -> assertThat (infoContributor ).isNotInstanceOf (JavaInfoContributor .class ));
59
+ this .contextRunner .withPropertyValues ("management.info.java.enabled=false" )
60
+ .run ((context ) -> assertThat (context ).doesNotHaveBean (JavaInfoContributor .class ));
72
61
}
73
62
74
63
@ Test
75
64
void defaultInfoContributorsEnabled () {
76
- load ();
77
- Map <String , InfoContributor > beans = this .context .getBeansOfType (InfoContributor .class );
78
- assertThat (beans ).hasSize (2 ).extractingFromEntries (Map .Entry ::getValue )
79
- .anySatisfy (
80
- (infoContributor ) -> assertThat (infoContributor ).isInstanceOf (EnvironmentInfoContributor .class ))
81
- .anySatisfy ((infoContributor ) -> assertThat (infoContributor ).isInstanceOf (JavaInfoContributor .class ));
65
+ this .contextRunner .run ((context ) -> {
66
+ assertThat (context ).hasSingleBean (EnvironmentInfoContributor .class )
67
+ .hasSingleBean (JavaInfoContributor .class );
68
+ assertThat (context .getBeansOfType (InfoContributor .class )).hasSize (2 );
69
+ });
82
70
}
83
71
84
72
@ Test
85
73
void defaultInfoContributorsDisabled () {
86
- load ("management.info.defaults.enabled:false" );
87
- Map <String , InfoContributor > beans = this .context .getBeansOfType (InfoContributor .class );
88
- assertThat (beans ).hasSize (0 );
74
+ this .contextRunner .withPropertyValues ("management.info.defaults.enabled=false" )
75
+ .run ((context ) -> assertThat (context ).doesNotHaveBean (InfoContributor .class ));
89
76
}
90
77
91
78
@ Test
92
79
void defaultInfoContributorsDisabledWithCustomOne () {
93
- load (CustomInfoContributorConfiguration .class , "management.info.defaults.enabled:false" );
94
- Map <String , InfoContributor > beans = this .context .getBeansOfType (InfoContributor .class );
95
- assertThat (beans ).hasSize (1 );
96
- assertThat (this .context .getBean ("customInfoContributor" )).isSameAs (beans .values ().iterator ().next ());
80
+ this .contextRunner .withPropertyValues ("management.info.defaults.enabled=false" )
81
+ .withUserConfiguration (CustomInfoContributorConfiguration .class ).run ((context ) -> {
82
+ assertThat (context ).hasSingleBean (InfoContributor .class );
83
+ assertThat (context .getBean (InfoContributor .class ))
84
+ .isSameAs (context .getBean ("customInfoContributor" ));
85
+ });
97
86
}
98
87
99
88
@ SuppressWarnings ("unchecked" )
100
89
@ Test
101
90
void gitPropertiesDefaultMode () {
102
- load (GitPropertiesConfiguration .class );
103
- Map <String , InfoContributor > beans = this .context .getBeansOfType (InfoContributor .class );
104
- assertThat (beans ).containsKeys ("gitInfoContributor" );
105
- Map <String , Object > content = invokeContributor (
106
- this .context .getBean ("gitInfoContributor" , InfoContributor .class ));
107
- Object git = content .get ("git" );
108
- assertThat (git ).isInstanceOf (Map .class );
109
- Map <String , Object > gitInfo = (Map <String , Object >) git ;
110
- assertThat (gitInfo ).containsOnlyKeys ("branch" , "commit" );
91
+ this .contextRunner .withUserConfiguration (GitPropertiesConfiguration .class ).run ((context ) -> {
92
+ assertThat (context ).hasSingleBean (GitInfoContributor .class );
93
+ Map <String , Object > content = invokeContributor (context .getBean (GitInfoContributor .class ));
94
+ Object git = content .get ("git" );
95
+ assertThat (git ).isInstanceOf (Map .class );
96
+ Map <String , Object > gitInfo = (Map <String , Object >) git ;
97
+ assertThat (gitInfo ).containsOnlyKeys ("branch" , "commit" );
98
+ });
111
99
}
112
100
113
101
@ SuppressWarnings ("unchecked" )
114
102
@ Test
115
103
void gitPropertiesFullMode () {
116
- load (GitPropertiesConfiguration .class , "management.info.git.mode=full" );
117
- Map <String , Object > content = invokeContributor (
118
- this .context .getBean ("gitInfoContributor" , InfoContributor .class ));
119
- Object git = content .get ("git" );
120
- assertThat (git ).isInstanceOf (Map .class );
121
- Map <String , Object > gitInfo = (Map <String , Object >) git ;
122
- assertThat (gitInfo ).containsOnlyKeys ("branch" , "commit" , "foo" );
123
- assertThat (gitInfo .get ("foo" )).isEqualTo ("bar" );
104
+ this .contextRunner .withPropertyValues ("management.info.git.mode=full" )
105
+ .withUserConfiguration (GitPropertiesConfiguration .class ).run ((context ) -> {
106
+ assertThat (context ).hasSingleBean (GitInfoContributor .class );
107
+ Map <String , Object > content = invokeContributor (context .getBean (GitInfoContributor .class ));
108
+ Object git = content .get ("git" );
109
+ assertThat (git ).isInstanceOf (Map .class );
110
+ Map <String , Object > gitInfo = (Map <String , Object >) git ;
111
+ assertThat (gitInfo ).containsOnlyKeys ("branch" , "commit" , "foo" );
112
+ assertThat (gitInfo .get ("foo" )).isEqualTo ("bar" );
113
+ });
124
114
}
125
115
126
116
@ Test
127
117
void customGitInfoContributor () {
128
- load (CustomGitInfoContributorConfiguration .class );
129
- assertThat (this .context .getBean (GitInfoContributor .class ))
130
- .isSameAs (this .context .getBean ("customGitInfoContributor" ));
118
+ this .contextRunner .withUserConfiguration (CustomGitInfoContributorConfiguration .class ).run ((context ) -> {
119
+ assertThat (context ).hasSingleBean (GitInfoContributor .class );
120
+ assertThat (context .getBean (GitInfoContributor .class )).isSameAs (context .getBean ("customGitInfoContributor" ));
121
+ });
131
122
}
132
123
133
124
@ SuppressWarnings ("unchecked" )
134
125
@ Test
135
126
void buildProperties () {
136
- load (BuildPropertiesConfiguration .class );
137
- Map <String , InfoContributor > beans = this .context .getBeansOfType (InfoContributor .class );
138
- assertThat (beans ).containsKeys ("buildInfoContributor" );
139
- Map <String , Object > content = invokeContributor (
140
- this .context .getBean ("buildInfoContributor" , InfoContributor .class ));
141
- Object build = content .get ("build" );
142
- assertThat (build ).isInstanceOf (Map .class );
143
- Map <String , Object > buildInfo = (Map <String , Object >) build ;
144
- assertThat (buildInfo ).containsOnlyKeys ("group" , "artifact" , "foo" );
145
- assertThat (buildInfo .get ("foo" )).isEqualTo ("bar" );
127
+ this .contextRunner .withUserConfiguration (BuildPropertiesConfiguration .class ).run ((context ) -> {
128
+ assertThat (context ).hasSingleBean (BuildInfoContributor .class );
129
+ Map <String , Object > content = invokeContributor (context .getBean (BuildInfoContributor .class ));
130
+ Object build = content .get ("build" );
131
+ assertThat (build ).isInstanceOf (Map .class );
132
+ Map <String , Object > buildInfo = (Map <String , Object >) build ;
133
+ assertThat (buildInfo ).containsOnlyKeys ("group" , "artifact" , "foo" );
134
+ assertThat (buildInfo .get ("foo" )).isEqualTo ("bar" );
135
+ });
146
136
}
147
137
148
138
@ Test
149
139
void customBuildInfoContributor () {
150
- load (CustomBuildInfoContributorConfiguration .class );
151
- assertThat (this .context .getBean (BuildInfoContributor .class ))
152
- .isSameAs (this .context .getBean ("customBuildInfoContributor" ));
140
+ this .contextRunner .withUserConfiguration (CustomBuildInfoContributorConfiguration .class ).run ((context ) -> {
141
+ assertThat (context ).hasSingleBean (BuildInfoContributor .class );
142
+ assertThat (context .getBean (BuildInfoContributor .class ))
143
+ .isSameAs (context .getBean ("customBuildInfoContributor" ));
144
+ });
153
145
}
154
146
155
147
@ Test
156
148
void javaInfoContributor () {
157
- load ();
158
- Map <String , InfoContributor > beans = this .context .getBeansOfType (InfoContributor .class );
159
- assertThat (beans ).containsKeys ("javaInfoContributor" );
160
- Map <String , Object > content = invokeContributor (
161
- this .context .getBean ("javaInfoContributor" , InfoContributor .class ));
162
- Object javaInfo = content .get ("java" );
163
- assertThat (javaInfo ).isInstanceOf (JavaInfo .class );
149
+ this .contextRunner .run ((context ) -> {
150
+ assertThat (context ).hasSingleBean (JavaInfoContributor .class );
151
+ Map <String , Object > content = invokeContributor (context .getBean (JavaInfoContributor .class ));
152
+ assertThat (content ).containsKey ("java" );
153
+ assertThat (content .get ("java" )).isInstanceOf (JavaInfo .class );
154
+ });
164
155
}
165
156
166
157
private Map <String , Object > invokeContributor (InfoContributor contributor ) {
@@ -169,21 +160,6 @@ private Map<String, Object> invokeContributor(InfoContributor contributor) {
169
160
return builder .build ().getDetails ();
170
161
}
171
162
172
- private void load (String ... environment ) {
173
- load (null , environment );
174
- }
175
-
176
- private void load (Class <?> config , String ... environment ) {
177
- AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ();
178
- if (config != null ) {
179
- context .register (config );
180
- }
181
- context .register (InfoContributorAutoConfiguration .class );
182
- TestPropertyValues .of (environment ).applyTo (context );
183
- context .refresh ();
184
- this .context = context ;
185
- }
186
-
187
163
@ Configuration (proxyBeanMethods = false )
188
164
static class GitPropertiesConfiguration {
189
165
0 commit comments