Skip to content

Commit 5d17257

Browse files
committed
Polish "Add Java InfoContributor"
See gh-28136
1 parent fc87da7 commit 5d17257

File tree

11 files changed

+247
-286
lines changed

11 files changed

+247
-286
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/info/InfoContributorAutoConfiguration.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,7 +20,7 @@
2020
import org.springframework.boot.actuate.info.EnvironmentInfoContributor;
2121
import org.springframework.boot.actuate.info.GitInfoContributor;
2222
import org.springframework.boot.actuate.info.InfoContributor;
23-
import org.springframework.boot.actuate.info.java.JavaInfoContributor;
23+
import org.springframework.boot.actuate.info.JavaInfoContributor;
2424
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2525
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2626
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -82,7 +82,7 @@ public InfoContributor buildInfoContributor(BuildProperties buildProperties) {
8282
@Bean
8383
@ConditionalOnEnabledInfoContributor("java")
8484
@Order(DEFAULT_ORDER)
85-
public InfoContributor javaInfoContributor() {
85+
public JavaInfoContributor javaInfoContributor() {
8686
return new JavaInfoContributor();
8787
}
8888

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/info/InfoContributorAutoConfigurationTests.java

Lines changed: 66 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,20 +19,19 @@
1919
import java.util.Map;
2020
import java.util.Properties;
2121

22-
import org.junit.jupiter.api.AfterEach;
2322
import org.junit.jupiter.api.Test;
2423

2524
import org.springframework.boot.actuate.info.BuildInfoContributor;
2625
import org.springframework.boot.actuate.info.EnvironmentInfoContributor;
2726
import org.springframework.boot.actuate.info.GitInfoContributor;
2827
import org.springframework.boot.actuate.info.Info;
2928
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;
3231
import org.springframework.boot.info.BuildProperties;
3332
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;
3635
import org.springframework.context.annotation.Bean;
3736
import org.springframework.context.annotation.Configuration;
3837

@@ -46,121 +45,113 @@
4645
*/
4746
class InfoContributorAutoConfigurationTests {
4847

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));
5750

5851
@Test
5952
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));
6455
}
6556

6657
@Test
6758
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));
7261
}
7362

7463
@Test
7564
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+
});
8270
}
8371

8472
@Test
8573
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));
8976
}
9077

9178
@Test
9279
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+
});
9786
}
9887

9988
@SuppressWarnings("unchecked")
10089
@Test
10190
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+
});
11199
}
112100

113101
@SuppressWarnings("unchecked")
114102
@Test
115103
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+
});
124114
}
125115

126116
@Test
127117
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+
});
131122
}
132123

133124
@SuppressWarnings("unchecked")
134125
@Test
135126
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+
});
146136
}
147137

148138
@Test
149139
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+
});
153145
}
154146

155147
@Test
156148
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+
});
164155
}
165156

166157
private Map<String, Object> invokeContributor(InfoContributor contributor) {
@@ -169,21 +160,6 @@ private Map<String, Object> invokeContributor(InfoContributor contributor) {
169160
return builder.build().getDetails();
170161
}
171162

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-
187163
@Configuration(proxyBeanMethods = false)
188164
static class GitPropertiesConfiguration {
189165

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021-2021 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,10 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.boot.actuate.info.java;
17+
package org.springframework.boot.actuate.info;
1818

1919
import org.springframework.boot.actuate.info.Info.Builder;
20-
import org.springframework.boot.actuate.info.InfoContributor;
20+
import org.springframework.boot.info.JavaInfo;
2121

2222
/**
2323
* An {@link InfoContributor} that exposes {@link JavaInfo}.

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/java/JavaInfo.java

Lines changed: 0 additions & 59 deletions
This file was deleted.

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/info/java/JreInfo.java

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)