Skip to content

Commit 5fa71a1

Browse files
committed
Switch test to ApplicationContextRunner
1 parent 299eca0 commit 5fa71a1

File tree

1 file changed

+54
-68
lines changed

1 file changed

+54
-68
lines changed

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceJmxConfigurationTests.java

Lines changed: 54 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.boot.autoconfigure.jdbc;
1818

1919
import java.lang.management.ManagementFactory;
20-
import java.sql.SQLException;
2120
import java.util.Set;
2221
import java.util.UUID;
2322

@@ -30,13 +29,11 @@
3029
import org.apache.tomcat.jdbc.pool.DataSource;
3130
import org.apache.tomcat.jdbc.pool.DataSourceProxy;
3231
import org.apache.tomcat.jdbc.pool.jmx.ConnectionPool;
33-
import org.junit.After;
3432
import org.junit.Test;
3533

34+
import org.springframework.boot.autoconfigure.AutoConfigurations;
3635
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
37-
import org.springframework.boot.test.util.TestPropertyValues;
38-
import org.springframework.context.ConfigurableApplicationContext;
39-
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
36+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
4037

4138
import static org.assertj.core.api.Assertions.assertThat;
4239

@@ -47,27 +44,25 @@
4744
*/
4845
public class DataSourceJmxConfigurationTests {
4946

50-
private ConfigurableApplicationContext context;
51-
52-
@After
53-
public void close() {
54-
if (this.context != null) {
55-
this.context.close();
56-
}
57-
}
47+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
48+
.withPropertyValues("spring.datasource.url="
49+
+ "jdbc:hsqldb:mem:test-" + UUID.randomUUID())
50+
.withConfiguration(AutoConfigurations.of(JmxAutoConfiguration.class,
51+
DataSourceAutoConfiguration.class));
5852

5953
@Test
60-
public void hikariAutoConfiguredCanUseRegisterMBeans()
61-
throws MalformedObjectNameException {
54+
public void hikariAutoConfiguredCanUseRegisterMBeans() {
6255
String poolName = UUID.randomUUID().toString();
63-
load("spring.datasource.type=" + HikariDataSource.class.getName(),
56+
this.contextRunner.withPropertyValues(
57+
"spring.datasource.type=" + HikariDataSource.class.getName(),
6458
"spring.datasource.name=" + poolName,
65-
"spring.datasource.hikari.register-mbeans=true");
66-
assertThat(this.context.getBeansOfType(HikariDataSource.class)).hasSize(1);
67-
assertThat(this.context.getBean(HikariDataSource.class).isRegisterMbeans())
68-
.isTrue();
69-
MBeanServer mBeanServer = this.context.getBean(MBeanServer.class);
70-
validateHikariMBeansRegistration(mBeanServer, poolName, true);
59+
"spring.datasource.hikari.register-mbeans=true").run((context) -> {
60+
assertThat(context).hasSingleBean(HikariDataSource.class);
61+
assertThat(context.getBean(HikariDataSource.class).isRegisterMbeans())
62+
.isTrue();
63+
MBeanServer mBeanServer = context.getBean(MBeanServer.class);
64+
validateHikariMBeansRegistration(mBeanServer, poolName, true);
65+
});
7166
}
7267

7368
@Test
@@ -76,72 +71,63 @@ public void hikariAutoConfiguredWithoutDataSourceName()
7671
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
7772
Set<ObjectInstance> existingInstances = mBeanServer
7873
.queryMBeans(new ObjectName("com.zaxxer.hikari:type=*"), null);
79-
load("spring.datasource.type=" + HikariDataSource.class.getName(),
80-
"spring.datasource.hikari.register-mbeans=true");
81-
assertThat(this.context.getBeansOfType(HikariDataSource.class)).hasSize(1);
82-
assertThat(this.context.getBean(HikariDataSource.class).isRegisterMbeans())
83-
.isTrue();
84-
// We can't rely on the number of MBeans so we're checking that the pool and pool
85-
// config MBeans were registered
86-
assertThat(mBeanServer
87-
.queryMBeans(new ObjectName("com.zaxxer.hikari:type=*"), null).size())
88-
.isEqualTo(existingInstances.size() + 2);
74+
this.contextRunner.withPropertyValues(
75+
"spring.datasource.type=" + HikariDataSource.class.getName(),
76+
"spring.datasource.hikari.register-mbeans=true").run((context) -> {
77+
assertThat(context).hasSingleBean(HikariDataSource.class);
78+
assertThat(context.getBean(HikariDataSource.class).isRegisterMbeans())
79+
.isTrue();
80+
// We can't rely on the number of MBeans so we're checking that the pool and pool
81+
// config MBeans were registered
82+
assertThat(mBeanServer
83+
.queryMBeans(new ObjectName("com.zaxxer.hikari:type=*"), null).size())
84+
.isEqualTo(existingInstances.size() + 2);
85+
});
8986
}
9087

9188
@Test
92-
public void hikariAutoConfiguredUsesJmsFlag() throws MalformedObjectNameException {
89+
public void hikariAutoConfiguredUsesJmsFlag() {
9390
String poolName = UUID.randomUUID().toString();
94-
load("spring.datasource.type=" + HikariDataSource.class.getName(),
91+
this.contextRunner.withPropertyValues(
92+
"spring.datasource.type=" + HikariDataSource.class.getName(),
9593
"spring.jmx.enabled=false", "spring.datasource.name=" + poolName,
96-
"spring.datasource.hikari.register-mbeans=true");
97-
assertThat(this.context.getBeansOfType(HikariDataSource.class)).hasSize(1);
98-
assertThat(this.context.getBean(HikariDataSource.class).isRegisterMbeans())
99-
.isTrue();
100-
// Hikari can still register mBeans
101-
validateHikariMBeansRegistration(ManagementFactory.getPlatformMBeanServer(),
102-
poolName, true);
94+
"spring.datasource.hikari.register-mbeans=true").run((context) -> {
95+
assertThat(context).hasSingleBean(HikariDataSource.class);
96+
assertThat(context.getBean(HikariDataSource.class).isRegisterMbeans())
97+
.isTrue();
98+
// Hikari can still register mBeans
99+
validateHikariMBeansRegistration(ManagementFactory.getPlatformMBeanServer(),
100+
poolName, true);
101+
});
103102
}
104103

105104
private void validateHikariMBeansRegistration(MBeanServer mBeanServer,
106105
String poolName, boolean expected) throws MalformedObjectNameException {
107106
assertThat(mBeanServer.isRegistered(
108107
new ObjectName("com.zaxxer.hikari:type=Pool (" + poolName + ")")))
109-
.isEqualTo(expected);
108+
.isEqualTo(expected);
110109
assertThat(mBeanServer.isRegistered(
111110
new ObjectName("com.zaxxer.hikari:type=PoolConfig (" + poolName + ")")))
112-
.isEqualTo(expected);
111+
.isEqualTo(expected);
113112
}
114113

115114
@Test
116115
public void tomcatDoesNotExposeMBeanPoolByDefault() {
117-
load("spring.datasource.type=" + DataSource.class.getName());
118-
assertThat(this.context.getBeansOfType(ConnectionPool.class)).isEmpty();
116+
this.contextRunner
117+
.withPropertyValues("spring.datasource.type=" + DataSource.class.getName())
118+
.run((context) ->
119+
assertThat(context).doesNotHaveBean(ConnectionPool.class));
119120
}
120121

121122
@Test
122-
public void tomcatAutoConfiguredCanExposeMBeanPool() throws SQLException {
123-
load("spring.datasource.type=" + DataSource.class.getName(),
124-
"spring.datasource.jmx-enabled=true");
125-
assertThat(this.context.getBeansOfType(ConnectionPool.class)).hasSize(1);
126-
assertThat(this.context.getBean(DataSourceProxy.class).createPool().getJmxPool())
127-
.isSameAs(this.context.getBean(ConnectionPool.class));
128-
}
129-
130-
private void load(String... environment) {
131-
load(null, environment);
132-
}
133-
134-
private void load(Class<?> config, String... environment) {
135-
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
136-
String jdbcUrl = "jdbc:hsqldb:mem:test-" + UUID.randomUUID();
137-
TestPropertyValues.of(environment).and("spring.datasource.url=" + jdbcUrl)
138-
.applyTo(context);
139-
if (config != null) {
140-
context.register(config);
141-
}
142-
context.register(JmxAutoConfiguration.class, DataSourceAutoConfiguration.class);
143-
context.refresh();
144-
this.context = context;
123+
public void tomcatAutoConfiguredCanExposeMBeanPool() {
124+
this.contextRunner.withPropertyValues(
125+
"spring.datasource.type=" + DataSource.class.getName(),
126+
"spring.datasource.jmx-enabled=true").run((context) -> {
127+
assertThat(context).hasSingleBean(ConnectionPool.class);
128+
assertThat(context.getBean(DataSourceProxy.class).createPool().getJmxPool())
129+
.isSameAs(context.getBean(ConnectionPool.class));
130+
});
145131
}
146132

147133
}

0 commit comments

Comments
 (0)