17
17
package org .springframework .boot .autoconfigure .jdbc ;
18
18
19
19
import java .lang .management .ManagementFactory ;
20
- import java .sql .SQLException ;
21
20
import java .util .Set ;
22
21
import java .util .UUID ;
23
22
30
29
import org .apache .tomcat .jdbc .pool .DataSource ;
31
30
import org .apache .tomcat .jdbc .pool .DataSourceProxy ;
32
31
import org .apache .tomcat .jdbc .pool .jmx .ConnectionPool ;
33
- import org .junit .After ;
34
32
import org .junit .Test ;
35
33
34
+ import org .springframework .boot .autoconfigure .AutoConfigurations ;
36
35
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 ;
40
37
41
38
import static org .assertj .core .api .Assertions .assertThat ;
42
39
47
44
*/
48
45
public class DataSourceJmxConfigurationTests {
49
46
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 ));
58
52
59
53
@ Test
60
- public void hikariAutoConfiguredCanUseRegisterMBeans ()
61
- throws MalformedObjectNameException {
54
+ public void hikariAutoConfiguredCanUseRegisterMBeans () {
62
55
String poolName = UUID .randomUUID ().toString ();
63
- load ("spring.datasource.type=" + HikariDataSource .class .getName (),
56
+ this .contextRunner .withPropertyValues (
57
+ "spring.datasource.type=" + HikariDataSource .class .getName (),
64
58
"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
+ });
71
66
}
72
67
73
68
@ Test
@@ -76,72 +71,63 @@ public void hikariAutoConfiguredWithoutDataSourceName()
76
71
MBeanServer mBeanServer = ManagementFactory .getPlatformMBeanServer ();
77
72
Set <ObjectInstance > existingInstances = mBeanServer
78
73
.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
+ });
89
86
}
90
87
91
88
@ Test
92
- public void hikariAutoConfiguredUsesJmsFlag () throws MalformedObjectNameException {
89
+ public void hikariAutoConfiguredUsesJmsFlag () {
93
90
String poolName = UUID .randomUUID ().toString ();
94
- load ("spring.datasource.type=" + HikariDataSource .class .getName (),
91
+ this .contextRunner .withPropertyValues (
92
+ "spring.datasource.type=" + HikariDataSource .class .getName (),
95
93
"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
+ });
103
102
}
104
103
105
104
private void validateHikariMBeansRegistration (MBeanServer mBeanServer ,
106
105
String poolName , boolean expected ) throws MalformedObjectNameException {
107
106
assertThat (mBeanServer .isRegistered (
108
107
new ObjectName ("com.zaxxer.hikari:type=Pool (" + poolName + ")" )))
109
- .isEqualTo (expected );
108
+ .isEqualTo (expected );
110
109
assertThat (mBeanServer .isRegistered (
111
110
new ObjectName ("com.zaxxer.hikari:type=PoolConfig (" + poolName + ")" )))
112
- .isEqualTo (expected );
111
+ .isEqualTo (expected );
113
112
}
114
113
115
114
@ Test
116
115
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 ));
119
120
}
120
121
121
122
@ 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
+ });
145
131
}
146
132
147
133
}
0 commit comments