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 ;
25
+ import org .springframework .boot .actuate .info .EnvironmentInfoContributor ;
26
26
import org .springframework .boot .actuate .info .GitInfoContributor ;
27
27
import org .springframework .boot .actuate .info .Info ;
28
28
import org .springframework .boot .actuate .info .InfoContributor ;
29
+ import org .springframework .boot .actuate .info .JavaInfoContributor ;
30
+ import org .springframework .boot .autoconfigure .AutoConfigurations ;
29
31
import org .springframework .boot .info .BuildProperties ;
30
32
import org .springframework .boot .info .GitProperties ;
31
- import org .springframework .boot .test . util . TestPropertyValues ;
32
- import org .springframework .context .annotation . AnnotationConfigApplicationContext ;
33
+ import org .springframework .boot .info . JavaInfo ;
34
+ import org .springframework .boot . test . context .runner . ApplicationContextRunner ;
33
35
import org .springframework .context .annotation .Bean ;
34
36
import org .springframework .context .annotation .Configuration ;
35
37
39
41
* Tests for {@link InfoContributorAutoConfiguration}.
40
42
*
41
43
* @author Stephane Nicoll
44
+ * @author Jonatan Ivanov
42
45
*/
43
46
class InfoContributorAutoConfigurationTests {
44
47
45
- private AnnotationConfigApplicationContext context ;
48
+ private final ApplicationContextRunner contextRunner = new ApplicationContextRunner ()
49
+ .withConfiguration (AutoConfigurations .of (InfoContributorAutoConfiguration .class ));
46
50
47
- @ AfterEach
48
- void close () {
49
- if (this .context != null ) {
50
- this .context .close ();
51
- }
51
+ @ Test
52
+ void disableEnvContributor () {
53
+ this .contextRunner .withPropertyValues ("management.info.env.enabled=false" )
54
+ .run ((context ) -> assertThat (context ).doesNotHaveBean (EnvironmentInfoContributor .class ));
52
55
}
53
56
54
57
@ Test
55
- void disableEnvContributor () {
56
- load ("management.info.env.enabled:false" );
57
- Map <String , InfoContributor > beans = this .context .getBeansOfType (InfoContributor .class );
58
- assertThat (beans ).hasSize (0 );
58
+ void disableJavaContributor () {
59
+ this .contextRunner .withPropertyValues ("management.info.java.enabled=false" )
60
+ .run ((context ) -> assertThat (context ).doesNotHaveBean (JavaInfoContributor .class ));
61
+ }
62
+
63
+ @ Test
64
+ void defaultInfoContributorsEnabled () {
65
+ this .contextRunner .run ((context ) -> {
66
+ assertThat (context ).hasSingleBean (EnvironmentInfoContributor .class )
67
+ .hasSingleBean (JavaInfoContributor .class );
68
+ assertThat (context .getBeansOfType (InfoContributor .class )).hasSize (2 );
69
+ });
59
70
}
60
71
61
72
@ Test
62
73
void defaultInfoContributorsDisabled () {
63
- load ("management.info.defaults.enabled:false" );
64
- Map <String , InfoContributor > beans = this .context .getBeansOfType (InfoContributor .class );
65
- assertThat (beans ).hasSize (0 );
74
+ this .contextRunner .withPropertyValues ("management.info.defaults.enabled=false" )
75
+ .run ((context ) -> assertThat (context ).doesNotHaveBean (InfoContributor .class ));
66
76
}
67
77
68
78
@ Test
69
79
void defaultInfoContributorsDisabledWithCustomOne () {
70
- load (CustomInfoContributorConfiguration .class , "management.info.defaults.enabled:false" );
71
- Map <String , InfoContributor > beans = this .context .getBeansOfType (InfoContributor .class );
72
- assertThat (beans ).hasSize (1 );
73
- 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
+ });
74
86
}
75
87
76
88
@ SuppressWarnings ("unchecked" )
77
89
@ Test
78
90
void gitPropertiesDefaultMode () {
79
- load (GitPropertiesConfiguration .class );
80
- Map <String , InfoContributor > beans = this .context .getBeansOfType (InfoContributor .class );
81
- assertThat (beans ).containsKeys ("gitInfoContributor" );
82
- Map <String , Object > content = invokeContributor (
83
- this .context .getBean ("gitInfoContributor" , InfoContributor .class ));
84
- Object git = content .get ("git" );
85
- assertThat (git ).isInstanceOf (Map .class );
86
- Map <String , Object > gitInfo = (Map <String , Object >) git ;
87
- 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
+ });
88
99
}
89
100
90
101
@ SuppressWarnings ("unchecked" )
91
102
@ Test
92
103
void gitPropertiesFullMode () {
93
- load (GitPropertiesConfiguration .class , "management.info.git.mode=full" );
94
- Map <String , Object > content = invokeContributor (
95
- this .context .getBean ("gitInfoContributor" , InfoContributor .class ));
96
- Object git = content .get ("git" );
97
- assertThat (git ).isInstanceOf (Map .class );
98
- Map <String , Object > gitInfo = (Map <String , Object >) git ;
99
- assertThat (gitInfo ).containsOnlyKeys ("branch" , "commit" , "foo" );
100
- 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
+ });
101
114
}
102
115
103
116
@ Test
104
117
void customGitInfoContributor () {
105
- load (CustomGitInfoContributorConfiguration .class );
106
- assertThat (this .context .getBean (GitInfoContributor .class ))
107
- .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
+ });
108
122
}
109
123
110
124
@ SuppressWarnings ("unchecked" )
111
125
@ Test
112
126
void buildProperties () {
113
- load (BuildPropertiesConfiguration .class );
114
- Map <String , InfoContributor > beans = this .context .getBeansOfType (InfoContributor .class );
115
- assertThat (beans ).containsKeys ("buildInfoContributor" );
116
- Map <String , Object > content = invokeContributor (
117
- this .context .getBean ("buildInfoContributor" , InfoContributor .class ));
118
- Object build = content .get ("build" );
119
- assertThat (build ).isInstanceOf (Map .class );
120
- Map <String , Object > buildInfo = (Map <String , Object >) build ;
121
- assertThat (buildInfo ).containsOnlyKeys ("group" , "artifact" , "foo" );
122
- 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
+ });
123
136
}
124
137
125
138
@ Test
126
139
void customBuildInfoContributor () {
127
- load (CustomBuildInfoContributorConfiguration .class );
128
- assertThat (this .context .getBean (BuildInfoContributor .class ))
129
- .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
+ });
145
+ }
146
+
147
+ @ Test
148
+ void javaInfoContributor () {
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
+ });
130
155
}
131
156
132
157
private Map <String , Object > invokeContributor (InfoContributor contributor ) {
@@ -135,21 +160,6 @@ private Map<String, Object> invokeContributor(InfoContributor contributor) {
135
160
return builder .build ().getDetails ();
136
161
}
137
162
138
- private void load (String ... environment ) {
139
- load (null , environment );
140
- }
141
-
142
- private void load (Class <?> config , String ... environment ) {
143
- AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ();
144
- if (config != null ) {
145
- context .register (config );
146
- }
147
- context .register (InfoContributorAutoConfiguration .class );
148
- TestPropertyValues .of (environment ).applyTo (context );
149
- context .refresh ();
150
- this .context = context ;
151
- }
152
-
153
163
@ Configuration (proxyBeanMethods = false )
154
164
static class GitPropertiesConfiguration {
155
165
0 commit comments