Skip to content

Commit a90c718

Browse files
committed
Polish "Add auto-configuration for DiskSpaceMetrics"
See gh-26001
1 parent e3f03dd commit a90c718

File tree

2 files changed

+17
-25
lines changed

2 files changed

+17
-25
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/JvmMetricsAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 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.

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/JvmMetricsAutoConfigurationTests.java

Lines changed: 16 additions & 24 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.
@@ -27,7 +27,9 @@
2727

2828
import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun;
2929
import org.springframework.boot.autoconfigure.AutoConfigurations;
30+
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
3031
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
32+
import org.springframework.boot.test.context.runner.ContextConsumer;
3133
import org.springframework.context.annotation.Bean;
3234
import org.springframework.context.annotation.Configuration;
3335

@@ -47,53 +49,43 @@ class JvmMetricsAutoConfigurationTests {
4749

4850
@Test
4951
void autoConfiguresJvmMetrics() {
50-
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class)
51-
.hasSingleBean(JvmMemoryMetrics.class).hasSingleBean(JvmThreadMetrics.class)
52-
.hasSingleBean(ClassLoaderMetrics.class).hasSingleBean(DiskSpaceMetrics.class));
52+
this.contextRunner.run(assertMetricsBeans());
5353
}
5454

5555
@Test
5656
void allowsCustomJvmGcMetricsToBeUsed() {
5757
this.contextRunner.withUserConfiguration(CustomJvmGcMetricsConfiguration.class)
58-
.run((context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class).hasBean("customJvmGcMetrics")
59-
.hasSingleBean(JvmMemoryMetrics.class).hasSingleBean(JvmThreadMetrics.class)
60-
.hasSingleBean(ClassLoaderMetrics.class).hasSingleBean(DiskSpaceMetrics.class));
58+
.run(assertMetricsBeans().andThen((context) -> assertThat(context).hasBean("customJvmGcMetrics")));
6159
}
6260

6361
@Test
6462
void allowsCustomJvmMemoryMetricsToBeUsed() {
6563
this.contextRunner.withUserConfiguration(CustomJvmMemoryMetricsConfiguration.class)
66-
.run((context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class)
67-
.hasSingleBean(JvmMemoryMetrics.class).hasBean("customJvmMemoryMetrics")
68-
.hasSingleBean(JvmThreadMetrics.class).hasSingleBean(ClassLoaderMetrics.class)
69-
.hasSingleBean(DiskSpaceMetrics.class));
64+
.run(assertMetricsBeans().andThen((context) -> assertThat(context).hasBean("customJvmMemoryMetrics")));
7065
}
7166

7267
@Test
7368
void allowsCustomJvmThreadMetricsToBeUsed() {
7469
this.contextRunner.withUserConfiguration(CustomJvmThreadMetricsConfiguration.class)
75-
.run((context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class)
76-
.hasSingleBean(JvmMemoryMetrics.class).hasSingleBean(JvmThreadMetrics.class)
77-
.hasBean("customJvmThreadMetrics").hasSingleBean(ClassLoaderMetrics.class)
78-
.hasSingleBean(DiskSpaceMetrics.class));
70+
.run(assertMetricsBeans().andThen((context) -> assertThat(context).hasBean("customJvmThreadMetrics")));
7971
}
8072

8173
@Test
8274
void allowsCustomClassLoaderMetricsToBeUsed() {
83-
this.contextRunner.withUserConfiguration(CustomClassLoaderMetricsConfiguration.class)
84-
.run((context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class)
85-
.hasSingleBean(JvmMemoryMetrics.class).hasSingleBean(JvmThreadMetrics.class)
86-
.hasSingleBean(ClassLoaderMetrics.class).hasBean("customClassLoaderMetrics")
87-
.hasSingleBean(DiskSpaceMetrics.class));
75+
this.contextRunner.withUserConfiguration(CustomClassLoaderMetricsConfiguration.class).run(
76+
assertMetricsBeans().andThen((context) -> assertThat(context).hasBean("customClassLoaderMetrics")));
8877
}
8978

9079
@Test
9180
void allowsCustomDiskSpaceMetricsToBeUsed() {
9281
this.contextRunner.withUserConfiguration(CustomDiskSpaceMetricsConfiguration.class)
93-
.run((context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class)
94-
.hasSingleBean(JvmMemoryMetrics.class).hasSingleBean(JvmThreadMetrics.class)
95-
.hasSingleBean(ClassLoaderMetrics.class).hasSingleBean(DiskSpaceMetrics.class)
96-
.hasBean("customDiskSpaceMetrics"));
82+
.run(assertMetricsBeans().andThen((context) -> assertThat(context).hasBean("customDiskSpaceMetrics")));
83+
}
84+
85+
private ContextConsumer<AssertableApplicationContext> assertMetricsBeans() {
86+
return (context) -> assertThat(context).hasSingleBean(JvmGcMetrics.class).hasSingleBean(JvmMemoryMetrics.class)
87+
.hasSingleBean(JvmThreadMetrics.class).hasSingleBean(ClassLoaderMetrics.class)
88+
.hasSingleBean(DiskSpaceMetrics.class);
9789
}
9890

9991
@Configuration(proxyBeanMethods = false)

0 commit comments

Comments
 (0)